Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(react): out-of-sync persisted contract results #552

Merged
merged 6 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions packages/react/src/hooks/contracts/useContractRead.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { useQueryClient } from 'react-query'

import { QueryConfig, QueryFunctionArgs } from '../../types'
import { useBlockNumber } from '../network-status'
import { useChainId, useQuery } from '../utils'
import { parseContractResult, useChainId, useQuery } from '../utils'

type UseContractReadArgs = Partial<ReadContractConfig> & {
/** If set to `true`, the cache will depend on the block number */
Expand Down Expand Up @@ -127,7 +127,7 @@ export function useContractRead(
watch,
])

return useQuery(queryKey_, queryFn, {
const contractReadQuery = useQuery(queryKey_, queryFn, {
tmm marked this conversation as resolved.
Show resolved Hide resolved
cacheTime,
enabled,
staleTime,
Expand All @@ -136,4 +136,19 @@ export function useContractRead(
onSettled,
onSuccess,
})

const data = React.useMemo(
() =>
parseContractResult({
contractInterface: contractConfig.contractInterface,
data: contractReadQuery.data,
functionName,
}),
[contractReadQuery.data],
)

return {
...contractReadQuery,
data,
}
}
1 change: 1 addition & 0 deletions packages/react/src/hooks/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export { parseContractResult } from './parseContractResult'
tmm marked this conversation as resolved.
Show resolved Hide resolved
export { useChainId } from './useChainId'
export { useForceUpdate } from './useForceUpdate'
export { useQuery } from './useQuery'
47 changes: 47 additions & 0 deletions packages/react/src/hooks/utils/parseContractResult.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { BigNumber } from 'ethers'

import { parseContractResult } from './parseContractResult'

const contractInterface = [
{
inputs: [
{
internalType: 'uint256',
name: '',
type: 'uint256',
},
],
name: 'gms',
tmm marked this conversation as resolved.
Show resolved Hide resolved
outputs: [
{
internalType: 'uint256',
name: 'timestamp',
type: 'uint256',
},
{
internalType: 'address',
name: 'sender',
type: 'address',
},
],
stateMutability: 'view',
type: 'function',
},
]

describe('parseContractResult', () => {
it('should parse the data to an ethers Result if there are no named keys', () => {
tmm marked this conversation as resolved.
Show resolved Hide resolved
const data = [
BigNumber.from(1654322661),
'0xa5cc3c03994DB5b0d9A5eEdD10CabaB0813678AC',
]
expect(
parseContractResult({
contractInterface,
data,

functionName: 'gms',
}),
).toEqual(Object.assign(data, { timestamp: data[0], sender: data[1] }))
})
})
28 changes: 28 additions & 0 deletions packages/react/src/hooks/utils/parseContractResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Contract, ContractInterface } from 'ethers/lib/ethers'
import { FunctionFragment, Result } from 'ethers/lib/utils'

export function parseContractResult({
contractInterface,
data,
functionName,
}: {
contractInterface: ContractInterface
data: Result | undefined
functionName: string
}) {
if (data && Array.isArray(data) && Object.keys(data).length === data.length) {
const { fragments } = Contract.getInterface(contractInterface)
const functionFragment = FunctionFragment.from(
fragments.find((fragment) => fragment.name === functionName) || {},
)
const dataObject = functionFragment.outputs?.reduce(
(dataObject, output, i) => ({
...dataObject,
[output.name]: data[i],
}),
{},
)
return Object.assign(data, dataObject)
}
return data
}