estimateContractGasAndCollateral
Estimate gas and collateral to successfully execute a contract write function call.
Internally, estimateGasAndCollateral uses a Public Client to call the estimateGasAndCollateral action with ABI-encoded data.
Usage
Below is a very basic example of how to estimate gas (with no arguments).
The mint function accepts no arguments, and returns a token ID.
import { , } from "./config";
import { } from "./abi";
const = await .({
: "cfx:...",
: ,
: "mint",
,
});
// {
// "gasLimit": 32488n,
// "gasUsed": 32488n,
// "storageCollateralized": 64n,
// }export const Abi = [
{
inputs: [],
name: "mint",
outputs: [{ name: "", type: "uint32" }],
stateMutability: "view",
type: "function",
},
] as const;import { createPublicClient, http } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { mainnet } from "cive/chains";
export const account = privateKeyToAccount("0x...", { networkId: mainnet.id });
export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});Passing Arguments
If your function requires argument(s), you can pass them through with the args attribute.
TypeScript types for args will be inferred from the function name & ABI, to guard you from inserting the wrong values.
For example, the mint function name below requires a tokenId argument, and it is typed as [number].
import { account, publicClient } from "./config";
import { Abi } from "./abi";
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
args: [69420],
account,
});
// 69420nexport const Abi = [
{
inputs: [{ name: "owner", type: "uint32" }],
name: "mint",
outputs: [{ name: "", type: "uint32" }],
stateMutability: "nonpayable",
type: "function",
},
] as const;import { createPublicClient, http } from "cive";
import { privateKeyToAccount } from "cive/accounts";
import { mainnet } from "cive/chains";
export const account = privateKeyToAccount("0x...", { networkId: mainnet.id });
export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});Return Value
The estimate result object.
address
- Type:
Address
The contract address.
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
account,
});abi
- Type:
Abi
The contract's ABI.
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
account,
});functionName
- Type:
string
A function to extract from the ABI.
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
account,
});account
- Type:
Account | Address
The Account to estimate gas from.
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
account,
});accessList (optional)
- Type:
AccessList
The access list.
accessList: [
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
account,
accessList: [
{
address: "cfx:...",
storageKeys: ["0x1"],
},
],
});args (optional)
- Type: Inferred from ABI.
Arguments to pass to function call.
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
args: [69420],
account,
});gasPrice (optional)
- Type:
bigint
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
gasPrice: parseGDrip("20"),
account,
});maxFeePerGas (optional)
- Type:
bigint
Total fee per gas (in wei), inclusive of maxPriorityFeePerGas. Only applies to EIP-1559 Transactions
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
maxFeePerGas: parseGDrip("20"),
account,
});maxPriorityFeePerGas (optional)
- Type:
bigint
Max priority fee per gas (in wei). Only applies to EIP-1559 Transactions
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
maxFeePerGas: parseGDrip("20"),
maxPriorityFeePerGas: parseGDrip("2"),
account,
});nonce (optional)
- Type:
number
Unique number identifying this transaction.
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
maxFeePerGas: parseGDrip("20"),
account,
nonce: 100,
});value (optional)
- Type:
number
Value in wei sent with this transaction.
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
maxFeePerGas: parseGDrip("20"),
account,
value: parseCFX("1"),
});epochNumber (optional)
- Type:
bigint
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
account,
epochNumber: 1n,
});Epoch tag (Optional)
- Type:
latest_state|latest_confirmed|latest_checkpoint|earliest - Defaut:
latest_state
const data = await publicClient.estimateContractGasAndCollateral({
address: "cfx:...",
abi: Abi,
functionName: "mint",
account,
epochTag: "latest_state",
});