Sample TypeScript Agent

Running circuit init with the CLI installed will get you similar starter code in your directory.

See TypeScript SDK for full details on each of these methods and how they're used.

/**
 * Circuit Agent - SDK v1.2 TypeScript Demo
 *
 * This agent demonstrates the v1.2 sdk features. You may need to alter the values for the self send, swidge, and polymarket examples based on your test wallets holdings.
 * This sample agent will get and set a memory value, send a self send transaction, execute a swap, and sell some steelers positions on Polymarket.
 */

import { Agent, type AgentContext } from "@circuitorg/agent-sdk";

async function run(agent: AgentContext): Promise<void> {
  await agent.log("Starting execution");

  // Memory example
  await agent.log("Memory example");
  let runCount: number;
  const runCountMemory = await agent.memory.get("run-count");
  if (runCountMemory.success && runCountMemory.data) {
    runCount = parseInt(runCountMemory.data?.value || "0", 10);
  } else {
    runCount = 0;
  }
  runCount += 1;

  await agent.memory.set("run-count", runCount.toString());
  await agent.log(`Session run count: ${runCount}`);

  // Self send example using low level sign and send function
  await agent.log("Self send example using low level sign and send function");
  const signAndSend = await agent.signAndSend({
    network: "ethereum:1",
    request: {
      toAddress: agent.sessionWalletAddress as `0x${string}`,
      data: "0x" as `0x${string}`, // Self send
      value: "100000000000000", // 0.0001 ETH
    },
    message: "Self-send demo",
  });
  if (signAndSend.success && signAndSend.data) {
    await agent.log(`Transaction sent: ${signAndSend.data.txHash}`);
  } else {
    await agent.log(signAndSend.error || signAndSend.errorMessage || "Transaction failed", { error: true });
  }

  // Swidge example
  await agent.log("Swidge example");
  const ARBITRUM_NETWORK = "ethereum:42161";
  const BASE_NETWORK = "ethereum:8453";

  const quote = await agent.swidge.quote({
    from: { network: ARBITRUM_NETWORK, address: agent.sessionWalletAddress },
    to: { network: BASE_NETWORK, address: agent.sessionWalletAddress },
    amount: "100000000000000", // 0.0001 ETH
    slippage: "2.0",
  });
  if (quote.success && quote.data) {
    const execute = await agent.swidge.execute(quote.data);
    if (execute.success && execute.data) {
      await agent.log(`Transaction sent: ${execute.data.out.txs[0]}`);
    } else {
      await agent.log(execute.errorMessage || execute.error || "Execute failed", { error: true });
    }
  } else {
    await agent.log(quote.errorMessage || quote.error || "Quote failed", { error: true });
  }

  // Polymarket example
  await agent.log("Polymarket example");
  const sellOrder = await agent.platforms.polymarket.marketOrder({
    tokenId:
      "43316042420532542168140438864501868551184980388178338105513819372697332591887", // Steelers vs Bengals - Steelers
    size: 3,
    side: "SELL",
  });

  if (sellOrder.success) {
    await agent.log("Sold some steelers positions");
  } else {
    await agent.log(`Sell order failed: ${sellOrder.errorMessage || sellOrder.error || "Unknown error"}`, { error: true });
  }
}

async function stop(agent: AgentContext): Promise<void> {
  await agent.log("Stopping execution");
  await agent.log("Execution stopped");
}

const agent = new Agent({ runFunction: run, stopFunction: stop });

export default agent.getExport();

Last updated