Skip to content

Commit

Permalink
➕ Add queryParams to useToken hook (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
rockorama committed Dec 7, 2022
1 parent 9d76a8e commit 737d43c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-garlics-relate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@usedapp/core': patch
---

Add queryParams to useToken hook
42 changes: 29 additions & 13 deletions packages/core/src/hooks/useToken.test.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,51 @@
import { Contract } from 'ethers'
import { BigNumber, Contract } from 'ethers'
import { useToken } from '..'
import { expect } from 'chai'
import type { Config } from '../constants'
import {
renderDAppHook,
deployMockToken,
MOCK_TOKEN_INITIAL_BALANCE,
SECOND_MOCK_TOKEN_INITIAL_BALANCE,
TestingNetwork,
setupTestingConfig,
} from '../testing'

describe('useToken', async () => {
let token: Contract
let secondToken: Contract
let config: Config
let network1: TestingNetwork
let network2: TestingNetwork

beforeEach(async () => {
;({ config, network1 } = await setupTestingConfig())
;({ config, network1, network2 } = await setupTestingConfig())
token = await deployMockToken(network1.deployer)
secondToken = await deployMockToken(network2.deployer, SECOND_MOCK_TOKEN_INITIAL_BALANCE)
})

it('returns correct token constants', async () => {
const { result, waitForCurrent } = await renderDAppHook(() => useToken(token.address), {
await testMultiChainUseToken(token)
})

it('setting chainId on query params returns correct token constants from non-default chain', async () => {
await testMultiChainUseToken(secondToken, SECOND_MOCK_TOKEN_INITIAL_BALANCE, network2.chainId)
})

it('should not throw error when token address is Falsy', async () => {
const { result } = await renderDAppHook(() => useToken(null), {
config,
})
expect(result.error).to.be.undefined
expect(result.current).to.be.undefined
})

const testMultiChainUseToken = async (
contract: Contract,
totalSupply: BigNumber = MOCK_TOKEN_INITIAL_BALANCE,
chainId?: number
) => {
const { result, waitForCurrent } = await renderDAppHook(() => useToken(contract.address, { chainId }), {
config,
})
await waitForCurrent((val) => val !== undefined)
Expand All @@ -30,16 +54,8 @@ describe('useToken', async () => {
name: 'MOCKToken',
symbol: 'MOCK',
decimals: 18,
totalSupply: MOCK_TOKEN_INITIAL_BALANCE,
totalSupply,
}
expect(JSON.parse(JSON.stringify(result.current))).to.deep.equal(JSON.parse(JSON.stringify(res)))
})

it('should not throw error when token address is Falsy', async () => {
const { result } = await renderDAppHook(() => useToken(null), {
config,
})
expect(result.error).to.be.undefined
expect(result.current).to.be.undefined
})
}
})
7 changes: 4 additions & 3 deletions packages/core/src/hooks/useToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ERC20Interface } from '../constants'
import { ERC20Interface, QueryParams } from '../constants'
import { Falsy } from '../model/types'
import { TokenInfo } from '../model/TokenInfo'
import { Call, useCalls } from './useCall'
Expand All @@ -7,6 +7,7 @@ import { Contract } from 'ethers'
/**
* Returns name, symbol, decimals and token supply of a given token.
* @param tokenAddress address of a token contract.
* @param queryParams see {@link QueryParams}.
* @returns a token info object (see {@link TokenInfo}) or `undefined` if all four methods don't exist on a token.
* @public
* @example
Expand All @@ -22,7 +23,7 @@ import { Contract } from 'ethers'
* </>
* ) : null
*/
export function useToken(tokenAddress: string | Falsy): TokenInfo | undefined {
export function useToken(tokenAddress: string | Falsy, queryParams: QueryParams = {}): TokenInfo | undefined {
const partialCall = tokenAddress && {
contract: new Contract(tokenAddress, ERC20Interface),
address: tokenAddress,
Expand All @@ -31,7 +32,7 @@ export function useToken(tokenAddress: string | Falsy): TokenInfo | undefined {
const args = ['name', 'symbol', 'decimals', 'totalSupply'].map(
(method): Call | Falsy => partialCall && { ...partialCall, method }
)
const [name, symbol, decimals, totalSupply] = useCalls(args)
const [name, symbol, decimals, totalSupply] = useCalls(args, queryParams)

if (!name && !symbol && !decimals && !totalSupply) {
return undefined
Expand Down

2 comments on commit 737d43c

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