|
| 1 | +import { Trigger } from "../generated/Contract/Contract"; |
| 2 | +import { Address, BigDecimal, BigInt, ethereum } from "@graphprotocol/graph-ts"; |
| 3 | + |
| 4 | +// Test that host exports work in globals. |
| 5 | +let one = BigDecimal.fromString("1"); |
| 6 | + |
| 7 | +export function handleTrigger(event: Trigger): void { |
| 8 | + testBigDecimal(); |
| 9 | + testEthereumAbi(); |
| 10 | +} |
| 11 | + |
| 12 | +function testBigDecimal(): void { |
| 13 | + // There are 35 digits after the dot. |
| 14 | + // big_decimal exponent will be: -35 - 6109 = -6144. |
| 15 | + // Minimum exponent is: -6143. |
| 16 | + // So 1 digit will be removed, the 8, and the 6 will be rounded to 7. |
| 17 | + let small = BigDecimal.fromString("0.99999999999999999999999999999999968"); |
| 18 | + |
| 19 | + small.exp -= BigInt.fromI32(6109); |
| 20 | + |
| 21 | + // Round-trip through the node so it truncates. |
| 22 | + small = small * new BigDecimal(BigInt.fromI32(1)); |
| 23 | + |
| 24 | + assert(small.exp == BigInt.fromI32(-6143), "wrong exp"); |
| 25 | + |
| 26 | + // This has 33 nines and the 7 which was rounded from 6. |
| 27 | + assert( |
| 28 | + small.digits == |
| 29 | + BigDecimal.fromString("9999999999999999999999999999999997").digits, |
| 30 | + "wrong digits " + small.digits.toString() |
| 31 | + ); |
| 32 | + |
| 33 | + // This has 35 nines, but we round to 34 decimal digits. |
| 34 | + let big = BigDecimal.fromString("99999999999999999999999999999999999"); |
| 35 | + |
| 36 | + // This has 35 zeros. |
| 37 | + let rounded = BigDecimal.fromString("100000000000000000000000000000000000"); |
| 38 | + |
| 39 | + assert(big == rounded, "big not equal to rounded"); |
| 40 | + |
| 41 | + // This has 35 eights, but we round to 34 decimal digits. |
| 42 | + let big2 = BigDecimal.fromString("88888888888888888888888888888888888"); |
| 43 | + |
| 44 | + // This has 33 eights. |
| 45 | + let rounded2 = BigDecimal.fromString("88888888888888888888888888888888890"); |
| 46 | + |
| 47 | + assert(big2 == rounded2, "big2 not equal to rounded2 " + big2.toString()); |
| 48 | + |
| 49 | + // Test big decimal division. |
| 50 | + assert(one / BigDecimal.fromString("10") == BigDecimal.fromString("0.1")); |
| 51 | + |
| 52 | + // Test big int fromString |
| 53 | + assert(BigInt.fromString("8888") == BigInt.fromI32(8888)); |
| 54 | + |
| 55 | + let bigInt = BigInt.fromString("8888888888888888"); |
| 56 | + |
| 57 | + // Test big int bit or |
| 58 | + assert( |
| 59 | + (bigInt | BigInt.fromI32(42)) == BigInt.fromString("8888888888888890") |
| 60 | + ); |
| 61 | + |
| 62 | + // Test big int bit and |
| 63 | + assert((bigInt & BigInt.fromI32(42)) == BigInt.fromI32(40)); |
| 64 | + |
| 65 | + // Test big int left shift |
| 66 | + assert(bigInt << 6 == BigInt.fromString("568888888888888832")); |
| 67 | + |
| 68 | + // Test big int right shift |
| 69 | + assert(bigInt >> 6 == BigInt.fromString("138888888888888")); |
| 70 | +} |
| 71 | + |
| 72 | +function testEthereumAbi(): void { |
| 73 | + ethereumAbiSimpleCase(); |
| 74 | + ethereumAbiComplexCase(); |
| 75 | +} |
| 76 | + |
| 77 | +function ethereumAbiSimpleCase(): void { |
| 78 | + let address = ethereum.Value.fromAddress(Address.fromString("0x0000000000000000000000000000000000000420")); |
| 79 | + |
| 80 | + let encoded = ethereum.encode(address)!; |
| 81 | + |
| 82 | + let decoded = ethereum.decode("address", encoded); |
| 83 | + |
| 84 | + assert(address.toAddress() == decoded.toAddress(), "address ethereum encoded does not equal the decoded value"); |
| 85 | +} |
| 86 | + |
| 87 | +function ethereumAbiComplexCase(): void { |
| 88 | + let address = ethereum.Value.fromAddress(Address.fromString("0x0000000000000000000000000000000000000420")); |
| 89 | + let bigInt1 = ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(62)); |
| 90 | + let bigInt2 = ethereum.Value.fromUnsignedBigInt(BigInt.fromI32(63)); |
| 91 | + let bool = ethereum.Value.fromBoolean(true); |
| 92 | + |
| 93 | + let fixedSizedArray = ethereum.Value.fromFixedSizedArray([ |
| 94 | + bigInt1, |
| 95 | + bigInt2 |
| 96 | + ]); |
| 97 | + |
| 98 | + let tupleArray: Array<ethereum.Value> = [ |
| 99 | + fixedSizedArray, |
| 100 | + bool |
| 101 | + ]; |
| 102 | + |
| 103 | + let tuple = ethereum.Value.fromTuple(tupleArray as ethereum.Tuple); |
| 104 | + |
| 105 | + let token: Array<ethereum.Value> = [ |
| 106 | + address, |
| 107 | + tuple |
| 108 | + ]; |
| 109 | + |
| 110 | + let encoded = ethereum.encode(ethereum.Value.fromTuple(token as ethereum.Tuple))!; |
| 111 | + |
| 112 | + let decoded = ethereum.decode("(address,(uint256[2],bool))", encoded).toTuple(); |
| 113 | + |
| 114 | + let decodedAddress = decoded[0].toAddress(); |
| 115 | + let decodedTuple = decoded[1].toTuple(); |
| 116 | + let decodedFixedSizedArray = decodedTuple[0].toArray(); |
| 117 | + let decodedBigInt1 = decodedFixedSizedArray[0].toBigInt(); |
| 118 | + let decodedBigInt2 = decodedFixedSizedArray[1].toBigInt(); |
| 119 | + let decodedBool = decodedTuple[1].toBoolean(); |
| 120 | + |
| 121 | + assert(address.toAddress() == decodedAddress, "address ethereum encoded does not equal the decoded value"); |
| 122 | + assert(bigInt1.toBigInt() == decodedBigInt1, "uint256[0] ethereum encoded does not equal the decoded value"); |
| 123 | + assert(bigInt2.toBigInt() == decodedBigInt2, "uint256[1] ethereum encoded does not equal the decoded value"); |
| 124 | + assert(bool.toBoolean() == decodedBool, "boolean ethereum encoded does not equal the decoded value"); |
| 125 | +} |
0 commit comments