metadata.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import * as anchor from '@coral-xyz/anchor';
  2. import type { Program } from '@coral-xyz/anchor';
  3. import { unpack } from '@solana/spl-token-metadata';
  4. import type { Metadata } from '../target/types/metadata';
  5. describe('metadata', () => {
  6. const provider = anchor.AnchorProvider.env();
  7. anchor.setProvider(provider);
  8. const program = anchor.workspace.Metadata as Program<Metadata>;
  9. const mintKeypair = new anchor.web3.Keypair();
  10. const metadata = {
  11. name: 'OPOS',
  12. symbol: 'OPOS',
  13. uri: 'https://raw.githubusercontent.com/solana-developers/opos-asset/main/assets/DeveloperPortal/metadata.json',
  14. };
  15. it('Create Mint with MetadataPointer and TokenMetadata Extensions', async () => {
  16. const tx = await program.methods
  17. .initialize(metadata)
  18. .accounts({ mintAccount: mintKeypair.publicKey })
  19. .signers([mintKeypair])
  20. .rpc({ skipPreflight: true });
  21. console.log('Your transaction signature', tx);
  22. });
  23. it('Update existing metadata field', async () => {
  24. // Add your test here.
  25. const tx = await program.methods
  26. .updateField({
  27. field: { name: {} }, // Update the name field
  28. value: 'Solana',
  29. })
  30. .accounts({ mintAccount: mintKeypair.publicKey })
  31. .rpc({ skipPreflight: true });
  32. console.log('Your transaction signature', tx);
  33. });
  34. it('Update metadata with custom field', async () => {
  35. const tx = await program.methods
  36. .updateField({
  37. field: { key: { 0: 'color' } }, // Add a custom field named "color"
  38. value: 'red',
  39. })
  40. .accounts({ mintAccount: mintKeypair.publicKey })
  41. .rpc({ skipPreflight: true });
  42. console.log('Your transaction signature', tx);
  43. });
  44. it('Remove custom field', async () => {
  45. const tx = await program.methods
  46. .removeKey('color') // Remove the custom field named "color"
  47. .accounts({ mintAccount: mintKeypair.publicKey })
  48. .rpc({ skipPreflight: true });
  49. console.log('Your transaction signature', tx);
  50. });
  51. it('Change update authority', async () => {
  52. const tx = await program.methods
  53. .updateAuthority()
  54. .accounts({
  55. mintAccount: mintKeypair.publicKey,
  56. newAuthority: null, // Set the update authority to null
  57. })
  58. .rpc({ skipPreflight: true });
  59. console.log('Your transaction signature', tx);
  60. });
  61. it('Emit metadata, decode transaction logs', async () => {
  62. const txSignature = await program.methods
  63. .emit()
  64. .accounts({ mintAccount: mintKeypair.publicKey })
  65. .rpc({ commitment: 'confirmed', skipPreflight: true });
  66. console.log('Your transaction signature', txSignature);
  67. // Fetch the transaction response
  68. const transactionResponse = await provider.connection.getTransaction(txSignature, {
  69. commitment: 'confirmed',
  70. });
  71. // Extract the log message that starts with "Program return:"
  72. const prefix = 'Program return: ';
  73. let log = transactionResponse.meta.logMessages.find((log) => log.startsWith(prefix));
  74. log = log.slice(prefix.length);
  75. const [_, data] = log.split(' ', 2);
  76. // Decode the data from base64 and unpack it into TokenMetadata
  77. const buffer = Buffer.from(data, 'base64');
  78. const metadata = unpack(buffer);
  79. console.log('Metadata', metadata);
  80. });
  81. it('Emit metadata, decode simulated transaction', async () => {
  82. const simulateResponse = await program.methods.emit().accounts({ mintAccount: mintKeypair.publicKey }).simulate();
  83. // Extract the log message that starts with "Program return:"
  84. const prefix = 'Program return: ';
  85. let log = simulateResponse.raw.find((log) => log.startsWith(prefix));
  86. log = log.slice(prefix.length);
  87. const [_, data] = log.split(' ', 2);
  88. // Decode the data from base64 and unpack it into TokenMetadata
  89. const buffer = Buffer.from(data, 'base64');
  90. const metadata = unpack(buffer);
  91. console.log('Metadata', metadata);
  92. });
  93. });