Skip to content
Closed
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
26 changes: 25 additions & 1 deletion account-kit/react/src/AlchemySolanaWeb3Context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,32 @@
"use client";

import { Connection } from "@solana/web3.js";
import type { Connection } from "@solana/web3.js";
import { createContext } from "react";

/**
* Context for the Alchemy Solana web3 connection.
*
* @example
* ```tsx twoslash
* import { AlchemySolanaWeb3Context } from "@account-kit/react";
* import { Connection } from "@solana/web3.js";
*
* function App() {
* const connection = new Connection("<SolanaRpcUrl>");
* return (
* <AlchemySolanaWeb3Context.Provider value={{ connection }}>
* <App />
* </AlchemySolanaWeb3Context.Provider>
* );
* }
* ```
*
* @param {React.PropsWithChildren<{
* connection: Connection;
* policyId?: string;
* }>} props the props for the AlchemySolanaWeb3Context.Provider
* @returns {React.JSX.Element} The element to wrap your application in for Alchemy Solana web3 context.
*/
export const AlchemySolanaWeb3Context = createContext<null | {
connection: Connection;
policyId?: string;
Expand Down
6 changes: 6 additions & 0 deletions account-kit/react/src/hooks/useSolanaSigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export type SignerSet = {
* This hook is used to create a SolanaSigner instance.
* It is used to sign transactions and messages for the Solana blockchain.
*
* ```tsx
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding some docs

* import { useSolanaSigner } from "@account-kit/react";
*
* const signer = useSolanaSigner({});
* ```
*
* @param {object} opts - The options for the useSolanaSigner hook.
* @param {SignerSet} [opts.signerSet] - The signer set to use.
* @returns {object} A SolanaSigner instance.
Expand Down
75 changes: 65 additions & 10 deletions account-kit/react/src/hooks/useSolanaTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,72 @@ export type SolanaTransactionHookParams = {
* This is the hook that will be used to send a transaction.
*
* @example
* ```ts
const {mutate} = useSolanaTransaction({
policyId: "<policyId>",
});
* ```tsx twoslash
* import React from "react";
* import { useSolanaTransaction } from "@account-kit/react";
* import { Connection } from "@solana/web3.js";
*
* function MyComponent() {
* const connection = new Connection(
* "https://solana-devnet.g.alchemy.com/v2/<API_KEY>",
* {
* wsEndpoint: "wss://api.devnet.solana.com",
* commitment: "confirmed",
* }
* );
* const policyId = "<policyId>";
*
* const {
* mutate: doTransaction,
* isPending,
* signer,
* reset,
* data: { hash: txHash = null } = {},
* } = useSolanaTransaction({
* connection,
* // Used if we have a alchemy sponsor
* policyId,
* });

mutate({
transfer: {
amount: <amount:number>,
toAddress: "<toAddress>",
},
* ```
* if (!signer) {
* return <div>Loading alchemy signer...</div>;
* }

* return (
* <div>
* Solana Address: { signer.address }
* <button
* onClick={() =>
* doTransaction({
* transfer: {
* amount: 1000000,
* toAddress: "<ToSolanaAddress>",
* },
* })
* }
* >Go make Transfer</button>
* {
* !!txHash &&
* <button onClick={() => reset()}>Reset</button>
* }
* { !!txHash && <a href={`https://explorer.solana.com/tx/${txHash}?cluster=devnet`} target="_blank">Go To Tracker</a> }
* </div>
* );
* }
* ```
*
* This hook:
*
* - Signs the transaction and broadcasts it to the network via the Connection passed in
* - Returns a mutate to fire and forget a transaction, either a transfer or list of web3 instructions
* - Provides the state for the result of the txHash that results after the broadcast
* - Has an isPending to know if the transaction is currently sending
* - Has the signer, so we can see if we had provided an alchemy signer in the app. (A signer could have been passed in instead of provided)
* - Has other onMutation hooks that could be passed in as the mutation: argument for the hook
*
* Note: This example assumes that the alchemy signer is defined higher up in the React tree. If this is not the case this transaction will not work. Instead, one could pass down the signer as a parameter for the hook
* Note: This example uses the connection passed in, but there is a AlchemySolanaWeb3Context if we would like do input as a provider.
*
* @param {SolanaTransactionHookParams} opts Options for the hook to get setup, The transaction is required.
* @returns {SolanaTransaction} The transaction hook.
*/
Expand Down
6 changes: 6 additions & 0 deletions docs/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ navigation:
path: wallets/pages/reference/account-kit/react/hooks/useSignTypedData.mdx
- page: useWaitForUserOperationTransaction
path: wallets/pages/reference/account-kit/react/hooks/useWaitForUserOperationTransaction.mdx
- section: Solana Actions
contents:
- page: useSolanaTransaction
path: wallets/pages/reference/account-kit/react/hooks/useSolanaTransaction.mdx
- page: useSolanaSigner
path: wallets/pages/reference/account-kit/react/hooks/useSolanaSigner.mdx
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be useSolanaSignMessage.mdx instead of useSolanaSigner.mdx?

- section: Bundler/RPC Client
contents:
- page: useBundlerClient
Expand Down
64 changes: 64 additions & 0 deletions site/pages/react/quickstart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -394,4 +394,68 @@ export default function Home() {
</main>
);
}
c;
```

## Other examples
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should remove this, I didn't know where to put, then I found that the components where already being rendered


### Sending a solana transaction

This is with the assumption that we have logged in, and we want a component to send a transaction

```tsx twoslash
import React from "react";
import { useSolanaTransaction } from "@account-kit/react";
import { Connection } from "@solana/web3.js";

function MyComponent() {
const connection = new Connection(
"https://solana-devnet.g.alchemy.com/v2/<API_KEY>",
{
wsEndpoint: "wss://api.devnet.solana.com",
commitment: "confirmed",
}
);

const {
mutate: doTransaction,
isPending,
signer,
reset,
data: { hash: txHash = null } = {},
} = useSolanaTransaction({
connection,
});

if (!signer) {
return <div>Loading alchemy signer...</div>;
}

return (
<div>
Solana Address {signer.address}
<button
onClick={() =>
doTransaction({
transfer: {
amount: 1000000,
toAddress: "<ToSolanaAddress>",
},
})
}
>
Go make Transfer
</button>
{!!txHash && <button onClick={() => reset()}> Reset </button>}
{!!txHash && (
<a
href={`https://explorer.solana.com/tx/${txHash}?cluster=devnet`}
target="_blank"
>
Go To Tracker
</a>
)}
</div>
);
}
```

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions site/scripts/prebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const hookGroupings = {
"useClientActions",
"useChain",
],
"Solana Actions": ["useSolanaTransaction", "useSolanaSignMessage"],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was the biggest aha moment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@moldy530 What about the provider? Should I make a context or make it part of the other alchemy provider, and should I add this in as a seperate config field, or part of chain setup, which I have to filter now I think for viem vs not

"Bundler/RPC Client": ["useBundlerClient"],
Utilities: [
"useAlchemyAccountContext",
Expand Down
34 changes: 34 additions & 0 deletions site/sidebar/reference/aa-sdk/core.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions site/sidebar/reference/account-kit/react-native.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions site/sidebar/reference/account-kit/react.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading