2

account a has giving account b approval as a delegate to x tokens, how do i transfer x - 1 tokens from account a to account c, after the delegate transaction has been approved. i keep trying with transferChecked but im running into an error TypeError: Cannot read properties of undefined (reading 'publicKey') at getSigners

1 Answer 1

2

Here is an Solana Playground example of transferring tokens using a delegate.

Once you approve a delegate for a token account, you can submit a token transfer using the delegate as the "owner" of the token account. The delegate can transfer up to the approved amount.

import { Connection, Keypair, clusterApiUrl } from "@solana/web3.js";
import {
  createMint,
  mintTo,
  createAccount,
  transfer,
  approve,
} from "@solana/spl-token";

// Playground wallet
const payer = pg.wallet.keypair;

// Connection to devnet cluster
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");

// Transaction signature returned from sent transaction
let transactionSignature: string;

// Authority that can mint new tokens
const mintAuthority = pg.wallet.publicKey;

// Create new mint account using `createMint` helper function
const mint = await createMint(
  connection,
  payer, // payer
  payer.publicKey, // mint authority
  null, // freeze authority
  2 // decimals
);

console.log(
  "Mint Account: ",
  `https://explorer.solana.com/address/${mint.toString()}?cluster=devnet`
);

// Random keypair to use as owner of Token Account
const randomKeypair = new Keypair();
// Create Token Account for random keypair
const sourceTokenAccount = await createAccount(
  connection,
  payer, // Payer to create Token Account
  mint, // Mint Account address
  randomKeypair.publicKey // Token Account owner
);

// Create Token Account for Playground wallet
const destinationTokenAccount = await createAccount(
  connection,
  payer, // Payer to create Token Account
  mint, // Mint Account address
  payer.publicKey // Token Account owner
);

// Mint tokens to sourceTokenAccount, owned by randomKeypair
transactionSignature = await mintTo(
  connection,
  payer, // Transaction fee payer
  mint, // Mint Account address
  sourceTokenAccount, // Mint to
  mintAuthority, // Mint Authority address
  100 // Amount
);

console.log(
  "\nMint Tokens:",
  `https://solana.fm/tx/${transactionSignature}?cluster=devnet-solana`
);

// Approve delegate, (playground wallet as delegate)
transactionSignature = await approve(
  connection,
  payer, // Transaction fee payer
  sourceTokenAccount, // Token Account to set delegate for
  payer.publicKey, // Delegate
  randomKeypair.publicKey, // Token Account owner
  100, // Amount delegated
  [randomKeypair] // Signer
);

console.log(
  "\nApprove Delegate:",
  `https://solana.fm/tx/${transactionSignature}?cluster=devnet-solana`
);

// Transfer with delegate
transactionSignature = await transfer(
  connection,
  payer, // Transaction fee payer
  sourceTokenAccount, // Transfer from
  destinationTokenAccount, // Transfer to
  payer.publicKey, // Pass in delegate as owner
  100 // Amount
);

console.log(
  "\nTranfer with Delegate:",
  `https://solana.fm/tx/${transactionSignature}?cluster=devnet-solana`
);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.