simple_collectible.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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: true },
  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. .accounts({ dataAccount: storage.publicKey })
  62. .remainingAccounts([
  63. { pubkey: new_owner_token_account.address, isSigner: false, isWritable: true },
  64. { pubkey: owner_token_account.address, isSigner: false, isWritable: true },
  65. { pubkey: nft_owner.publicKey, isSigner: true, isWritable: true },
  66. ])
  67. .signers([nft_owner])
  68. .rpc();
  69. // Confirm that the ownership transference worked
  70. const verify_transfer_result = await program.methods.isOwner(
  71. new_owner.publicKey,
  72. new_owner_token_account.address)
  73. .accounts({ dataAccount: storage.publicKey })
  74. .remainingAccounts([
  75. { pubkey: new_owner_token_account.address, isSigner: false, isWritable: false },
  76. ])
  77. .view();
  78. expect(verify_transfer_result).toBe(true);
  79. // Retrieve information about the NFT
  80. const token_uri = await program.methods.getNftUri()
  81. .accounts({ dataAccount: storage.publicKey })
  82. .view();
  83. expect(token_uri).toBe(nft_uri);
  84. // Update the NFT URI
  85. const new_uri = "www.token.com";
  86. await program.methods.updateNftUri(new_uri)
  87. .accounts({ dataAccount: storage.publicKey })
  88. .remainingAccounts([
  89. { pubkey: metadata_authority.publicKey, isSigner: true, isWritable: true },
  90. ])
  91. .signers([metadata_authority])
  92. .rpc();
  93. const new_uri_saved = await program.methods.getNftUri()
  94. .accounts({ dataAccount: storage.publicKey })
  95. .view();
  96. expect(new_uri_saved).toBe(new_uri);
  97. });
  98. });