Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// #region all
import { Connection, PublicKey } from "@solana/web3.js";
import { TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
import { address, createSolanaRpc } from "@solana/kit";

// connection
const connection = new Connection("http://localhost:8899", "confirmed");
const rpc = createSolanaRpc("http://localhost:8899");

const owner = new PublicKey("G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoY");
let response = await connection.getParsedTokenAccountsByOwner(owner, {
programId: TOKEN_PROGRAM_ID,
});
const owner = address("G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoY");
const response = await rpc
.getTokenAccountsByOwner(
owner,
{ programId: TOKEN_PROGRAM_ADDRESS },
{ encoding: "jsonParsed" },
)
.send();

response.value.forEach((accountInfo) => {
console.log(`pubkey: ${accountInfo.pubkey.toBase58()}`);
console.log(`pubkey: ${accountInfo.pubkey}`);
console.log(`mint: ${accountInfo.account.data["parsed"]["info"]["mint"]}`);
console.log(`owner: ${accountInfo.account.data["parsed"]["info"]["owner"]}`);
console.log(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
// #region fetch
import { Connection, PublicKey } from "@solana/web3.js";
import { getAccount, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
import { fetchToken } from "@solana-program/token-2022";
import { address, createSolanaRpc } from "@solana/kit";

const connection = new Connection("http://localhost:8899", "confirmed");
const rpc = createSolanaRpc("http://localhost:8899");

const tokenAccountPubkey = new PublicKey(
const tokenAccountAddress = address(
"GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ",
);

const tokenAccount = await getAccount(
connection,
tokenAccountPubkey,
"confirmed",
TOKEN_2022_PROGRAM_ID,
);
const tokenAccount = await fetchToken(rpc, tokenAccountAddress, {
commitment: "confirmed",
});
console.log(tokenAccount);
// #endregion fetch
28 changes: 6 additions & 22 deletions packages/docs-examples/cookbook/tokens/get-token-mint/legacy.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
// #region fetch
import { Connection, PublicKey } from "@solana/web3.js";
import { getMint, TOKEN_2022_PROGRAM_ID } from "@solana/spl-token";
import { fetchMint } from "@solana-program/token-2022";
import { address, createSolanaRpc } from "@solana/kit";

const connection = new Connection("http://localhost:8899", "confirmed");
const rpc = createSolanaRpc("http://localhost:8899");

const mintAddress = new PublicKey(
"2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo",
);
const mintAddress = address("2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo");

let mintAccount = await getMint(
connection,
mintAddress,
undefined,
TOKEN_2022_PROGRAM_ID,
);
const mintAccount = await fetchMint(rpc, mintAddress);

console.log(
JSON.stringify(
{
...mintAccount,
supply: mintAccount.supply.toString(),
},
null,
2,
),
);
console.log(mintAccount);
// #endregion fetch
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { describe, it } from "vitest";
import { expectExampleLogsSignature } from "../../../test/assert-signature";

describe("cookbook/transactions/fee-sponsorship/legacy-helper", () => {
it("uses spl-token helpers, fees paid by separate signer", async () => {
it("uses Kit token helpers, fees paid by separate signer", async () => {
await expectExampleLogsSignature(() => import("./legacy-helper"));
});
});
Original file line number Diff line number Diff line change
@@ -1,143 +1,97 @@
// #region sponsor
import { Connection, Keypair, LAMPORTS_PER_SOL } from "@solana/web3.js";
import { createClient, generateKeyPairSigner, lamports } from "@solana/kit";
import { rpcAirdrop, solanaRpc } from "@solana/kit-plugin-rpc";
import { airdropPayer, payer } from "@solana/kit-plugin-signer";
import {
createMint,
createAssociatedTokenAccount,
mintTo,
TOKEN_PROGRAM_ID,
transfer,
getAccount,
} from "@solana/spl-token";
fetchToken,
findAssociatedTokenPda,
tokenProgram,
TOKEN_PROGRAM_ADDRESS,
} from "@solana-program/token";

const connection = new Connection("http://localhost:8899", "confirmed");
const latestBlockhash = await connection.getLatestBlockhash();
// Generate keypairs for fee payer, sender, recipient, and mint
const feePayer = await generateKeyPairSigner();
const sender = await generateKeyPairSigner();
const recipient = await generateKeyPairSigner();
const mint = await generateKeyPairSigner();

const feePayer = Keypair.generate();
const sender = Keypair.generate();
const recipient = Keypair.generate();
console.log("Fee Payer Address:", feePayer.address);
console.log("Sender Address:", sender.address);
console.log("Recipient Address:", recipient.address);
console.log("Mint Address:", mint.address);

// Airdrop 1 SOL to fee payer
const airdropSignature = await connection.requestAirdrop(
feePayer.publicKey,
LAMPORTS_PER_SOL,
);
await connection.confirmTransaction({
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
signature: airdropSignature,
});
// Build a Kit client: fee payer (funded with 1 SOL), local RPC, and the token program plugin
const client = await createClient()
.use(payer(feePayer))
.use(
solanaRpc({
rpcUrl: "http://localhost:8899",
rpcSubscriptionsUrl: "ws://localhost:8900",
}),
)
.use(rpcAirdrop())
.use(airdropPayer(lamports(1_000_000_000n)))
.use(tokenProgram());

// Airdrop 1 SOL to sender so it can act as mint authority
const senderAirdropSignature = await connection.requestAirdrop(
sender.publicKey,
LAMPORTS_PER_SOL,
);
await connection.confirmTransaction({
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
signature: senderAirdropSignature,
// Create the mint and mint 1.00 tokens to sender's ATA (the plugin auto-creates the ATA)
const createMintIx = client.token.instructions.createMint({
newMint: mint,
decimals: 2,
mintAuthority: sender.address,
});

// Airdrop 0.1 SOL to recipient for rent exemption
const recipientAirdropSignature = await connection.requestAirdrop(
recipient.publicKey,
LAMPORTS_PER_SOL / 10,
);
await connection.confirmTransaction({
blockhash: latestBlockhash.blockhash,
lastValidBlockHeight: latestBlockhash.lastValidBlockHeight,
signature: recipientAirdropSignature,
const mintToSenderIx = await client.token.instructions.mintToATA({
mint: mint.address,
owner: sender.address,
mintAuthority: sender,
amount: 100n,
decimals: 2,
});

// Create mint with feePayer paying the SOL fees
const mintPubkey = await createMint(
connection,
feePayer, // fee payer
sender.publicKey, // mint authority
sender.publicKey, // freeze authority
2, // decimals
Keypair.generate(),
{ commitment: "confirmed" },
TOKEN_PROGRAM_ID,
);
console.log("Mint Address:", mintPubkey.toBase58());

// Create sender's ATA (feePayer covers the fee)
const senderATA = await createAssociatedTokenAccount(
connection,
feePayer,
mintPubkey,
sender.publicKey,
{ commitment: "confirmed" },
TOKEN_PROGRAM_ID,
);
console.log("Sender ATA Address:", senderATA.toBase58());

// Create recipient's ATA (feePayer covers the fee)
const recipientATA = await createAssociatedTokenAccount(
connection,
feePayer,
mintPubkey,
recipient.publicKey,
{ commitment: "confirmed" },
TOKEN_PROGRAM_ID,
);
console.log("Recipient ATA Address:", recipientATA.toBase58());

// Mint 100 tokens (1.00 with 2 decimals) to the sender's ATA
const mintAmount = 100;
const mintSignature = await mintTo(
connection,
feePayer, // payer (covers SOL fee)
mintPubkey,
senderATA,
sender.publicKey, // mint authority
mintAmount,
[sender], // mint authority must sign
{ commitment: "confirmed" },
TOKEN_PROGRAM_ID,
);
const setup = await client.sendTransaction([createMintIx, mintToSenderIx]);
console.log("Transaction Signature:", setup.context.signature);
console.log("Successfully minted 1.0 tokens");
console.log("Transaction Signature:", mintSignature);

// Transfer 50 tokens from sender to recipient (feePayer pays SOL fee)
const transferAmount = 50;
const transactionSignature2 = await transfer(
connection,
feePayer,
senderATA,
recipientATA,
sender.publicKey,
transferAmount,
[sender],
{ commitment: "confirmed" },
TOKEN_PROGRAM_ID,
);
// Transfer 0.50 tokens from sender to recipient — fee paid by feePayer
const transferToRecipientIx = await client.token.instructions.transferToATA({
mint: mint.address,
authority: sender,
recipient: recipient.address,
amount: 50n,
decimals: 2,
});

const transfer = await client.sendTransaction([transferToRecipientIx]);
console.log("Transaction Signature:", transfer.context.signature);
console.log("Successfully transferred 0.5 tokens");
console.log("Transaction Signature:", transactionSignature2);

const senderTokenAccount = await getAccount(
connection,
senderATA,
"confirmed",
TOKEN_PROGRAM_ID,
);
const recipientTokenAccount = await getAccount(
connection,
recipientATA,
"confirmed",
TOKEN_PROGRAM_ID,
);
// Verify final balances
const [senderAta] = await findAssociatedTokenPda({
mint: mint.address,
owner: sender.address,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
const [recipientAta] = await findAssociatedTokenPda({
mint: mint.address,
owner: recipient.address,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});

const senderTokenAccount = await fetchToken(client.rpc, senderAta, {
commitment: "confirmed",
});
const recipientTokenAccount = await fetchToken(client.rpc, recipientAta, {
commitment: "confirmed",
});

console.log("=== Final Balances ===");
console.log(
"Sender balance:",
Number(senderTokenAccount.amount) / 100,
Number(senderTokenAccount.data.amount) / 100,
"tokens",
);
console.log(
"Recipient balance:",
Number(recipientTokenAccount.amount) / 100,
Number(recipientTokenAccount.data.amount) / 100,
"tokens",
);
// #endregion sponsor
Loading
Loading