Skip to content

Commit

Permalink
feat(frontend): check if exchange enabled for initialization (#3363)
Browse files Browse the repository at this point in the history
# Motivation

The derived store `exchangeInitialized` should be considered `true` when
the exchange is disabled. This mainly happens for test environments
(like E2E) where we don't provide API keys and we forcibly turn off the
exchanges via env variable `VITE_EXCHANGE_DISABLED`: it is mostly used
for loading states, so just for visual changes.

# Changes

- Extract parsed env variable `EXCHANGE_DISABLED` and export it
globally.
- Include it in the logic of `exchangeInitialized`: it is true if either
`exchangeInitialized` data is non-nullish or if exchange is disabled.

# Tests

Created some tests for the derived.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
AntonioVentilii and github-actions[bot] authored Nov 6, 2024
1 parent 42d7c66 commit ca05ca0
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/frontend/src/env/exchange.env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const EXCHANGE_DISABLED =
JSON.parse(import.meta.env.VITE_EXCHANGE_DISABLED ?? false) === true;
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
<script lang="ts">
import { debounce } from '@dfinity/utils';
import { onDestroy, onMount } from 'svelte';
import { EXCHANGE_DISABLED } from '$env/exchange.env';
import { enabledIcrcLedgerCanisterIdsNoCk } from '$icp/derived/icrc.derived';
import { enabledMergedErc20TokensAddresses } from '$icp-eth/derived/icrc-erc20.derived';
import { type ExchangeWorker, initExchangeWorker } from '$lib/services/worker.exchange.services';
let worker: ExchangeWorker | undefined;
const EXCHANGE_DISABLED = JSON.parse(import.meta.env.VITE_EXCHANGE_DISABLED ?? false) === true;
onMount(async () => {
if (EXCHANGE_DISABLED) {
// Using the exchange API during development if not necessary and given its limitation of calls per minute is annoying at it often throws errors on server reload.
Expand Down
7 changes: 5 additions & 2 deletions src/frontend/src/lib/derived/exchange.derived.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EXCHANGE_DISABLED } from '$env/exchange.env';
import {
BTC_MAINNET_TOKEN_ID,
BTC_REGTEST_TOKEN_ID,
Expand All @@ -13,10 +14,12 @@ import type { ExchangesData } from '$lib/types/exchange';
import { nonNullish } from '@dfinity/utils';
import { derived, type Readable } from 'svelte/store';

export const exchangeInitialized: Readable<boolean> = derived([exchangeStore], ([$exchangeStore]) =>
nonNullish($exchangeStore)
export const exchangeInitialized: Readable<boolean> = derived(
[exchangeStore],
([$exchangeStore]) => EXCHANGE_DISABLED || nonNullish($exchangeStore)
);

// TODO: create tests for store
export const exchanges: Readable<ExchangesData> = derived(
[exchangeStore, enabledErc20Tokens, enabledIcrcTokens],
([$exchangeStore, $erc20Tokens, $icrcTokens]) => {
Expand Down
36 changes: 36 additions & 0 deletions src/frontend/src/tests/lib/derived/exchange.derived.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as exchangeEnv from '$env/exchange.env';
import { exchangeInitialized } from '$lib/derived/exchange.derived';
import { exchangeStore } from '$lib/stores/exchange.store';
import { get } from 'svelte/store';
import { expect } from 'vitest';

describe('exchange.derived', () => {
describe('exchangeInitialized', () => {
beforeEach(() => {
vi.spyOn(exchangeEnv, 'EXCHANGE_DISABLED', 'get').mockImplementation(() => false);
});

it('should return false when exchange store is empty', () => {
expect(get(exchangeInitialized)).toBe(false);
});

it('should return true when exchange is disabled', () => {
vi.spyOn(exchangeEnv, 'EXCHANGE_DISABLED', 'get').mockImplementationOnce(() => true);

expect(get(exchangeInitialized)).toBe(true);
});

it('should return true when exchange store is not empty', () => {
exchangeStore.set([{ ethereum: { usd: 1 } }]);

expect(get(exchangeInitialized)).toBe(true);
});

it('should return false when exchange store is reset', () => {
exchangeStore.set([{ ethereum: { usd: 1 } }]);
exchangeStore.reset();

expect(get(exchangeInitialized)).toBe(false);
});
});
});

0 comments on commit ca05ca0

Please sign in to comment.