test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID } from "@metaplex-foundation/mpl-token-metadata";
  2. import * as anchor from "@coral-xyz/anchor";
  3. import { TransferTokens } from "../target/types/transfer_tokens";
  4. import {
  5. PublicKey,
  6. Keypair,
  7. SYSVAR_RENT_PUBKEY,
  8. SystemProgram,
  9. } from "@solana/web3.js";
  10. import {
  11. getAssociatedTokenAddressSync,
  12. ASSOCIATED_TOKEN_PROGRAM_ID,
  13. TOKEN_PROGRAM_ID,
  14. } from "@solana/spl-token";
  15. describe("Transfer Tokens", () => {
  16. const provider = anchor.AnchorProvider.env();
  17. anchor.setProvider(provider);
  18. const payer = provider.wallet as anchor.Wallet;
  19. const program = anchor.workspace
  20. .TransferTokens as anchor.Program<TransferTokens>;
  21. const metadata = {
  22. name: "Solana Gold",
  23. symbol: "GOLDSOL",
  24. uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
  25. };
  26. // Generate new keypair to use as address for mint account.
  27. const mintKeypair = new Keypair();
  28. // Generate new keypair to use as address for recipient wallet.
  29. const recipient = new Keypair();
  30. // Derive the associated token address account for the mint and payer.
  31. const senderTokenAddress = getAssociatedTokenAddressSync(
  32. mintKeypair.publicKey,
  33. payer.publicKey
  34. );
  35. // Derive the associated token address account for the mint and recipient.
  36. const recepientTokenAddress = getAssociatedTokenAddressSync(
  37. mintKeypair.publicKey,
  38. recipient.publicKey
  39. );
  40. it("Create an SPL Token!", async () => {
  41. // Derive the metadata account address.
  42. const [metadataAddress] = PublicKey.findProgramAddressSync(
  43. [
  44. Buffer.from("metadata"),
  45. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  46. mintKeypair.publicKey.toBuffer(),
  47. ],
  48. TOKEN_METADATA_PROGRAM_ID
  49. );
  50. const transactionSignature = await program.methods
  51. .createToken(metadata.name, metadata.symbol, metadata.uri)
  52. .accounts({
  53. payer: payer.publicKey,
  54. mintAccount: mintKeypair.publicKey,
  55. metadataAccount: metadataAddress,
  56. tokenProgram: TOKEN_PROGRAM_ID,
  57. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  58. systemProgram: SystemProgram.programId,
  59. rent: SYSVAR_RENT_PUBKEY,
  60. })
  61. .signers([mintKeypair])
  62. .rpc();
  63. console.log("Success!");
  64. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  65. console.log(` Transaction Signature: ${transactionSignature}`);
  66. });
  67. it("Mint tokens!", async () => {
  68. // Amount of tokens to mint.
  69. const amount = new anchor.BN(100);
  70. // Mint the tokens to the associated token account.
  71. const transactionSignature = await program.methods
  72. .mintToken(amount)
  73. .accounts({
  74. mintAuthority: payer.publicKey,
  75. recepient: payer.publicKey,
  76. mintAccount: mintKeypair.publicKey,
  77. associatedTokenAccount: senderTokenAddress,
  78. tokenProgram: TOKEN_PROGRAM_ID,
  79. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  80. systemProgram: SystemProgram.programId,
  81. })
  82. .rpc();
  83. console.log("Success!");
  84. console.log(` Associated Token Account Address: ${senderTokenAddress}`);
  85. console.log(` Transaction Signature: ${transactionSignature}`);
  86. });
  87. it("Transfer tokens!", async () => {
  88. // Amount of tokens to transfer.
  89. const amount = new anchor.BN(50);
  90. const transactionSignature = await program.methods
  91. .transferTokens(amount)
  92. .accounts({
  93. sender: payer.publicKey,
  94. recipient: recipient.publicKey,
  95. mintAccount: mintKeypair.publicKey,
  96. senderTokenAccount: senderTokenAddress,
  97. recipientTokenAccount: recepientTokenAddress,
  98. tokenProgram: TOKEN_PROGRAM_ID,
  99. associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
  100. systemProgram: SystemProgram.programId,
  101. })
  102. .rpc();
  103. console.log("Success!");
  104. console.log(` Transaction Signature: ${transactionSignature}`);
  105. });
  106. });