Skip to content

Commit e51139d

Browse files
committed
Contract constructor support
1 parent be1bb30 commit e51139d

File tree

3 files changed

+233
-139
lines changed

3 files changed

+233
-139
lines changed

tests/all_tests.nim

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ import
1919
test_json_marshalling,
2020
test_signed_tx,
2121
test_execution_types,
22-
test_string_decoder
22+
test_string_decoder,
23+
test_contract_dsl

tests/test_contract_dsl.nim

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
 (0)