-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathweb3.eth.pas
556 lines (497 loc) · 22.5 KB
/
web3.eth.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
{******************************************************************************}
{ }
{ Delphereum }
{ }
{ Copyright(c) 2018 Stefan van As <[email protected]> }
{ 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;
{$I web3.inc}
interface
uses
// Delphi
System.SysUtils,
// Velthuis' BigNumbers
Velthuis.BigIntegers,
// web3
web3,
web3.eth.types;
const
// To remember the differences between the block tags you can think of them in the order of oldest to newest block numbers: earliest < finalized < safe < latest < pending
BLOCK_EARLIEST = 'earliest'; // The lowest numbered block the client has available. Intuitively, you can think of this as the first block created.
BLOCK_FINALIZED = 'finalized'; // The most recent crypto-economically secure block, that has been accepted by >2/3 of validators. Typically finalized in two epochs. Cannot be re-orged outside of manual intervention driven by community coordination. Intuitively, this block is very unlikely to be re-orged.
BLOCK_SAFE = 'safe'; // The most recent crypto-economically secure block, cannot be re-orged outside of manual intervention driven by community coordination. Intuitively, this block is “unlikely” to be re-orged.
BLOCK_LATEST = 'latest'; // The most recent block in the canonical chain observed by the client, this block may be re-orged out of the canonical chain even under healthy/normal conditions. Intuitively, this block is the most recent block observed by the client.
BLOCK_PENDING = 'pending'; // A sample next block built by the client on top of latest and containing the set of transactions usually taken from local mempool. Intuitively, you can think of these as blocks that have not been mined yet.
// safe and finalized are new blog tags introduced after The Merge that define commitment levels for block finality. Unlike latest which increments one block at a time (ex 101, 102, 103), safe and finalized increment every "epoch" (32 blocks), which is every ~6 minutes assuming an average ~12 second block times.
const
BLOCKS_PER_DAY = 5760; // 4 * 60 * 24
function blockNumber(const client: IWeb3): IResult<BigInteger>; overload; // blocking
procedure blockNumber(const client: IWeb3; const callback: TProc<BigInteger, IError>); overload; // async
procedure getBlockByNumber(const client: IWeb3; const callback: TProc<IBlock, IError>); overload;
procedure getBlockByNumber(const client: IWeb3; const block: string; const callback: TProc<IBlock, IError>); overload;
procedure getBalance(const client: IWeb3; const address: TAddress; const callback: TProc<BigInteger, IError>); overload;
procedure getBalance(const client: IWeb3; const address: TAddress; const block: string; const callback: TProc<BigInteger, IError>); overload;
procedure getTransactionCount(const client: IWeb3; const address: TAddress; const callback: TProc<BigInteger, IError>); overload;
procedure getTransactionCount(const client: IWeb3; const address: TAddress; const block: string; const callback: TProc<BigInteger, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<string, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<string, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<string, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<string, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<BigInteger, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<BigInteger, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<BigInteger, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<BigInteger, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<Boolean, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<Boolean, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<Boolean, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<Boolean, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<TBytes32, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<TBytes32, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TBytes32, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TBytes32, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<TTuple, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<TTuple, IError>); overload;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TTuple, IError>); overload;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TTuple, IError>); overload;
procedure sign(const client: IWeb3; const from: TPrivateKey; const &to: TAddress; const value: TWei; const data: string; const estimatedGas: BigInteger; const callback: TProc<string, IError>);
// transact with a non-payable function.
// default to the median gas price from the latest blocks.
// gas limit is twice the estimated gas.
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const func : string;
const args : array of const;
const callback: TProc<TTxHash, IError>); overload;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const func : string;
const args : array of const;
const callback: TProc<ITxReceipt, IError>); overload;
// transact with a payable function.
// default to the median gas price from the latest blocks.
// gas limit is twice the estimated gas.
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const func : string;
const args : array of const;
const callback: TProc<TTxHash, IError>); overload;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const func : string;
const args : array of const;
const callback: TProc<ITxReceipt, IError>); overload;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const data : string;
const estimatedGas: BigInteger;
const callback : TProc<TTxHash, IError>); overload;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const data : string;
const estimatedGas: BigInteger;
const callback : TProc<ITxReceipt, IError>); overload;
implementation
uses
// Delphi
System.JSON,
// web3
web3.eth.abi,
web3.eth.gas,
web3.eth.nonce,
web3.eth.tx,
web3.json,
web3.utils;
function blockNumber(const client: IWeb3): IResult<BigInteger>;
begin
const response = client.Call('eth_blockNumber', []);
if Assigned(response.Value) then
try
Result := TResult<BigInteger>.Ok(web3.json.getPropAsStr(response.Value, 'result'));
EXIT;
finally
Response.Value.Free;
end;
Result := TResult<BigInteger>.Err(response.Error);
end;
procedure blockNumber(const client: IWeb3; const callback: TProc<BigInteger, IError>);
begin
client.Call('eth_blockNumber', [], procedure(response: TJsonObject; err: IError)
begin
if Assigned(err) then
callback(0, err)
else
callback(web3.json.getPropAsStr(response, 'result'), nil);
end);
end;
type
TBlock = class(TDeserialized, IBlock)
public
function baseFeePerGas: TWei;
end;
function TBlock.baseFeePerGas: TWei;
begin
Result := getPropAsStr(FJsonValue, 'baseFeePerGas', '0x0');
end;
procedure getBlockByNumber(const client: IWeb3; const callback: TProc<IBlock, IError>);
begin
getBlockByNumber(client, BLOCK_PENDING, callback);
end;
procedure getBlockByNumber(const client: IWeb3; const block: string; const callback: TProc<IBlock, IError>);
begin
client.Call('eth_getBlockByNumber', [block, False], procedure(response: TJsonObject; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
callback(TBlock.Create(web3.json.getPropAsObj(response, 'result')), nil);
end);
end;
procedure getBalance(const client: IWeb3; const address: TAddress; const callback: TProc<BigInteger, IError>);
begin
getBalance(client, address, BLOCK_LATEST, callback);
end;
procedure getBalance(const client: IWeb3; const address: TAddress; const block: string; const callback: TProc<BigInteger, IError>);
begin
client.Call('eth_getBalance', [address, block], procedure(response: TJsonObject; err: IError)
begin
if Assigned(err) then
callback(0, err)
else
callback(web3.json.getPropAsStr(response, 'result'), nil);
end);
end;
procedure getTransactionCount(const client: IWeb3; const address: TAddress; const callback: TProc<BigInteger, IError>);
begin
getTransactionCount(client, address, BLOCK_LATEST, callback);
end;
// returns the number of transations *sent* from an address
procedure getTransactionCount(const client: IWeb3; const address: TAddress; const block: string; const callback: TProc<BigInteger, IError>);
begin
client.Call('eth_getTransactionCount', [address, block], procedure(response: TJsonObject; err: IError)
begin
if Assigned(err) then
callback(0, err)
else
callback(web3.json.getPropAsStr(response, 'result'), nil);
end);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<string, IError>);
begin
call(client, TAddress.Zero, &to, func, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<string, IError>);
begin
call(client, from, &to, func, BLOCK_LATEST, args, callback);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<string, IError>);
begin
call(client, TAddress.Zero, &to, func, block, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<string, IError>);
begin
// step #1: encode the function abi
const abi = web3.eth.abi.encode(func, args);
// step #2: construct the transaction call object
const obj = web3.json.unmarshal(Format(
'{"from": %s, "to": %s, "data": %s}', [
web3.json.quoteString(string(from), '"'),
web3.json.quoteString(string(&to), '"'),
web3.json.quoteString(abi, '"')
]
)) as TJsonObject;
try
// step #3: execute a message call (without creating a transaction on the blockchain)
client.Call('eth_call', [obj, block], procedure(response: TJsonObject; err: IError)
begin
if Assigned(err) then
callback('', err)
else
callback(web3.json.getPropAsStr(response, 'result'), nil);
end);
finally
obj.Free;
end;
end;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<BigInteger, IError>);
begin
call(client, TAddress.Zero, &to, func, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<BigInteger, IError>);
begin
call(client, from, &to, func, BLOCK_LATEST, args, callback);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<BigInteger, IError>);
begin
call(client, TAddress.Zero, &to, func, block, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<BigInteger, IError>);
begin
call(client, from, &to, func, block, args, procedure(hex: string; err: IError)
begin
if Assigned(err) then
callback(0, err)
else
if (hex = '') or (hex.ToLower = '0x') then
callback(0, nil)
else
begin
const buf = web3.utils.fromHex(hex);
if Length(buf) <= 32 then
callback(hex, nil)
else
callback(web3.utils.toHex(Copy(buf, 0, 32)), nil);
end;
end);
end;
procedure call(const client: IWeb3;const &to: TAddress; const func: string; const args: array of const; const callback: TProc<Boolean, IError>);
begin
call(client, TAddress.Zero, &to, func, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<Boolean, IError>);
begin
call(client, from, &to, func, BLOCK_LATEST, args, callback);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<Boolean, IError>);
begin
call(client, TAddress.Zero, &to, func, block, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<Boolean, IError>);
begin
call(client, from, &to, func, block, args, procedure(hex: string; err: IError)
begin
if Assigned(err) then
callback(False, err)
else
begin
const buf = web3.utils.fromHex(hex);
callback((Length(buf) > 0) and (buf[High(buf)] <> 0), nil);
end;
end);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<TBytes32, IError>);
begin
call(client, TAddress.Zero, &to, func, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<TBytes32, IError>);
begin
call(client, from, &to, func, BLOCK_LATEST, args, callback);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TBytes32, IError>);
begin
call(client, TAddress.Zero, &to, func, block, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TBytes32, IError>);
const
ZERO: TBytes32 = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
begin
call(client, from, &to, func, block, args, procedure(hex: string; err: IError)
begin
if Assigned(err) then
begin
callback(ZERO, err);
EXIT;
end;
const buffer = web3.utils.fromHex(hex);
if Length(buffer) < 32 then
begin
callback(ZERO, nil);
EXIT;
end;
callback(byteArrayToBytes32(buffer), nil);
end);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func: string; const args: array of const; const callback: TProc<TTuple, IError>);
begin
call(client, TAddress.Zero, &to, func, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func: string; const args: array of const; const callback: TProc<TTuple, IError>);
begin
call(client, from, &to, func, BLOCK_LATEST, args, callback);
end;
procedure call(const client: IWeb3; const &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TTuple, IError>);
begin
call(client, TAddress.Zero, &to, func, block, args, callback);
end;
procedure call(const client: IWeb3; const from, &to: TAddress; const func, block: string; const args: array of const; const callback: TProc<TTuple, IError>);
begin
call(client, from, &to, func, block, args, procedure(hex: string; err: IError)
begin
if Assigned(err) then
callback([], err)
else
callback(TTuple.From(hex), nil);
end);
end;
procedure sign(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const data : string;
const estimatedGas: BigInteger;
const callback : TProc<string, IError>);
begin
from.GetAddress
.ifErr(procedure(err: IError)
begin
callback('', err)
end)
.&else(procedure(sender: TAddress)
begin
web3.eth.nonce.get(client, sender, procedure(nonce: BigInteger; err: IError)
begin
if Assigned(err) then
callback('', err)
else
signTransaction(client, nonce, from, &to, value, data, 2 * estimatedGas, estimatedGas, callback);
end);
end);
end;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const func : string;
const args : array of const;
const callback: TProc<TTxHash, IError>);
begin
write(client, from, &to, 0, func, args, callback);
end;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const func : string;
const args : array of const;
const callback: TProc<ITxReceipt, IError>);
begin
write(client, from, &to, 0, func, args, callback);
end;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const func : string;
const args : array of const;
const callback: TProc<TTxHash, IError>);
begin
const sender = from.GetAddress;
if sender.isErr then
begin
callback('', sender.Error);
EXIT;
end;
const data = web3.eth.abi.encode(func, args);
web3.eth.gas.estimateGas(client, sender.Value, &to, data, procedure(estimatedGas: BigInteger; err: IError)
begin
if Assigned(err) then
callback('', err)
else
write(client, from, &to, value, data, estimatedGas, callback);
end);
end;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const func : string;
const args : array of const;
const callback: TProc<ITxReceipt, IError>);
begin
const sender = from.GetAddress;
if sender.isErr then
begin
callback(nil, sender.Error);
EXIT;
end;
const data = web3.eth.abi.encode(func, args);
web3.eth.gas.estimateGas(client, sender.Value, &to, data, procedure(estimatedGas: BigInteger; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
write(client, from, &to, value, data, estimatedGas, callback);
end);
end;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const data : string;
const estimatedGas: BigInteger;
const callback : TProc<TTxHash, IError>);
begin
sign(client, from, &to, value, data, estimatedGas, procedure(sig: string; err: IError)
begin
if Assigned(err) then
callback('', err)
else
sendTransaction(client, sig, procedure(hash: TTxHash; err: IError)
begin
if Assigned(err) and (err.Message = 'nonce too low') then
write(client, from, &to, value, data, estimatedGas, callback)
else
callback(hash, err);
end);
end);
end;
procedure write(
const client : IWeb3;
const from : TPrivateKey;
const &to : TAddress;
const value : TWei;
const data : string;
const estimatedGas: BigInteger;
const callback : TProc<ITxReceipt, IError>);
begin
sign(client, from, &to, value, data, estimatedGas, procedure(sig: string; err: IError)
begin
if Assigned(err) then
callback(nil, err)
else
sendTransaction(client, sig, procedure(rcpt: ITxReceipt; err: IError)
begin
if Assigned(err) and (err.Message = 'nonce too low') then
write(client, from, &to, value, data, estimatedGas, callback)
else
callback(rcpt, err);
end);
end);
end;
end.