Skip to content

Transactions

This example demonstrates how to generate frame URLs for different types of transactions.

cmd
/send 1 eth to @bo
/swap 1 DAI to USDC
/mint 0x73a333cb82862d4f66f0154229755b184fb4f5b0 1
/show

The frame used in this example is BaseFrame, made by builders.garden

Main code

src/handler/frames.ts
import { HandlerContext } from "@xmtp/message-kit";
 
// Main handler function for processing commands
export function handler(context: HandlerContext) {
  const {
    message: {
      content: { command, params },
    },
  } = context;
 
  const baseUrl = "https://base-frame-lyart.vercel.app/transaction";
 
  switch (command) {
    case "send":
      // Destructure and validate parameters for the send command
      const { amount: amountSend, token: tokenSend, username } = params; 
 
      if (!amountSend || !tokenSend || !username) {
        context.reply(
          "Missing required parameters. Please provide amount, token, and username.",
        );
        return;
      }
      // Generate URL for the send transaction
      let url_send = generateFrameURL(baseUrl, "send", {
        amount: amountSend,
        token: tokenSend,
        receiver: username[0]?.address,
      });
      context.reply(`${url_send}`);
      break;
    case "swap":
      // Destructure and validate parameters for the swap command
      const { amount, token_from, token_to } = params; 
 
      if (!amount || !token_from || !token_to) {
        context.reply(
          "Missing required parameters. Please provide amount, token_from, and token_to.",
        );
        return;
      }
      // Generate URL for the swap transaction
      let url_swap = generateFrameURL(baseUrl, "swap", {
        amount,
        token_from,
        token_to,
      });
      context.reply(`${url_swap}`);
      break;
    case "mint":
      // Destructure and provide default values for the mint command
      const { collection, tokenId } = params; 
 
      if (!collection || !tokenId) {
        context.reply(
          "Missing required parameters. Please provide collection address and token id.",
        );
        return;
      }
      // Generate URL for the mint transaction
      let url_mint = generateFrameURL(baseUrl, "mint", {
        collection,
        token_id: tokenId,
      });
      context.reply(`${url_mint}`);
      break;
    case "show": 
      // Show the base URL without the transaction path
      context.reply(`${baseUrl.replace("/transaction", "")}`);
      break;
    default:
      // Handle unknown commands
      context.reply("Unknown command. Use help to see all available commands.");
  }
}
 
// Function to generate a URL with query parameters for transactions
function generateFrameURL(
  baseUrl: string,
  transaction_type: string,
  params: any,
) {
  // Filter out undefined parameters
  let filteredParams: { [key: string]: any } = {};
 
  for (const key in params) {
    if (params[key] !== undefined) {
      filteredParams[key] = params[key];
    }
  }
  let queryParams = new URLSearchParams({
    transaction_type,
    ...filteredParams,
  }).toString();
  return `${baseUrl}?${queryParams}`;
}