Skip to content
This repository has been archived by the owner on Feb 9, 2024. It is now read-only.

Commit

Permalink
sync useCall and useDryRun code and aligning some type names. Res…
Browse files Browse the repository at this point in the history
…olves #96
  • Loading branch information
peetzweg committed Sep 14, 2023
1 parent efdc3b2 commit e808173
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 72 deletions.
42 changes: 13 additions & 29 deletions packages/useink/src/react/hooks/contracts/useCall.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback, useState } from 'react';
import {
DecodedContractResult,
LazyCallOptions,
Expand All @@ -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<T> = (
args?: unknown[],
export type CallSend = (
args?: Array<unknown>,
options?: LazyCallOptions,
) => Promise<DecodedContractResult<T> | undefined>;
) => Promise<void>;

export interface UseCall<T> {
send: CallSend<T>;
isSubmitting: boolean;
}

export enum CallError {
ContractUndefined = 'Contract is undefined',
InvalidAbiMessage = 'Invalid ABI Message',
NoResponse = 'No response',
}

export interface Call<T> extends UseCall<T> {
result?: DecodedContractResult<T>;
send: CallSend;
}

export function useCall<T>(
chainContract: ChainContract | undefined,
message: string,
): Call<T> {
const [result, setResult] = useState<DecodedContractResult<T>>();
): UseCall<T> {
const [isSubmitting, setIsSubmitting] = useState(false);
const abiMessage = useAbiMessage(chainContract?.contract, message);
const [result, setResult] = useState<DecodedContractResult<T>>();
const { account } = useWallet();
const abiMessage = useAbiMessage(chainContract?.contract, message);
const defaultCaller = useDefaultCaller(chainContract?.chainId);

const send = useCallback(
async (
args: Parameters<typeof call>[3],
options?: LazyCallOptions,
): Promise<DecodedContractResult<T> | undefined> => {
const send: CallSend = useCallback(
async (args: Parameters<typeof call>[3], options?: LazyCallOptions) => {
const caller = account?.address
? account.address
: options?.defaultCaller
Expand All @@ -60,13 +47,10 @@ export function useCall<T>(
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],
Expand Down
68 changes: 34 additions & 34 deletions packages/useink/src/react/hooks/contracts/useDryRun.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useCallback, useState } from 'react';
import {
DecodedTxResult,
LazyCallOptions,
Expand All @@ -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<T> = DecodedTxResult<T>;
type DryRunResult<T> = DecodedTxResult<T> | undefined;

export type Send<T> = (
args?: unknown[],
o?: LazyCallOptions,
) => Promise<DryRunResult<T> | undefined>;
export type DryRunSend<T> = (
args?: Array<unknown>,
options?: LazyCallOptions,
) => Promise<DryRunResult<T>>;

export interface DryRun<T> {
send: Send<T>;
export interface UseDryRun<T> {
isSubmitting: boolean;
result?: DryRunResult<T>;
resolved: Boolean;
resetState: () => void;
resolved: boolean;
result?: DecodedTxResult<T>;
send: DryRunSend<T>;
}

export function useDryRun<T>(
chainContract: ChainContract | undefined,
message: string,
): DryRun<T> {
const { account } = useWallet();
const defaultCaller = useDefaultCaller(chainContract?.chainId);
const [result, setResult] = useState<DecodedTxResult<T>>();
): UseDryRun<T> {
const [isSubmitting, setIsSubmitting] = useState(false);
const [result, setResult] = useState<DecodedTxResult<T>>();
const { account } = useWallet();
const abiMessage = useAbiMessage(chainContract?.contract, message);
const defaultCaller = useDefaultCaller(chainContract?.chainId);

const send: Send<T> = useMemo(
() => async (params, options) => {
const send: DryRunSend<T> = useCallback(
async (args, options) => {
const tx = chainContract?.contract?.tx?.[message];
const caller = account?.address
? account.address
Expand All @@ -48,46 +48,46 @@ export function useDryRun<T>(
return;
}

setIsSubmitting(true);

try {
const resp = await call<T>(
setIsSubmitting(true);
const callResult = await call<T>(
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],
Expand Down
18 changes: 9 additions & 9 deletions packages/useink/src/react/hooks/contracts/useTx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -19,8 +19,8 @@ export type ContractSubmittableResultCallback = (
) => void;

export type SignAndSend = (
args?: unknown[],
o?: LazyContractOptions,
args?: Array<unknown>,
options?: LazyContractOptions,
cb?: ContractSubmittableResultCallback,
) => void;

Expand All @@ -36,20 +36,20 @@ export function useTx<T>(
chainContract: ChainContract | undefined,
message: string,
): Tx<T> {
const { account } = useWallet();
const [status, setStatus] = useState<TransactionStatus>('None');
const [result, setResult] = useState<ContractSubmittableResult>();
const [status, setStatus] = useState<TransactionStatus>('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');
Expand All @@ -68,7 +68,7 @@ export function useTx<T>(

tx(
{ gasLimit: gasRequired, ...toContractOptions(options) },
...(params || []),
...(args || []),
)
.signAndSend(
account.address,
Expand Down

0 comments on commit e808173

Please sign in to comment.