fix: eth_call use signers address (#43)

This commit is contained in:
Adam Uhlíř 2023-06-13 16:24:59 +02:00 committed by GitHub
parent 5a4f786757
commit 18e225607c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 3 deletions

View File

@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
nim: [1.2.16, stable]
nim: [1.6.12, stable]
steps:
- name: Checkout
uses: actions/checkout@v2

View File

@ -3,6 +3,7 @@ author = "Nim Ethers Authors"
description = "library for interacting with Ethereum"
license = "MIT"
requires "nim >= 1.6.0"
requires "chronos >= 3.0.0 & < 4.0.0"
requires "contractabi >= 0.4.6 & < 0.5.0"
requires "questionable >= 0.10.2 & < 0.11.0"

View File

@ -93,7 +93,11 @@ proc call(contract: Contract,
function: string,
parameters: tuple,
overrides = TransactionOverrides()) {.async.} =
let transaction = createTransaction(contract, function, parameters, overrides)
var transaction = createTransaction(contract, function, parameters, overrides)
if signer =? contract.signer and transaction.sender.isNone:
transaction.sender = some(await signer.getAddress())
discard await contract.provider.call(transaction, overrides)
proc call(contract: Contract,
@ -102,7 +106,11 @@ proc call(contract: Contract,
ReturnType: type,
returnMultiple: static bool,
overrides = TransactionOverrides()): Future[ReturnType] {.async.} =
let transaction = createTransaction(contract, function, parameters, overrides)
var transaction = createTransaction(contract, function, parameters, overrides)
if signer =? contract.signer and transaction.sender.isNone:
transaction.sender = some(await signer.getAddress())
let response = await contract.provider.call(transaction, overrides)
return decodeResponse(ReturnType, returnMultiple, response)

View File

@ -13,6 +13,7 @@ type
TestToken = ref object of Erc20Token
method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.}
method myBalance(token: TestToken): UInt256 {.contract, view.}
suite "Contracts":
@ -43,6 +44,13 @@ suite "Contracts":
check (await token.totalSupply()) == 100.u256
check (await token.balanceOf(accounts[1])) == 100.u256
test "can call constant functions with a signer and the account is used for the call":
let signer0 = provider.getSigner(accounts[0])
let signer1 = provider.getSigner(accounts[1])
discard await token.connect(signer0).mint(accounts[1], 100.u256)
check (await token.connect(signer0).myBalance()) == 0.u256
check (await token.connect(signer1).myBalance()) == 100.u256
test "can call non-constant functions without a signer":
discard await token.mint(accounts[1], 100.u256)
check (await token.balanceOf(accounts[1])) == 0.u256

View File

@ -17,4 +17,8 @@ contract TestToken is ERC20 {
function burn(address holder, uint amount) public {
_burn(holder, amount);
}
function myBalance() public view returns (uint256) {
return balanceOf(msg.sender);
}
}