From e808173f12adbb29fed9bdbf509f2578fdd97d12 Mon Sep 17 00:00:00 2001 From: peetzweg/ Date: Thu, 14 Sep 2023 12:57:11 +0200 Subject: [PATCH] sync `useCall` and `useDryRun` code and aligning some type names. Resolves #96 --- .../src/react/hooks/contracts/useCall.ts | 42 ++++-------- .../src/react/hooks/contracts/useDryRun.ts | 68 +++++++++---------- .../useink/src/react/hooks/contracts/useTx.ts | 18 ++--- 3 files changed, 56 insertions(+), 72 deletions(-) diff --git a/packages/useink/src/react/hooks/contracts/useCall.ts b/packages/useink/src/react/hooks/contracts/useCall.ts index b5ef110..534b7a8 100644 --- a/packages/useink/src/react/hooks/contracts/useCall.ts +++ b/packages/useink/src/react/hooks/contracts/useCall.ts @@ -1,3 +1,4 @@ +import { useCallback, useState } from 'react'; import { DecodedContractResult, LazyCallOptions, @@ -6,43 +7,29 @@ import { import { ChainContract, useDefaultCaller } from '../index'; import { useWallet } from '../wallets/useWallet.ts'; import { useAbiMessage } from './useAbiMessage.ts'; -import { useCallback, useState } from 'react'; -export type CallSend = ( - args?: unknown[], +export type CallSend = ( + args?: Array, options?: LazyCallOptions, -) => Promise | undefined>; +) => Promise; export interface UseCall { - send: CallSend; isSubmitting: boolean; -} - -export enum CallError { - ContractUndefined = 'Contract is undefined', - InvalidAbiMessage = 'Invalid ABI Message', - NoResponse = 'No response', -} - -export interface Call extends UseCall { result?: DecodedContractResult; + send: CallSend; } - export function useCall( chainContract: ChainContract | undefined, message: string, -): Call { - const [result, setResult] = useState>(); +): UseCall { const [isSubmitting, setIsSubmitting] = useState(false); - const abiMessage = useAbiMessage(chainContract?.contract, message); + const [result, setResult] = useState>(); const { account } = useWallet(); + const abiMessage = useAbiMessage(chainContract?.contract, message); const defaultCaller = useDefaultCaller(chainContract?.chainId); - const send = useCallback( - async ( - args: Parameters[3], - options?: LazyCallOptions, - ): Promise | undefined> => { + const send: CallSend = useCallback( + async (args: Parameters[3], options?: LazyCallOptions) => { const caller = account?.address ? account.address : options?.defaultCaller @@ -60,13 +47,10 @@ export function useCall( options, ); setResult(callResult); + } catch (error: unknown) { + console.error(error); + } finally { setIsSubmitting(false); - - return callResult; - } catch (e: unknown) { - console.error(e); - setIsSubmitting(false); - return; } }, [account, abiMessage], diff --git a/packages/useink/src/react/hooks/contracts/useDryRun.ts b/packages/useink/src/react/hooks/contracts/useDryRun.ts index ee4634b..554ed18 100644 --- a/packages/useink/src/react/hooks/contracts/useDryRun.ts +++ b/packages/useink/src/react/hooks/contracts/useDryRun.ts @@ -1,3 +1,4 @@ +import { useCallback, useState } from 'react'; import { DecodedTxResult, LazyCallOptions, @@ -8,35 +9,34 @@ import { useDefaultCaller } from '../config/index'; import { useWallet } from '../wallets/useWallet.ts'; import { ChainContract } from './types.ts'; import { useAbiMessage } from './useAbiMessage.ts'; -import { useMemo, useState } from 'react'; -export type DryRunResult = DecodedTxResult; +type DryRunResult = DecodedTxResult | undefined; -export type Send = ( - args?: unknown[], - o?: LazyCallOptions, -) => Promise | undefined>; +export type DryRunSend = ( + args?: Array, + options?: LazyCallOptions, +) => Promise>; -export interface DryRun { - send: Send; +export interface UseDryRun { isSubmitting: boolean; - result?: DryRunResult; - resolved: Boolean; resetState: () => void; + resolved: boolean; + result?: DecodedTxResult; + send: DryRunSend; } export function useDryRun( chainContract: ChainContract | undefined, message: string, -): DryRun { - const { account } = useWallet(); - const defaultCaller = useDefaultCaller(chainContract?.chainId); - const [result, setResult] = useState>(); +): UseDryRun { const [isSubmitting, setIsSubmitting] = useState(false); + const [result, setResult] = useState>(); + const { account } = useWallet(); const abiMessage = useAbiMessage(chainContract?.contract, message); + const defaultCaller = useDefaultCaller(chainContract?.chainId); - const send: Send = useMemo( - () => async (params, options) => { + const send: DryRunSend = useCallback( + async (args, options) => { const tx = chainContract?.contract?.tx?.[message]; const caller = account?.address ? account.address @@ -48,46 +48,46 @@ export function useDryRun( return; } - setIsSubmitting(true); - try { - const resp = await call( + setIsSubmitting(true); + const callResult = await call( chainContract.contract, abiMessage, caller, - params, + args, options, ); - if (!resp || !resp.ok) return; + if (!callResult || !callResult.ok) { + setResult(callResult); + return callResult; + } - const { gasConsumed, gasRequired, storageDeposit } = resp.value.raw; + const { gasConsumed, gasRequired, storageDeposit } = + callResult.value.raw; const requiresNoArguments = tx.meta.args.length === 0; const { partialFee } = await (requiresNoArguments ? tx(toContractOptions(options)) - : tx(toContractOptions(options), ...(params || [])) + : tx(toContractOptions(options), ...(args || [])) ).paymentInfo(caller); - const r = { - ...resp, + const extendedCallResult = { + ...callResult, value: { - ...resp.value, + ...callResult.value, gasRequired, gasConsumed, storageDeposit, partialFee, }, }; - + setResult(extendedCallResult); + return extendedCallResult; + } catch (error: unknown) { + console.error(error); + } finally { setIsSubmitting(false); - setResult(r); - - return r; - } catch (e: unknown) { - console.error(e); - setIsSubmitting(false); - return; } }, [account, chainContract?.contract, abiMessage], diff --git a/packages/useink/src/react/hooks/contracts/useTx.ts b/packages/useink/src/react/hooks/contracts/useTx.ts index 235b3ea..53f350c 100644 --- a/packages/useink/src/react/hooks/contracts/useTx.ts +++ b/packages/useink/src/react/hooks/contracts/useTx.ts @@ -10,7 +10,7 @@ import { useWallet } from '../wallets/useWallet.ts'; import { ChainContract } from './types.ts'; import { useDryRun } from './useDryRun.ts'; import { useTxEvents } from './useTxEvents.ts'; -import { useMemo, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; export type ContractSubmittableResultCallback = ( result?: ContractSubmittableResult, @@ -19,8 +19,8 @@ export type ContractSubmittableResultCallback = ( ) => void; export type SignAndSend = ( - args?: unknown[], - o?: LazyContractOptions, + args?: Array, + options?: LazyContractOptions, cb?: ContractSubmittableResultCallback, ) => void; @@ -36,20 +36,20 @@ export function useTx( chainContract: ChainContract | undefined, message: string, ): Tx { - const { account } = useWallet(); - const [status, setStatus] = useState('None'); const [result, setResult] = useState(); + const [status, setStatus] = useState('None'); + const { account } = useWallet(); const dryRun = useDryRun(chainContract, message); const txEvents = useTxEvents({ status, result }); - const signAndSend: SignAndSend = useMemo( - () => (params, options, cb) => { + const signAndSend: SignAndSend = useCallback( + async (args, options, cb) => { if (!chainContract?.contract || !account || !account.wallet?.extension) { return; } dryRun - .send(params, options) + .send(args, options) .then((response) => { if (!response || !response.ok) return; setStatus('PendingSignature'); @@ -68,7 +68,7 @@ export function useTx( tx( { gasLimit: gasRequired, ...toContractOptions(options) }, - ...(params || []), + ...(args || []), ) .signAndSend( account.address,