wrap transaction response into Confirmable

This is a breaking change for the API of nim-ethers.
This commit is contained in:
Mark Spanbroek 2024-05-14 11:00:45 +02:00 committed by markspanbroek
parent cdb230d30f
commit ab10354910
6 changed files with 18 additions and 25 deletions

View File

@ -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?"

View File

@ -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.

View File

@ -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()

View File

@ -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:

View File

@ -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

View File

@ -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"]: