From edfd60b951734253a594c3b9fd8ebdbfb97ed0ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Jandu=C5=82a?= <71319308+mj426382@users.noreply.github.com> Date: Tue, 7 Jun 2022 15:19:49 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=98=B6=E2=80=8D=F0=9F=8C=AB=EF=B8=8F=20Ad?= =?UTF-8?q?d=20receipt=20and=20tests=20for=20contract=20function=20and=20t?= =?UTF-8?q?ransaction=20(#811)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add receipt and test for contract function and transaction * Create add-receipt-for-write-functions.md * Remove await Co-authored-by: mateusz --- .changeset/add-receipt-for-write-functions.md | 5 ++++ .../src/hooks/useContractFunction.test.tsx | 24 +++++++++++++++++++ .../core/src/hooks/useContractFunction.ts | 4 +++- .../core/src/hooks/useSendTransaction.test.ts | 23 ++++++++++++++++++ packages/core/src/hooks/useSendTransaction.ts | 2 +- 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 .changeset/add-receipt-for-write-functions.md diff --git a/.changeset/add-receipt-for-write-functions.md b/.changeset/add-receipt-for-write-functions.md new file mode 100644 index 000000000..0b090f617 --- /dev/null +++ b/.changeset/add-receipt-for-write-functions.md @@ -0,0 +1,5 @@ +--- +"@usedapp/core": patch +--- + +πŸ˜Άβ€πŸŒ«οΈ Add receipt for executing contract functions and sending transactions diff --git a/packages/core/src/hooks/useContractFunction.test.tsx b/packages/core/src/hooks/useContractFunction.test.tsx index d4baebb09..b66b2a552 100644 --- a/packages/core/src/hooks/useContractFunction.test.tsx +++ b/packages/core/src/hooks/useContractFunction.test.tsx @@ -121,4 +121,28 @@ describe('useContractFunction', () => { const txCost = finalBalance.sub(startingBalance) expect(txCost).to.be.at.most(2 * CONTRACT_FUNCTION_COST) }) + + it('success with correct receipt', async () => { + const { result, waitForCurrent, waitForNextUpdate } = await renderWeb3Hook( + () => useContractFunction(token, 'approve'), + { + mockProvider, + } + ) + await waitForNextUpdate() + await result.current.send(spender.address, 200) + await waitForCurrent((val) => val.state !== undefined) + + expect(result.current.state.status).to.eq('Success') + expect(await token.allowance(deployer.address, spender.address)).to.eq(200) + + expect(result.current.state.receipt).to.not.be.undefined + expect(result.current.state.receipt?.to).to.eq(token.address) + expect(result.current.state.receipt?.from).to.eq(deployer.address) + expect(result.current.state.receipt?.gasUsed).to.be.gt(0) + expect(result.current.state.receipt?.status).to.eq(1) + expect(result.current.state.receipt?.blockHash).to.match(/^0x/) + expect(result.current.state.receipt?.transactionHash).to.match(/^0x/) + expect(result.current.state.receipt?.gasUsed).to.be.gt(0) + }) }) diff --git a/packages/core/src/hooks/useContractFunction.ts b/packages/core/src/hooks/useContractFunction.ts index 0b45a49ca..3e2d553d1 100644 --- a/packages/core/src/hooks/useContractFunction.ts +++ b/packages/core/src/hooks/useContractFunction.ts @@ -6,6 +6,7 @@ import { useEthers } from './useEthers' import { estimateContractFunctionGasLimit, usePromiseTransaction } from './usePromiseTransaction' import { LogDescription } from 'ethers/lib/utils' import { ContractFunctionNames, Falsy, Params, TypedContract } from '../model/types' +import { TransactionReceipt } from '@ethersproject/abstract-provider' type JsonRpcProvider = providers.JsonRpcProvider @@ -73,7 +74,7 @@ export function useContractFunction): Promise => { + async (...args: Params): Promise => { if (contract) { const hasOpts = args.length > (contract.interface?.getFunction(functionName).inputs.length ?? 0) @@ -107,6 +108,7 @@ export function useContractFunction { expect(result.current.state.status).to.eq('Exception') expect(result.current.state.errorMessage).to.eq('invalid address') }) + + it('Returns receipt after correct transaction', async () => { + const { result, waitForCurrent } = await renderWeb3Hook(useSendTransaction, { mockProvider }) + + const spenderBalance = await spender.getBalance() + const receiverBalance = await receiver.getBalance() + + await result.current.sendTransaction({ to: receiver.address, value: BigNumber.from(10), gasPrice: 0 }) + + await waitForCurrent((val) => val.state !== undefined) + expect(result.current.state.status).to.eq('Success') + expect(await receiver.getBalance()).to.eq(receiverBalance.add(10)) + expect(await spender.getBalance()).to.eq(spenderBalance.sub(10)) + + expect(result.current.state.receipt).to.not.be.undefined + expect(result.current.state.receipt?.to).to.eq(receiver.address) + expect(result.current.state.receipt?.from).to.eq(spender.address) + expect(result.current.state.receipt?.gasUsed).to.be.gt(0) + expect(result.current.state.receipt?.status).to.eq(1) + expect(result.current.state.receipt?.blockHash).to.match(/^0x/) + expect(result.current.state.receipt?.transactionHash).to.match(/^0x/) + expect(result.current.state.receipt?.gasUsed).to.be.gt(0) + }) }) diff --git a/packages/core/src/hooks/useSendTransaction.ts b/packages/core/src/hooks/useSendTransaction.ts index 33154f67c..e84567e1c 100644 --- a/packages/core/src/hooks/useSendTransaction.ts +++ b/packages/core/src/hooks/useSendTransaction.ts @@ -36,7 +36,7 @@ export function useSendTransaction(options?: TransactionOptions) { if (signer) { const gasLimit = await estimateTransactionGasLimit(transactionRequest, signer, bufferGasLimitPercentage) - await promiseTransaction( + return promiseTransaction( signer.sendTransaction({ ...transactionRequest, gasLimit,