Skip to main content
Context functions allow task sandboxes to access the outside world. Context functions support individual retry configuration and are automatically logged for auditing and debugging. All communication outside of the task sandbox happens via Context Functions, making Compose apps run determistic, given the same world context.

Retry Configuration

All Context functions accept an optional retry configuration:
type HostFunctionRetryConfig = {
  max_attempts?: number; // Default: 3
  initial_interval_ms?: number; // Default: 1000
  backoff_factor: number; // Default: 2
};
How Context Function Retries Work:
  • Each context function call can have its own retry configuration
  • If a context function fails, it retries according to its configuration
  • If all context function retries are exhausted does the context function will throw
  • If you don’t catch a failed context function call, a task-level retry may trigger, restarting the task and any context-function retries
  • Default retry behavior for itempotent host functions (fetch GET or PUT, readContract, simulate, etc) is as follows:
// default context function retry configuration
{
  max_attempts: 3,
  initial_interval_ms: 1000,
  backoff_factor: 2,
}
  • By Default, non-itempotent host functions don’t retry (fetch POST or DELETE, writeContract) but you can pass a retry configuration to override that.

Blockchain Transactions - writeContract

Execute blockchain transactions with automatic wallet management.
writeContract(
  chain: Chain, // viem Chain object
  contractAddress: string,
  functionSig: string,
  args: unknown[],
  retryConfig?: HostFunctionRetryConfig
): Promise<string>
// TODO: Need to update this once smart wallets are integrated and their behaviour solidified

Wallet Configuration

The writeContract function supports multiple wallet configurations:
  1. Environment Variable: Set PRIVATE_KEY with your private key
  2. Privy Integration: Uses Privy-managed wallets (configured automatically)

Examples

Simple Token Transfer

/// <reference types="../../.compose/types.d.ts" /> 

export async function main({ writeContract, chains }) {
  const txHash = await writeContract(
    chains.ethereum,
    "0x1234567890123456789012345678901234567890", // Token contract
    "transfer(address,uint256)",
    [
      "0xabcdef1234567890123456789012345678901234", // Recipient
      "1000000000000000000", // 1 token (18 decimals)
    ]
  );

  return { success: true, txHash };
}

Blockchain Calls - readContract

Execute read-only blockchain calls.
readContract(
  chain: Chain, // viem Chain object
  contractAddress: string,
  functionSig: string, // Must include return type
  args: unknown[],
  retryConfig?: HostFunctionRetryConfig
): Promise<string>

Examples

Simple Balance Check

/// <reference types="../../.compose/types.d.ts" /> 

export async function main({ readContract, chains }) {
  const balance = await readContract(
    chains.ethereum,
    "0x1234567890123456789012345678901234567890", // Token contract
    "balanceOf(address) returns (uint256)",
    ["0xabcdef1234567890123456789012345678901234"] // Account address
  );

  return { balance };
}

Next Steps