Repository navigation
Expand file tree
/
Copy pathdiffTokenLists.ts
More file actions
124 lines (115 loc) Β· 3.19 KB
/
Copy pathdiffTokenLists.ts
File metadata and controls
124 lines (115 loc) Β· 3.19 KB
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
import { TokenInfo } from './types';
export type TokenInfoChangeKey = Exclude<
keyof TokenInfo,
'address' | 'chainId'
>;
export type TokenInfoChanges = Array<TokenInfoChangeKey>;
/**
* compares two token info key values
* this subset of full deep equal functionality does not work on objects or object arrays
* @param a comparison item a
* @param b comparison item b
*/
function compareTokenInfoProperty(a: unknown, b: unknown): boolean {
if (a === b) return true;
if (typeof a !== typeof b) return false;
if (Array.isArray(a) && Array.isArray(b)) {
return a.every((el, i) => b[i] === el);
}
return false;
}
/**
* Differences between a base list and an updated list.
*/
export interface TokenListDiff {
/**
* Tokens from updated with chainId/address not present in base list
*/
readonly added: TokenInfo[];
/**
* Tokens from base with chainId/address not present in the updated list
*/
readonly removed: TokenInfo[];
/**
* The token info that changed
*/
readonly changed: {
[chainId: number]: {
[address: string]: TokenInfoChanges;
};
};
}
/**
* Computes the diff of a token list where the first argument is the base and the second argument is the updated list.
* @param base base list
* @param update updated list
*/
export function diffTokenLists(
base: TokenInfo[],
update: TokenInfo[]
): TokenListDiff {
const indexedBase = base.reduce<{
[chainId: number]: { [address: string]: TokenInfo };
}>((memo, tokenInfo) => {
if (!memo[tokenInfo.chainId]) memo[tokenInfo.chainId] = {};
memo[tokenInfo.chainId][tokenInfo.address] = tokenInfo;
return memo;
}, {});
const newListUpdates = update.reduce<{
added: TokenInfo[];
changed: {
[chainId: number]: {
[address: string]: TokenInfoChanges;
};
};
index: {
[chainId: number]: {
[address: string]: true;
};
};
}>(
(memo, tokenInfo) => {
const baseToken = indexedBase[tokenInfo.chainId]?.[tokenInfo.address];
if (!baseToken) {
memo.added.push(tokenInfo);
} else {
const changes: TokenInfoChanges = Object.keys(tokenInfo)
.filter(
(s): s is TokenInfoChangeKey => s !== 'address' && s !== 'chainId'
)
.filter(s => {
return !compareTokenInfoProperty(tokenInfo[s], baseToken[s]);
});
if (changes.length > 0) {
if (!memo.changed[tokenInfo.chainId]) {
memo.changed[tokenInfo.chainId] = {};
}
memo.changed[tokenInfo.chainId][tokenInfo.address] = changes;
}
}
if (!memo.index[tokenInfo.chainId]) {
memo.index[tokenInfo.chainId] = {
[tokenInfo.address]: true,
};
} else {
memo.index[tokenInfo.chainId][tokenInfo.address] = true;
}
return memo;
},
{ added: [], changed: {}, index: {} }
);
const removed = base.reduce<TokenInfo[]>((list, curr) => {
if (
!newListUpdates.index[curr.chainId] ||
!newListUpdates.index[curr.chainId][curr.address]
) {
list.push(curr);
}
return list;
}, []);
return {
added: newListUpdates.added,
changed: newListUpdates.changed,
removed,
};
}