API

Events

Subscribe to SDK events for real-time feedback on round-ups, swaps, and errors.

Available Events

EventFires whenData
roundUpA transaction is wrapped with a round-up{ breakdown, roundUpCount }
skippedTransaction was an exact match — no charge{ txValueUsd, reason }
thresholdReachedAccumulated balance hits the threshold{ state }
swapExecutedJupiter swap completed successfully{ result, quote }
swapFailedJupiter swap failed{ error, inputLamports, asset }
priceUpdatedSOL price was fetched/refreshed{ solPriceUsd }

Usage

events.ts
typescript
1// Subscribe
2buff.events.on("roundUp", ({ breakdown, roundUpCount }) => {
3 showNotification(
4 "Invested $" + breakdown.userInvestmentUsd.toFixed(2) +
5 " (round-up #" + roundUpCount + ")"
6 )
7})
8
9buff.events.on("thresholdReached", ({ state }) => {
10 showNotification(
11 "Threshold reached! $" + state.balanceUsd.toFixed(2) +
12 " ready to invest"
13 )
14})
15
16buff.events.on("swapExecuted", ({ result, quote }) => {
17 showNotification(
18 "Bought " + result.asset + " with " +
19 result.inputSol.toFixed(4) + " SOL"
20 )
21})
22
23buff.events.on("swapFailed", ({ error, asset }) => {
24 showError("Failed to buy " + asset + ": " + error.message)
25})
26
27buff.events.on("skipped", ({ txValueUsd }) => {
28 // Exact dollar amount — no round-up needed
29})
30
31buff.events.on("priceUpdated", ({ solPriceUsd }) => {
32 updatePriceDisplay(solPriceUsd)
33})
34
35// Unsubscribe
36const handler = (data) => console.log(data)
37buff.events.on("roundUp", handler)
38buff.events.off("roundUp", handler)

React Example

useBuffEvents.tsx
typescript
1function useBuffEvents(buff: Buff | null) {
2 const [lastRoundUp, setLastRoundUp] = useState(null)
3 const [swapCount, setSwapCount] = useState(0)
4
5 useEffect(() => {
6 if (!buff) return
7
8 const onRoundUp = (data) => setLastRoundUp(data.breakdown)
9 const onSwap = () => setSwapCount(c => c + 1)
10
11 buff.events.on("roundUp", onRoundUp)
12 buff.events.on("swapExecuted", onSwap)
13
14 return () => {
15 buff.events.off("roundUp", onRoundUp)
16 buff.events.off("swapExecuted", onSwap)
17 }
18 }, [buff])
19
20 return { lastRoundUp, swapCount }
21}