Guide

React / Next.js

Full integration guide with Solana Wallet Adapter and Reown (WalletConnect).

Install

$npm install @buff/sdk @solana/wallet-adapter-react @solana/wallet-adapter-walletsCopy

Provider

BuffProvider.tsx
typescript
1"use client"
2import { createContext, useContext, useState, useEffect } from "react"
3import { useWallet } from "@solana/wallet-adapter-react"
4import { Buff } from "@buff/sdk"
5
6const BuffContext = createContext<Buff | null>(null)
7
8export function BuffProvider({ children, platformId }: {
9 children: React.ReactNode
10 platformId: string
11}) {
12 const { signMessage, publicKey } = useWallet()
13 const [buff, setBuff] = useState<Buff | null>(null)
14
15 useEffect(() => {
16 if (!signMessage || !publicKey) return
17
18 Buff.init({
19 platformId,
20 signMessage: (msg) => signMessage(msg),
21 plan: "sprout",
22 investInto: "BTC",
23 }).then(setBuff)
24 }, [signMessage, publicKey, platformId])
25
26 return (
27 <BuffContext.Provider value={buff}>
28 {children}
29 </BuffContext.Provider>
30 )
31}
32
33export const useBuff = () => useContext(BuffContext)

Wrapping Transactions

useSwap.tsx
typescript
1import { useBuff } from "./BuffProvider"
2import { useWallet } from "@solana/wallet-adapter-react"
3
4function SwapButton({ tx, valueUsd }) {
5 const buff = useBuff()
6 const { publicKey, sendTransaction } = useWallet()
7
8 const handleSwap = async () => {
9 if (!buff || !publicKey) return
10
11 const { transaction, breakdown } = await buff.wrap(
12 tx, publicKey, { txValueUsd: valueUsd }
13 )
14
15 if (!breakdown.skipped) {
16 toast("Investing $" + breakdown.roundUpUsd.toFixed(2))
17 }
18
19 await sendTransaction(transaction, connection)
20
21 const { swap } = await buff.checkAndInvest()
22 if (swap) toast("Bought " + swap.asset + "!")
23 }
24
25 return <button onClick={handleSwap}>Swap</button>
26}
Note
Both approaches produce the same Buff wallet from the same main wallet. The SDK's signMessage option is framework-agnostic — it just needs a function that takes Uint8Array and returns a signed Uint8Array.