Skip to content

Commit

Permalink
Contract constructor support
Browse files Browse the repository at this point in the history
  • Loading branch information
yglukhov committed Jan 3, 2024
1 parent be1bb30 commit e51139d
Show file tree
Hide file tree
Showing 3 changed files with 233 additions and 139 deletions.
3 changes: 2 additions & 1 deletion tests/all_tests.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ import
test_json_marshalling,
test_signed_tx,
test_execution_types,
test_string_decoder
test_string_decoder,
test_contract_dsl
50 changes: 50 additions & 0 deletions tests/test_contract_dsl.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# nim-web3
# Copyright (c) 2018-2024 Status Research & Development GmbH
# Licensed under either of
# * Apache License, version 2.0, ([LICENSE-APACHE](LICENSE-APACHE))
# * MIT license ([LICENSE-MIT](LICENSE-MIT))
# at your option.
# This file may not be copied, modified, or distributed except according to
# those terms.

import
pkg/unittest2,
stew/byteutils,
stint,
../web3/contract_dsl

type
DummySender = object

proc instantiateContract(t: typedesc): ContractInstance[t, DummySender] =
discard

proc checkData(d: ContractInvocation | ContractDeployment, expectedData: string) =
let b = hexToSeqByte(expectedData)
if d.data != b:
echo "actual: ", d.data.to0xHex()
echo "expect: ", b.to0xHex()
doAssert(d.data == b)

contract(TestContract):
proc getBool(): bool
proc setBool(a: bool)

contract(TestContractWithConstructor):
proc init(someArg1, someArg2: UInt256) {.constructor.}

contract(TestContractWithoutConstructor):
proc dummy()

suite "Contract DSL":
test "Function calls":
let c = instantiateContract(TestContract)
checkData(c.getBool(), "0x12a7b914")
checkData(c.setBool(true), "0x1e26fd330000000000000000000000000000000000000000000000000000000000000001")
checkData(c.setBool(false), "0x1e26fd330000000000000000000000000000000000000000000000000000000000000000")

test "Constructors":
let s = DummySender()
let dummyContractCode = hexToSeqByte "0xDEADC0DE"
checkData(s.deployContract(TestContractWithConstructor, dummyContractCode, 1.u256, 2.u256), "0xDEADC0DE00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000002")
checkData(s.deployContract(TestContractWithoutConstructor, dummyContractCode), "0xDEADC0DE")
Loading

0 comments on commit e51139d

Please sign in to comment.