test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { TransferTokens } from "../target/types/transfer_tokens";
  3. import { Keypair } from "@solana/web3.js";
  4. import { getAssociatedTokenAddressSync } from "@solana/spl-token";
  5. describe("Transfer Tokens", () => {
  6. const provider = anchor.AnchorProvider.env();
  7. anchor.setProvider(provider);
  8. const payer = provider.wallet as anchor.Wallet;
  9. const program = anchor.workspace
  10. .TransferTokens as anchor.Program<TransferTokens>;
  11. const metadata = {
  12. name: "Solana Gold",
  13. symbol: "GOLDSOL",
  14. uri: "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
  15. };
  16. // Generate new keypair to use as address for mint account.
  17. const mintKeypair = new Keypair();
  18. // Generate new keypair to use as address for recipient wallet.
  19. const recipient = new Keypair();
  20. // Derive the associated token address account for the mint and payer.
  21. const senderTokenAddress = getAssociatedTokenAddressSync(
  22. mintKeypair.publicKey,
  23. payer.publicKey
  24. );
  25. // Derive the associated token address account for the mint and recipient.
  26. const recepientTokenAddress = getAssociatedTokenAddressSync(
  27. mintKeypair.publicKey,
  28. recipient.publicKey
  29. );
  30. it("Create an SPL Token!", async () => {
  31. const transactionSignature = await program.methods
  32. .createToken(metadata.name, metadata.symbol, metadata.uri)
  33. .accounts({
  34. payer: payer.publicKey,
  35. mintAccount: mintKeypair.publicKey,
  36. })
  37. .signers([mintKeypair])
  38. .rpc();
  39. console.log("Success!");
  40. console.log(` Mint Address: ${mintKeypair.publicKey}`);
  41. console.log(` Transaction Signature: ${transactionSignature}`);
  42. });
  43. it("Mint tokens!", async () => {
  44. // Amount of tokens to mint.
  45. const amount = new anchor.BN(100);
  46. // Mint the tokens to the associated token account.
  47. const transactionSignature = await program.methods
  48. .mintToken(amount)
  49. .accounts({
  50. mintAuthority: payer.publicKey,
  51. recipient: payer.publicKey,
  52. mintAccount: mintKeypair.publicKey,
  53. associatedTokenAccount: senderTokenAddress,
  54. })
  55. .rpc();
  56. console.log("Success!");
  57. console.log(` Associated Token Account Address: ${senderTokenAddress}`);
  58. console.log(` Transaction Signature: ${transactionSignature}`);
  59. });
  60. it("Transfer tokens!", async () => {
  61. // Amount of tokens to transfer.
  62. const amount = new anchor.BN(50);
  63. const transactionSignature = await program.methods
  64. .transferTokens(amount)
  65. .accounts({
  66. sender: payer.publicKey,
  67. recipient: recipient.publicKey,
  68. mintAccount: mintKeypair.publicKey,
  69. senderTokenAccount: senderTokenAddress,
  70. recipientTokenAccount: recepientTokenAddress,
  71. })
  72. .rpc();
  73. console.log("Success!");
  74. console.log(` Transaction Signature: ${transactionSignature}`);
  75. });
  76. });