@tonic-foundation/token-list
Version:
This is a fork of the popular [@solana-labs/token-list]() for NEAR/Aurora.
83 lines (82 loc) • 3.21 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenListContainer = exports.TokenListProvider = exports.GitHubTokenListResolutionStrategy = exports.StaticTokenListResolutionStrategy = exports.Strategy = void 0;
const near_tokenlist_json_1 = __importDefault(require("./tokens/near.tokenlist.json"));
var Strategy;
(function (Strategy) {
Strategy["GitHub"] = "GitHub";
Strategy["Static"] = "Static";
})(Strategy = exports.Strategy || (exports.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 (_a) {
console.info(`@tonic-foundation/token-list: falling back to static repository.`);
return near_tokenlist_json_1.default;
}
})));
return responses
.map((tokenlist) => tokenlist.tokens || [])
.reduce((acc, arr) => acc.concat(arr), []);
};
class StaticTokenListResolutionStrategy {
constructor() {
this.resolve = () => {
return near_tokenlist_json_1.default.tokens || [];
};
}
}
exports.StaticTokenListResolutionStrategy = StaticTokenListResolutionStrategy;
class GitHubTokenListResolutionStrategy {
constructor() {
this.repositories = [
'https://raw.githubusercontent.com/tonic-foundation/token-list/master/src/tokens/near.tokenlist.json',
];
this.resolve = () => {
return queryJsonFiles(this.repositories);
};
}
}
exports.GitHubTokenListResolutionStrategy = GitHubTokenListResolutionStrategy;
class TokenListProvider {
constructor() {
this.resolve = async (strategy = Strategy.Static) => {
return new TokenListContainer(
// @ts-ignore
await TokenListProvider.strategies[strategy].resolve());
};
}
}
exports.TokenListProvider = TokenListProvider;
TokenListProvider.strategies = {
[Strategy.Static]: new StaticTokenListResolutionStrategy(),
[Strategy.GitHub]: new GitHubTokenListResolutionStrategy(),
};
class TokenListContainer {
constructor(tokenList) {
this.tokenList = tokenList;
this.filterByTag = (tag) => {
return new TokenListContainer(this.tokenList.filter((item) => (item.tags || []).includes(tag)));
};
this.filterByNearEnv = (nearEnv) => {
return new TokenListContainer(this.tokenList.filter((item) => item.nearEnv === nearEnv));
};
this.excludeByNearEnv = (nearEnv) => {
return new TokenListContainer(this.tokenList.filter((item) => item.nearEnv !== nearEnv));
};
this.excludeByTag = (tag) => {
return new TokenListContainer(this.tokenList.filter((item) => !(item.tags || []).includes(tag)));
};
this.getList = () => {
return this.tokenList;
};
}
}
exports.TokenListContainer = TokenListContainer;
;