Skip to main content
Compose allows you to interact with smart contracts with type safety and flexible wallet options. See wallets for details on the different types of wallets.

Code generation

To interact with smart contracts with full type safety, place your ABI JSON files in the src/contracts/ folder. Compose automatically generates TypeScript classes when you run compose start or compose deploy. You can also manually trigger code generation:
compose codegen

Generated classes

For each ABI file (e.g., src/contracts/USDC.json), Compose generates a typed class that you can access via evm.contracts:
import { TaskContext } from "compose";

export async function main({ evm, env, fetch }: TaskContext) {
  const wallet = await evm.wallet({ name: "my-wallet" });

  // Access generated contract class via evm.contracts
  const usdc = new evm.contracts.USDC(
    "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
    evm.chains.baseSepolia,
    wallet
  );

  // Convert timestamp and price to bytes32 format
  const timestamp = Date.now();
  const bitcoinPrice = response.bitcoin.usd;
  const timestampAsBytes32 = `0x${timestamp.toString(16).padStart(64, "0")}`;
  const priceAsBytes32 = `0x${Math.round(bitcoinPrice * 100).toString(16).padStart(64, "0")}`;

  const bitcoinOracleContract = new evm.contracts.BitcoinOracleContract(
    env.ORACLE_ADDRESS,
    evm.chains.base,
    wallet
  );

  // execute the "setPrice" method
  const { hash } = await bitcoinOracleContract.setPrice(
    timestampAsBytes32,
    priceAsBytes32
  );

  // read a "getLatestPrice" view method
  const result = await bitcoinOracleContract.getLatestPrice();
}
Contract methods are called directly on the instance without .write or .read suffixes. View functions return their result directly, while state-changing functions return { hash, receipt }.

Decoding event logs

Each generated contract class includes a static decodeEventLog() method for decoding onchain events with full type safety. This is commonly used in tasks triggered by onchain events:
import { TaskContext, OnchainEvent } from "compose";

export async function main({ evm }: TaskContext, payload: OnchainEvent) {
  // Decode using a generated contract class — returns a typed union of all events in the ABI
  const decoded = evm.contracts.MyContract.decodeEventLog(payload);

  if (decoded.eventName === "Transfer") {
    console.log("Transfer:", decoded.args);
  }

  // Or use the top-level decodeEventLog with any ABI
  const myAbi = [/* ... */];
  const decoded2 = await evm.decodeEventLog(myAbi, payload);
}
You can also use decodeEventLog with a generic type parameter for a single known event type:
const decoded = evm.contracts.MyContract.decodeEventLog<TransferEventDecoded>(payload);

Direct wallet methods

You can also interact with contracts directly through wallet methods without generated classes:
import { TaskContext } from "compose";

export async function main({ evm, env }: TaskContext) {
  const wallet = await evm.wallet();

  // Write to a contract
  const { hash, receipt } = await wallet.writeContract(
    evm.chains.polygon,
    env.CONTRACT_ADDRESS as `0x${string}`,
    "reportPayouts(bytes32,uint256[])",
    [resultId, payouts]
  );

  // Read from a contract — returns the decoded value directly
  const balance = await wallet.readContract<bigint>(
    evm.chains.polygon,
    env.CONTRACT_ADDRESS as `0x${string}`,
    "balanceOf(address) returns (uint256)",
    [wallet.address]
  );
}
Read more about wallet methods in the wallets documentation.