import type { Coin } from '@cosmjs/proto-signing' import type { logs } from '@cosmjs/stargate' import { useWallet } from 'contexts/wallet' import { useCallback, useEffect, useState } from 'react' import type { MigrateResponse, VendingMinterContract, VendingMinterInstance, VendingMinterMessages } from './contract' import { vendingMinter as initContract } from './contract' /*export interface InstantiateResponse { /** The address of the newly instantiated contract *-/ readonly contractAddress: string readonly logs: readonly logs.Log[] /** Block height in which the transaction is included *-/ readonly height: number /** Transaction hash (might be used as transaction ID). Guaranteed to be non-empty upper-case hex *-/ readonly transactionHash: string readonly gasWanted: number readonly gasUsed: number }*/ interface InstantiateResponse { readonly contractAddress: string readonly transactionHash: string readonly logs: readonly logs.Log[] } export interface UseVendingMinterContractProps { instantiate: ( codeId: number, initMsg: Record, label: string, admin?: string, funds?: Coin[], ) => Promise migrate: (contractAddress: string, codeId: number, migrateMsg: Record) => Promise use: (customAddress: string) => VendingMinterInstance | undefined updateContractAddress: (contractAddress: string) => void getContractAddress: () => string | undefined messages: (contractAddress: string) => VendingMinterMessages | undefined } export function useVendingMinterContract(): UseVendingMinterContractProps { const wallet = useWallet() const [address, setAddress] = useState('') const [vendingMinter, setVendingMinter] = useState() useEffect(() => { setAddress(localStorage.getItem('contract_address') || '') }, []) useEffect(() => { const VendingMinterBaseContract = initContract(wallet.getClient(), wallet.address) setVendingMinter(VendingMinterBaseContract) }, [wallet]) const updateContractAddress = (contractAddress: string) => { setAddress(contractAddress) } const instantiate = useCallback( (codeId: number, initMsg: Record, label: string, admin?: string): Promise => { return new Promise((resolve, reject) => { if (!vendingMinter) { reject(new Error('Contract is not initialized.')) return } vendingMinter.instantiate(wallet.address, codeId, initMsg, label, admin).then(resolve).catch(reject) }) }, [vendingMinter, wallet], ) const migrate = useCallback( (contractAddress: string, codeId: number, migrateMsg: Record): Promise => { return new Promise((resolve, reject) => { if (!vendingMinter) { reject(new Error('Contract is not initialized.')) return } console.log(wallet.address, contractAddress, codeId) vendingMinter.migrate(wallet.address, contractAddress, codeId, migrateMsg).then(resolve).catch(reject) }) }, [vendingMinter, wallet], ) const use = useCallback( (customAddress = ''): VendingMinterInstance | undefined => { return vendingMinter?.use(address || customAddress) }, [vendingMinter, address], ) const getContractAddress = (): string | undefined => { return address } const messages = useCallback( (customAddress = ''): VendingMinterMessages | undefined => { return vendingMinter?.messages(address || customAddress) }, [vendingMinter, address], ) return { instantiate, use, updateContractAddress, getContractAddress, messages, migrate, } }