Contract Instances
A Contract Instance is a type-safe interface for performing contract-related actions with a specific ABI and address, created by the getContract function.
Import
import { getContract } from "cive";Usage
You can create a Contract Instance with the getContract function by passing in a ABI, address, and Public and/or Wallet Client. Once created, you can call contract methods, fetch for events, listen to events, etc.
import { } from "cive";
import { } from "./abi";
import { , } from "./client";
// 1. Create contract instance
const = ({
: "cfx:...",
: ,
// 1a. Insert a single client
: ,
// 1b. Or public and/or wallet clients
// client: { public: publicClient, wallet: walletClient },
});
// 2. Call contract methods, fetch events, listen to events, etc.
const = await ..();import { createPublicClient, createWalletClient, http, custom } from "cive";
import { mainnet } from "cive/chains";
import { privateKeyToAccount } from "cive/accounts";
export const publicClient = createPublicClient({
chain: mainnet,
transport: http(),
});
const account = privateKeyToAccount("0x...", { networkId: mainnet.id });
export const walletClient = createWalletClient({
chain: mainnet,
transport: http(),
account,
});export const Abi = [
{
inputs: [],
name: "totalSupply",
outputs: [{ type: "uint256" }],
stateMutability: "view",
type: "function",
},
{
name: "Transfer",
type: "event",
inputs: [
{
indexed: true,
name: "from",
type: "address",
},
{ indexed: true, name: "to", type: "address" },
{
indexed: true,
name: "tokenId",
type: "uint256",
},
],
},
] as const;Using Contract Instances can make it easier to work with contracts if you don't want to pass the abi and address properties every time you perform contract actions, e.g. readContract, writeContract, estimateContractGasAndCollateral, etc. Switch between the tabs below to see the difference between standalone Contract Actions and Contract Instance Actions:
import { getContract } from "cive";
import { Abi } from "./abi";
import { publicClient, walletClient } from "./client";
const contract = getContract({
address: "cfx:...",
abi: Abi,
client: {
public: publicClient,
wallet: walletClient,
},
});
const balance = await contract.read.balanceOf(["cfx:..."]);
const hash = await contract.write.mint(["cfx:...", 69420n]);
const logs = await contract.getEvents.Transfer();import { Abi } from "./abi";
import { publicClient, walletClient } from "./client";
const balance = await publicClient.readContract({
address: "cfx:...",
abi: Abi,
functionName: "balanceOf",
args: ["cfx:..."],
});
const hash = await walletClient.writeContract({
address: "cfx:...",
abi: Abi,
functionName: "mint",
args: ["cfx:...", 69420n],
});export const Abi = [
{
inputs: [],
name: "totalSupply",
outputs: [{ type: "uint256" }],
stateMutability: "view",
type: "function",
},
{
name: "Transfer",
type: "event",
inputs: [
{
indexed: true,
name: "from",
type: "address",
},
{ indexed: true, name: "to", type: "address" },
{
indexed: true,
name: "tokenId",
type: "uint256",
},
],
},
{
inputs: [
{
internalType: "address",
name: "account",
type: "address",
},
],
name: "balanceOf",
outputs: [
{
internalType: "uint256",
name: "",
type: "uint256",
},
],
stateMutability: "view",
type: "function",
},
{
inputs: [
{
internalType: "address",
name: "to",
type: "address",
},
{
internalType: "uint256",
name: "amount",
type: "uint256",
},
],
name: "mint",
outputs: [],
stateMutability: "nonpayable",
type: "function",
},
] as const;Return Value
Contract instance object. Type is inferred.
Depending on if you create a contract instance with a Public Client, Wallet Client, or both, the methods available on the contract instance will vary.
With Public Client
If you pass in a publicClient, the following methods are available:
With Wallet Client
If you pass in a walletClient, the following methods are available:
Parameters
address
- Type:
Address
The contract address.
const contract = getContract({
address: "cfx:..",
abi: Abi,
client: publicClient,
});abi
- Type:
Abi
The contract's ABI.
const contract = getContract({
address: "cfx:..",
abi: Abi,
client: publicClient,
});client
The Client used for performing contract actions.
const contract = getContract({
address: "cfx:..",
abi: Abi,
client: publicClient,
});You can also pass in multiple clients (ie. a Wallet Client and a Public Client):
const contract = getContract({
address: "cfx:...",
abi: Abi,
client: {
public: publicClient,
wallet: walletClient,
},
});