From e61c70f5bae783872ce70c825f7fa7195b80d1e3 Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Wed, 8 Jun 2022 11:17:16 +0200 Subject: [PATCH] Fix: handle dynamically sized return values Dynamically sized return values such as strings and sequences were not decoded as part of a tuple, leading to a difference in byte representation. --- ethers/contract.nim | 4 ++-- testmodule/testContracts.nim | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/ethers/contract.nim b/ethers/contract.nim index ca92810..036341c 100644 --- a/ethers/contract.nim +++ b/ethers/contract.nim @@ -53,9 +53,9 @@ proc createTransaction(contract: Contract, Transaction(to: contract.address, data: data) proc decodeResponse(T: type, bytes: seq[byte]): T = - without decoded =? AbiDecoder.decode(bytes, T): + without decoded =? AbiDecoder.decode(bytes, (T,)): raiseContractError "unable to decode return value as " & $T - return decoded + return decoded[0] proc call(contract: Contract, function: string, diff --git a/testmodule/testContracts.nim b/testmodule/testContracts.nim index 50da490..f0a2169 100644 --- a/testmodule/testContracts.nim +++ b/testmodule/testContracts.nim @@ -16,6 +16,7 @@ type receiver {.indexed.}: Address value: UInt256 +method name*(erc20: Erc20): string {.base, contract, view.} method totalSupply*(erc20: Erc20): UInt256 {.base, contract, view.} method balanceOf*(erc20: Erc20, account: Address): UInt256 {.base, contract, view.} method allowance*(erc20: Erc20, owner, spender: Address): UInt256 {.base, contract, view.} @@ -40,6 +41,7 @@ suite "Contracts": discard await provider.send("evm_revert", @[snapshot]) test "can call constant functions": + check (await token.name()) == "TestToken" check (await token.totalSupply()) == 0.u256 check (await token.balanceOf(accounts[0])) == 0.u256 check (await token.allowance(accounts[0], accounts[1])) == 0.u256