Guide
Custom Storage
Client-side caching strategies for the Buff API.
Default Behavior
In Buff v1.0.0, all state is managed server-side via the buff.finance API. The SDK is a thin HTTP client and does not persist local state. Portfolio data, accumulator state, and round-up history are all fetched from the API on demand.
Fetching State
state.ts
typescript
1import { Buff } from "buff-protocol-sdk"23const buff = new Buff({ apiKey: "your-api-key" })45// Portfolio — requires wallet address6const portfolio = await buff.getPortfolio("wallet-address")7console.log("Holdings:", portfolio)89// Accumulator state — round-up totals and threshold status10const accumulator = await buff.getAccumulator("wallet-address")11console.log("Pending USD:", accumulator.pendingUsd)12console.log("Threshold reached:", accumulator.thresholdReached)1314// Calculate a round-up (stateless, no persistence needed)15const breakdown = await buff.calculateRoundUp(5.37)16console.log("Round-up:", breakdown.roundUpUsd)Client-Side Caching (Optional)
If you want to cache API responses locally to reduce network calls, you can implement your own caching layer around the SDK.
cache.ts
typescript
1import { Buff } from "buff-protocol-sdk"23const buff = new Buff({ apiKey: "your-api-key" })45// Simple in-memory cache example6const cache = new Map<string, { data: any; expiry: number }>()78async function getCachedPortfolio(address: string, ttlMs = 30000) {9 const key = "portfolio:" + address10 const cached = cache.get(key)11 if (cached && cached.expiry > Date.now()) return cached.data1213 const portfolio = await buff.getPortfolio(address)14 cache.set(key, { data: portfolio, expiry: Date.now() + ttlMs })15 return portfolio16}Server-Side Cache: Redis Example
redis-cache.ts
typescript
1import { Buff } from "buff-protocol-sdk"2import { createClient } from "redis"34const buff = new Buff({ apiKey: "your-api-key" })5const redis = createClient({ url: "redis://localhost:6379" })6await redis.connect()78async function getCachedPortfolio(address: string, ttlSec = 30) {9 const key = "buff:portfolio:" + address10 const cached = await redis.get(key)11 if (cached) return JSON.parse(cached)1213 const portfolio = await buff.getPortfolio(address)14 await redis.set(key, JSON.stringify(portfolio), { EX: ttlSec })15 return portfolio16}1718async function getCachedAccumulator(address: string, ttlSec = 10) {19 const key = "buff:accumulator:" + address20 const cached = await redis.get(key)21 if (cached) return JSON.parse(cached)2223 const accumulator = await buff.getAccumulator(address)24 await redis.set(key, JSON.stringify(accumulator), { EX: ttlSec })25 return accumulator26}Note
All state lives server-side in Buff v1.0.0. The SDK is stateless — caching is optional and only needed to reduce API calls in high-frequency scenarios.