metadata.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import * as anchor from "@coral-xyz/anchor";
  2. import { Program } from "@coral-xyz/anchor";
  3. import { Metadata } from "../target/types/metadata";
  4. import { unpack } from "@solana/spl-token-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(
  69. txSignature,
  70. {
  71. commitment: "confirmed",
  72. }
  73. );
  74. // Extract the log message that starts with "Program return:"
  75. const prefix = "Program return: ";
  76. let log = transactionResponse.meta.logMessages.find((log) =>
  77. log.startsWith(prefix)
  78. );
  79. log = log.slice(prefix.length);
  80. const [_, data] = log.split(" ", 2);
  81. // Decode the data from base64 and unpack it into TokenMetadata
  82. const buffer = Buffer.from(data, "base64");
  83. const metadata = unpack(buffer);
  84. console.log("Metadata", metadata);
  85. });
  86. it("Emit metadata, decode simulated transaction", async () => {
  87. const simulateResponse = await program.methods
  88. .emit()
  89. .accounts({ mintAccount: mintKeypair.publicKey })
  90. .simulate();
  91. // Extract the log message that starts with "Program return:"
  92. const prefix = "Program return: ";
  93. let log = simulateResponse.raw.find((log) => log.startsWith(prefix));
  94. log = log.slice(prefix.length);
  95. const [_, data] = log.split(" ", 2);
  96. // Decode the data from base64 and unpack it into TokenMetadata
  97. const buffer = Buffer.from(data, "base64");
  98. const metadata = unpack(buffer);
  99. console.log("Metadata", metadata);
  100. });
  101. });