Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: async tx builder #238

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shy-rules-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/react": minor
---

Added ability to builder transaction asynchronously.
24 changes: 15 additions & 9 deletions packages/react/src/hooks/use-mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import type {
} from "polkadot-api";
import { useCallback, useContext } from "react";

type MaybePromise<T> = T | Promise<T>;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type TxOptions<T extends Transaction<any, any, any, any>> = Parameters<
TxObservable<
Expand All @@ -32,18 +34,18 @@ type TxOptions<T extends Transaction<any, any, any, any>> = Parameters<
/**
* Hook for sending transactions to chains.
*
* @param action - The function to create the transaction
* @param builder - The function to create the transaction
* @param options - Additional options
* @returns The current transaction state & submit function
*/
export function useMutation<
TAction extends (
builder: TypedApi<Chains[TChainId]>["tx"],
TBuilder extends (
tx: TypedApi<Chains[TChainId]>["tx"],
) => // eslint-disable-next-line @typescript-eslint/no-explicit-any
Transaction<any, any, any, any>,
MaybePromise<Transaction<any, any, any, any>>,
TChainId extends ChainId,
>(
action: TAction,
builder: TBuilder,
options?: ChainHookOptions<TChainId> & {
/**
* Override default signer
Expand All @@ -52,7 +54,7 @@ export function useMutation<
/**
* Additional transaction options
*/
txOptions?: TxOptions<ReturnType<TAction>>;
txOptions?: TxOptions<Awaited<ReturnType<TBuilder>>>;
},
) {
const config = useConfig();
Expand All @@ -72,7 +74,11 @@ export function useMutation<

const submit = useAtomCallback<
void,
[options?: TxOptions<ReturnType<TAction>> & { signer: PolkadotSigner }]
[
options?: TxOptions<Awaited<ReturnType<TBuilder>>> & {
signer: PolkadotSigner;
},
]
>(
useCallback(
async (get, _set, submitOptions) => {
Expand All @@ -96,7 +102,7 @@ export function useMutation<

const api = await get(typedApiAtomFamily({ config, chainId }));

const transaction = action(api.tx);
const transaction = await builder(api.tx);

setState({ id, call: transaction.decodedCall, value: pending });

Expand All @@ -119,7 +125,7 @@ export function useMutation<
);
},
[
action,
builder,
chainId,
config,
contextSigner,
Expand Down