stargaze-studio/contracts/minter/useContract.ts
Serkan Reis aae2b14a01
Add burn & batch burn for sg721 (#39)
* Add burn & batch burn for sg721

* Add batch burn range

* Refactor contract messages logic

Co-authored-by: findolor <anakisci@gmail.com>
2022-08-16 10:18:35 +03:00

99 lines
2.9 KiB
TypeScript

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 { MinterContract, MinterInstance, MinterMessages } from './contract'
import { minter 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 UseMinterContractProps {
instantiate: (
codeId: number,
initMsg: Record<string, unknown>,
label: string,
admin?: string,
funds?: Coin[],
) => Promise<InstantiateResponse>
use: (customAddress: string) => MinterInstance | undefined
updateContractAddress: (contractAddress: string) => void
getContractAddress: () => string | undefined
messages: (contractAddress: string) => MinterMessages | undefined
}
export function useMinterContract(): UseMinterContractProps {
const wallet = useWallet()
const [address, setAddress] = useState<string>('')
const [minter, setMinter] = useState<MinterContract>()
useEffect(() => {
setAddress(localStorage.getItem('contract_address') || '')
}, [])
useEffect(() => {
const MinterBaseContract = initContract(wallet.getClient(), wallet.address)
setMinter(MinterBaseContract)
}, [wallet])
const updateContractAddress = (contractAddress: string) => {
setAddress(contractAddress)
}
const instantiate = useCallback(
(codeId: number, initMsg: Record<string, unknown>, label: string, admin?: string): Promise<InstantiateResponse> => {
return new Promise((resolve, reject) => {
if (!minter) {
reject(new Error('Contract is not initialized.'))
return
}
minter.instantiate(wallet.address, codeId, initMsg, label, admin).then(resolve).catch(reject)
})
},
[minter, wallet],
)
const use = useCallback(
(customAddress = ''): MinterInstance | undefined => {
return minter?.use(address || customAddress)
},
[minter, address],
)
const getContractAddress = (): string | undefined => {
return address
}
const messages = useCallback(
(customAddress = ''): MinterMessages | undefined => {
return minter?.messages(address || customAddress)
},
[minter, address],
)
return {
instantiate,
use,
updateContractAddress,
getContractAddress,
messages,
}
}