|
| 1 | +# nim-web3 |
| 2 | +# Copyright (c) 2018-2024 Status Research & Development GmbH |
| 3 | +# Licensed under either of |
| 4 | +# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE)) |
| 5 | +# * MIT license ([LICENSE-MIT](LICENSE-MIT)) |
| 6 | +# at your option. |
| 7 | +# This file may not be copied, modified, or distributed except according to |
| 8 | +# those terms. |
| 9 | + |
| 10 | +import |
| 11 | + pkg/unittest2, |
| 12 | + stew/byteutils, |
| 13 | + stint, |
| 14 | + ../web3/contract_dsl |
| 15 | + |
| 16 | +type |
| 17 | + DummySender = object |
| 18 | + |
| 19 | +proc instantiateContract(t: typedesc): ContractInstance[t, DummySender] = |
| 20 | + discard |
| 21 | + |
| 22 | +proc checkData(d: ContractInvocation | ContractDeployment, expectedData: string) = |
| 23 | + let b = hexToSeqByte(expectedData) |
| 24 | + if d.data != b: |
| 25 | + echo "actual: ", d.data.to0xHex() |
| 26 | + echo "expect: ", b.to0xHex() |
| 27 | + doAssert(d.data == b) |
| 28 | + |
| 29 | +contract(TestContract): |
| 30 | + proc getBool(): bool |
| 31 | + proc setBool(a: bool) |
| 32 | + |
| 33 | +contract(TestContractWithConstructor): |
| 34 | + proc init(someArg1, someArg2: UInt256) {.constructor.} |
| 35 | + |
| 36 | +contract(TestContractWithoutConstructor): |
| 37 | + proc dummy() |
| 38 | + |
| 39 | +suite "Contract DSL": |
| 40 | + test "Function calls": |
| 41 | + let c = instantiateContract(TestContract) |
| 42 | + checkData(c.getBool(), "0x12a7b914") |
| 43 | + checkData(c.setBool(true), "0x1e26fd330000000000000000000000000000000000000000000000000000000000000001") |
| 44 | + checkData(c.setBool(false), "0x1e26fd330000000000000000000000000000000000000000000000000000000000000000") |
| 45 | + |
| 46 | + test "Constructors": |
| 47 | + let s = DummySender() |
| 48 | + let dummyContractCode = hexToSeqByte "0xDEADC0DE" |
| 49 | + checkData(s.deployContract(TestContractWithConstructor, dummyContractCode, 1.u256, 2.u256), "0xDEADC0DE00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002") |
| 50 | + checkData(s.deployContract(TestContractWithoutConstructor, dummyContractCode), "0xDEADC0DE") |
0 commit comments