-
Notifications
You must be signed in to change notification settings - Fork 21
/
tokenlist.ts
165 lines (142 loc) · 4.28 KB
/
tokenlist.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { fetch } from 'cross-fetch';
import tokenlistMainnet from './../tokens/flow-mainnet.tokenlist.json';
import tokenlistTestnet from './../tokens/flow-testnet.tokenlist.json';
export enum ENV {
Mainnet = 'mainnet',
Testnet = 'testnet',
}
export interface TokenList {
readonly name: string;
readonly logoURI: string;
readonly tags: { [tag: string]: TagDetails };
readonly timestamp: string;
readonly tokens: TokenInfo[];
}
export interface TagDetails {
readonly name: string;
readonly description: string;
}
export interface PathDetail {
readonly vault: string;
readonly receiver: string;
readonly balance: string;
}
export interface TokenExtensions {
readonly website?: string;
readonly bridgeContract?: string;
readonly assetContract?: string;
readonly address?: string;
readonly explorer?: string;
readonly twitter?: string;
readonly github?: string;
readonly medium?: string;
readonly tgann?: string;
readonly tggroup?: string;
readonly discord?: string;
readonly coingeckoId?: string;
readonly imageUrl?: string;
readonly description?: string;
}
export interface TokenInfo {
readonly address: string;
readonly name: string;
readonly contractName: string;
readonly decimals: number;
readonly symbol: string;
readonly path: PathDetail;
readonly logoURI?: string;
readonly tags?: string[];
readonly extensions?: TokenExtensions;
}
export type TokenInfoMap = Map<string, TokenInfo>;
export const CLUSTER_SLUGS: { [id: string]: ENV } = {
mainnet: ENV.Mainnet,
testnet: ENV.Testnet,
};
export class GitHubTokenListResolutionStrategy {
repositories = {
[ENV.Mainnet]: [
'https://github.com/FlowFans/flow-token-list/blob/main/src/tokens/flow-mainnet.tokenlist.json',
],
[ENV.Testnet]: [
'https://github.com/FlowFans/flow-token-list/blob/main/src/tokens/flow-testnet.tokenlist.json',
],
};
resolve = (network: ENV = ENV.Mainnet) => {
return queryJsonFiles(this.repositories[network]);
};
}
export class CDNTokenListResolutionStrategy {
repositories = {
[ENV.Mainnet]: [
'https://cdn.jsdelivr.net/gh/FlowFans/flow-token-list@main/src/tokens/flow-mainnet.tokenlist.json',
],
[ENV.Testnet]: [
'https://cdn.jsdelivr.net/gh/FlowFans/flow-token-list@main/src/tokens/flow-testnet.tokenlist.json',
],
};
resolve = (network: ENV = ENV.Mainnet) => {
return queryJsonFiles(this.repositories[network]);
};
}
const queryJsonFiles = async (files: string[], network: ENV = ENV.Mainnet) => {
const responses: TokenList[] = (await Promise.all(
files.map(async (repo) => {
try {
const response = await fetch(repo);
const json = (await response.json()) as TokenList;
return json;
} catch {
console.info(
`@flowfans/flow-native-token-registry: falling back to static repository.`
);
return network === ENV.Mainnet ? tokenlistMainnet : tokenlistTestnet;
}
})
)) as TokenList[];
return responses
.map((tokenlist: TokenList) => tokenlist.tokens)
.reduce((acc, arr) => (acc as TokenInfo[]).concat(arr), []);
};
export enum Strategy {
GitHub = 'GitHub',
Static = 'Static',
CDN = 'CDN',
}
export class StaticTokenListResolutionStrategy {
resolve = (network: ENV = ENV.Mainnet) => {
if (network === ENV.Mainnet) return tokenlistMainnet.tokens;
return tokenlistTestnet.tokens;
};
}
export class TokenListProvider {
static strategies = {
[Strategy.GitHub]: new GitHubTokenListResolutionStrategy(),
[Strategy.Static]: new StaticTokenListResolutionStrategy(),
[Strategy.CDN]: new CDNTokenListResolutionStrategy(),
};
resolve = async (
strategy: Strategy = Strategy.CDN,
network: ENV = ENV.Mainnet
): Promise<TokenListContainer> => {
return new TokenListContainer(
await TokenListProvider.strategies[strategy].resolve(network)
);
};
}
export class TokenListContainer {
constructor(private tokenList: TokenInfo[]) {}
filterByTag = (tag: string) => {
return new TokenListContainer(
this.tokenList.filter((item) => (item.tags || []).includes(tag))
);
};
excludeByTag = (tag: string) => {
return new TokenListContainer(
this.tokenList.filter((item) => !(item.tags || []).includes(tag))
);
};
getList = () => {
return this.tokenList;
};
}