From ed830a50b8bd50d23b135d4ad115a8311911f2f5 Mon Sep 17 00:00:00 2001 From: Alice Date: Mon, 13 Sep 2021 16:03:26 +0200 Subject: [PATCH] Add function to update token metadata --- api/metaplex_api.py | 20 +++++++++++++++++++- metaplex/transactions.py | 25 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/api/metaplex_api.py b/api/metaplex_api.py index 66de3a3..53ec11c 100644 --- a/api/metaplex_api.py +++ b/api/metaplex_api.py @@ -2,7 +2,7 @@ from cryptography.fernet import Fernet import base58 from solana.account import Account -from metaplex.transactions import deploy, topup, mint, send, burn +from metaplex.transactions import deploy, topup, mint, send, burn, update_token_metadata from utils.execution_engine import execute class MetaplexAPI(): @@ -90,6 +90,24 @@ def mint(self, api_endpoint, contract_key, dest_key, link, max_retries=3, skip_c # except: # return json.dumps({"status": 400}) + def update_token_metadata(self, api_endpoint, mint_token_id, link, data, creators_addresses, creators_verified, creators_share, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True, supply=1 ): + """ + Updates the json metadata for a given mint token id. + """ + tx, signers = update_token_metadata(api_endpoint, self.account, mint_token_id, link, data, creators_addresses, creators_verified, creators_share) + resp = execute( + api_endpoint, + tx, + signers, + max_retries=max_retries, + skip_confirmation=skip_confirmation, + max_timeout=max_timeout, + target=target, + finalized=finalized, + ) + resp["status"] = 200 + return json.dumps(resp) + def send(self, api_endpoint, contract_key, sender_key, dest_key, encrypted_private_key, max_retries=3, skip_confirmation=False, max_timeout=60, target=20, finalized=True): """ Transfer a token on a given network and contract from the sender to the recipient. diff --git a/metaplex/transactions.py b/metaplex/transactions.py index 49cb0ed..801bd39 100644 --- a/metaplex/transactions.py +++ b/metaplex/transactions.py @@ -108,6 +108,31 @@ def topup(api_endpoint, sender_account, to, amount=None): return tx, signers +def update_token_metadata(api_endpoint, source_account, mint_token_id, link, data, creators_addresses, creators_verified, creators_share): + """ + Updates the json metadata for a given mint token id. + """ + mint_account = PublicKey(mint_token_id) + signers = [source_account] + + tx = Transaction() + update_metadata_data = update_metadata_instruction_data( + data['name'], + data['symbol'], + link, + creators_addresses, + creators_verified, + creators_share, + ) + update_metadata_ix = update_metadata_instruction( + update_metadata_data, + source_account.public_key(), + mint_account, + ) + tx = tx.add(update_metadata_ix) + return tx, signers + + def mint(api_endpoint, source_account, contract_key, dest_key, link, supply=1): """ Mint a token on the specified network and contract, into the wallet specified by address.