Newer
Older
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
166
167
import {
RawTokenJSON,
ValidatedTokenJSON,
NormalizedTokenJSON
} from "./types/TokensJson";
import { Token } from "./types/Token";
interface StrIdx<T> {
[key: string]: T;
}
const networks = [
{
networkName: "foundation",
networkNameIPFS: "eth"
},
{
networkName: "goerli",
networkNameIPFS: "gor"
},
{
networkName: "kovan",
networkNameIPFS: "kov"
},
{
networkName: "ropsten",
networkNameIPFS: "rop"
}
];
function processTokenJson(tokensJson: RawTokenJSON[]): Token[] {
const normalizedTokens = tokensJson
.map(validateTokenJSON)
.map(normalizeTokenJSON);
checkForDuplicateAddresses(normalizedTokens);
return handleDuplicateSymbols(normalizedTokens);
}
function validateTokenJSON(token: RawTokenJSON): ValidatedTokenJSON {
const isValid = (t: RawTokenJSON): t is ValidatedTokenJSON =>
!!(t.address && (t.decimals || t.decimals === 0) && t.name && t.symbol);
if (isValid(token)) {
return token;
}
throw Error(`Token failed validation, missing part of schema
Symbol: ${token.symbol}
Name: ${token.name}
Address: ${token.address}
Decimals: ${token.decimals}`);
}
function normalizeTokenJSON(token: ValidatedTokenJSON): NormalizedTokenJSON {
const { address, decimals, symbol, name, logo } = token;
return <NormalizedTokenJSON>{
address,
symbol,
decimals: +decimals,
name,
...(!!logo && logo.src ? { logo: logo.src } : {})
};
}
/**
*
* @description Checks for any duplicated addresses and halts the program if so
* @param {NormalizedTokenJSON[]} tokens
*/
function checkForDuplicateAddresses(tokens: NormalizedTokenJSON[]) {
const map: StrIdx<boolean> = {};
const errors: string[] = [];
for (const token of tokens) {
const { address } = token;
// We might want to strip hex prefixes here, and make all characters lowercase
if (map[address]) {
errors.push(
`Token ${token.symbol} has a duplicate address of ${token.address}`
);
}
map[address] = true;
}
if (errors.length) {
const err = errors.join("\n");
throw Error(err);
}
}
/**
*
* @description Finds any duplicated names in the fetched token json
* @param {NormalizedTokenJSON[]} tokens
* @returns
*/
function getDuplicatedNames(tokens: NormalizedTokenJSON[]) {
const checkedNames: StrIdx<boolean> = {};
const duplicatedNames: StrIdx<boolean> = {};
for (const token of tokens) {
const { name } = token;
if (checkedNames[name]) {
duplicatedNames[name] = true;
}
checkedNames[name] = true;
}
return duplicatedNames;
}
/**
*
* @description Handles any tokens with duplicated symbols by placing them in a map with each value being a bucket
* of other tokens with the same symbol, then renaming them appropriately so they do not conflict anymore
* @param {NormalizedTokenJSON[]} tokens
* @returns
*/
function handleDuplicateSymbols(tokens: NormalizedTokenJSON[]) {
// start by building a map of symbols => tokens
const map = new Map<string, NormalizedTokenJSON[]>();
for (const token of tokens) {
const { symbol } = token;
const v = map.get(symbol);
if (v) {
map.set(symbol, [...v, token]);
} else {
map.set(symbol, [token]);
}
}
const duplicatedNames = getDuplicatedNames(tokens);
const dedupedTokens: NormalizedTokenJSON[] = [];
map.forEach(tokenBucket =>
dedupedTokens.push(...renameSymbolCollisions(tokenBucket, duplicatedNames))
);
return dedupedTokens;
}
/**
*
* @description Any token collisions are handled in this manner:
* 1) If the name isnt a duplicate, the token symbol is prefixed with the token name
* 2) if it is a duplicate, then we simply use the token index + 1 (so we dont start at 0)
* @param {NormalizedTokenJSON[]} tokens
* @param {StrIdx<boolean>} duplicatedNames
* @returns
*/
function renameSymbolCollisions(
tokens: NormalizedTokenJSON[],
duplicatedNames: StrIdx<boolean>
) {
const renamedTokens: NormalizedTokenJSON[] = [];
if (tokens.length === 1) {
return tokens;
}
return tokens.reduce((prev, curr, idx) => {
const newName = `${curr.symbol} (${
duplicatedNames[curr.name] ? idx + 1 : curr.name
})`;
const tokenToInsert: NormalizedTokenJSON = {
...curr,
symbol: newName
};
console.warn(
`WARN: "${curr.symbol}" has a duplicate symbol, renaming to "${newName}"`
);
return [...prev, tokenToInsert];
}, renamedTokens);
}
export { networks, processTokenJson };