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"
2
3const buff = new Buff({ apiKey: "your-api-key" })
4
5// Portfolio — requires wallet address
6const portfolio = await buff.getPortfolio("wallet-address")
7console.log("Holdings:", portfolio)
8
9// Accumulator state — round-up totals and threshold status
10const accumulator = await buff.getAccumulator("wallet-address")
11console.log("Pending USD:", accumulator.pendingUsd)
12console.log("Threshold reached:", accumulator.thresholdReached)
13
14// 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"
2
3const buff = new Buff({ apiKey: "your-api-key" })
4
5// Simple in-memory cache example
6const cache = new Map<string, { data: any; expiry: number }>()
7
8async function getCachedPortfolio(address: string, ttlMs = 30000) {
9 const key = "portfolio:" + address
10 const cached = cache.get(key)
11 if (cached && cached.expiry > Date.now()) return cached.data
12
13 const portfolio = await buff.getPortfolio(address)
14 cache.set(key, { data: portfolio, expiry: Date.now() + ttlMs })
15 return portfolio
16}

Server-Side Cache: Redis Example

redis-cache.ts
typescript
1import { Buff } from "buff-protocol-sdk"
2import { createClient } from "redis"
3
4const buff = new Buff({ apiKey: "your-api-key" })
5const redis = createClient({ url: "redis://localhost:6379" })
6await redis.connect()
7
8async function getCachedPortfolio(address: string, ttlSec = 30) {
9 const key = "buff:portfolio:" + address
10 const cached = await redis.get(key)
11 if (cached) return JSON.parse(cached)
12
13 const portfolio = await buff.getPortfolio(address)
14 await redis.set(key, JSON.stringify(portfolio), { EX: ttlSec })
15 return portfolio
16}
17
18async function getCachedAccumulator(address: string, ttlSec = 10) {
19 const key = "buff:accumulator:" + address
20 const cached = await redis.get(key)
21 if (cached) return JSON.parse(cached)
22
23 const accumulator = await buff.getAccumulator(address)
24 await redis.set(key, JSON.stringify(accumulator), { EX: ttlSec })
25 return accumulator
26}
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.