transfer-tokens.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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(wallet.publicKey)
  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, // payer
  44. mintKeypair.publicKey, // mint
  45. wallet.publicKey, // mint authority
  46. wallet.publicKey, // freeze authority
  47. metadataAddress, // metadata address
  48. 9, // 0 decimals for NFT
  49. nftTitle, // NFT name
  50. nftSymbol, // NFT symbol
  51. nftUri // NFT URI
  52. )
  53. .accounts({ dataAccount: dataAccount.publicKey })
  54. .remainingAccounts([
  55. {
  56. pubkey: wallet.publicKey,
  57. isWritable: true,
  58. isSigner: true,
  59. },
  60. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: true },
  61. {
  62. pubkey: new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"),
  63. isWritable: false,
  64. isSigner: false,
  65. },
  66. { pubkey: metadataAddress, isWritable: true, isSigner: false },
  67. { pubkey: SystemProgram.programId, isWritable: false, isSigner: false },
  68. { pubkey: SYSVAR_RENT_PUBKEY, isWritable: false, isSigner: false },
  69. ])
  70. .signers([mintKeypair])
  71. .rpc({ skipPreflight: true })
  72. console.log("Your transaction signature", tx)
  73. })
  74. it("Mint some tokens to your wallet!", async () => {
  75. // Wallet's associated token account address for mint
  76. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  77. connection,
  78. wallet.payer, // payer
  79. mintKeypair.publicKey, // mint
  80. wallet.publicKey // owner
  81. )
  82. const tx = await program.methods
  83. .mintTo(
  84. wallet.publicKey, // payer
  85. tokenAccount.address, // associated token account address
  86. mintKeypair.publicKey, // mint
  87. wallet.publicKey, // owner of token account
  88. new anchor.BN(150) // amount to mint
  89. )
  90. .accounts({ dataAccount: dataAccount.publicKey })
  91. .remainingAccounts([
  92. {
  93. pubkey: wallet.publicKey,
  94. isWritable: true,
  95. isSigner: true,
  96. },
  97. { pubkey: tokenAccount.address, isWritable: true, isSigner: false },
  98. { pubkey: mintKeypair.publicKey, isWritable: true, isSigner: false },
  99. {
  100. pubkey: SystemProgram.programId,
  101. isWritable: false,
  102. isSigner: false,
  103. },
  104. { pubkey: TOKEN_PROGRAM_ID, isWritable: false, isSigner: false },
  105. {
  106. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  107. isWritable: false,
  108. isSigner: false,
  109. },
  110. ])
  111. .rpc({ skipPreflight: true })
  112. console.log("Your transaction signature", tx)
  113. })
  114. it("Transfer some tokens to another wallet!", async () => {
  115. // Wallet's associated token account address for mint
  116. const tokenAccount = await getOrCreateAssociatedTokenAccount(
  117. connection,
  118. wallet.payer, // payer
  119. mintKeypair.publicKey, // mint
  120. wallet.publicKey // owner
  121. )
  122. const receipient = anchor.web3.Keypair.generate()
  123. const receipientTokenAccount = await getOrCreateAssociatedTokenAccount(
  124. connection,
  125. wallet.payer, // payer
  126. mintKeypair.publicKey, // mint account
  127. receipient.publicKey // owner account
  128. )
  129. const tx = await program.methods
  130. .transferTokens(
  131. tokenAccount.address,
  132. receipientTokenAccount.address,
  133. new anchor.BN(150)
  134. )
  135. .accounts({ dataAccount: dataAccount.publicKey })
  136. .remainingAccounts([
  137. {
  138. pubkey: wallet.publicKey,
  139. isWritable: true,
  140. isSigner: true,
  141. },
  142. {
  143. pubkey: mintKeypair.publicKey,
  144. isWritable: false,
  145. isSigner: false,
  146. },
  147. {
  148. pubkey: tokenAccount.address,
  149. isWritable: true,
  150. isSigner: false,
  151. },
  152. {
  153. pubkey: receipientTokenAccount.address,
  154. isWritable: true,
  155. isSigner: false,
  156. },
  157. ])
  158. .rpc()
  159. console.log("Your transaction signature", tx)
  160. })
  161. })