Skip to content

Commit

Permalink
chore: update viem
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Apr 9, 2024
1 parent 2ffeecc commit 845c779
Show file tree
Hide file tree
Showing 19 changed files with 102 additions and 31 deletions.
Binary file modified bun.lockb
Binary file not shown.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"ethers": "^6.7.1",
"eventemitter3": "^5.0.1",
"human-id": "^4.0.0",
"mipd": "^0.0.5",
"mipd": "^0.0.7",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-flatten-children": "^1.1.2",
Expand All @@ -70,7 +70,7 @@
"remeda": "^1.14.0",
"sonner": "^0.7.2",
"use-sync-external-store": "^1.2.0",
"viem": "^1.10.12",
"viem": "^2.9.13",
"zustand": "^4.3.8"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion src/design-system/components/ButtonCopy.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { forwardRef, useEffect, useState } from 'react'

import type { UnionOmit } from '~/utils/types'
import type { UnionOmit } from '~/types/utils'

import { ButtonRoot, type ButtonRootProps } from './Button'
import type { ButtonHeight } from './Button.css'
Expand Down
2 changes: 1 addition & 1 deletion src/design-system/components/ButtonSymbol.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { forwardRef } from 'react'

import type { UnionOmit } from '~/utils/types'
import type { UnionOmit } from '~/types/utils'

import { Tooltip } from '../../components'
import type { SymbolName } from '../tokens'
Expand Down
3 changes: 2 additions & 1 deletion src/entries/background/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
custom,
numberToHex,
} from 'viem'
import { type RpcResponse, rpc } from 'viem/utils'
import { rpc } from 'viem/utils'

