Cardinal
Search…
⌃K

Transfer Token (SDK + CPI)

CPI

// Cargo.toml
cardinal-creator-standard = { version = "^2", features = ["cpi"] }
// anchor program transfer CPI example
// Assume token has been delegated to some program account, called user_escrow
invoke_signed(
&transfer(
cardinal_creator_standard::id(),
ctx.accounts.mint_manager.key(),
ctx.accounts.ruleset.key(),
ctx.accounts.mint.key(),
ctx.accounts.from_mint_token_account.key(),
ctx.accounts.to_mint_token_account.key(),
ctx.accounts.user_escrow.key(),
)?,
&[
ctx.accounts.mint_manager.to_account_info(),
ctx.accounts.ruleset.to_account_info(),
ctx.accounts.mint.to_account_info(),
ctx.accounts.from_mint_token_account.to_account_info(),
ctx.accounts.to_mint_token_account.to_account_info(),
ctx.accounts.user_escrow.to_account_info(),
ctx.accounts.system_program.to_account_info(),
ctx.accounts.token_program.to_account_info(),
ctx.accounts.instructions.to_account_info(),
],
&[&user_escrow_seeds.iter().map(|s| s.as_slice()).collect::<Vec<&[u8]>>()],
)?;

Client SDK

// Imports
import { findMintManagerId, createTransferInstruction } from "@cardinal/creator-standard";
import { createAssociatedTokenAccountInstruction, getAssociatedTokenAddressSync } from "@solana/spl-token";
const rulesetName = "ruleset-name";
const mintId = new PublicKey("mint-id");
const rulesetId = findRulsetId(rulesetName);
const mintManagerId = findMintManagerId(mintKeypair.publicKey);
const from = Keypair.fromSecretKey(
anchor.utils.bytes.bs58.decode("wallet secret key")
); // sender
const fromAtaId = getAssociatedTokenAddressSync(
mintId,
from
);
const to = new PublicKey("receiver");
const toAtaId = getAssociatedTokenAddressSync(
mintId,
to
);
const tx = new Transaction();
tx.add(
createAssociatedTokenAccountInstruction(
provider.wallet.publicKey,
toAtaId,
recipient.publicKey,
mintKeypair.publicKey
)
createTransferInstruction({
mintManager: mintManagerId,
ruleset: rulesetId,
mint: mintId,
from: fromAtaId,
to: toAtaId,
authority: to,
instructions: SYSVAR_INSTRUCTIONS_PUBKEY,
})
);
// Execute transaction
txid = await executeTransaction(
connection,
transaction,
new anchor.Wallet(from)
);