- Consolidated duplicate UndoManagers to single instance - Fixed connection promise to only resolve on 'connected' status - Fixed WebSocketProvider import (WebsocketProvider) - Added proper doc.destroy() cleanup - Renamed isPresenceInitialized property to avoid conflict Co-Authored-By: Paperclip <noreply@paperclip.ing>
148 lines
4.7 KiB
TypeScript
148 lines
4.7 KiB
TypeScript
import type { Abi } from 'abitype'
|
|
|
|
import type { Account } from '../../accounts/types.js'
|
|
import {
|
|
type ParseAccountErrorType,
|
|
parseAccount,
|
|
} from '../../accounts/utils/parseAccount.js'
|
|
import type { Client } from '../../clients/createClient.js'
|
|
import type { Transport } from '../../clients/transports/createTransport.js'
|
|
import type { BaseError } from '../../errors/base.js'
|
|
import type { Chain } from '../../types/chain.js'
|
|
import type {
|
|
ContractFunctionArgs,
|
|
ContractFunctionName,
|
|
ContractFunctionParameters,
|
|
GetValue,
|
|
} from '../../types/contract.js'
|
|
import type { Hex } from '../../types/misc.js'
|
|
import type { UnionOmit } from '../../types/utils.js'
|
|
import {
|
|
type EncodeFunctionDataErrorType,
|
|
type EncodeFunctionDataParameters,
|
|
encodeFunctionData,
|
|
} from '../../utils/abi/encodeFunctionData.js'
|
|
import {
|
|
type GetContractErrorReturnType,
|
|
getContractError,
|
|
} from '../../utils/errors/getContractError.js'
|
|
import { getAction } from '../../utils/getAction.js'
|
|
import {
|
|
type EstimateGasErrorType,
|
|
type EstimateGasParameters,
|
|
estimateGas,
|
|
} from './estimateGas.js'
|
|
|
|
export type EstimateContractGasParameters<
|
|
abi extends Abi | readonly unknown[] = Abi,
|
|
functionName extends ContractFunctionName<
|
|
abi,
|
|
'nonpayable' | 'payable'
|
|
> = ContractFunctionName<abi, 'nonpayable' | 'payable'>,
|
|
args extends ContractFunctionArgs<
|
|
abi,
|
|
'nonpayable' | 'payable',
|
|
functionName
|
|
> = ContractFunctionArgs<abi, 'nonpayable' | 'payable', functionName>,
|
|
chain extends Chain | undefined = Chain | undefined,
|
|
> = ContractFunctionParameters<
|
|
abi,
|
|
'nonpayable' | 'payable',
|
|
functionName,
|
|
args
|
|
> &
|
|
UnionOmit<EstimateGasParameters<chain>, 'data' | 'to' | 'value'> &
|
|
GetValue<
|
|
abi,
|
|
functionName,
|
|
EstimateGasParameters<chain> extends EstimateGasParameters
|
|
? EstimateGasParameters<chain>['value']
|
|
: EstimateGasParameters['value']
|
|
> & {
|
|
/** Data to append to the end of the calldata. Useful for adding a ["domain" tag](https://opensea.notion.site/opensea/Seaport-Order-Attributions-ec2d69bf455041a5baa490941aad307f). */
|
|
dataSuffix?: Hex | undefined
|
|
}
|
|
|
|
export type EstimateContractGasReturnType = bigint
|
|
|
|
export type EstimateContractGasErrorType = GetContractErrorReturnType<
|
|
EncodeFunctionDataErrorType | EstimateGasErrorType | ParseAccountErrorType
|
|
>
|
|
|
|
/**
|
|
* Estimates the gas required to successfully execute a contract write function call.
|
|
*
|
|
* - Docs: https://viem.sh/docs/contract/estimateContractGas
|
|
*
|
|
* Internally, uses a [Public Client](https://viem.sh/docs/clients/public) to call the [`estimateGas` action](https://viem.sh/docs/actions/public/estimateGas) with [ABI-encoded `data`](https://viem.sh/docs/contract/encodeFunctionData).
|
|
*
|
|
* @param client - Client to use
|
|
* @param parameters - {@link EstimateContractGasParameters}
|
|
* @returns The gas estimate (in wei). {@link EstimateContractGasReturnType}
|
|
*
|
|
* @example
|
|
* import { createPublicClient, http, parseAbi } from 'viem'
|
|
* import { mainnet } from 'viem/chains'
|
|
* import { estimateContractGas } from 'viem/contract'
|
|
*
|
|
* const client = createPublicClient({
|
|
* chain: mainnet,
|
|
* transport: http(),
|
|
* })
|
|
* const gas = await estimateContractGas(client, {
|
|
* address: '0xFBA3912Ca04dd458c843e2EE08967fC04f3579c2',
|
|
* abi: parseAbi(['function mint() public']),
|
|
* functionName: 'mint',
|
|
* account: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
|
|
* })
|
|
*/
|
|
export async function estimateContractGas<
|
|
const abi extends Abi | readonly unknown[],
|
|
functionName extends ContractFunctionName<abi, 'nonpayable' | 'payable'>,
|
|
args extends ContractFunctionArgs<abi, 'pure' | 'view', functionName>,
|
|
chain extends Chain | undefined,
|
|
account extends Account | undefined = undefined,
|
|
>(
|
|
client: Client<Transport, chain, account>,
|
|
parameters: EstimateContractGasParameters<abi, functionName, args, chain>,
|
|
): Promise<EstimateContractGasReturnType> {
|
|
const {
|
|
abi,
|
|
address,
|
|
args,
|
|
functionName,
|
|
dataSuffix = typeof client.dataSuffix === 'string'
|
|
? client.dataSuffix
|
|
: client.dataSuffix?.value,
|
|
...request
|
|
} = parameters as EstimateContractGasParameters
|
|
const data = encodeFunctionData({
|
|
abi,
|
|
args,
|
|
functionName,
|
|
} as EncodeFunctionDataParameters)
|
|
|
|
try {
|
|
const gas = await getAction(
|
|
client,
|
|
estimateGas,
|
|
'estimateGas',
|
|
)({
|
|
data: `${data}${dataSuffix ? dataSuffix.replace('0x', '') : ''}`,
|
|
to: address,
|
|
...request,
|
|
} as unknown as EstimateGasParameters)
|
|
return gas
|
|
} catch (error) {
|
|
const account = request.account ? parseAccount(request.account) : undefined
|
|
throw getContractError(error as BaseError, {
|
|
abi,
|
|
address,
|
|
args,
|
|
docsPath: '/docs/contract/estimateContractGas',
|
|
functionName,
|
|
sender: account?.address,
|
|
})
|
|
}
|
|
}
|