UNPKG

@tonic-foundation/token-list

Version:

This is a fork of the popular [@solana-labs/token-list]() for NEAR/Aurora.

68 lines (67 loc) 2.28 kB
import tokenlist from './tokens/near.tokenlist.json'; export var Strategy; (function (Strategy) { Strategy["GitHub"] = "GitHub"; Strategy["Static"] = "Static"; })(Strategy || (Strategy = {})); const queryJsonFiles = async (files) => { const responses = (await Promise.all(files.map(async (repo) => { try { const response = await fetch(repo); const json = (await response.json()); return json; } catch { console.info(`@tonic-foundation/token-list: falling back to static repository.`); return tokenlist; } }))); return responses .map((tokenlist) => tokenlist.tokens || []) .reduce((acc, arr) => acc.concat(arr), []); }; export class StaticTokenListResolutionStrategy { resolve = () => { return tokenlist.tokens || []; }; } export class GitHubTokenListResolutionStrategy { repositories = [ 'https://raw.githubusercontent.com/tonic-foundation/token-list/master/src/tokens/near.tokenlist.json', ]; resolve = () => { return queryJsonFiles(this.repositories); }; } export class TokenListProvider { static strategies = { [Strategy.Static]: new StaticTokenListResolutionStrategy(), [Strategy.GitHub]: new GitHubTokenListResolutionStrategy(), }; resolve = async (strategy = Strategy.Static) => { return new TokenListContainer( // @ts-ignore await TokenListProvider.strategies[strategy].resolve()); }; } export class TokenListContainer { tokenList; constructor(tokenList) { this.tokenList = tokenList; } filterByTag = (tag) => { return new TokenListContainer(this.tokenList.filter((item) => (item.tags || []).includes(tag))); }; filterByNearEnv = (nearEnv) => { return new TokenListContainer(this.tokenList.filter((item) => item.nearEnv === nearEnv)); }; excludeByNearEnv = (nearEnv) => { return new TokenListContainer(this.tokenList.filter((item) => item.nearEnv !== nearEnv)); }; excludeByTag = (tag) => { return new TokenListContainer(this.tokenList.filter((item) => !(item.tags || []).includes(tag))); }; getList = () => { return this.tokenList; }; }