-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
web3.eth.tokenlists.pas
337 lines (297 loc) · 10.3 KB
/
web3.eth.tokenlists.pas
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2022 Stefan van As <svanas@runbox.com> }
{ Github Repository <https://github.com/svanas/delphereum> }
{ }
{ Distributed under GNU AGPL v3.0 with Commons Clause }
{ }
{ This program is free software: you can redistribute it and/or modify }
{ it under the terms of the GNU Affero General Public License as published }
{ by the Free Software Foundation, either version 3 of the License, or }
{ (at your option) any later version. }
{ }
{ This program is distributed in the hope that it will be useful, }
{ but WITHOUT ANY WARRANTY; without even the implied warranty of }
{ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the }
{ GNU Affero General Public License for more details. }
{ }
{ You should have received a copy of the GNU Affero General Public License }
{ along with this program. If not, see <https://www.gnu.org/licenses/> }
{ }
{******************************************************************************}
unit web3.eth.tokenlists;
{$I web3.inc}
interface
uses
// Delphi
System.JSON,
System.SysUtils,
System.Types,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.types;
type
IToken = interface
function ChainId: UInt64;
function Address: TAddress;
function Name: string;
function Symbol: string;
function Decimals: Integer;
function Logo: TURL;
procedure Balance(const client: IWeb3; const owner: TAddress; const callback: TProc<BigInteger, IError>);
end;
TTokens = TArray<IToken>;
TTokensHelper = record helper for TTokens
procedure Enumerate(const foreach: TProc<Integer, TProc>; const done: TProc);
function IndexOf(const address: TAddress): Integer;
function Length: Integer;
end;
function count(const source: TURL; const callback: TProc<BigInteger, IError>): IAsyncResult; overload;
function count(const chain: TChain; const callback: TProc<BigInteger, IError>): IAsyncResult; overload;
function tokens(const source: TURL; const callback: TProc<TJsonArray, IError>): IAsyncResult; overload;
function tokens(const source: TURL; const callback: TProc<TTokens, IError>): IAsyncResult; overload;
function tokens(const chain: TChain; const callback: TProc<TTokens, IError>): IAsyncResult; overload;
function unsupported(const chain: TChain; const callback: TProc<TTokens, IError>): IAsyncResult;
function token(const chain: TChain; const token: TAddress; const callback: TProc<IToken, IError>): IAsyncResult;
function DAI: IToken;
implementation
uses
// Delphi
System.Generics.Collections,
// web3
web3.eth.erc20,
web3.http,
web3.json;
{----------------------------------- TToken -----------------------------------}
type
TToken = class(TCustomDeserialized, IToken)
private
FChainId: UInt64;
FAddress: TAddress;
FName: string;
FSymbol: string;
FDecimals: Integer;
FLogo: TURL;
public
function ChainId: UInt64;
function Address: TAddress;
function Name: string;
function Symbol: string;
function Decimals: Integer;
function Logo: TURL;
procedure Balance(const client: IWeb3; const owner: TAddress; const callback: TProc<BigInteger, IError>);
constructor Create(const aJsonValue: TJsonValue); overload; override;
constructor Create(const aChainId: UInt64; const aAddress: TAddress; const aName, aSymbol: string; const aDecimals: Integer; const aLogo: TURL); reintroduce; overload;
end;
constructor TToken.Create(const aJsonValue: TJsonValue);
begin
inherited Create(aJsonValue);
FChainId := getPropAsInt(aJsonValue, 'chainId');
FAddress := TAddress.Create(getPropAsStr(aJsonValue, 'address', getPropAsStr(aJsonValue, 'contract')));
FName := getPropAsStr(aJsonValue, 'name');
FSymbol := getPropAsStr(aJsonValue, 'symbol');
FDecimals := getPropAsInt(aJsonValue, 'decimals');
FLogo := getPropAsStr(aJsonValue, 'logoURI', getPropAsStr(aJsonValue, 'image'));
end;
constructor TToken.Create(const aChainId: UInt64; const aAddress: TAddress; const aName, aSymbol: string; const aDecimals: Integer; const aLogo: TURL);
begin
inherited Create(nil);
FChainId := aChainId;
FAddress := aAddress;
FName := aName;
FSymbol := aSymbol;
FDecimals := aDecimals;
FLogo := aLogo;
end;
function TToken.ChainId: UInt64;
begin
Result := FChainId;
end;
function TToken.Address: TAddress;
begin
Result := FAddress;
end;
function TToken.Name: string;
begin
Result := FName;
end;
function TToken.Symbol: string;
begin
Result := FSymbol;
end;
function TToken.Decimals: Integer;
begin
Result := FDecimals;
end;
function TToken.Logo: TURL;
begin
Result := FLogo;
end;
procedure TToken.Balance(const client: IWeb3; const owner: TAddress; const callback: TProc<BigInteger, IError>);
begin
web3.eth.erc20.create(client, Self.Address).BalanceOf(owner, callback);
end;
{------------------------------- TTokensHelper --------------------------------}
procedure TTokensHelper.Enumerate(const foreach: TProc<Integer, TProc>; const done: TProc);
begin
var next: TProc<TTokens, Integer>;
next := procedure(tokens: TTokens; idx: Integer)
begin
if idx >= tokens.Length then
begin
if Assigned(done) then done;
EXIT;
end;
foreach(idx, procedure
begin
next(tokens, idx + 1);
end);
end;
if Self.Length = 0 then
begin
if Assigned(done) then done;
EXIT;
end;
next(Self, 0);
end;
function TTokensHelper.IndexOf(const address: TAddress): Integer;
begin
for Result := 0 to Self.Length - 1 do
if Self[Result].Address.SameAs(address) then
EXIT;
Result := -1;
end;
function TTokensHelper.Length: Integer;
begin
Result := System.Length(Self);
end;
{------------------------------ public functions ------------------------------}
function count(const source: TURL; const callback: TProc<BigInteger, IError>): IAsyncResult;
begin
Result := tokens(source, procedure(arr: TJsonArray; err: IError)
begin
if Assigned(err) then
callback(0, err)
else if not Assigned(arr) then
callback(0, nil)
else
callback(arr.Count, nil);
end);
end;
function count(const chain: TChain; const callback: TProc<BigInteger, IError>): IAsyncResult;
begin
Result := tokens(chain, procedure(tokens: TTokens; err: IError)
begin
callback(tokens.Length, nil);
end);
end;
function tokens(const source: TURL; const callback: TProc<TJsonArray, IError>): IAsyncResult;
begin
Result := web3.http.get(source, [], procedure(response: TJsonValue; err: IError)
begin
const result = (function: TJsonArray
begin
Result := getPropAsArr(response, 'tokens');
if (Result = nil) and (response is TJsonArray) then Result := response as TJsonArray;
end)();
callback(result, err);
end);
end;
function tokens(const source: TURL; const callback: TProc<TTokens, IError>): IAsyncResult;
begin
Result := tokens(source, procedure(arr: TJsonArray; err: IError)
begin
if Assigned(err) or not Assigned(arr) then
begin
callback(nil, err);
EXIT;
end;
const result = (function: TTokens
begin
SetLength(Result, arr.Count);
for var I := 0 to Pred(arr.Count) do
Result[I] := TToken.Create(arr[I] as TJsonObject);
end)();
callback(result, nil);
end);
end;
function tokens(const chain: TChain; const callback: TProc<TTokens, IError>): IAsyncResult;
begin
// step #1: get the (multi-chain) Uniswap list
Result := tokens('https://tokens.uniswap.org', procedure(tokens1: TTokens; err1: IError)
begin
if Assigned(err1) or not Assigned(tokens1) then
begin
callback(nil, err1);
EXIT;
end;
var result: TTokens;
for var token1 in tokens1 do
if token1.ChainId = chain.Id then
result := result + [token1];
// step #2: add tokens from a chain-specific token list (if any)
if chain.Tokens = '' then
begin
callback(result, nil);
EXIT;
end;
tokens(chain.Tokens, procedure(tokens2: TTokens; err2: IError)
begin
if Assigned(err2) or not Assigned(tokens2) then
begin
callback(result, err2);
EXIT;
end;
for var token2 in tokens2 do
if ((token2.ChainId = chain.Id) or (token2.ChainId = 0)) and (result.IndexOf(token2.Address) = -1) then
result := result + [token2];
callback(result, nil);
end);
end);
end;
function unsupported(const chain: TChain; const callback: TProc<TTokens, IError>): IAsyncResult;
begin
Result := tokens('https://unsupportedtokens.uniswap.org', procedure(tokens: TTokens; err: IError)
begin
if Assigned(err) or not Assigned(tokens) then
callback(nil, err)
else
callback((function: TTokens
begin
Result := [];
for var token in tokens do if token.ChainId = chain.Id then result := result + [token];
end)(), nil);
end);
end;
function token(const chain: TChain; const token: TAddress; const callback: TProc<IToken, IError>): IAsyncResult;
begin
Result := tokens(chain, procedure(tokens: TTokens; err: IError)
begin
if Assigned(err) then
begin
callback(nil, err);
EXIT;
end;
const I = tokens.IndexOf(token);
if I = -1 then
callback(nil, nil)
else
callback(tokens[I], nil);
end);
end;
function DAI: IToken;
begin
Result := TToken.Create(
1,
'0x6B175474E89094C44Da98b954EedeAC495271d0F',
'Dai Stablecoin',
'DAI',
18,
'https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/ethereum/assets/0x6B175474E89094C44Da98b954EedeAC495271d0F/logo.png'
);
end;
end.