viem
Version:
110 lines (103 loc) • 3.04 kB
text/typescript
import type { Address } from 'abitype'
import type { Client } from '../../clients/createClient.js'
import type { Transport } from '../../clients/transports/createTransport.js'
import type { ErrorType } from '../../errors/utils.js'
import type { BlockTag } from '../../types/block.js'
import type { Chain } from '../../types/chain.js'
import type { Hash } from '../../types/misc.js'
import type { Proof } from '../../types/proof.js'
import {
type FormatBlockParameterErrorType,
formatBlockParameter,
} from '../../utils/block/formatBlockParameter.js'
import type { RequestErrorType } from '../../utils/buildRequest.js'
import {
type FormatProofErrorType,
formatProof,
} from '../../utils/formatters/proof.js'
export type GetProofParameters = {
/** Account address. */
address: Address
/** Array of storage-keys that should be proofed and included. */
storageKeys: Hash[]
} & (
| {
/** The block number. */
blockNumber?: bigint | undefined
blockTag?: undefined
blockHash?: undefined
requireCanonical?: undefined
}
| {
blockNumber?: undefined
/**
* The block tag.
* @default 'latest'
*/
blockTag?: BlockTag | undefined
blockHash?: undefined
requireCanonical?: undefined
}
| {
blockNumber?: undefined
blockTag?: undefined
/** The proof at a block specified by block hash. */
blockHash: Hash
/** Whether or not to throw an error if the block is not in the canonical chain. Only allowed in conjunction with `blockHash`. */
requireCanonical?: boolean | undefined
}
)
export type GetProofReturnType = Proof
export type GetProofErrorType =
| FormatBlockParameterErrorType
| FormatProofErrorType
| RequestErrorType
| ErrorType
/**
* Returns the account and storage values of the specified account including the Merkle-proof.
*
* - Docs: https://viem.sh/docs/actions/public/getProof
* - JSON-RPC Methods:
* - Calls [`eth_getProof`](https://eips.ethereum.org/EIPS/eip-1186)
*
* @param client - Client to use
* @param parameters - {@link GetProofParameters}
* @returns Proof data. {@link GetProofReturnType}
*
* @example
* import { createPublicClient, http } from 'viem'
* import { mainnet } from 'viem/chains'
* import { getProof } from 'viem/public'
*
* const client = createPublicClient({
* chain: mainnet,
* transport: http(),
* })
* const block = await getProof(client, {
* address: '0x...',
* storageKeys: ['0x...'],
* })
*/
export async function getProof<chain extends Chain | undefined>(
client: Client<Transport, chain>,
{
address,
blockHash,
blockNumber,
blockTag = 'latest',
requireCanonical,
storageKeys,
}: GetProofParameters,
): Promise<GetProofReturnType> {
const block = formatBlockParameter({
blockHash,
blockNumber,
blockTag,
requireCanonical,
})
const proof = await client.request({
method: 'eth_getProof',
params: [address, storageKeys, block],
})
return formatProof(proof)
}