simple_collectible.spec.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. .accounts({
  38. dataAccount: storage.publicKey,
  39. mintAccount: mint,
  40. ownerTokenAccount: owner_token_account.address,
  41. mintAuthority: mint_authority.publicKey,
  42. })
  43. .signers([mint_authority])
  44. .rpc();
  45. const new_owner = Keypair.generate();
  46. // A new owner must have an associated token account
  47. const new_owner_token_account = await getOrCreateAssociatedTokenAccount(
  48. connection,
  49. payer,
  50. mint, // Mint account associated to the NFT
  51. new_owner.publicKey // New owner account
  52. );
  53. // Transfer ownership to another owner
  54. await program.methods.transferOwnership()
  55. .accounts({
  56. oldTokenAccount: owner_token_account.address,
  57. newTokenAccount: new_owner_token_account.address,
  58. oldOwner: nft_owner.publicKey
  59. })
  60. .signers([nft_owner])
  61. .rpc();
  62. // Confirm that the ownership transference worked
  63. const verify_transfer_result = await program.methods.isOwner(
  64. new_owner.publicKey)
  65. .accounts({
  66. dataAccount: storage.publicKey,
  67. tokenAccount: new_owner_token_account.address
  68. })
  69. .view();
  70. expect(verify_transfer_result).toBe(true);
  71. // Update the NFT URI
  72. const new_uri = "www.token.com";
  73. await program.methods.updateNftUri(new_uri)
  74. .accounts({
  75. dataAccount: storage.publicKey,
  76. metadataSigner: metadata_authority.publicKey
  77. })
  78. .signers([metadata_authority])
  79. .rpc();
  80. const new_uri_saved = await program.methods.getNftUri()
  81. .accounts({ dataAccount: storage.publicKey })
  82. .view();
  83. expect(new_uri_saved).toBe(new_uri);
  84. });
  85. });