From ab1035491014eb33281efe7a21bfe8afcd1c6bff Mon Sep 17 00:00:00 2001 From: Mark Spanbroek Date: Tue, 14 May 2024 11:00:45 +0200 Subject: [PATCH] wrap transaction response into Confirmable This is a breaking change for the API of nim-ethers. --- ethers/contract.nim | 10 ++++++---- ethers/erc20.nim | 10 +++++----- testmodule/helpers.nim | 2 +- testmodule/testContracts.nim | 17 ++++------------- testmodule/testCustomErrors.nim | 2 +- testmodule/testErc20.nim | 2 +- 6 files changed, 18 insertions(+), 25 deletions(-) diff --git a/ethers/contract.nim b/ethers/contract.nim index dddd61b..f204d85 100644 --- a/ethers/contract.nim +++ b/ethers/contract.nim @@ -37,7 +37,8 @@ type blockTag*: ?BlockTag ContractError* = object of EthersError - Confirmable* = ?TransactionResponse + Confirmable* = object + response*: ?TransactionResponse EventHandler*[E: Event] = proc(event: E) {.gcsafe, raises:[].} func new*(ContractType: type Contract, @@ -229,7 +230,8 @@ func addContractCall(procedure: var NimNode) = "missing {.view.}, {.pure.} or {.getter.} ?" .} let convert = customErrorConversion(`errors`) - return await send(`contract`, `function`, `parameters`, overrides, convert) + let response = await send(`contract`, `function`, `parameters`, overrides, convert) + Confirmable(response: response) procedure[6] = if procedure.isConstant: @@ -294,7 +296,7 @@ proc subscribe*[E: Event](contract: Contract, contract.provider.subscribe(filter, logHandler) -proc confirm*(tx: Future[?TransactionResponse], +proc confirm*(tx: Future[Confirmable], confirmations: int = EthersDefaultConfirmations, timeout: int = EthersReceiptTimeoutBlks): Future[TransactionReceipt] {.async.} = @@ -303,7 +305,7 @@ proc confirm*(tx: Future[?TransactionResponse], ## `await token.connect(signer0) ## .mint(accounts[1], 100.u256) ## .confirm(3)` - without response =? (await tx): + without response =? (await tx).response: raise newException( EthersError, "Transaction hash required. Possibly was a call instead of a send?" diff --git a/ethers/erc20.nim b/ethers/erc20.nim index 5b49275..f0ef2e1 100644 --- a/ethers/erc20.nim +++ b/ethers/erc20.nim @@ -46,17 +46,17 @@ method allowance*(token: Erc20Token, method transfer*(token: Erc20Token, recipient: Address, - amount: UInt256): ?TransactionResponse {.base, contract.} + amount: UInt256): Confirmable {.base, contract.} ## Moves `amount` tokens from the caller's account to `recipient`. method approve*(token: Erc20Token, spender: Address, - amount: UInt256): ?TransactionResponse {.base, contract.} + amount: UInt256): Confirmable {.base, contract.} ## Sets `amount` as the allowance of `spender` over the caller's tokens. method increaseAllowance*(token: Erc20Token, spender: Address, - addedValue: UInt256): ?TransactionResponse {.base, contract.} + addedValue: UInt256): Confirmable {.base, contract.} ## Atomically increases the allowance granted to spender by the caller. ## This is an alternative to approve that can be used as a mitigation for problems described in IERC20.approve. ## Emits an Approval event indicating the updated allowance. @@ -65,7 +65,7 @@ method increaseAllowance*(token: Erc20Token, method decreaseAllowance*(token: Erc20Token, spender: Address, - addedValue: UInt256): ?TransactionResponse {.base, contract.} + addedValue: UInt256): Confirmable {.base, contract.} ## Atomically decreases the allowance granted to spender by the caller. ## This is an alternative to approve that can be used as a mitigation for problems described in IERC20.approve. ## Emits an Approval event indicating the updated allowance. @@ -75,6 +75,6 @@ method decreaseAllowance*(token: Erc20Token, method transferFrom*(token: Erc20Token, spender: Address, recipient: Address, - amount: UInt256): ?TransactionResponse {.base, contract.} + amount: UInt256): Confirmable {.base, contract.} ## Moves `amount` tokens from `spender` to `recipient` using the allowance ## mechanism. `amount` is then deducted from the caller's allowance. diff --git a/testmodule/helpers.nim b/testmodule/helpers.nim index ced15ca..689c085 100644 --- a/testmodule/helpers.nim +++ b/testmodule/helpers.nim @@ -7,7 +7,7 @@ type method doRevert*( self: TestHelpers, revertReason: string -): ?TransactionResponse {.base, contract.} +): Confirmable {.base, contract.} proc new*(_: type TestHelpers, signer: Signer): TestHelpers = let deployment = readDeployment() diff --git a/testmodule/testContracts.nim b/testmodule/testContracts.nim index e5727a8..86079fa 100644 --- a/testmodule/testContracts.nim +++ b/testmodule/testContracts.nim @@ -13,7 +13,7 @@ import ./mocks type TestToken = ref object of Erc20Token -method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.} +method mint(token: TestToken, holder: Address, amount: UInt256): Confirmable {.base, contract.} method myBalance(token: TestToken): UInt256 {.base, contract, view.} for url in ["ws://localhost:8545", "http://localhost:8545"]: @@ -71,24 +71,15 @@ for url in ["ws://localhost:8545", "http://localhost:8545"]: await token.mint(accounts[1], 100.u256) check (await balanceOf(token, accounts[1])) == 100.u256 - test "can call non-constant functions with a ?TransactionResponse return type": - token = TestToken.new(token.address, provider.getSigner()) - proc mint(token: TestToken, - holder: Address, - amount: UInt256): ?TransactionResponse {.contract.} - let txResp = await token.mint(accounts[1], 100.u256) - check txResp is (?TransactionResponse) - check txResp.isSome - test "can call non-constant functions with a Confirmable return type": token = TestToken.new(token.address, provider.getSigner()) proc mint(token: TestToken, holder: Address, amount: UInt256): Confirmable {.contract.} - let txResp = await token.mint(accounts[1], 100.u256) - check txResp is Confirmable - check txResp.isSome + let confirmable = await token.mint(accounts[1], 100.u256) + check confirmable is Confirmable + check confirmable.response.isSome test "fails to compile when function has an implementation": let works = compiles: diff --git a/testmodule/testCustomErrors.nim b/testmodule/testCustomErrors.nim index 752ea86..e66c384 100644 --- a/testmodule/testCustomErrors.nim +++ b/testmodule/testCustomErrors.nim @@ -113,7 +113,7 @@ suite "Contract custom errors": check error.arguments.two == true test "handles transaction confirmation errors": - proc revertsTransaction(contract: TestCustomErrors): ?TransactionResponse + proc revertsTransaction(contract: TestCustomErrors): Confirmable {.contract, errors:[ErrorWithArguments].} # skip gas estimation diff --git a/testmodule/testErc20.nim b/testmodule/testErc20.nim index 3643820..7f597f6 100644 --- a/testmodule/testErc20.nim +++ b/testmodule/testErc20.nim @@ -9,7 +9,7 @@ import ./hardhat type TestToken = ref object of Erc20Token -method mint(token: TestToken, holder: Address, amount: UInt256): ?TransactionResponse {.base, contract.} +method mint(token: TestToken, holder: Address, amount: UInt256): Confirmable {.base, contract.} for url in ["ws://localhost:8545", "http://localhost:8545"]: