Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .env.development
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ VITE_BN_REGISTRATIONS_URL=https://icp0.io/registrations
VITE_BN_CUSTOM_DOMAINS_URL=https://icp0.io/custom-domains/v1
VITE_JUNO_CDN_URL=
VITE_CYCLE_EXPRESS_URL=https://cycle.express/
VITE_KONGSWAP_API_URL=
VITE_ICP_EXPLORER_URL=https://dashboard.internetcomputer.org
VITE_EMULATOR_ADMIN_URL=http://localhost:5999
1 change: 0 additions & 1 deletion .env.production
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ VITE_BN_REGISTRATIONS_URL=https://icp0.io/registrations
VITE_BN_CUSTOM_DOMAINS_URL=https://icp0.io/custom-domains/v1
VITE_JUNO_CDN_URL=https://cdn.juno.build
VITE_CYCLE_EXPRESS_URL=https://cycle.express/
VITE_KONGSWAP_API_URL=https://api.kongswap.io
VITE_ICP_EXPLORER_URL=https://dashboard.internetcomputer.org
29 changes: 29 additions & 0 deletions src/frontend/src/lib/rest/api.rest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { JUNO_API_URL } from '$lib/constants/app.constants';
import { ApiExchangePriceResponseSchema } from '$lib/schemas/api.schema';
import type { ApiExchangePriceResponse } from '$lib/types/api';
import { assertResponseOk } from '$lib/utils/rest.utils';
import { isEmptyString } from '@dfinity/utils';
import type { PrincipalText } from '@junobuild/schema';

export const fetchExchangePrice = async ({
ledgerId
}: {
ledgerId: PrincipalText;
}): Promise<ApiExchangePriceResponse | undefined> => {
if (isEmptyString(JUNO_API_URL)) {
return undefined;
}

const response = await fetch(`${JUNO_API_URL}/v1/exchange/price/${ledgerId}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json'
}
});

assertResponseOk(response, 'Fetching Juno API failed.');

const data = await response.json();

return ApiExchangePriceResponseSchema.parse(data);
};
30 changes: 0 additions & 30 deletions src/frontend/src/lib/rest/kongswap.rest.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/frontend/src/lib/schemas/api.schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as z from 'zod';

const BinanceTickerPriceSchema = z.strictObject({
symbol: z.string(),
price: z.string()
});

const ExchangePriceSchema = z.strictObject({
...BinanceTickerPriceSchema.shape,
fetchedAt: z.iso.datetime()
});

export const ApiExchangePriceResponseSchema = z.strictObject({
exchange: ExchangePriceSchema
});
3 changes: 0 additions & 3 deletions src/frontend/src/lib/schemas/exchange.schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,5 @@ import * as z from 'zod';

export const ExchangePriceSchema = z.object({
usd: z.number(),
usdMarketCap: z.number().optional(),
usdVolume24h: z.number().optional(),
usdChange24h: z.number().optional(),
updatedAt: z.number() // Represents Date.getTime()
});
43 changes: 0 additions & 43 deletions src/frontend/src/lib/schemas/kongswap.schema.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/frontend/src/lib/types/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { ApiExchangePriceResponseSchema } from '$lib/schemas/api.schema';
import type * as z from 'zod';

export type ApiExchangePriceResponse = z.infer<typeof ApiExchangePriceResponseSchema>;
4 changes: 0 additions & 4 deletions src/frontend/src/lib/types/kongswap.ts

This file was deleted.

21 changes: 5 additions & 16 deletions src/frontend/src/lib/workers/exchange.worker.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { ICP_LEDGER_CANISTER_ID, SYNC_TOKENS_TIMER_INTERVAL } from '$lib/constants/app.constants';
import { PRICE_VALIDITY_TIMEFRAME } from '$lib/constants/exchange.constants';
import { fetchKongSwapToken } from '$lib/rest/kongswap.rest';
import { fetchExchangePrice } from '$lib/rest/api.rest';
import { exchangeIdbStore } from '$lib/stores/app/idb.store';
import type { CanisterIdText } from '$lib/types/canister';
import type { ExchangePrice } from '$lib/types/exchange';
import type { KongSwapToken } from '$lib/types/kongswap';
import type { PostMessageDataResponseExchange, PostMessageRequest } from '$lib/types/post-message';
import { isNullish, nonNullish } from '@dfinity/utils';
import { isNullish } from '@dfinity/utils';
import { del, entries, set } from 'idb-keyval';

export const onExchangeMessage = async ({ data: dataMsg }: MessageEvent<PostMessageRequest>) => {
Expand Down Expand Up @@ -85,32 +84,22 @@ const syncExchange = async () => {
};

const exchangeRateICPToUsd = async (): Promise<ExchangePrice | undefined> => {
const icp = await findICPToken();
const icp = await fetchExchangePrice({ ledgerId: ICP_LEDGER_CANISTER_ID });

if (isNullish(icp)) {
return undefined;
}

const {
metrics: { price, updated_at, market_cap, volume_24h, price_change_24h }
exchange: { price, fetchedAt }
} = icp;

if (isNullish(price)) {
return undefined;
}

return {
usd: Number(price),
...(nonNullish(market_cap) && { usdMarketCap: Number(market_cap) }),
...(nonNullish(volume_24h) && { usdVolume24h: Number(volume_24h) }),
...(nonNullish(price_change_24h) && { usdChange24h: Number(price_change_24h) }),
updatedAt: nonNullish(updated_at) ? new Date(updated_at).getTime() : new Date().getTime()
updatedAt: new Date(fetchedAt).getTime()
};
};

const findICPToken = async (): Promise<KongSwapToken | undefined> =>
await fetchKongSwapToken({ ledgerId: ICP_LEDGER_CANISTER_ID });

const syncExchangePrice = async (exchangePrice: ExchangePrice) => {
// Save information in indexed-db as well to load previous values on navigation and refresh
await set(ICP_LEDGER_CANISTER_ID, exchangePrice, exchangeIdbStore);
Expand Down
1 change: 0 additions & 1 deletion src/frontend/src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ interface ImportMetaEnv {
readonly VITE_BN_CUSTOM_DOMAINS_URL: string | '' | undefined;
readonly VITE_JUNO_CDN_URL: string | '' | undefined;
readonly VITE_CYCLE_EXPRESS_URL: string | '' | undefined;
readonly VITE_KONGSWAP_API_URL: string | '' | undefined;
readonly VITE_ICP_EXPLORER_URL: string | '' | undefined;
readonly VITE_EMULATOR_ADMIN_URL: string | '' | undefined;
}
Expand Down
Loading