Skip to content
Open
Show file tree
Hide file tree
Changes from 13 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/tender-terms-repeat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@gillsdk/react": minor
---

added `useSendTransaction` hook
17 changes: 17 additions & 0 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ Fetch data from the Solana blockchain with the gill hooks:
- [`useTokenMint`](#get-token-mint-account) - get a decoded token's Mint account
- [`useTokenAccount`](#get-token-account) - get the token account for a given mint and owner (or ATA)

Send data to the Solana blockchain with the gill hooks:
Comment thread
jkrishnad marked this conversation as resolved.

- [`useSendTransaction`](#send-transaction) - send a serialized solana transaction (Base64 encoded) to the Solana blockchain

### Example Usage of useSendTransaction hook
```tsx
const { send, signature, isPending, isError } = useSendTransaction({
transaction: base64EncodedTransaction,
config: {
skipPreflight: true,
preflightCommitment: 'finalized'
}
});
// Send the transaction
send();
````

### Wrap your React app in a context provider

Wrap your app with the `SolanaProvider` React context provider and pass your Solana client to it:
Expand Down
38 changes: 38 additions & 0 deletions packages/react/src/__typeset__/send-transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { useSendTransaction } from "../hooks";
import { Base64EncodedWireTransaction, SendTransactionApi} from "gill";

// [DESCRIBE] useSendTransaction
{
const transaction = null as unknown as Base64EncodedWireTransaction | string;
{
const signature = useSendTransaction({ transaction });
signature.transaction 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 signature = useSendTransaction({
config: {
encoding: "base64",
preflightCommitment: "confirmed",
skipPreflight: false,
},
transaction,
});
signature.transaction satisfies ReturnType<SendTransactionApi["sendTransaction"]>;
}

{
// Should require `signature` input
const signature = useSendTransaction({
transaction: "5Pj5fCupXLUePYn18JkY8SrRaWFiUctuDTRwvUy2ML9yvkENLb1QMYbcBGcBXRrSVDjp7RjUwk9a3rLC6gpvtYpZ",
});

signature.transaction satisfies ReturnType<SendTransactionApi["sendTransaction"]>;
}
}
1 change: 1 addition & 0 deletions packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export * from "./slot.js";
export * from "./token-account.js";
export * from "./token-mint.js";
export * from "./transaction.js";
export * from "./send-transaction.js";
59 changes: 59 additions & 0 deletions packages/react/src/hooks/send-transaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"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.
*/
Comment thread
jkrishnad marked this conversation as resolved.
transaction: 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>({
transaction,
config,
}: UseSendTransactionInput<TConfig>) {
const { rpc } = useSolanaClient();

const { data, ...rest } = useMutation({
Comment thread
jkrishnad marked this conversation as resolved.
Outdated
mutationFn: async () => {
// The RPC method expects the base-64 encoded transaction as the first argument.
const response = await rpc
.sendTransaction(transaction 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"],
Comment thread
jkrishnad marked this conversation as resolved.
networkMode: "offlineFirst",
retry: 3,
retryDelay: (index) => Math.min(1000 * 2 ** index, 3000),
});

return {
...rest,
transaction: data as UseSendTransactionResponse,
};
Comment thread
jkrishnad marked this conversation as resolved.
}