-
Notifications
You must be signed in to change notification settings - Fork 996
/
Copy pathmapping.ts
103 lines (75 loc) · 3.54 KB
/
mapping.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
import { Trigger } from "../generated/Contract/Contract";
import { Address, BigDecimal, BigInt, ethereum } from "@graphprotocol/graph-ts";
// Test that host exports work in globals.
let one = BigDecimal.fromString("1");
export function handleTrigger(event: Trigger): void {
testBigDecimal();
testEthereumAbi();
}
function testBigDecimal(): void {
// There are 35 digits after the dot.
// big_decimal exponent will be: -35 - 6109 = -6144.
// Minimum exponent is: -6143.
// So 1 digit will be removed, the 8, and the 6 will be rounded to 7.
let small = BigDecimal.fromString("0.99999999999999999999999999999999968");
small.exp -= BigInt.fromI32(6109);
// Round-trip through the node so it truncates.
small = small * new BigDecimal(BigInt.fromI32(1));
assert(small.exp == BigInt.fromI32(-6143), "wrong exp");
// This has 33 nines and the 7 which was rounded from 6.
assert(
small.digits ==
BigDecimal.fromString("9999999999999999999999999999999997").digits,
"wrong digits " + small.digits.toString()
);
// This has 35 nines, but we round to 34 decimal digits.
let big = BigDecimal.fromString("99999999999999999999999999999999999");
// This has 35 zeros.
let rounded = BigDecimal.fromString("100000000000000000000000000000000000");
assert(big == rounded, "big not equal to rounded");
// This has 35 eights, but we round to 34 decimal digits.
let big2 = BigDecimal.fromString("88888888888888888888888888888888888");
// This has 33 eights.
let rounded2 = BigDecimal.fromString("88888888888888888888888888888888890");
assert(big2 == rounded2, "big2 not equal to rounded2 " + big2.toString());
// Test big decimal division.
assert(one / BigDecimal.fromString("10") == BigDecimal.fromString("0.1"));
// Test big int fromString
assert(BigInt.fromString("8888") == BigInt.fromI32(8888));
let bigInt = BigInt.fromString("8888888888888888");
// Test big int bit or
assert(
(bigInt | BigInt.fromI32(42)) == BigInt.fromString("8888888888888890")
);
// Test big int bit and
assert((bigInt & BigInt.fromI32(42)) == BigInt.fromI32(40));
// Test big int left shift
assert(bigInt << 6 == BigInt.fromString("568888888888888832"));
// Test big int right shift
assert(bigInt >> 6 == BigInt.fromString("138888888888888"));
}
function testEthereumAbi(): void {
let address = ethereum.Value.fromAddress(Address.fromString("0x0000000000000000000000000000000000000420"));
let bigInt1 = ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(62));
let bigInt2 = ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(63));
let bool = ethereum.Value.fromBoolean(true);
let fixedSizedArray = ethereum.Value.fromFixedSizedArray([
bigInt1,
bigInt2
]);
let tupleArray: Array<ethereum.Value> = [
fixedSizedArray,
bool
];
let tuple = ethereum.Value.fromTuple(tupleArray as ethereum.Tuple);
let params: Array<ethereum.Value> = [
address,
tuple
];
let encoded = ethereum.encode(params)!;
let decoded = ethereum.decode("(address,(uint256[2],bool))", encoded).toTuple();
assert(address.toAddress() == decoded[0].toAddress(), "address ethereum encoded does not equal the decoded value");
assert(bigInt1.toBigInt() == decoded[1].toTuple()[0].toArray()[0].toBigInt(), "uint256[0] ethereum encoded does not equal the decoded value");
assert(bigInt2.toBigInt() == decoded[1].toTuple()[0].toArray()[1].toBigInt(), "uint256[1] ethereum encoded does not equal the decoded value");
assert(bool.toBoolean() == decoded[1].toTuple()[1].toBoolean(), "boolean ethereum encoded does not equal the decoded value");
}