From 9fcd5b82f43c91fa85cf889e9e2fe75696703fab Mon Sep 17 00:00:00 2001 From: Serkan Reis Date: Sat, 7 Oct 2023 09:05:54 +0300 Subject: [PATCH] Init Royalty Registry contract hooks --- contracts/royaltyRegistry/index.ts | 2 + contracts/royaltyRegistry/useContract.ts | 99 ++++++++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 contracts/royaltyRegistry/index.ts create mode 100644 contracts/royaltyRegistry/useContract.ts diff --git a/contracts/royaltyRegistry/index.ts b/contracts/royaltyRegistry/index.ts new file mode 100644 index 0000000..6dc6461 --- /dev/null +++ b/contracts/royaltyRegistry/index.ts @@ -0,0 +1,2 @@ +export * from './contract' +export * from './useContract' diff --git a/contracts/royaltyRegistry/useContract.ts b/contracts/royaltyRegistry/useContract.ts new file mode 100644 index 0000000..7d73394 --- /dev/null +++ b/contracts/royaltyRegistry/useContract.ts @@ -0,0 +1,99 @@ +/* eslint-disable eslint-comments/disable-enable-pair */ + +import { useWallet } from 'contexts/wallet' +import { useCallback, useEffect, useState } from 'react' + +import type { + InstantiateResponse, + MigrateResponse, + RoyaltyRegistryContract, + RoyaltyRegistryInstance, + RoyaltyRegistryMessages, +} from './contract' +import { RoyaltyRegistry as initContract } from './contract' + +export interface UseRoyaltyRegistryContractProps { + instantiate: ( + codeId: number, + initMsg: Record, + label: string, + admin?: string, + ) => Promise + + migrate: (contractAddress: string, codeId: number, migrateMsg: Record) => Promise + + use: (customAddress?: string) => RoyaltyRegistryInstance | undefined + + updateContractAddress: (contractAddress: string) => void + + messages: (contractAddress: string) => RoyaltyRegistryMessages | undefined +} + +export function useRoyaltyRegistryContract(): UseRoyaltyRegistryContractProps { + const wallet = useWallet() + + const [address, setAddress] = useState('') + const [royaltyRegistry, setRoyaltyRegistry] = useState() + + useEffect(() => { + setAddress(localStorage.getItem('contract_address') || '') + }, []) + + useEffect(() => { + const royaltyRegistryContract = initContract(wallet.getClient(), wallet.address) + setRoyaltyRegistry(royaltyRegistryContract) + }, [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 (!royaltyRegistry) { + reject(new Error('Contract is not initialized.')) + return + } + royaltyRegistry.instantiate(codeId, initMsg, label, admin).then(resolve).catch(reject) + }) + }, + [royaltyRegistry], + ) + + const migrate = useCallback( + (contractAddress: string, codeId: number, migrateMsg: Record): Promise => { + return new Promise((resolve, reject) => { + if (!royaltyRegistry) { + reject(new Error('Contract is not initialized.')) + return + } + console.log(wallet.address, contractAddress, codeId) + royaltyRegistry.migrate(wallet.address, contractAddress, codeId, migrateMsg).then(resolve).catch(reject) + }) + }, + [royaltyRegistry, wallet], + ) + + const use = useCallback( + (customAddress = ''): RoyaltyRegistryInstance | undefined => { + return royaltyRegistry?.use(address || customAddress) + }, + [royaltyRegistry, address], + ) + + const messages = useCallback( + (customAddress = ''): RoyaltyRegistryMessages | undefined => { + return royaltyRegistry?.messages(address || customAddress) + }, + [royaltyRegistry, address], + ) + + return { + instantiate, + migrate, + use, + updateContractAddress, + messages, + } +}