-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathsend-transaction.ts
More file actions
73 lines (65 loc) · 2.33 KB
/
Copy pathsend-transaction.ts
File metadata and controls
73 lines (65 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"use client";
import { useSolanaClient } from "./client.js";
import { GILL_HOOK_CLIENT_KEY } from "../const.js";
import { useMutation, UseMutationOptions } from "@tanstack/react-query";
import { Base64EncodedWireTransaction, SendTransactionApi, Simplify } from "gill";
type RpcConfig = Simplify<Parameters<SendTransactionApi["sendTransaction"]>[1]>;
type UseSendTransactionResponse = ReturnType<SendTransactionApi["sendTransaction"]>;
type UseSendTransactionInput<TConfig extends RpcConfig = RpcConfig> = {
/**
* Signal used to abort the RPC operation
*
* See MDN docs for {@link https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal | AbortSignal}
*/
abortSignal?: AbortSignal;
/**
* RPC configuration passed to the RPC method being called
*/
config?: TConfig;
/**
* Options passed to the {@link useMutation} hook
*/
options?: UseMutationOptions<UseSendTransactionResponse, unknown, SendTransactionArgument, unknown>;
};
type SendTransactionArgument = Base64EncodedWireTransaction | string;
/**
* Send a transaction using the Solana RPC method of
* [`sendTransaction`](https://solana.com/docs/rpc/http/sendtransaction)
*
* @param config Optional RPC configuration (e.g. `skipPreflight`, `encoding`, `preflightCommitment`s).
* @returns
* - `sendTransaction`: async function to send a transaction
* - all standard `useMutation` fields (`isLoading`, `error`, etc.)
*
* The returned signature can be viewed in Solana Explorer.
*/
export function useSendTransaction<TConfig extends RpcConfig = RpcConfig>({
options,
config,
abortSignal,
}: UseSendTransactionInput<TConfig>) {
const { rpc } = useSolanaClient();
const mutation = useMutation({
mutationFn: async (tx: SendTransactionArgument) => {
// The RPC method expects the base-64 encoded transaction as the first argument.
const response = await rpc
.sendTransaction(tx as Base64EncodedWireTransaction, {
encoding: "base64",
preflightCommitment: "confirmed",
skipPreflight: false,
...config,
})
.send({ abortSignal });
return response;
},
mutationKey: [GILL_HOOK_CLIENT_KEY, "sendTransaction"],
networkMode: "offlineFirst",
retry: false,
...options,
});
return {
...mutation,
send: mutation.mutate,
sendTransaction: mutation.mutateAsync,
};
}