-
Notifications
You must be signed in to change notification settings - Fork 106
[react]: add useSendTransaction hook #229
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
Open
jkrishnad
wants to merge
15
commits into
gillsdk:master
Choose a base branch
from
jkrishnad:useSendTransaction
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6d07f3b
feat: implement useSendTransaction hook
jkrishnad 49dc25e
updated README
jkrishnad 4fa027d
chore: changeset
jkrishnad 386a9d6
updated comments
jkrishnad 0c53ae0
Revert "chore: changeset"
jkrishnad 49a6d7f
added changeset
jkrishnad 4eb56f8
Merge branch 'gillsdk:master' into useSendTransaction
jkrishnad 15cc77e
Revert changeset
jkrishnad d1d46a3
Merge branch 'useSendTransaction' of https://github.com/JkrishnaD/gil…
jkrishnad 349e01e
updated the parameters signature to transaction
jkrishnad 86809a8
added retry logic
jkrishnad dabf818
updated README
jkrishnad e22e4c1
Merge branch 'master' into useSendTransaction
jkrishnad 9700cc4
refactor: refine useSendTransaction types and improve documentation
jkrishnad d9dce41
chore: corrected the return type and readme fix
jkrishnad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@gillsdk/react": minor | ||
| --- | ||
|
|
||
| added `useSendTransaction` hook |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { useSendTransaction } from "../hooks"; | ||
| import { Base64EncodedWireTransaction, SendTransactionApi} from "gill"; | ||
|
|
||
| // [DESCRIBE] useSendTransaction | ||
| { | ||
| { | ||
| const { sendTransaction } = useSendTransaction({ | ||
| config: { | ||
| encoding: "base64", | ||
| preflightCommitment: "confirmed", | ||
| skipPreflight: false, | ||
| }, | ||
| }); | ||
| sendTransaction satisfies (tx: Base64EncodedWireTransaction | string) => Promise<ReturnType<SendTransactionApi["sendTransaction"]>>; | ||
| // @ts-expect-error - Should not allow no argument | ||
| useSendTransaction(); | ||
| } | ||
|
|
||
| // Should accept `config` input | ||
| { | ||
| const { sendTransaction } = useSendTransaction({ | ||
| config: { | ||
| encoding: "base64", | ||
| preflightCommitment: "confirmed", | ||
| skipPreflight: false, | ||
| }, | ||
| }); | ||
| sendTransaction satisfies (tx: Base64EncodedWireTransaction | string) => Promise<ReturnType<SendTransactionApi["sendTransaction"]>>; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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 | ||
| */ | ||
|
jkrishnad marked this conversation as resolved.
|
||
| 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, { | ||
|
jkrishnad marked this conversation as resolved.
|
||
| encoding: "base64", | ||
| preflightCommitment: "confirmed", | ||
| skipPreflight: false, | ||
| ...config, | ||
| }) | ||
| .send({ abortSignal }); | ||
| return response; | ||
| }, | ||
| mutationKey: [GILL_HOOK_CLIENT_KEY, "sendTransaction"], | ||
|
jkrishnad marked this conversation as resolved.
|
||
| networkMode: "offlineFirst", | ||
| retry: false, | ||
| ...options, | ||
| }); | ||
|
|
||
| return { | ||
| ...mutation, | ||
| send: mutation.mutate, | ||
| sendTransaction: mutation.mutateAsync, | ||
| }; | ||
|
jkrishnad marked this conversation as resolved.
|
||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.