simple_collectible.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // SPDX-License-Identifier: Apache-2.0
  2. // DISCLAIMER: This file is an example of how to mint and transfer NFTs on Solana. It is not production ready and has not been audited for security.
  3. // Use it at your own risk.
  4. import { loadContractAndCallConstructor, newConnectionAndPayer } from "./setup";
  5. import { Keypair } from "@solana/web3.js";
  6. import { createMint, getOrCreateAssociatedTokenAccount, TOKEN_PROGRAM_ID } from "@solana/spl-token";
  7. import expect from "expect";
  8. describe('Simple collectible', function () {
  9. this.timeout(500000);
  10. it('nft example', async function mint_nft() {
  11. const [connection, payer] = newConnectionAndPayer();
  12. const mint_authority = Keypair.generate();
  13. const freezeAuthority = Keypair.generate();
  14. // Create and initialize a new mint based on the funding account and a mint authority
  15. const mint = await createMint(
  16. connection,
  17. payer,
  18. mint_authority.publicKey,
  19. freezeAuthority.publicKey,
  20. 0
  21. );
  22. const nft_owner = Keypair.generate();
  23. const metadata_authority = Keypair.generate();
  24. // On Solana, an account must have an associated token account to save information about how many tokens
  25. // the owner account owns. The associated account depends on both the mint account and the owner
  26. const owner_token_account = await getOrCreateAssociatedTokenAccount(
  27. connection,
  28. payer,
  29. mint, // Mint account
  30. nft_owner.publicKey // Owner account
  31. );
  32. // Each contract in this example is a unique NFT
  33. const { provider, program, storage } = await loadContractAndCallConstructor('SimpleCollectible', [mint, metadata_authority.publicKey]);
  34. const nft_uri = "www.nft.com";
  35. // Create a collectible for an owner given a mint authority.
  36. await program.methods.createCollectible(
  37. nft_uri,
  38. mint_authority.publicKey,
  39. owner_token_account.address)
  40. .accounts({ dataAccount: storage.publicKey })
  41. .remainingAccounts([
  42. { pubkey: mint, isSigner: false, isWritable: true },
  43. { pubkey: owner_token_account.address, isSigner: false, isWritable: true },
  44. { pubkey: mint_authority.publicKey, isSigner: true, isWritable: false },
  45. { pubkey: metadata_authority.publicKey, isSigner: true, isWritable: true }
  46. ])
  47. .signers([mint_authority, metadata_authority])
  48. .rpc();
  49. const new_owner = Keypair.generate();
  50. // A new owner must have an associated token account
  51. const new_owner_token_account = await getOrCreateAssociatedTokenAccount(
  52. connection,
  53. payer,
  54. mint, // Mint account associated to the NFT
  55. new_owner.publicKey // New owner account
  56. );
  57. // Transfer ownership to another owner
  58. await program.methods.transferOwnership(
  59. owner_token_account.address,
  60. new_owner_token_account.address)
  61. .remainingAccounts([
  62. { pubkey: new_owner_token_account.address, isSigner: false, isWritable: true },
  63. { pubkey: owner_token_account.address, isSigner: false, isWritable: true },
  64. { pubkey: nft_owner.publicKey, isSigner: true, isWritable: false },
  65. ])
  66. .signers([nft_owner])
  67. .rpc();
  68. // Confirm that the ownership transference worked
  69. const verify_transfer_result = await program.methods.isOwner(
  70. new_owner.publicKey,
  71. new_owner_token_account.address)
  72. .accounts({ dataAccount: storage.publicKey })
  73. .remainingAccounts([
  74. { pubkey: new_owner_token_account.address, isSigner: false, isWritable: false },
  75. ])
  76. .view();
  77. expect(verify_transfer_result).toBe(true);
  78. // Retrieve information about the NFT
  79. const token_uri = await program.methods.getNftUri()
  80. .accounts({ dataAccount: storage.publicKey })
  81. .view();
  82. expect(token_uri).toBe(nft_uri);
  83. // Update the NFT URI
  84. const new_uri = "www.token.com";
  85. await program.methods.updateNftUri(new_uri)
  86. .accounts({ dataAccount: storage.publicKey })
  87. .remainingAccounts([
  88. { pubkey: metadata_authority.publicKey, isSigner: true, isWritable: true },
  89. ])
  90. .signers([metadata_authority])
  91. .rpc();
  92. const new_uri_saved = await program.methods.getNftUri()
  93. .accounts({ dataAccount: storage.publicKey })
  94. .view();
  95. expect(new_uri_saved).toBe(new_uri);
  96. });
  97. });