Skip to content

Support onchain transactions with your agent built with XMTP

This package provides an XMTP content type to support sending transactions to a wallet for execution.

For an example of an agent that implements the transaction content type, see the Transaction example in the xmtp-agent-examples repo.

Install the package

npm
npm i @xmtp/content-type-wallet-send-calls

Configure the content type

After importing the package, you can register the codec.

Node
import {
  WalletSendCallsCodec,
} from "@xmtp/content-type-wallet-send-calls";
// Create the XMTP client
const xmtp = await Client.create(signer, {
  env: "dev",
  codecs: [new WalletSendCallsCodec()],
});

Create a transaction request

With XMTP, a transaction request is represented using wallet_sendCalls with additional metadata for display.

Node
const walletSendCalls: WalletSendCallsParams = {
  version: "1.0",
  from: "0x123...abc",
  chainId: "0x2105",
  calls: [
    {
      to: "0x456...def",
      value: "0x5AF3107A4000",
      metadata: {
        description: "Send 0.0001 ETH on base to 0x456...def",
        transactionType: "transfer",
        currency: "ETH",
        amount: 100000000000000,
        decimals: 18,
        toAddress: "0x456...def",
      },
    },
    {
      to: "0x789...cba",
      data: "0xdead...beef",
      metadata: {
        description: "Lend 10 USDC on base with Morpho @ 8.5% APY",
        transactionType: "lend",
        currency: "USDC",
        amount: 10000000,
        decimals: 6,
        platform: "morpho",
        apy: "8.5",
      },
    },
  ],
};

Send a transaction request

Once you have a transaction reference, you can send it as part of your conversation:

Node
await conversation.messages.send(walletSendCalls, {
  contentType: ContentTypeWalletSendCalls,
});

Receive a transaction request

To receive and process a transaction request:

Node
// Assume `loadLastMessage` is a thing you have
const message: DecodedMessage = await loadLastMessage();
 
if (!message.contentType.sameAs(ContentTypeWalletSendCalls)) {
  // Handle non-transaction request message
  return;
}
 
const walletSendCalls: WalletSendCallsParams = message.content;
// Process the transaction request here