-
Notifications
You must be signed in to change notification settings - Fork 222
/
Copy pathethereum.ts
729 lines (651 loc) · 21.9 KB
/
ethereum.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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
import { Bytes, Wrapped } from '../common/collections';
import { Address, BigInt } from '../common/numbers';
/** Host Ethereum interface */
export declare namespace ethereum {
function call(call: SmartContractCall): Array<Value> | null;
function getBalance(address: Address): BigInt;
function hasCode(address: Address): Wrapped<bool>;
function encode(token: Value): Bytes | null;
function decode(types: string, data: Bytes): Value | null;
}
export namespace ethereum {
/** Type hint for Ethereum values. */
export enum ValueKind {
ADDRESS = 0,
FIXED_BYTES = 1,
BYTES = 2,
INT = 3,
UINT = 4,
BOOL = 5,
STRING = 6,
FIXED_ARRAY = 7,
ARRAY = 8,
TUPLE = 9,
}
/**
* Pointer type for Ethereum value data.
*
* Big enough to fit any pointer or native `this.data`.
*/
export type ValuePayload = u64;
/**
* A dynamically typed value used when accessing Ethereum data.
*/
export class Value {
constructor(
public kind: ValueKind,
public data: ValuePayload,
) {}
@operator('<')
lt(_: Value): boolean {
abort("Less than operator isn't supported in Value");
return false;
}
@operator('>')
gt(_: Value): boolean {
abort("Greater than operator isn't supported in Value");
return false;
}
toAddress(): Address {
assert(this.kind == ValueKind.ADDRESS, 'Ethereum value is not an address');
return changetype<Address>(this.data as u32);
}
toBoolean(): boolean {
assert(this.kind == ValueKind.BOOL, 'Ethereum value is not a boolean.');
return this.data != 0;
}
toBytes(): Bytes {
assert(
this.kind == ValueKind.FIXED_BYTES || this.kind == ValueKind.BYTES,
'Ethereum value is not bytes.',
);
return changetype<Bytes>(this.data as u32);
}
toI32(): i32 {
assert(
this.kind == ValueKind.INT || this.kind == ValueKind.UINT,
'Ethereum value is not an int or uint.',
);
const bigInt = changetype<BigInt>(this.data as u32);
return bigInt.toI32();
}
toBigInt(): BigInt {
assert(
this.kind == ValueKind.INT || this.kind == ValueKind.UINT,
'Ethereum value is not an int or uint.',
);
return changetype<BigInt>(this.data as u32);
}
toString(): string {
assert(this.kind == ValueKind.STRING, 'Ethereum value is not a string.');
return changetype<string>(this.data as u32);
}
toArray(): Array<Value> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array.',
);
return changetype<Array<Value>>(this.data as u32);
}
toTuple(): Tuple {
assert(this.kind == ValueKind.TUPLE, 'Ethereum value is not a tuple.');
return changetype<Tuple>(this.data as u32);
}
toMatrix(): Array<Array<Value>> {
const valueArray = this.toArray();
const out = new Array<Array<Value>>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toArray();
}
return out;
}
toTupleArray<T extends Tuple>(): Array<T> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array.',
);
const valueArray = this.toArray();
const out = new Array<T>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = changetype<T>(valueArray[i].toTuple());
}
return out;
}
toTupleMatrix<T extends Tuple>(): Array<Array<T>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<T>>(valueMatrix.length);
for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<T>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = changetype<T>(valueMatrix[i][j].toTuple());
}
}
return out;
}
toBooleanArray(): Array<boolean> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array or fixed array.',
);
const valueArray = this.toArray();
const out = new Array<boolean>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toBoolean();
}
return out;
}
toBytesArray(): Array<Bytes> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array or fixed array.',
);
const valueArray = this.toArray();
const out = new Array<Bytes>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toBytes();
}
return out;
}
toAddressArray(): Array<Address> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array or fixed array.',
);
const valueArray = this.toArray();
const out = new Array<Address>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toAddress();
}
return out;
}
toStringArray(): Array<string> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array or fixed array.',
);
const valueArray = this.toArray();
const out = new Array<string>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toString();
}
return out;
}
toI32Array(): Array<i32> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array or fixed array.',
);
const valueArray = this.toArray();
const out = new Array<i32>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toI32();
}
return out;
}
toBigIntArray(): Array<BigInt> {
assert(
this.kind == ValueKind.ARRAY || this.kind == ValueKind.FIXED_ARRAY,
'Ethereum value is not an array or fixed array.',
);
const valueArray = this.toArray();
const out = new Array<BigInt>(valueArray.length);
for (let i: i32 = 0; i < valueArray.length; i++) {
out[i] = valueArray[i].toBigInt();
}
return out;
}
toBooleanMatrix(): Array<Array<boolean>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<boolean>>(valueMatrix.length);
for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<boolean>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = valueMatrix[i][j].toBoolean();
}
}
return out;
}
toBytesMatrix(): Array<Array<Bytes>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<Bytes>>(valueMatrix.length);
for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<Bytes>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = valueMatrix[i][j].toBytes();
}
}
return out;
}
toAddressMatrix(): Array<Array<Address>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<Address>>(valueMatrix.length);
for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<Address>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = valueMatrix[i][j].toAddress();
}
}
return out;
}
toStringMatrix(): Array<Array<string>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<string>>(valueMatrix.length);
for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<string>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = valueMatrix[i][j].toString();
}
}
return out;
}
toI32Matrix(): Array<Array<i32>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<i32>>(valueMatrix.length);
for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<i32>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = valueMatrix[i][j].toI32();
}
}
return out;
}
toBigIntMatrix(): Array<Array<BigInt>> {
const valueMatrix = this.toMatrix();
const out = new Array<Array<BigInt>>(valueMatrix.length);
for (let i: i32 = 0; i < valueMatrix.length; i++) {
out[i] = new Array<BigInt>(valueMatrix[i].length);
for (let j: i32 = 0; j < valueMatrix[i].length; j++) {
out[i][j] = valueMatrix[i][j].toBigInt();
}
}
return out;
}
static fromAddress(address: Address): Value {
assert(address.length == 20, 'Address must contain exactly 20 bytes');
return new Value(ValueKind.ADDRESS, changetype<u32>(address));
}
static fromBoolean(b: boolean): Value {
return new Value(ValueKind.BOOL, b ? 1 : 0);
}
static fromBytes(bytes: Bytes): Value {
return new Value(ValueKind.BYTES, changetype<u32>(bytes));
}
static fromFixedBytes(bytes: Bytes): Value {
return new Value(ValueKind.FIXED_BYTES, changetype<u32>(bytes));
}
static fromI32(i: i32): Value {
return new Value(ValueKind.INT, changetype<u32>(BigInt.fromI32(i)));
}
static fromSignedBigInt(i: BigInt): Value {
return new Value(ValueKind.INT, changetype<u32>(i));
}
static fromUnsignedBigInt(i: BigInt): Value {
return new Value(ValueKind.UINT, changetype<u32>(i));
}
static fromString(s: string): Value {
return new Value(ValueKind.STRING, changetype<u32>(s));
}
static fromArray(values: Array<Value>): Value {
return new Value(ValueKind.ARRAY, changetype<u32>(values));
}
static fromFixedSizedArray(values: Array<Value>): Value {
return new Value(ValueKind.FIXED_ARRAY, changetype<u32>(values));
}
static fromTuple(values: Tuple): Value {
return new Value(ValueKind.TUPLE, changetype<u32>(values));
}
static fromMatrix(values: Array<Array<Value>>): Value {
const innerOut = new Array<Value>(values.length);
for (let i: i32 = 0; i < innerOut.length; i++) {
innerOut[i] = Value.fromArray(values[i]);
}
return Value.fromArray(innerOut);
}
static fromTupleArray(values: Array<Tuple>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromTuple(values[i]);
}
return Value.fromArray(out);
}
static fromTupleMatrix(values: Array<Array<Tuple>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromTuple(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromBooleanArray(values: Array<boolean>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromBoolean(values[i]);
}
return Value.fromArray(out);
}
static fromBytesArray(values: Array<Bytes>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromBytes(values[i]);
}
return Value.fromArray(out);
}
static fromFixedBytesArray(values: Array<Bytes>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromFixedBytes(values[i]);
}
return Value.fromArray(out);
}
static fromAddressArray(values: Array<Address>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromAddress(values[i]);
}
return Value.fromArray(out);
}
static fromStringArray(values: Array<string>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromString(values[i]);
}
return Value.fromArray(out);
}
static fromI32Array(values: Array<i32>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromI32(values[i]);
}
return Value.fromArray(out);
}
static fromSignedBigIntArray(values: Array<BigInt>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromSignedBigInt(values[i]);
}
return Value.fromArray(out);
}
static fromUnsignedBigIntArray(values: Array<BigInt>): Value {
const out = new Array<Value>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = Value.fromUnsignedBigInt(values[i]);
}
return Value.fromArray(out);
}
static fromBooleanMatrix(values: Array<Array<boolean>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromBoolean(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromBytesMatrix(values: Array<Array<Bytes>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromBytes(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromFixedBytesMatrix(values: Array<Array<Bytes>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromFixedBytes(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromAddressMatrix(values: Array<Array<Address>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromAddress(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromStringMatrix(values: Array<Array<string>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromString(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromI32Matrix(values: Array<Array<i32>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromI32(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromSignedBigIntMatrix(values: Array<Array<BigInt>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromSignedBigInt(values[i][j]);
}
}
return Value.fromMatrix(out);
}
static fromUnsignedBigIntMatrix(values: Array<Array<BigInt>>): Value {
const out = new Array<Array<Value>>(values.length);
for (let i: i32 = 0; i < values.length; i++) {
out[i] = new Array<Value>(values[i].length);
for (let j: i32 = 0; j < values[i].length; j++) {
out[i][j] = Value.fromUnsignedBigInt(values[i][j]);
}
}
return Value.fromMatrix(out);
}
}
/**
* Common representation for Ethereum tuples / Solidity structs.
*
* This base class stores the tuple/struct values in an array. The Graph CLI
* code generation then creates subclasses that provide named getters to
* access the members by name.
*/
export class Tuple extends Array<Value> {}
/**
* An Ethereum block.
*/
export class Block {
constructor(
public hash: Bytes,
public parentHash: Bytes,
public unclesHash: Bytes,
public author: Address,
public stateRoot: Bytes,
public transactionsRoot: Bytes,
public receiptsRoot: Bytes,
public number: BigInt,
public gasUsed: BigInt,
public gasLimit: BigInt,
public timestamp: BigInt,
public difficulty: BigInt,
public totalDifficulty: BigInt,
public size: BigInt | null,
public baseFeePerGas: BigInt | null,
) {}
}
/**
* An Ethereum transaction.
*/
export class Transaction {
constructor(
public hash: Bytes,
public index: BigInt,
public from: Address,
public to: Address | null,
public value: BigInt,
public gasLimit: BigInt,
public gasPrice: BigInt,
public input: Bytes,
public nonce: BigInt,
) {}
}
/**
* An Ethereum transaction receipt.
*/
export class TransactionReceipt {
constructor(
public transactionHash: Bytes,
public transactionIndex: BigInt,
public blockHash: Bytes,
public blockNumber: BigInt,
public cumulativeGasUsed: BigInt,
public gasUsed: BigInt,
public contractAddress: Address,
public logs: Array<Log>,
public status: BigInt,
public root: Bytes,
public logsBloom: Bytes,
) {}
}
/**
* An Ethereum event log.
*/
export class Log {
constructor(
public address: Address,
public topics: Array<Bytes>,
public data: Bytes,
public blockHash: Bytes,
public blockNumber: Bytes,
public transactionHash: Bytes,
public transactionIndex: BigInt,
public logIndex: BigInt,
public transactionLogIndex: BigInt,
public logType: string,
public removed: Wrapped<bool> | null,
) {}
}
/**
* Common representation for Ethereum smart contract calls.
*/
export class Call {
constructor(
public to: Address,
public from: Address,
public block: Block,
public transaction: Transaction,
public inputValues: Array<EventParam>,
public outputValues: Array<EventParam>,
) {}
}
/**
* Common representation for Ethereum smart contract events.
*/
export class Event {
constructor(
public address: Address,
public logIndex: BigInt,
public transactionLogIndex: BigInt,
public logType: string | null,
public block: Block,
public transaction: Transaction,
public parameters: Array<EventParam>,
public receipt: TransactionReceipt | null,
) {}
}
/**
* A dynamically-typed Ethereum event parameter.
*/
export class EventParam {
constructor(
public name: string,
public value: Value,
) {}
}
export class SmartContractCall {
contractName: string;
contractAddress: Address;
functionName: string;
functionSignature: string;
functionParams: Array<Value>;
constructor(
contractName: string,
contractAddress: Address,
functionName: string,
functionSignature: string,
functionParams: Array<Value>,
) {
this.contractName = contractName;
this.contractAddress = contractAddress;
this.functionName = functionName;
this.functionSignature = functionSignature;
this.functionParams = functionParams;
}
}
/**
* Low-level interaction with Ethereum smart contracts
*/
export class SmartContract {
_name: string;
_address: Address;
protected constructor(name: string, address: Address) {
this._name = name;
this._address = address;
}
call(name: string, signature: string, params: Array<Value>): Array<Value> {
const call = new SmartContractCall(this._name, this._address, name, signature, params);
const result = ethereum.call(call);
assert(
result != null,
'Call reverted, probably because an `assert` or `require` in the contract failed, ' +
'consider using `try_' +
name +
'` to handle this in the mapping.',
);
return changetype<Array<Value>>(result);
}
tryCall(name: string, signature: string, params: Array<Value>): CallResult<Array<Value>> {
const call = new SmartContractCall(this._name, this._address, name, signature, params);
const result = ethereum.call(call);
if (result == null) {
return new CallResult();
}
return CallResult.fromValue(changetype<Array<Value>>(result));
}
}
export class CallResult<T> {
// `null` indicates a reverted call.
private _value: Wrapped<T> | null;
constructor() {
this._value = null;
}
static fromValue<T>(value: T): CallResult<T> {
const result = new CallResult<T>();
result._value = new Wrapped(value);
return result;
}
get reverted(): bool {
return this._value == null;
}
get value(): T {
assert(
!this.reverted,
'accessed value of a reverted call, ' +
'please check the `reverted` field before accessing the `value` field',
);
return changetype<Wrapped<T>>(this._value).inner;
}
}
}