INTEGRATION
Integrating with viem
Reading Ivy contracts and sending transactions on behalf of users with viem.
viem is one of the most practical ways to work with Ivy. For setup, see Quick Start.
#Reading (public client)
import {createPublicClient, http} from "viem";
import {kaia} from "./kaia"; // the chain definition from Quick Start
const client = createPublicClient({chain: kaia, transport: http()});
// Example: total number of Ivy accounts
const total = await client.readContract({
address: "0x43522E0aB410246C014359B7F5c55F7a196dddFc",
abi: [{type:"function", name:"totalSupply", stateMutability:"view", inputs:[], outputs:[{type:"uint256"}]}],
functionName: "totalSupply",
});
#Writing (wallet client)
Transactions are signed with the user's wallet. Example: send a tip to a post.
import {createWalletClient, custom, parseEther, keccak256, toBytes} from "viem";
import {kaia} from "./kaia";
const wallet = createWalletClient({chain: kaia, transport: custom(window.ethereum)});
const [account] = await wallet.requestAddresses();
const TIP_ENGINE = "0x098366647c8e5C07242f8787Aa4E75579D5C7551";
// postId is the unique id of the content; it is passed to the chain as a keccak256 digest.
const postIdHash = keccak256(toBytes("post-uuid-or-id"));
const hash = await wallet.writeContract({
account,
address: TIP_ENGINE,
abi: [{
type: "function", name: "tip", stateMutability: "payable",
inputs: [{name:"creatorIvyId", type:"uint256"}, {name:"postIdHash", type:"bytes32"}],
outputs: [],
}],
functionName: "tip",
args: [123n, postIdHash], // 123n = the creator's ivyId
value: parseEther("0.5"), // a 0.5 KAIA tip (min 0.1)
});
#Simulation (recommended)
Catch revert reasons before writing, with simulateContract:
await client.simulateContract({
account,
address: TIP_ENGINE,
abi: [/* ... */],
functionName: "tip",
args: [123n, postIdHash],
value: parseEther("0.5"),
});
For engine signatures see Social Engines. For fees see Fees & Distribution.
Takıldığın bir yer mi var?
Teknik destek, entegrasyon ve ortaklık için ekibe ulaş: info@ivy.live