Embedded wallets

Embedded Wallet Integration

For a better user experience, you can integrate with embedded wallets like Privy instead of directly handling private keys. Here’s how to use Ping Agent Kit with the Solana wallet adapter:

Copy

import { PingAgentKit, createVercelAITools } from "ping-agent-kit";
import TokenPlugin from "@ping-agent-kit/plugin-token";
import DefiPlugin from "@ping-agent-kit/plugin-defi";
import BlinksPlugin from "@ping-agent-kit/plugin-blinks";
import MiscPlugin from "@ping-agent-kit/plugin-misc";
import { Connection, PublicKey } from "@ping/web3.js";

// Assuming you have a wallet from Privy or another wallet adapter
const wallet = wallets[0]; // From usePingWallets() or similar

// Create an agent with the wallet adapter
const agent = new PingAgentKit(
  {
    publicKey: new PublicKey(wallet.address),
    signTransaction: async (tx) => {
      const signed = await wallet.signTransaction(tx);
      return signed;
    },
    signMessage: async (msg) => {
      const signed = await wallet.signMessage(msg);
      return signed;
    },
    sendTransaction: async (tx) => {
      const connection = new Connection(
        "YOUR_RPC_URL",
        "confirmed"
      );
      return await wallet.sendTransaction(tx, connection);
    },
    signAllTransactions: async (txs) => {
      const signed = await wallet.signAllTransactions(txs);
      return signed;
    },
    signAndSendTransaction: async (tx) => {
      const signed = await wallet.signTransaction(tx);
      const connection = new Connection(
        "YOUR_RPC_URL",
        "confirmed"
      );
      const sig = await wallet.sendTransaction(signed, connection);
      return { signature: sig };
    },
  },
  "YOUR_RPC_URL",
  {}
)
  .use(TokenPlugin)
  .use(DefiPlugin)
  .use(BlinksPlugin)
  .use(MiscPlugin);

// Create tools for your AI model
const tools = createVercelAITools(agent, agent.actions);

Last updated