Skip to content

Commit

Permalink
🧪 Add estimateContractFunctionGasLimit tests (#807)
Browse files Browse the repository at this point in the history
* Add tests

* Add useContractFunction test

* Change to at most

* Rename started into starting

Co-authored-by: mateusz <[email protected]>
  • Loading branch information
mj426382 and mateusz authored Jun 7, 2022
1 parent a3b1340 commit 3faea12
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
21 changes: 20 additions & 1 deletion packages/core/src/hooks/estimateGasLimit.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { expect } from 'chai'
import { MockProvider } from 'ethereum-waffle'
import { estimateTransactionGasLimit } from './usePromiseTransaction'
import { Contract } from 'ethers'
import { deployMockToken } from '../testing'
import { estimateContractFunctionGasLimit, estimateTransactionGasLimit } from './usePromiseTransaction'

const BASE_TX_COST = 21000
const LIMITED_TX_COST = 23100 // 21000 * 1.1

const CONTRACT_FUNCTION_COST = 51941 // mock transfer transaction cost

describe('estimateGasLimit', () => {
const mockProvider = new MockProvider()
const [signer, receiver] = mockProvider.getWallets()
let token: Contract

beforeEach(async () => {
token = await deployMockToken(signer)
})

it('sending ether transaction', async () => {
const gasLimit = await estimateTransactionGasLimit(
Expand Down Expand Up @@ -62,4 +71,14 @@ describe('estimateGasLimit', () => {

expect(gasLimit).to.equal(LIMITED_TX_COST)
})

it('transfer token', async () => {
const gasLimit = await estimateContractFunctionGasLimit(token, 'transfer', [receiver.address, 1], 0)
expect(gasLimit).to.equal(CONTRACT_FUNCTION_COST)
})

it('transfer token with limit', async () => {
const gasLimit = await estimateContractFunctionGasLimit(token, 'transfer', [receiver.address, 1], 100)
expect(gasLimit).to.equal(2 * CONTRACT_FUNCTION_COST)
})
})
31 changes: 29 additions & 2 deletions packages/core/src/hooks/useContractFunction.test.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { useContractFunction } from '../../src'
import { Config, useContractFunction } from '../../src'
import { expect } from 'chai'
import { MockProvider } from 'ethereum-waffle'
import { BigNumber, Contract } from 'ethers'
import { renderWeb3Hook, contractCallOutOfGasMock, deployMockToken } from '../../src/testing'
import { renderWeb3Hook, contractCallOutOfGasMock, deployMockToken, setupTestingConfig } from '../../src/testing'
import { renderDAppHook } from '../testing/renderDAppHook'

const CONTRACT_FUNCTION_COST = 51941 // mock transfer transaction cost

describe('useContractFunction', () => {
const mockProvider = new MockProvider()
const [deployer, spender] = mockProvider.getWallets()
let token: Contract
let config: Config

beforeEach(async () => {
;({ config } = await setupTestingConfig())
token = await deployMockToken(deployer)
})

Expand Down Expand Up @@ -94,4 +99,26 @@ describe('useContractFunction', () => {
await waitForNextUpdate()
await result.current.send(spender.address, 200)
})

it('transfer amount with limit', async () => {
const { result, waitForCurrent, waitForNextUpdate } = await renderDAppHook(
() => useContractFunction(token, 'transfer'),
{
config: {
...config,
bufferGasLimitPercentage: 100,
},
}
)

await waitForNextUpdate()
const startingBalance = await deployer.getBalance()
await result.current.send(spender.address, 200)
await waitForCurrent((val) => val.state !== undefined)

expect(result.current.state.status).to.eq('Success')
const finalBalance = await deployer.getBalance()
const txCost = finalBalance.sub(startingBalance)
expect(txCost).to.be.at.most(2 * CONTRACT_FUNCTION_COST)
})
})

3 comments on commit 3faea12

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