test.ts 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. import {
  2. PROGRAM_ID as TOKEN_METADATA_PROGRAM_ID
  3. } from '@metaplex-foundation/mpl-token-metadata';
  4. import * as anchor from "@project-serum/anchor";
  5. import { ASSOCIATED_PROGRAM_ID } from '@project-serum/anchor/dist/cjs/utils/token';
  6. import { TransferTokens } from "../target/types/transfer_tokens";
  7. describe("Transfer Tokens", () => {
  8. const provider = anchor.AnchorProvider.env();
  9. anchor.setProvider(provider);
  10. const payer = provider.wallet as anchor.Wallet;
  11. const program = anchor.workspace.TransferTokens as anchor.Program<TransferTokens>;
  12. const tokenMintKeypair: anchor.web3.Keypair = anchor.web3.Keypair.generate();
  13. const nftMintKeypair: anchor.web3.Keypair = anchor.web3.Keypair.generate();
  14. const recipientWallet: anchor.web3.Keypair = anchor.web3.Keypair.generate();
  15. it("Create an SPL Token!", async () => {
  16. const metadataAddress = anchor.web3.PublicKey.findProgramAddressSync(
  17. [
  18. Buffer.from("metadata"),
  19. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  20. tokenMintKeypair.publicKey.toBuffer(),
  21. ],
  22. TOKEN_METADATA_PROGRAM_ID
  23. )[0];
  24. const sx = await program.methods.createToken(
  25. "Solana Gold",
  26. "GOLDSOL",
  27. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/spl-token.json",
  28. 9,
  29. )
  30. .accounts({
  31. metadataAccount: metadataAddress,
  32. mintAccount: tokenMintKeypair.publicKey,
  33. mintAuthority: payer.publicKey,
  34. payer: payer.publicKey,
  35. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  36. systemProgram: anchor.web3.SystemProgram.programId,
  37. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  38. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  39. })
  40. .signers([tokenMintKeypair, payer.payer])
  41. .rpc();
  42. console.log("Success!");
  43. console.log(` Mint Address: ${tokenMintKeypair.publicKey}`);
  44. console.log(` Tx Signature: ${sx}`);
  45. });
  46. it("Create an NFT!", async () => {
  47. const metadataAddress = anchor.web3.PublicKey.findProgramAddressSync(
  48. [
  49. Buffer.from("metadata"),
  50. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  51. nftMintKeypair.publicKey.toBuffer(),
  52. ],
  53. TOKEN_METADATA_PROGRAM_ID
  54. )[0];
  55. const sx = await program.methods.createToken(
  56. "Homer NFT",
  57. "HOMR",
  58. "https://raw.githubusercontent.com/solana-developers/program-examples/new-examples/tokens/tokens/.assets/nft.json",
  59. 0,
  60. )
  61. .accounts({
  62. metadataAccount: metadataAddress,
  63. mintAccount: nftMintKeypair.publicKey,
  64. mintAuthority: payer.publicKey,
  65. payer: payer.publicKey,
  66. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  67. systemProgram: anchor.web3.SystemProgram.programId,
  68. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  69. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  70. })
  71. .signers([nftMintKeypair, payer.payer])
  72. .rpc();
  73. console.log("Success!");
  74. console.log(` Mint Address: ${nftMintKeypair.publicKey}`);
  75. console.log(` Tx Signature: ${sx}`);
  76. });
  77. it("Mint some tokens to your wallet!", async () => {
  78. const associatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
  79. mint: tokenMintKeypair.publicKey,
  80. owner: payer.publicKey,
  81. });
  82. const sx = await program.methods.mintSpl(
  83. new anchor.BN(150)
  84. )
  85. .accounts({
  86. associatedTokenAccount: associatedTokenAccountAddress,
  87. mintAccount: tokenMintKeypair.publicKey,
  88. mintAuthority: payer.publicKey,
  89. payer: payer.publicKey,
  90. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  91. systemProgram: anchor.web3.SystemProgram.programId,
  92. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  93. associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
  94. })
  95. .signers([payer.payer])
  96. .rpc();
  97. console.log("Success!");
  98. console.log(` Mint Address: ${tokenMintKeypair.publicKey}`);
  99. console.log(` Tx Signature: ${sx}`);
  100. });
  101. it("Mint the NFT to your wallet!", async () => {
  102. const metadataAddress = (anchor.web3.PublicKey.findProgramAddressSync(
  103. [
  104. Buffer.from("metadata"),
  105. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  106. nftMintKeypair.publicKey.toBuffer(),
  107. ],
  108. TOKEN_METADATA_PROGRAM_ID
  109. ))[0];
  110. const editionAddress = (anchor.web3.PublicKey.findProgramAddressSync(
  111. [
  112. Buffer.from("metadata"),
  113. TOKEN_METADATA_PROGRAM_ID.toBuffer(),
  114. nftMintKeypair.publicKey.toBuffer(),
  115. Buffer.from("edition"),
  116. ],
  117. TOKEN_METADATA_PROGRAM_ID
  118. ))[0];
  119. const associatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
  120. mint: nftMintKeypair.publicKey,
  121. owner: payer.publicKey,
  122. });
  123. const sx = await program.methods.mintNft()
  124. .accounts({
  125. associatedTokenAccount: associatedTokenAccountAddress,
  126. editionAccount: editionAddress,
  127. metadataAccount: metadataAddress,
  128. mintAccount: nftMintKeypair.publicKey,
  129. mintAuthority: payer.publicKey,
  130. payer: payer.publicKey,
  131. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  132. systemProgram: anchor.web3.SystemProgram.programId,
  133. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  134. associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
  135. tokenMetadataProgram: TOKEN_METADATA_PROGRAM_ID,
  136. })
  137. .signers([payer.payer])
  138. .rpc();
  139. console.log("Success!");
  140. console.log(` ATA Address: ${associatedTokenAccountAddress}`);
  141. console.log(` Tx Signature: ${sx}`);
  142. });
  143. it("Prep a new test wallet for transfers", async () => {
  144. await provider.connection.confirmTransaction(
  145. await provider.connection.requestAirdrop(
  146. recipientWallet.publicKey,
  147. await provider.connection.getMinimumBalanceForRentExemption(0),
  148. )
  149. );
  150. console.log(`Recipient Pubkey: ${recipientWallet.publicKey}`);
  151. });
  152. it("Transfer some tokens to another wallet!", async () => {
  153. const fromAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
  154. mint: tokenMintKeypair.publicKey,
  155. owner: payer.publicKey,
  156. });
  157. const toAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
  158. mint: tokenMintKeypair.publicKey,
  159. owner: recipientWallet.publicKey,
  160. });
  161. const sx = await program.methods.transferTokens(
  162. new anchor.BN(150)
  163. )
  164. .accounts({
  165. mintAccount: tokenMintKeypair.publicKey,
  166. fromAssociatedTokenAccount: fromAssociatedTokenAccountAddress,
  167. owner: payer.publicKey,
  168. toAssociatedTokenAccount: toAssociatedTokenAccountAddress,
  169. recipient: recipientWallet.publicKey,
  170. payer: payer.publicKey,
  171. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  172. systemProgram: anchor.web3.SystemProgram.programId,
  173. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  174. associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
  175. })
  176. .signers([payer.payer])
  177. .rpc();
  178. console.log("Success!");
  179. console.log(` Mint Address: ${tokenMintKeypair.publicKey}`);
  180. console.log(` Tx Signature: ${sx}`);
  181. });
  182. it("Transfer the NFT to another wallet!", async () => {
  183. const fromAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
  184. mint: nftMintKeypair.publicKey,
  185. owner: payer.publicKey,
  186. });
  187. const toAssociatedTokenAccountAddress = await anchor.utils.token.associatedAddress({
  188. mint: nftMintKeypair.publicKey,
  189. owner: recipientWallet.publicKey,
  190. });
  191. const sx = await program.methods.transferTokens(
  192. new anchor.BN(1)
  193. )
  194. .accounts({
  195. mintAccount: nftMintKeypair.publicKey,
  196. fromAssociatedTokenAccount: fromAssociatedTokenAccountAddress,
  197. owner: payer.publicKey,
  198. toAssociatedTokenAccount: toAssociatedTokenAccountAddress,
  199. recipient: recipientWallet.publicKey,
  200. payer: payer.publicKey,
  201. rent: anchor.web3.SYSVAR_RENT_PUBKEY,
  202. systemProgram: anchor.web3.SystemProgram.programId,
  203. tokenProgram: anchor.utils.token.TOKEN_PROGRAM_ID,
  204. associatedTokenProgram: ASSOCIATED_PROGRAM_ID,
  205. })
  206. .signers([payer.payer])
  207. .rpc();
  208. console.log("Success!");
  209. console.log(` Mint Address: ${nftMintKeypair.publicKey}`);
  210. console.log(` Tx Signature: ${sx}`);
  211. });
  212. });