-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokencache.ts
276 lines (249 loc) · 7.64 KB
/
tokencache.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// SPDX-License-Identifier: Apache-2.0
"use strict";
import { Signer } from "ethers";
import { Address, addressKey } from "#erdstall/ledger";
import { TokenRegistered, TokenTypeRegistered } from "#erdstall/ledger";
import { TokenType, requireTokenType, ETHZERO } from "#erdstall/ledger/assets";
import { Erdstall, ERC20__factory, ERC721__factory } from "./contracts";
interface Responder {
symbol(): Promise<string>;
}
/**
* Describes an entity with the ability to query token information related to
* Erdstall and its onchain contracts.
*/
export interface TokenProvider {
/**
* Sets the locally cached token type for the given token address.
*
* @param tokenAddr - The address of the token to update.
* @param ttype - The token type.
*/
setType(tokenAddr: string, ttype: TokenType): void;
/**
* Retrieves the address of the token holder contract for the given token
* type related to the given Erdstall contract.
*
* @param erdstall - The Erdstall contract.
* @param ttype - The token type which is handled by some holder contract.
* @throws An error when no token holder contract is registered for the given
* token type.
* @returns The address of the holder contract in string representation.
*/
tokenHolderFor(erdstall: Erdstall, ttype: TokenType): Promise<string>;
/**
* Retrieves the token type for a given token address related to the given
* Erdstall contract.
*
* @param erdstall - The Erdstall contract.
* @param tokenAddr - The address of a token contract.
* @throws An error when the given token address is not registered with the
* Erdstall contract.
* @returns The token type of the given token address.
*/
tokenTypeOf(erdstall: Erdstall, tokenAddr: string): Promise<TokenType>;
/**
* Finds the address of the first registered token contract in Erdstall,
* which has the given symbol as its symbol according to the ERC20Detailed
* specification.
*
* @param erdstall - The Erdstall contract.
* @param symbol - The symbol to search for.
* @param fromBlock - When omitted the starting block from when the Erdstall
* contract was deployed will be used.
* @returns The address when a token contract matching the symbol could be
* found, otherwise undefined.
*
* @remarks
* [ERC20Detailed](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/token/ERC20/ERC20Detailed.sol)
*/
findRegisteredTokenWithSymbol(
erdstall: Erdstall,
symbol: string,
fromBlock?: number,
): Promise<Address | undefined>;
/**
* Queries the registered token types on the given Erdstall contract.
*
* @param erdstall - The Erdstall contract.
* @param fromBlock - When omitted the starting block from when the Erdstall
* contract was deployed will be used.
* @returns A list of TokenTypeRegistered events, which might be empty if no
* token types were registered.
*/
queryRegisteredTokenTypes(
erdstall: Erdstall,
fromBlock?: number,
): Promise<TokenTypeRegistered[]>;
/**
* Queries the registered tokens on the given Erdstall contract.
*
* @param erdstall - The Erdstall contract.
* @param fromBlock - When omitted the starting block from when the Erdstall
* contract was deployed will be used.
* @returns A list of TokenRegistered events, which might be empty if no
* tokens were registered.
*/
queryRegisteredTokens(
erdstall: Erdstall,
fromBlock?: number,
): Promise<TokenRegistered[]>;
}
type TokenTypes = Map<string, TokenType>;
type TokenHolders = Map<TokenType, string>;
export class TokenFetcher implements TokenProvider {
readonly typeCache: TokenTypes;
readonly holderCache: TokenHolders;
private bigbang?: number;
constructor() {
this.typeCache = new Map<string, TokenType>();
this.typeCache.set(ETHZERO, "ETH");
this.holderCache = new Map<TokenType, string>();
}
setType(tokenAddr: string, ttype: TokenType) {
this.typeCache.set(addressKey(tokenAddr), ttype);
}
async tokenHolderFor(
erdstall: Erdstall,
ttype: TokenType,
): Promise<string> {
if (this.holderCache.has(ttype)) {
return this.holderCache.get(ttype)!;
}
await this.queryRegisteredTokenTypes(erdstall, this.bigbang);
if (!this.holderCache.has(ttype)) {
throw new Error(
"no holder for the given tokentype registered on erdstall",
);
}
return this.holderCache.get(ttype)!;
}
async tokenTypeOf(
erdstall: Erdstall,
tokenAddr: string,
): Promise<TokenType> {
tokenAddr = addressKey(tokenAddr);
if (this.typeCache.has(tokenAddr)) {
return this.typeCache.get(tokenAddr)!;
}
const ttype = await this.queryTokenType(erdstall, tokenAddr);
if (!ttype) {
return Promise.reject(
new Error(`given token not registered: ${tokenAddr}`),
);
}
this.typeCache.set(tokenAddr, ttype!);
return ttype!;
}
async queryTokenType(
erdstall: Erdstall,
tokenAddr: string,
): Promise<TokenType | undefined> {
tokenAddr = addressKey(tokenAddr);
await this.queryRegisteredTokens(erdstall, this.bigbang);
return this.typeCache.get(tokenAddr);
}
async findRegisteredTokenWithSymbol(
erdstall: Erdstall,
symbol: string,
fromBlock?: number,
): Promise<Address | undefined> {
const from = fromBlock
? fromBlock
: (await erdstall.bigBang()).toNumber();
const registeredTokens = await this.queryRegisteredTokens(
erdstall,
from,
);
for (const ev of registeredTokens) {
try {
const token = this.resolveTokenType(
erdstall.signer,
requireTokenType(ev.tokenType),
ev.token,
);
const sym = await token.symbol();
if (sym === symbol) {
return ev.token;
}
} catch {
return;
}
}
return;
}
async queryRegisteredTokenTypes(
erdstall: Erdstall,
fromBlock?: number,
): Promise<TokenTypeRegistered[]> {
const from = fromBlock
? fromBlock
: (await erdstall.bigBang()).toNumber();
const filter = erdstall.filters.TokenTypeRegistered(null, null);
return erdstall.queryFilter(filter, from).then((ev) => {
return ev.map((entry) => {
const ttype = requireTokenType(entry.args.tokenType);
const tokenHolder = entry.args.tokenHolder;
if (!this.holderCache.has(ttype)) {
this.holderCache.set(ttype, tokenHolder);
}
return {
tokenType: ttype,
tokenHolder: Address.fromString(tokenHolder),
};
});
});
}
// queryRegisteredTokens queries the registered tokens from the
// erdstall-contract. It also updates the tokentype cache accordingly.
async queryRegisteredTokens(
erdstall: Erdstall,
fromBlock?: number,
): Promise<TokenRegistered[]> {
const from = fromBlock
? fromBlock
: (await erdstall.bigBang()).toNumber();
const filter = erdstall.filters.TokenRegistered(null, null, null);
return erdstall.queryFilter(filter, from).then((ev) => {
return ev.map((entry) => {
const token = addressKey(entry.args.token);
const ttype = requireTokenType(entry.args.tokenType);
const tokenHolder = entry.args.tokenHolder;
if (!this.typeCache.has(token)) {
this.setType(token, ttype);
}
if (!this.holderCache.has(ttype)) {
this.holderCache.set(ttype, tokenHolder);
}
return {
token: Address.fromString(token),
tokenType: ttype,
tokenHolder: Address.fromString(tokenHolder),
};
});
});
}
resolveTokenType(
signer: Signer,
ttype: TokenType,
token: string | Address,
): Responder {
token = addressKey(token);
switch (ttype) {
case "ERC20":
return ERC20__factory.connect(token, signer);
case "ERC721":
return ERC721__factory.connect(token, signer);
case "ERC721Mintable":
return ERC721__factory.connect(token, signer);
case "ETH":
return {
symbol: async function (): Promise<string> {
return "ETH";
},
};
default:
throw Error("not implemented");
}
}
}