Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frontend): add burn icrc token from json #3523

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 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
25 changes: 25 additions & 0 deletions src/frontend/src/env/map-icrc-data.ts
AntonioVentilii marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import type { EnvAdditionalIcrcTokens } from '$env/types/env-icrc-additional-token';
import type { EnvTokenSymbol } from '$env/types/env-token-common';
import type { IcInterface } from '$icp/types/ic-token';
import { BETA, PROD, STAGING } from '$lib/constants/app.constants';
import { nonNullish } from '@dfinity/utils';

/**
* Additional ICRC tokens from JSON file
*/
export const mapIcrcData = (
icrcTokens: EnvAdditionalIcrcTokens
): Record<EnvTokenSymbol, Omit<IcInterface, 'position'>> =>
Object.entries(icrcTokens).reduce(
(acc, [key, value]) => ({
...acc,
...((STAGING || BETA || PROD) &&
nonNullish(value) && {
[key]: {
...value,
exchangeCoinId: 'internet-computer'
}
})
}),
{}
);
14 changes: 14 additions & 0 deletions src/frontend/src/env/networks.icrc.env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CKETH_EXPLORER_URL,
CKETH_SEPOLIA_EXPLORER_URL
} from '$env/explorers.env';
import { mapIcrcData } from '$env/map-icrc-data';
import { EURC_TOKEN } from '$env/tokens-erc20/tokens.eurc.env';
import { LINK_TOKEN, SEPOLIA_LINK_TOKEN } from '$env/tokens-erc20/tokens.link.env';
import { OCT_TOKEN } from '$env/tokens-erc20/tokens.oct.env';
Expand All @@ -18,6 +19,7 @@ import { XAUT_TOKEN } from '$env/tokens-erc20/tokens.xaut.env';
import { BTC_MAINNET_TOKEN, BTC_TESTNET_TOKEN } from '$env/tokens.btc.env';
import { ckErc20Production, ckErc20Staging } from '$env/tokens.ckerc20.env';
import { ETHEREUM_TOKEN, SEPOLIA_TOKEN } from '$env/tokens.env';
import { additionalIcrcTokensProduction } from '$env/tokens.icrc.env';
import type { EnvCkErc20Tokens } from '$env/types/env-token-ckerc20';
import type { EnvTokenSymbol } from '$env/types/env-token-common';
import type { LedgerCanisterIdText } from '$icp/types/canister';
Expand Down Expand Up @@ -385,6 +387,15 @@ const CKXAUT_IC_DATA: IcCkInterface | undefined = nonNullish(CKERC20_PRODUCTION_
}
: undefined;

const ADDITIONAL_ICRC_PRODUCTION_DATA = mapIcrcData(additionalIcrcTokensProduction);

const BURN_IC_DATA: IcInterface | undefined = nonNullish(ADDITIONAL_ICRC_PRODUCTION_DATA?.BURN)
? {
...ADDITIONAL_ICRC_PRODUCTION_DATA.BURN,
position: 12
}
: undefined;

export const CKERC20_LEDGER_CANISTER_TESTNET_IDS: CanisterIdText[] = [
...(nonNullish(LOCAL_CKUSDC_LEDGER_CANISTER_ID) ? [LOCAL_CKUSDC_LEDGER_CANISTER_ID] : []),
...(nonNullish(CKUSDC_STAGING_DATA?.ledgerCanisterId)
Expand Down Expand Up @@ -428,8 +439,11 @@ export const PUBLIC_ICRC_TOKENS: IcInterface[] = [
...(nonNullish(CKUSDC_IC_DATA) ? [CKUSDC_IC_DATA] : [])
];

const ADDITIONAL_ICRC_TOKENS: IcInterface[] = [...(nonNullish(BURN_IC_DATA) ? [BURN_IC_DATA] : [])];

export const ICRC_TOKENS: IcInterface[] = [
...PUBLIC_ICRC_TOKENS,
...ADDITIONAL_ICRC_TOKENS,
...(nonNullish(CKBTC_LOCAL_DATA) ? [CKBTC_LOCAL_DATA] : []),
...(nonNullish(CKBTC_STAGING_DATA) ? [CKBTC_STAGING_DATA] : []),
...(nonNullish(CKETH_LOCAL_DATA) ? [CKETH_LOCAL_DATA] : []),
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/src/env/tokens.icrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"production": {},
"production": {
"BURN": {
"indexCanisterId": "nrant-tyaaa-aaaag-atsjq-cai",
"ledgerCanisterId": "egjwt-lqaaa-aaaak-qi2aa-cai"
}
},
"staging": {}
}
4 changes: 2 additions & 2 deletions src/frontend/src/env/types/env-icrc-additional-token.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EnvTokensAdditionalIcrcSchema } from '$env/schema/env-additional-icrc-token.schema';
import { EnvAdditionalIcrcTokensSchema } from '$env/schema/env-additional-icrc-token.schema';
import { z } from 'zod';

export type EnvAdditionalIcrcTokens = z.infer<typeof EnvTokensAdditionalIcrcSchema>;
export type EnvAdditionalIcrcTokens = z.infer<typeof EnvAdditionalIcrcTokensSchema>;
38 changes: 38 additions & 0 deletions src/frontend/src/tests/env/map-icrc-data.spec.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good one! however, the tests should be for each env, so that if at some point we change something, they fail

Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { describe, expect, it } from 'vitest';

import { mapIcrcData } from '$env/map-icrc-data';

describe('mapIcrcData', () => {
beforeEach(() => {
vi.mock('$lib/constants/app.constants', () => ({
PROD: true,
STAGING: false,
BETA: false
}));
});

afterEach(() => vi.clearAllMocks());

it('should map ICRC tokens with exchangeCoinId', () => {
const token = {
TESTTOKEN: {
ledgerCanisterId: 'dummy-ledger-id',
indexCanisterId: 'dummy-canister-id'
}
};

const result = mapIcrcData(token);

expect(result).toEqual({
TESTTOKEN: {
...token.TESTTOKEN,
exchangeCoinId: 'internet-computer'
}
});
});

it('should handle empty input', () => {
const result = mapIcrcData({});
expect(result).toEqual({});
});
});