From e9fcffc23bac9cd51f1fffc9916fabe9d3e67f3c Mon Sep 17 00:00:00 2001 From: yihau Date: Mon, 26 Feb 2024 13:12:19 +0800 Subject: [PATCH] add update metadata v2 method --- .../token-metadata/update-metadata/main.go | 9 +++-- .../metaplex/token_metadata/instruction.go | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/docs/_examples/program/metaplex/token-metadata/update-metadata/main.go b/docs/_examples/program/metaplex/token-metadata/update-metadata/main.go index 58cc5e6b..c73820f0 100644 --- a/docs/_examples/program/metaplex/token-metadata/update-metadata/main.go +++ b/docs/_examples/program/metaplex/token-metadata/update-metadata/main.go @@ -22,13 +22,13 @@ var alice, _ = types.AccountFromBase58("4voSPg3tYuWbKzimpQK9EbXHmuyy5fUrtXvpLDML func main() { c := client.NewClient(rpc.DevnetRPCEndpoint) - // mint address + // NFT nft := common.PublicKeyFromString("FK8eFRgmqewUzr7R6pYUxSPnSNusYmkhAV4e3pUJYdCd") + // get the metadata account address tokenMetadataPubkey, err := token_metadata.GetTokenMetaPubkey(nft) if err != nil { log.Fatalf("failed to find a valid token metadata, err: %v", err) - } recentBlockhashResponse, err := c.GetLatestBlockhash(context.Background()) @@ -42,10 +42,10 @@ func main() { FeePayer: feePayer.PublicKey, RecentBlockhash: recentBlockhashResponse.Blockhash, Instructions: []types.Instruction{ - token_metadata.UpdateMetadataAccount(token_metadata.UpdateMetadataAccountParam{ + token_metadata.UpdateMetadataAccountV2(token_metadata.UpdateMetadataAccountV2Param{ MetadataAccount: tokenMetadataPubkey, UpdateAuthority: feePayer.PublicKey, - Data: &token_metadata.Data{ + Data: &token_metadata.DataV2{ Name: "Fake Fake SMS #1355", Symbol: "FFSMB", Uri: "https://34c7ef24f4v2aejh75xhxy5z6ars4xv47gpsdrei6fiowptk2nqq.arweave.net/3wXyF1wvK6ARJ_9ue-O58CMuXrz5nyHEiPFQ6z5q02E", @@ -60,6 +60,7 @@ func main() { }, NewUpdateAuthority: &alice.PublicKey, PrimarySaleHappened: pointer.Get[bool](true), + IsMutable: pointer.Get[bool](true), }), }, }), diff --git a/program/metaplex/token_metadata/instruction.go b/program/metaplex/token_metadata/instruction.go index 8bcbdcf2..37e96e2d 100644 --- a/program/metaplex/token_metadata/instruction.go +++ b/program/metaplex/token_metadata/instruction.go @@ -135,6 +135,7 @@ func CreateMetadataAccount(param CreateMetadataAccountParam) types.Instruction { } } +// Deprecated: please use UpdateMetadataAccountV2 type UpdateMetadataAccountParam struct { MetadataAccount common.PublicKey UpdateAuthority common.PublicKey @@ -143,6 +144,7 @@ type UpdateMetadataAccountParam struct { PrimarySaleHappened *bool } +// Deprecated: please use UpdateMetadataAccountV2 func UpdateMetadataAccount(param UpdateMetadataAccountParam) types.Instruction { data, err := borsh.Serialize(struct { Instruction Instruction @@ -177,6 +179,43 @@ func UpdateMetadataAccount(param UpdateMetadataAccountParam) types.Instruction { } } +type UpdateMetadataAccountV2Param struct { + MetadataAccount common.PublicKey + UpdateAuthority common.PublicKey + Data *DataV2 + NewUpdateAuthority *common.PublicKey + PrimarySaleHappened *bool + IsMutable *bool +} + +func UpdateMetadataAccountV2(param UpdateMetadataAccountV2Param) types.Instruction { + data, err := borsh.Serialize(struct { + Instruction Instruction + Data *DataV2 + NewUpdateAuthority *common.PublicKey + PrimarySaleHappened *bool + IsMutable *bool + }{ + Instruction: InstructionUpdateMetadataAccountV2, + Data: param.Data, + NewUpdateAuthority: param.NewUpdateAuthority, + PrimarySaleHappened: param.PrimarySaleHappened, + IsMutable: param.IsMutable, + }) + if err != nil { + panic(err) + } + + return types.Instruction{ + ProgramID: common.MetaplexTokenMetaProgramID, + Accounts: []types.AccountMeta{ + {PubKey: param.MetadataAccount, IsSigner: false, IsWritable: true}, + {PubKey: param.UpdateAuthority, IsSigner: true, IsWritable: false}, + }, + Data: data, + } +} + type CreateMasterEditionParam struct { Edition common.PublicKey Mint common.PublicKey