Skip to content

Token Info

Get real-time information about a token and generate a Frames to display it.

src/skills/token.ts
import { Context } from "@xmtp/message-kit";
import type { Skill } from "@xmtp/message-kit";
import { baselinks } from "@xmtp/message-kit";
 
export const token: Skill[] = [
  {
    skill: "token",
    handler: handler,
    examples: ["/token bitcoin", "/token ethereum"],
    description: "Get real time price of a any token.",
    params: {
      symbol: {
        type: "string",
      },
    },
  },
];
export async function handler(context: Context) {
  const {
    message: {
      content: {
        params: { symbol },
      },
    },
  } = context;
  const response = await fetch(
    `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=${symbol}`,
  );
  if (!response.ok) {
    context.send("Token not found");
    context.send("try with its full name, instead of btc it would be bitcoin");
    return;
  }
  const data = (await response.json()) as any;
  const token = data[0];
 
  const tokenInfo = {
    name: token.name,
    symbol: token.symbol.toUpperCase(),
    price: token.current_price,
    image: token.image,
    link: `https://www.coingecko.com/en/coins/${token.id}`,
  };
 
  let frame = {
    title: tokenInfo.name,
    buttons: [
      { content: "Buy", action: "link", target: tokenInfo.link },
      {
        content: `Price (${tokenInfo.price})`,
        action: "link",
        target: tokenInfo.link,
      },
    ],
    image: tokenInfo.image,
  };
  const url = await baselinks.sendCustomFrame(frame);
  await context.dm(url);
}