-
Notifications
You must be signed in to change notification settings - Fork 86
[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
13
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 9 commits
Commits
Show all changes
13 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 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,38 @@ | ||
import { useSendTransaction } from "../hooks/send-transaction"; | ||
import { Base64EncodedWireTransaction, SendTransactionApi} from "gill"; | ||
|
||
// [DESCRIBE] useSendTransaction | ||
{ | ||
const signature = null as unknown as Base64EncodedWireTransaction | string; | ||
{ | ||
const transaction = useSendTransaction({ signature }); | ||
transaction.signature satisfies ReturnType<SendTransactionApi["sendTransaction"]>; | ||
// @ts-expect-error - Should not allow no argument | ||
useSendTransaction(); | ||
|
||
// @ts-expect-error - Should not allow empty argument object | ||
useSendTransaction({}); | ||
} | ||
|
||
{ | ||
// Should accept `config` input | ||
const transaction = useSendTransaction({ | ||
config: { | ||
encoding: "base64", | ||
preflightCommitment: "confirmed", | ||
skipPreflight: false, | ||
}, | ||
signature, | ||
}); | ||
transaction.signature satisfies ReturnType<SendTransactionApi["sendTransaction"]>; | ||
} | ||
|
||
{ | ||
// Should require `signature` input | ||
const transaction = useSendTransaction({ | ||
signature: "5Pj5fCupXLUePYn18JkY8SrRaWFiUctuDTRwvUy2ML9yvkENLb1QMYbcBGcBXRrSVDjp7RjUwk9a3rLC6gpvtYpZ", | ||
}); | ||
|
||
transaction.signature satisfies 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,57 @@ | ||
"use client"; | ||
|
||
import { GillUseRpcHook } from "./types"; | ||
import { useSolanaClient } from "./client"; | ||
import { GILL_HOOK_CLIENT_KEY } from "../const"; | ||
import { useMutation } from "@tanstack/react-query"; | ||
import { Base64EncodedWireTransaction, SendTransactionApi, Simplify } from "gill"; | ||
|
||
// Define the configuration type for the RPC method | ||
type RpConfig = Simplify<Parameters<SendTransactionApi["sendTransaction"]>[1]>; | ||
|
||
// Define the response type for the hook | ||
type UseSendTransactionResponse = ReturnType<SendTransactionApi["sendTransaction"]>; | ||
|
||
type UseSendTransactionInput<TConfig extends RpConfig = RpConfig> = GillUseRpcHook<TConfig> & { | ||
config?: TConfig; | ||
/** | ||
* The signed transaction in wire format, as a base-64 encoded string. | ||
*/ | ||
JkrishnaD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
signature: Base64EncodedWireTransaction | string; | ||
}; | ||
|
||
/** | ||
* Send a transaction using the Solana RPC method of | ||
* [`sendTransaction`](https://solana.com/docs/rpc/http/sendtransaction) | ||
* | ||
* @returns An object from useMutation, including `signature`(the transaction signature) in an object | ||
*/ | ||
export function useSendTransaction<TConfig extends RpConfig = RpConfig>({ | ||
signature, | ||
config, | ||
}: UseSendTransactionInput<TConfig>) { | ||
const { rpc } = useSolanaClient(); | ||
|
||
const { data, ...rest } = useMutation({ | ||
JkrishnaD marked this conversation as resolved.
Show resolved
Hide resolved
|
||
mutationFn: async () => { | ||
// The RPC method expects the base-64 encoded transaction as the first argument. | ||
const response = await rpc | ||
.sendTransaction(signature as Base64EncodedWireTransaction, { | ||
encoding: "base64", | ||
preflightCommitment: "confirmed", | ||
skipPreflight: false, | ||
...(config || {}), | ||
}) | ||
.send(); | ||
// return the transaction signature as a base-64 encoded string | ||
return response; | ||
}, | ||
mutationKey: [GILL_HOOK_CLIENT_KEY, "sendTransaction"], | ||
networkMode: "offlineFirst", | ||
}); | ||
|
||
return { | ||
...rest, | ||
signature: data as UseSendTransactionResponse, | ||
}; | ||
} |
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.