API

Webhooks & Polling

Monitor round-ups, swaps, and portfolio changes in your application.

Polling Pattern

Since the SDK v1.0.0 is a stateless API client, use polling to monitor state changes:

monitor.ts
typescript
1import { Buff } from "buff-protocol-sdk"
2
3const buff = new Buff({ apiKey: "YOUR_KEY" })
4const walletAddress = "YOUR_BUFF_WALLET"
5
6// Poll accumulator for threshold changes
7async function checkThreshold() {
8 const acc = await buff.getAccumulator(walletAddress)
9
10 if (acc.thresholdReached) {
11 console.log("Threshold reached! $" + acc.balanceUsd.toFixed(2))
12
13 // Build and execute swap
14 const result = await buff.buildSwap(walletAddress)
15 if (result.ready) {
16 for (const tx of result.transactions) {
17 // Sign tx.transaction with your wallet, then:
18 const executed = await buff.executeSwap(signedTx)
19 console.log("Swapped to " + tx.asset + ": " + executed.txSignature)
20 }
21 }
22 } else {
23 console.log("$" + acc.balanceUsd.toFixed(2) + " / $" + acc.thresholdUsd)
24 }
25}
26
27// Check every 60 seconds
28setInterval(checkThreshold, 60_000)

React Hook Example

useBuffMonitor.tsx
typescript
1function useBuffMonitor(buff: Buff | null, walletAddress: string) {
2 const [accumulator, setAccumulator] = useState(null)
3 const [portfolio, setPortfolio] = useState(null)
4
5 useEffect(() => {
6 if (!buff || !walletAddress) return
7
8 const poll = async () => {
9 const [acc, port] = await Promise.all([
10 buff.getAccumulator(walletAddress),
11 buff.getPortfolio(walletAddress),
12 ])
13 setAccumulator(acc)
14 setPortfolio(port)
15 }
16
17 poll() // initial fetch
18 const interval = setInterval(poll, 30_000)
19 return () => clearInterval(interval)
20 }, [buff, walletAddress])
21
22 return { accumulator, portfolio }
23}

Available Data

EndpointWhat to monitorSuggested interval
getAccumulator(addr)Balance vs threshold — triggers swap30-60s
getPortfolio(addr)Token balances after swaps60s
getPrices()SOL/BTC/ETH price changes120s
calculateRoundUp(usd)Preview before wrappingOn demand
Note
The Buff API has rate limiting (60 requests/min per IP). Keep polling intervals at 30s+ to stay well within limits.