Skip to content
Cive

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 ..();

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();

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

The contract address.

const contract = getContract({
  address: "cfx:..", 
  abi: Abi,
  client: publicClient,
});

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, 
  }, 
});