transfer-tokens.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { TransferTokens } from "../target/types/transfer_tokens";
  4. import { PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
  5. import { Metaplex } from "@metaplex-foundation/js";
  6. import {
  7. ASSOCIATED_TOKEN_PROGRAM_ID,
  8. getAssociatedTokenAddressSync,
  9. getOrCreateAssociatedTokenAccount,
  10. TOKEN_PROGRAM_ID,
  11. } from "@solana/spl-token";
  12. describe("Transfer Tokens", () => {
  13. // Configure the client to use the local cluster.
  14. const provider = anchor.AnchorProvider.env();
  15. anchor.setProvider(provider);
  16. const dataAccount = anchor.web3.Keypair.generate();
  17. const mintKeypair = anchor.web3.Keypair.generate();
  18. const wallet = provider.wallet as anchor.Wallet;
  19. const connection = provider.connection;
  20. const program = anchor.workspace.TransferTokens as Program<TransferTokens>;
  21. const nftTitle = "Homer NFT";
  22. const nftSymbol = "HOMR";
  23. const nftUri =
  24. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json";
  25. it("Is initialized!", async () => {
  26. // Add your test here.
  27. const tx = await program.methods
  28. .new()
  29. .accounts({ dataAccount: dataAccount.publicKey })
  30. .signers([dataAccount])
  31. .rpc();
  32. console.log("Your transaction signature", tx);
  33. });
  34. it("Create an SPL Token!", async () => {
  35. const metaplex = Metaplex.make(connection);
  36. const metadataAddress = await metaplex
  37. .nfts()
  38. .pdas()
  39. .metadata({ mint: mintKeypair.publicKey });
  40. // Add your test here.
  41. const tx = await program.methods
  42. .createTokenMint(
  43. wallet.publicKey, // freeze authority
  44. 9, // 0 decimals for NFT
  45. nftTitle, // NFT name
  46. nftSymbol, // NFT symbol
  47. nftUri // NFT URI
  48. )
  49. .accounts({
  50. payer: wallet.publicKey,
  51. mint: mintKeypair.publicKey,
  52. metadata: metadataAddress,
  53. mintAuthority: wallet.publicKey,
  54. rentAddress: SYSVAR_RENT_PUBKEY,
  55. metadataProgramId: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  56. })
  57. .signers([mintKeypair])
  58. .rpc({ skipPreflight: true });
  59. console.log("Your transaction signature", tx);
  60. });
  61. it("Mint some tokens to your wallet!", async () => {
  62. // Wallet's associated token account address for mint
  63. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  64. connection,
  65. wallet.payer, // payer
  66. mintKeypair.publicKey, // mint
  67. wallet.publicKey // owner
  68. );
  69. const tx = await program.methods
  70. .mintTo(
  71. new anchor.BN(150) // amount to mint
  72. )
  73. .accounts({
  74. mintAuthority: wallet.publicKey,
  75. tokenAccount: tokenAccount.address,
  76. mint: mintKeypair.publicKey,
  77. })
  78. .rpc({ skipPreflight: true });
  79. console.log("Your transaction signature", tx);
  80. });
  81. it("Transfer some tokens to another wallet!", async () => {
  82. // Wallet's associated token account address for mint
  83. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  84. connection,
  85. wallet.payer, // payer
  86. mintKeypair.publicKey, // mint
  87. wallet.publicKey // owner
  88. );
  89. const receipient = anchor.web3.Keypair.generate();
  90. const receipientTokenAccount = await getOrCreateAssociatedTokenAccount(
  91. connection,
  92. wallet.payer, // payer
  93. mintKeypair.publicKey, // mint account
  94. receipient.publicKey // owner account
  95. );
  96. const tx = await program.methods
  97. .transferTokens(
  98. new anchor.BN(150)
  99. )
  100. .accounts({
  101. from: tokenAccount.address,
  102. to: receipientTokenAccount.address,
  103. owner: wallet.publicKey,
  104. })
  105. .rpc();
  106. console.log("Your transaction signature", tx);
  107. });
  108. });