From eb82d10140080eb335c3ea608a0c5c2e306002c7 Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Tue, 11 Apr 2023 13:40:29 +0300 Subject: [PATCH] Update contract helpers for sg721 --- contracts/sg721/contract.ts | 29 +++++++++++++++++++++++++++-- contracts/sg721/useContract.ts | 17 ++++++++++++++++- 2 files changed, 43 insertions(+), 3 deletions(-) diff --git a/contracts/sg721/contract.ts b/contracts/sg721/contract.ts index ba108f1..f15d8c2 100644 --- a/contracts/sg721/contract.ts +++ b/contracts/sg721/contract.ts @@ -1,6 +1,6 @@ import type { MsgExecuteContractEncodeObject, SigningCosmWasmClient } from '@cosmjs/cosmwasm-stargate' import { toBase64, toUtf8 } from '@cosmjs/encoding' -import type { Coin } from '@cosmjs/stargate' +import type { Coin, logs } from '@cosmjs/stargate' import { coin } from '@cosmjs/stargate' import { MsgExecuteContract } from 'cosmjs-types/cosmwasm/wasm/v1/tx' @@ -9,6 +9,11 @@ export interface InstantiateResponse { readonly transactionHash: string } +export interface MigrateResponse { + readonly transactionHash: string + readonly logs: readonly logs.Log[] +} + export type Expiration = { at_height: number } | { at_time: string } | { never: Record } export interface SG721Instance { @@ -206,6 +211,13 @@ export interface SG721Contract { admin?: string, ) => Promise + migrate: ( + senderAddress: string, + contractAddress: string, + codeId: number, + migrateMsg: Record, + ) => Promise + use: (contractAddress: string) => SG721Instance messages: (contractAddress: string) => Sg721Messages @@ -540,6 +552,19 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con } } + const migrate = async ( + senderAddress: string, + contractAddress: string, + codeId: number, + migrateMsg: Record, + ): Promise => { + const result = await client.migrate(senderAddress, contractAddress, codeId, migrateMsg, 'auto') + return { + transactionHash: result.transactionHash, + logs: result.logs, + } + } + const instantiate = async ( senderAddress: string, codeId: number, @@ -734,5 +759,5 @@ export const SG721 = (client: SigningCosmWasmClient, txSigner: string): SG721Con } } - return { use, instantiate, messages } + return { use, instantiate, migrate, messages } } diff --git a/contracts/sg721/useContract.ts b/contracts/sg721/useContract.ts index d5fee82..4eae403 100644 --- a/contracts/sg721/useContract.ts +++ b/contracts/sg721/useContract.ts @@ -2,7 +2,7 @@ import type { Coin } from '@cosmjs/proto-signing' import { useWallet } from 'contexts/wallet' import { useCallback, useEffect, useState } from 'react' -import type { SG721Contract, SG721Instance, Sg721Messages } from './contract' +import type { MigrateResponse, SG721Contract, SG721Instance, Sg721Messages } from './contract' import { SG721 as initContract } from './contract' interface InstantiateResponse { @@ -18,6 +18,7 @@ export interface UseSG721ContractProps { admin?: string, funds?: Coin[], ) => Promise + migrate: (contractAddress: string, codeId: number, migrateMsg: Record) => Promise use: (customAddress: string) => SG721Instance | undefined updateContractAddress: (contractAddress: string) => void messages: (contractAddress: string) => Sg721Messages | undefined @@ -55,6 +56,19 @@ export function useSG721Contract(): UseSG721ContractProps { [SG721, wallet], ) + const migrate = useCallback( + (contractAddress: string, codeId: number, migrateMsg: Record): Promise => { + return new Promise((resolve, reject) => { + if (!SG721) { + reject(new Error('Contract is not initialized.')) + return + } + SG721.migrate(wallet.address, contractAddress, codeId, migrateMsg).then(resolve).catch(reject) + }) + }, + [SG721, wallet], + ) + const use = useCallback( (customAddress = ''): SG721Instance | undefined => { return SG721?.use(address || customAddress) @@ -74,5 +88,6 @@ export function useSG721Contract(): UseSG721ContractProps { use, updateContractAddress, messages, + migrate, } }