2022-07-19 07:53:03 +00:00
|
|
|
import type { Coin } from '@cosmjs/proto-signing'
|
|
|
|
import type { logs } from '@cosmjs/stargate'
|
2022-07-14 10:16:50 +00:00
|
|
|
import { useCallback, useEffect, useState } from 'react'
|
2023-10-11 23:36:14 +00:00
|
|
|
import { useWallet } from 'utils/wallet'
|
2022-07-14 10:16:50 +00:00
|
|
|
|
2022-12-09 08:27:50 +00:00
|
|
|
import type { MigrateResponse, VendingMinterContract, VendingMinterInstance, VendingMinterMessages } from './contract'
|
|
|
|
import { vendingMinter as initContract } from './contract'
|
2022-07-14 10:16:50 +00:00
|
|
|
|
|
|
|
/*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[]
|
|
|
|
}
|
|
|
|
|
2022-12-09 08:27:50 +00:00
|
|
|
export interface UseVendingMinterContractProps {
|
2022-07-14 10:16:50 +00:00
|
|
|
instantiate: (
|
|
|
|
codeId: number,
|
|
|
|
initMsg: Record<string, unknown>,
|
|
|
|
label: string,
|
|
|
|
admin?: string,
|
2022-07-19 07:53:03 +00:00
|
|
|
funds?: Coin[],
|
2022-07-14 10:16:50 +00:00
|
|
|
) => Promise<InstantiateResponse>
|
2022-11-30 18:35:01 +00:00
|
|
|
migrate: (contractAddress: string, codeId: number, migrateMsg: Record<string, unknown>) => Promise<MigrateResponse>
|
2022-12-09 08:27:50 +00:00
|
|
|
use: (customAddress: string) => VendingMinterInstance | undefined
|
2022-07-14 10:16:50 +00:00
|
|
|
updateContractAddress: (contractAddress: string) => void
|
|
|
|
getContractAddress: () => string | undefined
|
2022-12-09 08:27:50 +00:00
|
|
|
messages: (contractAddress: string) => VendingMinterMessages | undefined
|
2022-07-14 10:16:50 +00:00
|
|
|
}
|
|
|
|
|
2022-12-09 08:27:50 +00:00
|
|
|
export function useVendingMinterContract(): UseVendingMinterContractProps {
|
2022-07-14 10:16:50 +00:00
|
|
|
const wallet = useWallet()
|
|
|
|
|
|
|
|
const [address, setAddress] = useState<string>('')
|
2022-12-09 08:27:50 +00:00
|
|
|
const [vendingMinter, setVendingMinter] = useState<VendingMinterContract>()
|
2022-07-14 10:16:50 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setAddress(localStorage.getItem('contract_address') || '')
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
useEffect(() => {
|
2023-10-11 23:36:14 +00:00
|
|
|
if (!wallet.isWalletConnected) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const load = async () => {
|
|
|
|
const client = await wallet.getSigningCosmWasmClient()
|
|
|
|
const contract = initContract(client, wallet.address || '')
|
|
|
|
setVendingMinter(contract)
|
|
|
|
}
|
|
|
|
|
|
|
|
load().catch(console.error)
|
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [wallet.isWalletConnected, wallet.address])
|
2022-07-14 10:16:50 +00:00
|
|
|
|
|
|
|
const updateContractAddress = (contractAddress: string) => {
|
|
|
|
setAddress(contractAddress)
|
|
|
|
}
|
|
|
|
|
|
|
|
const instantiate = useCallback(
|
2022-07-19 07:53:03 +00:00
|
|
|
(codeId: number, initMsg: Record<string, unknown>, label: string, admin?: string): Promise<InstantiateResponse> => {
|
2022-07-14 10:16:50 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-12-09 08:27:50 +00:00
|
|
|
if (!vendingMinter) {
|
2022-07-19 07:53:03 +00:00
|
|
|
reject(new Error('Contract is not initialized.'))
|
|
|
|
return
|
|
|
|
}
|
2023-10-11 23:36:14 +00:00
|
|
|
vendingMinter
|
|
|
|
.instantiate(wallet.address || '', codeId, initMsg, label, admin)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject)
|
2022-07-14 10:16:50 +00:00
|
|
|
})
|
|
|
|
},
|
2022-12-09 08:27:50 +00:00
|
|
|
[vendingMinter, wallet],
|
2022-07-14 10:16:50 +00:00
|
|
|
)
|
|
|
|
|
2022-11-30 18:35:01 +00:00
|
|
|
const migrate = useCallback(
|
|
|
|
(contractAddress: string, codeId: number, migrateMsg: Record<string, unknown>): Promise<MigrateResponse> => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2022-12-09 08:27:50 +00:00
|
|
|
if (!vendingMinter) {
|
2022-11-30 18:35:01 +00:00
|
|
|
reject(new Error('Contract is not initialized.'))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
console.log(wallet.address, contractAddress, codeId)
|
2023-10-11 23:36:14 +00:00
|
|
|
vendingMinter
|
|
|
|
.migrate(wallet.address || '', contractAddress, codeId, migrateMsg)
|
|
|
|
.then(resolve)
|
|
|
|
.catch(reject)
|
2022-11-30 18:35:01 +00:00
|
|
|
})
|
|
|
|
},
|
2022-12-09 08:27:50 +00:00
|
|
|
[vendingMinter, wallet],
|
2022-11-30 18:35:01 +00:00
|
|
|
)
|
|
|
|
|
2022-07-14 10:16:50 +00:00
|
|
|
const use = useCallback(
|
2022-12-09 08:27:50 +00:00
|
|
|
(customAddress = ''): VendingMinterInstance | undefined => {
|
|
|
|
return vendingMinter?.use(address || customAddress)
|
2022-07-14 10:16:50 +00:00
|
|
|
},
|
2022-12-09 08:27:50 +00:00
|
|
|
[vendingMinter, address],
|
2022-07-14 10:16:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const getContractAddress = (): string | undefined => {
|
|
|
|
return address
|
|
|
|
}
|
|
|
|
|
2022-08-16 07:18:35 +00:00
|
|
|
const messages = useCallback(
|
2022-12-09 08:27:50 +00:00
|
|
|
(customAddress = ''): VendingMinterMessages | undefined => {
|
|
|
|
return vendingMinter?.messages(address || customAddress)
|
2022-08-16 07:18:35 +00:00
|
|
|
},
|
2022-12-09 08:27:50 +00:00
|
|
|
[vendingMinter, address],
|
2022-08-16 07:18:35 +00:00
|
|
|
)
|
2022-07-19 07:53:03 +00:00
|
|
|
|
2022-07-14 10:16:50 +00:00
|
|
|
return {
|
|
|
|
instantiate,
|
|
|
|
use,
|
|
|
|
updateContractAddress,
|
|
|
|
getContractAddress,
|
2022-07-19 07:53:03 +00:00
|
|
|
messages,
|
2022-11-30 18:35:01 +00:00
|
|
|
migrate,
|
2022-07-14 10:16:50 +00:00
|
|
|
}
|
|
|
|
}
|