Skip to content

Commit

Permalink
useLogs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad committed Aug 16, 2023
1 parent de0427c commit b4496f1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
3 changes: 1 addition & 2 deletions packages/core/src/helpers/logs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { BlockTag, Filter, FilterByBlockHash, Log } from '@ethersproject/abstract-provider'
import { TypedFilter } from '../hooks/useLogs'
import { Awaited, ContractEventNames, DetailedEventRecord, EventRecord, Falsy, TypedContract } from '../model/types'
import { EventFragment } from 'ethers'
import { BlockTag, EventFragment, Filter, FilterByBlockHash, Log } from 'ethers'

/**
* @internal Intended for internal use - use it on your own risk
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function setItem(key: string, value: any, storage: WindowLocalStorage['localStor
if (value === undefined) {
storage.removeItem(key)
} else {
const toStore = JSON.stringify(value)
const toStore = JSON.stringify(value, (k, v) => k === '_wallets' ? undefined : typeof v === 'bigint' ? v.toString() : v)
try {
storage.setItem(key, toStore)
return JSON.parse(toStore)
Expand Down
27 changes: 13 additions & 14 deletions packages/core/src/hooks/useLogs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TransactionRequest } from '@ethersproject/abstract-provider'
import { Contract } from 'ethers'
import { Contract, TransactionRequest } from 'ethers'
import { expect } from 'chai'
import { ethers, getAddress, ZeroAddress } from 'ethers'
import { Config, ERC20MockInterface } from '../constants'
Expand Down Expand Up @@ -41,7 +40,7 @@ describe('useLogs', () => {
const txData = ERC20MockInterface.encodeFunctionData('transfer(address,uint)', [to, amount])

const tx: TransactionRequest = {
to: token.address,
to: token.target,
value: BigInt(0),
data: txData,
}
Expand All @@ -62,7 +61,7 @@ describe('useLogs', () => {

const fromAddress = from.address
const toAddress = to.address
const amount = BigNumber.from(1)
const amount = BigInt(1)

await sendToken(from, toAddress, amount)

Expand Down Expand Up @@ -94,15 +93,15 @@ describe('useLogs', () => {
expect(getAddress(log.data['from'])).to.equal(getAddress(fromAddress), 'From')
expect(getAddress(log.data['to'])).to.equal(getAddress(toAddress), 'To')
expect(log.data['value']).to.equal(amount, 'Amount')
})
}).timeout(120000)

it('Can get all token transfer logs using the default log query parameters', async () => {
const from = network1.deployer
const to = network1.wallets[0]

const fromAddress = from.address
const toAddress = to.address
const amount = BigNumber.from(1)
const amount = BigInt(1)

await sendToken(from, toAddress, amount)

Expand Down Expand Up @@ -229,7 +228,7 @@ describe('useLogs', () => {

it('Can query mint transfer logs by sender', async () => {
// Send to emit another Transfer token that our filter should filter out
await sendToken(network1.deployer, network1.wallets[1].address, BigNumber.from(1))
await sendToken(network1.deployer, network1.wallets[1].address, BigInt(1))

const { result, waitForCurrent } = await renderDAppHook(
() =>
Expand Down Expand Up @@ -263,7 +262,7 @@ describe('useLogs', () => {

it('Can query mint transfer logs by receiver', async () => {
// Send to emit another Transfer token that our filter should filter out
await sendToken(network1.deployer, network1.wallets[1].address, BigNumber.from(1))
await sendToken(network1.deployer, network1.wallets[1].address, BigInt(1))

const { result, waitForCurrent } = await renderDAppHook(
() =>
Expand Down Expand Up @@ -297,7 +296,7 @@ describe('useLogs', () => {

it('We get an error when we query by un-indexed values', async () => {
// Send to emit another Transfer token that our filter should filter out
await sendToken(network1.deployer, network1.wallets[0].address, BigNumber.from(1))
await sendToken(network1.deployer, network1.wallets[0].address, BigInt(1))

const { result, waitForCurrent } = await renderDAppHook(
() =>
Expand All @@ -322,9 +321,9 @@ describe('useLogs', () => {
expect(result.current?.error).to.not.be.undefined
})

it('Can query by block hash', async () => {
it.only('Can query by block hash', async () => {
// Send to emit another Transfer token that our filter should filter out
const { receipt } = await sendToken(network1.deployer, network1.wallets[0].address, BigNumber.from(1))
const { receipt } = await sendToken(network1.deployer, network1.wallets[0].address, BigInt(1))

const { result, waitForCurrent } = await renderDAppHook(
() =>
Expand Down Expand Up @@ -353,10 +352,10 @@ describe('useLogs', () => {

expect(getAddress(log.data['from'])).to.equal(getAddress(network1.deployer.address), 'From')
expect(getAddress(log.data['to'])).to.equal(getAddress(network1.wallets[0].address), 'To')
expect(log.data['value']).to.equal(BigNumber.from(1), 'Amount')
expect(log.data['value']).to.equal(BigInt(1), 'Amount')
expect(log.blockHash).to.equal(receipt?.blockHash, 'Block hash')
expect(log.blockNumber).to.equal(receipt?.blockNumber, 'Block number')
expect(log.transactionHash).to.equal(receipt?.transactionHash, 'Transaction hash')
expect(log.transactionIndex).to.equal(receipt?.transactionIndex, 'Transaction index')
expect(log.transactionHash).to.equal(receipt?.hash, 'Transaction hash')
expect(log.transactionIndex).to.equal(receipt?.index, 'Transaction index')
})
})
4 changes: 2 additions & 2 deletions packages/core/src/hooks/useLogs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ export interface TypedFilter<
* @returns an array of decoded logs (see {@link LogsResult})
* @public
*/
export function useLogs<T extends TypedContract = Contract, EN extends ContractEventNames<T> = ContractEventNames<T>>(
export function useLogs<T extends Contract = Contract, EN extends ContractEventNames<T> = ContractEventNames<T>>(
filter: TypedFilter<T, EN> | Falsy,
queryParams: LogQueryParams = {}
): LogsResult<T, EN> {
) {
const { fromBlock, toBlock, blockHash } = queryParams

const rawFilter = useMemo(() => encodeFilterData(filter, fromBlock, toBlock, blockHash), [
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/providers/LocalMulticallProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ReactNode, useEffect, useState } from 'react'
import { utils } from 'ethers'
import { getChainById } from '../helpers'
import { useEthers, useBlockNumber, useConfig, useUpdateConfig, useLocalStorage } from '../hooks'
import multicallABI from '../constants/abi/MultiCall.json'
import multicall2ABI from '../constants/abi/MultiCall2.json'
import { deployContract } from '../helpers/contract'
import { isAddress } from 'ethers'

interface LocalMulticallProps {
children: ReactNode
Expand Down Expand Up @@ -39,7 +39,7 @@ export function LocalMulticallProvider({ children }: LocalMulticallProps) {
const checkDeployed = async () => {
const multicallAddress = getCurrent()

if (typeof multicallAddress === 'string' && utils.isAddress(multicallAddress)) {
if (typeof multicallAddress === 'string' && isAddress(multicallAddress)) {
const multicallCode = await library.getCode(multicallAddress)
if (multicallCode !== '0x') {
updateConfig({ multicallAddresses: { [chainId]: multicallAddress } })
Expand Down

0 comments on commit b4496f1

Please sign in to comment.