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

Cprod 1537 dynamic token list.json #30

Merged
merged 8 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
5 changes: 5 additions & 0 deletions .changeset/nervous-trainers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@infinityswapofficial/token-lists': patch
---

updated token-list path
26 changes: 22 additions & 4 deletions src/tokens.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { SnsWasmCanister } from '@dfinity/nns';
import axios from 'axios';
import TokensJson from './tokenlist.json';
import TestnetTokensJson from './tokenlist.testnet.json';
import { HttpAgent } from '@dfinity/agent';
import { initSnsWrapper } from '@dfinity/sns';
import { Principal } from '@dfinity/principal';
import { isDefined } from './utils';

const IC_API_BASE_URL = 'https://ic-api.internetcomputer.org';
export const TOKEN_LIST_BASE_URL =
'https://raw.githubusercontent.com/infinity-swap/token-lists/main/src';
export const TOKENS_JSON_LIST_URL = `${TOKEN_LIST_BASE_URL}/tokenlist.json`;
export const TEST_TOKENS_JSON_LIST_URL = `${TOKEN_LIST_BASE_URL}/tokenlist.testnet.json`;

export interface CanisterInfo {
canisterId: string;
Expand Down Expand Up @@ -131,17 +133,33 @@ export class TokenList {
this.tokens = tokens;
}

static async init() {
try {
const URLS = [TOKENS_JSON_LIST_URL, TEST_TOKENS_JSON_LIST_URL];
const fetchURL = (url: string) => axios.get(url);
const promises = URLS.map(fetchURL);
const result = await Promise.all(promises);
return {
dynamicTokens: result[0]?.data,
dynamicTestTokens: result[0]?.data
};
} catch (_) {
return {};
}
}

static async create({
env,
host,
snsWasmCanisterId
}: TokenListCreateOptions = {}): Promise<TokenList> {
let tokensJson: JsonableTokenList = TokensJson;
const { dynamicTokens, dynamicTestTokens } = await this.init();
alexshelkov marked this conversation as resolved.
Show resolved Hide resolved
let tokensJson: JsonableTokenList = dynamicTokens;
let snsWasmId = snsWasmCanisterId;
let snsTokens: Token[] = [];

if (env === 'testnet') {
tokensJson = TestnetTokensJson;
tokensJson = dynamicTestTokens;
}
if (snsWasmCanisterId) {
snsWasmId = snsWasmCanisterId || MAINNET_SNS_WASM_CANISTER_ID;
Expand Down