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,