import {
UnsupportedProviderMethodError,
UserRejectedRequestError,
} from '~/errors'
import { type Messenger, getMessenger } from '~/messengers'
import type { RpcResponse } from '~/types/rpc'
import { buildChain } from '~/viem'
import {
accountStore,
Expand Down
3 changes: 1 addition & 2 deletions src/errors/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { RpcRequest } from 'viem/utils'

import type { RpcRequest } from '~/types/rpc'
import { BaseError } from './base.js'

export type ProviderRpcErrorCode =
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useErc20Balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export function useErc20BalanceQueryOptions({
const contract = getContract({
address: tokenAddress,
abi: erc20Abi,
publicClient: client,
client,
})
return contract.read.balanceOf([address])
},
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useErc20Metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export function useErc20MetadataQueryOptions({
const contract = getContract({
address: tokenAddress,
abi: erc20Abi,
publicClient: client,
client,
})
const [name, symbol, decimals, totalSupply] = await Promise.all([
contract.read.name(),
Expand Down
16 changes: 13 additions & 3 deletions src/hooks/useReadContract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
type BaseError,
type CallParameters,
type Client,
type ContractFunctionName,
type DecodeFunctionResultParameters,
type EncodeFunctionDataParameters,
type Hash,
Expand All @@ -18,7 +19,10 @@ import { useClient } from './useClient'

type ReadContractParameters<
TAbi extends Abi | readonly unknown[] = Abi,
TFunctionName extends string = string,
TFunctionName extends ContractFunctionName<
TAbi,
'pure' | 'view'
> = ContractFunctionName<TAbi, 'pure' | 'view'>,
> = ReadContractParameters_viem<TAbi, TFunctionName> & {
enabled?: boolean
raw?: boolean
Expand All @@ -31,7 +35,10 @@ export const readContractQueryKey = createQueryKey<

export function useReadContractQueryOptions<
const TAbi extends Abi | readonly unknown[] = Abi,
TFunctionName extends string = string,
TFunctionName extends ContractFunctionName<
TAbi,
'pure' | 'view'
> = ContractFunctionName<TAbi, 'pure' | 'view'>,
>(params: ReadContractParameters<TAbi, TFunctionName>) {
const client = useClient()
return queryOptions({
Expand Down Expand Up @@ -72,7 +79,10 @@ export function useReadContractQueryOptions<

export function useReadContract<
const TAbi extends Abi | readonly unknown[] = Abi,
TFunctionName extends string = string,
TFunctionName extends ContractFunctionName<
TAbi,
'pure' | 'view'
> = ContractFunctionName<TAbi, 'pure' | 'view'>,
>(args: ReadContractParameters<TAbi, TFunctionName>) {
const queryOptions = useReadContractQueryOptions(args)
return useQuery(queryOptions)
Expand Down
27 changes: 22 additions & 5 deletions src/hooks/useWriteContract.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,38 @@
import { useMutation } from '@tanstack/react-query'
import type { Abi, Account, Chain, WriteContractParameters } from 'viem'
import type {
Abi,
Account,
Chain,
ContractFunctionArgs,
ContractFunctionName,
WriteContractParameters,
} from 'viem'

import { useClient } from './useClient'

type UseWriteContractParameters<
TAbi extends Abi | readonly unknown[],
TFunctionName extends string,
> = WriteContractParameters<TAbi, TFunctionName, Chain, Account>
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
TArgs extends ContractFunctionArgs<
TAbi,
'nonpayable' | 'payable',
TFunctionName
> = ContractFunctionArgs<TAbi, 'nonpayable' | 'payable', TFunctionName>,
> = WriteContractParameters<TAbi, TFunctionName, TArgs, Chain, Account>

export function useWriteContract<
const TAbi extends Abi | readonly unknown[],
TFunctionName extends string,
TFunctionName extends ContractFunctionName<TAbi, 'nonpayable' | 'payable'>,
TArgs extends ContractFunctionArgs<
TAbi,
'nonpayable' | 'payable',
TFunctionName
>,
>() {
const client = useClient()

return useMutation({
mutationFn(args: UseWriteContractParameters<TAbi, TFunctionName>) {
mutationFn(args: UseWriteContractParameters<TAbi, TFunctionName, TArgs>) {
return client.writeContract({ ...args, chain: null } as any)
},
})
Expand Down
6 changes: 2 additions & 4 deletions src/messengers/schema.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import type { Address, EIP1193Parameters, EIP1474Methods } from 'viem'
import type { RpcResponse } from 'viem/utils'
import type { Address } from 'viem'

import type { RpcRequest, RpcResponse } from '~/types/rpc'
import type { SessionsState } from '~/zustand'

export type RpcRequest = EIP1193Parameters<EIP1474Methods> & { id: number }

export type Schema = {
accountsChanged: [
payload: { accounts: Address[]; sessions: SessionsState['sessions'] },
Expand Down
2 changes: 1 addition & 1 deletion src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
UserRejectedRequestError,
} from '~/errors'
import type { Messenger } from '~/messengers'
import type { RpcRequest } from '~/messengers/schema'
import type { RpcRequest } from '~/types/rpc'

const providerCache = new Map<string, EIP1193Provider>()
export function getProvider({
Expand Down
39 changes: 39 additions & 0 deletions src/types/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { EIP1193Parameters, EIP1474Methods } from 'viem'

type SuccessResult<T> = {
method?: never | undefined
result: T
error?: never | undefined
}
type ErrorResult<T> = {
method?: never | undefined
result?: never | undefined
error: T
}
type Subscription<TResult, TError> = {
method: 'eth_subscription'
error?: never | undefined
result?: never | undefined
params: {
subscription: string
} & (
| {
result: TResult
error?: never | undefined
}
| {
result?: never | undefined
error: TError
}
)
}
export type RpcResponse<TResult = any, TError = any> = {
jsonrpc: `${number}`
id: number
} & (
| SuccessResult<TResult>
| ErrorResult<TError>
| Subscription<TResult, TError>
)

export type RpcRequest = EIP1193Parameters<EIP1474Methods> & { id: number }
File renamed without changes.
9 changes: 8 additions & 1 deletion src/utils/abi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { AbiEvent } from 'abitype'
import { FunctionFragment } from 'ethers'
import {
type AbiItem,
type ContractEventName,
type DecodeEventLogParameters,
type Hex,
decodeEventLog,
Expand All @@ -18,7 +19,13 @@ export function decodeEventLogs_guessed<
data,
topics,
}: { abiItem: AbiEvent } & Pick<
DecodeEventLogParameters<[TAbiEvent], string, TTopics, TData, true>,
DecodeEventLogParameters<
[TAbiEvent],
ContractEventName<[TAbiEvent]>,
TTopics,
TData,
true
>,
'data' | 'topics'
>) {
const indexedValues = topics.slice(1)
Expand Down
5 changes: 2 additions & 3 deletions src/zustand/account.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { uniqBy } from 'remeda'
import type {
Address,
JsonRpcAccount as JsonRpcAccount_,
LocalAccount,
} from 'viem'

import { useSyncExternalStoreWithTracked } from '~/hooks/useSyncExternalStoreWithTracked'
import type { OneOf } from '~/types/utils'

import { uniqBy } from 'remeda'
import type { OneOf } from '../utils/types'
import { createStore } from './utils'

// Only support JSON-RPC Accounts for now. In the future, we may want to add support
Expand Down
3 changes: 2 additions & 1 deletion src/zustand/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import type { Abi, Address } from 'abitype'
import type { Hex, TransactionReceipt } from 'viem'
import { useStore } from 'zustand'

import type { RequiredBy } from '~/utils/types'
import type { RequiredBy } from '~/types/utils'

import { createStore, getKey } from './utils'

type Contract = {
Expand Down
2 changes: 1 addition & 1 deletion src/zustand/pending-requests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useSyncExternalStoreWithTracked } from '~/hooks/useSyncExternalStoreWithTracked'
import type { RpcRequest } from '~/messengers/schema'
import type { RpcRequest } from '~/types/rpc'

import { createStore } from './utils'

Expand Down
6 changes: 3 additions & 3 deletions test/dapp/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ function ContractPlayground() {
chain: null,
})
const transaction = await client.waitForTransactionReceipt({ hash })
setDeployedAddress(transaction.contractAddress)
setDeployedAddress(transaction.contractAddress!)
}

const test_rivet_1 = async (e: React.FormEvent) => {
Expand Down Expand Up @@ -619,7 +619,7 @@ function ContractMockERC20() {
args: ['MockERC20', 'M20', 18],
})
const transaction = await client.waitForTransactionReceipt({ hash })
setDeployedAddress(transaction.contractAddress)
setDeployedAddress(transaction.contractAddress!)
}

const mint = async (e: React.FormEvent) => {
Expand Down Expand Up @@ -665,7 +665,7 @@ function ContractMockERC721() {
args: ['MockERC721', 'M721'],
})
const transaction = await client.waitForTransactionReceipt({ hash })
setDeployedAddress(transaction.contractAddress)
setDeployedAddress(transaction.contractAddress!)
}

const mint = async (e: React.FormEvent) => {
Expand Down

0 comments on commit 845c779

Please sign in to comment.