Skip to content

Commit

Permalink
😶‍🌫️ Add receipt and tests for contract function and transaction (#811)
Browse files Browse the repository at this point in the history
* Add receipt and test for contract function and transaction

* Create add-receipt-for-write-functions.md

* Remove await

Co-authored-by: mateusz <[email protected]>
  • Loading branch information
mj426382 and mateusz authored Jun 7, 2022
1 parent 3faea12 commit edfd60b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/add-receipt-for-write-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@usedapp/core": patch
---

😶‍🌫️ Add receipt for executing contract functions and sending transactions
24 changes: 24 additions & 0 deletions packages/core/src/hooks/useContractFunction.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
})
4 changes: 3 additions & 1 deletion packages/core/src/hooks/useContractFunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -73,7 +74,7 @@ export function useContractFunction<T extends TypedContract, FN extends Contract
const { bufferGasLimitPercentage = 0 } = useConfig()

const send = useCallback(
async (...args: Params<T, FN>): Promise<void> => {
async (...args: Params<T, FN>): Promise<TransactionReceipt | undefined> => {
if (contract) {
const hasOpts = args.length > (contract.interface?.getFunction(functionName).inputs.length ?? 0)

Expand Down Expand Up @@ -107,6 +108,7 @@ export function useContractFunction<T extends TypedContract, FN extends Contract
}, [] as LogDescription[])
setEvents(events)
}
return receipt
}
},
[contract, functionName, options, library]
Expand Down
23 changes: 23 additions & 0 deletions packages/core/src/hooks/useSendTransaction.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,27 @@ describe('useSendTransaction', () => {
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)
})
})
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useSendTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

3 comments on commit edfd60b

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.