From 4e646b9cf457d737855e9153e3e5ac11854d7981 Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Sun, 26 Feb 2023 10:13:26 +0300 Subject: [PATCH] Update sg721 helpers to include metadata update related functions --- contracts/sg721/contract.ts | 163 ++++++++++++++++++++++++++++++++++++ 1 file changed, 163 insertions(+) diff --git a/contracts/sg721/contract.ts b/contracts/sg721/contract.ts index 270eb73..918cf9b 100644 --- a/contracts/sg721/contract.ts +++ b/contracts/sg721/contract.ts @@ -86,6 +86,9 @@ export interface SG721Instance { burn: (tokenId: string) => Promise batchBurn: (tokenIds: string) => Promise batchTransfer: (recipient: string, tokenIds: string) => Promise + updateTokenMetadata: (tokenId: string, tokenURI: string) => Promise + batchUpdateTokenMetadata: (tokenIds: string, tokenURI: string) => Promise + freezeMetadata: () => Promise } export interface Sg721Messages { @@ -101,6 +104,9 @@ export interface Sg721Messages { batchTransfer: (recipient: string, tokenIds: string) => BatchTransferMessage updateCollectionInfo: (collectionInfo: CollectionInfo) => UpdateCollectionInfoMessage freezeCollectionInfo: () => FreezeCollectionInfoMessage + updateTokenMetadata: (tokenId: string, tokenURI: string) => UpdateTokenMetadataMessage + batchUpdateTokenMetadata: (tokenIds: string, tokenURI: string) => BatchUpdateTokenMetadataMessage + freezeMetadata: () => FreezeMetadataMessage } export interface TransferNFTMessage { @@ -215,6 +221,32 @@ export interface BatchTransferMessage { funds: Coin[] } +export interface UpdateTokenMetadataMessage { + sender: string + contract: string + msg: { + update_token_metadata: { + token_id: string + token_uri: string + } + } + funds: Coin[] +} + +export interface BatchUpdateTokenMetadataMessage { + sender: string + contract: string + msg: Record[] + funds: Coin[] +} + +export interface FreezeMetadataMessage { + sender: string + contract: string + msg: { freeze_metadata: Record } + funds: Coin[] +} + export interface UpdateCollectionInfoMessage { sender: string contract: string @@ -570,6 +602,65 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con return res.transactionHash } + const batchUpdateTokenMetadata = async (tokenIds: string, baseURI: string): Promise => { + const executeContractMsgs: MsgExecuteContractEncodeObject[] = [] + if (tokenIds.includes(':')) { + const [start, end] = tokenIds.split(':').map(Number) + for (let i = start; i <= end; i++) { + const msg = { + update_token_metadata: { token_id: i.toString(), token_uri: `${baseURI}/${i}` }, + } + const executeContractMsg: MsgExecuteContractEncodeObject = { + typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract', + value: MsgExecuteContract.fromPartial({ + sender: txSigner, + contract: contractAddress, + msg: toUtf8(JSON.stringify(msg)), + }), + } + + executeContractMsgs.push(executeContractMsg) + } + } else { + const tokenNumbers = tokenIds.split(',').map(Number) + for (let i = 0; i < tokenNumbers.length; i++) { + const msg = { + update_token_metadata: { token_id: tokenNumbers[i].toString(), token_uri: `${baseURI}/${tokenNumbers[i]}` }, + } + const executeContractMsg: MsgExecuteContractEncodeObject = { + typeUrl: '/cosmwasm.wasm.v1.MsgExecuteContract', + value: MsgExecuteContract.fromPartial({ + sender: txSigner, + contract: contractAddress, + msg: toUtf8(JSON.stringify(msg)), + }), + } + + executeContractMsgs.push(executeContractMsg) + } + } + + const res = await client.signAndBroadcast(txSigner, executeContractMsgs, 'auto', 'batch update metadata') + + return res.transactionHash + } + + const updateTokenMetadata = async (tokenId: string, tokenURI: string): Promise => { + const res = await client.execute( + txSigner, + contractAddress, + { + update_token_metadata: { + token_id: tokenId, + token_uri: tokenURI, + }, + }, + 'auto', + '', + ) + return res.transactionHash + } + const freezeCollectionInfo = async (): Promise => { const res = await client.execute( txSigner, @@ -583,6 +674,19 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con return res.transactionHash } + const freezeMetadata = async (): Promise => { + const res = await client.execute( + txSigner, + contractAddress, + { + freeze_metadata: {}, + }, + 'auto', + '', + ) + return res.transactionHash + } + return { contractAddress, ownerOf, @@ -609,6 +713,9 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con batchTransfer, updateCollectionInfo, freezeCollectionInfo, + updateTokenMetadata, + batchUpdateTokenMetadata, + freezeMetadata, } } @@ -804,6 +911,58 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con funds: [], } } + + const batchUpdateTokenMetadata = (tokenIds: string, baseURI: string): BatchUpdateTokenMetadataMessage => { + const msg: Record[] = [] + if (tokenIds.includes(':')) { + const [start, end] = tokenIds.split(':').map(Number) + for (let i = start; i <= end; i++) { + msg.push({ + update_token_metadata: { token_id: i.toString(), token_uri: `${baseURI}/${i}` }, + }) + } + } else { + const tokenNumbers = tokenIds.split(',').map(Number) + for (let i = 0; i < tokenNumbers.length; i++) { + msg.push({ + update_token_metadata: { token_id: tokenNumbers[i].toString(), token_uri: `${baseURI}/${tokenNumbers[i]}` }, + }) + } + } + + return { + sender: txSigner, + contract: contractAddress, + msg, + funds: [], + } + } + + const updateTokenMetadata = (tokenId: string, tokenURI: string) => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + update_token_metadata: { + token_id: tokenId, + token_uri: tokenURI, + }, + }, + funds: [], + } + } + + const freezeMetadata = () => { + return { + sender: txSigner, + contract: contractAddress, + msg: { + freeze_metadata: {}, + }, + funds: [], + } + } + const updateCollectionInfo = (collectionInfo: CollectionInfo) => { return { sender: txSigner, @@ -814,6 +973,7 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con funds: [], } } + const freezeCollectionInfo = () => { return { sender: txSigner, @@ -838,6 +998,9 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con batchTransfer, updateCollectionInfo, freezeCollectionInfo, + updateTokenMetadata, + batchUpdateTokenMetadata, + freezeMetadata, } }