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"23const buff = new Buff({ apiKey: "YOUR_KEY" })4const walletAddress = "YOUR_BUFF_WALLET"56// Poll accumulator for threshold changes7async function checkThreshold() {8 const acc = await buff.getAccumulator(walletAddress)910 if (acc.thresholdReached) {11 console.log("Threshold reached! $" + acc.balanceUsd.toFixed(2))1213 // Build and execute swap14 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}2627// Check every 60 seconds28setInterval(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)45 useEffect(() => {6 if (!buff || !walletAddress) return78 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 }1617 poll() // initial fetch18 const interval = setInterval(poll, 30_000)19 return () => clearInterval(interval)20 }, [buff, walletAddress])2122 return { accumulator, portfolio }23}Available Data
| Endpoint | What to monitor | Suggested interval |
|---|---|---|
| getAccumulator(addr) | Balance vs threshold — triggers swap | 30-60s |
| getPortfolio(addr) | Token balances after swaps | 60s |
| getPrices() | SOL/BTC/ETH price changes | 120s |
| calculateRoundUp(usd) | Preview before wrapping | On demand |
Note
The Buff API has rate limiting (60 requests/min per IP). Keep polling intervals at 30s+ to stay well within limits.