API

buff.checkAndInvest()

Check accumulated balance and auto-swap to target asset when threshold is reached.

Signature

types.ts
typescript
1async checkAndInvest(): Promise<{
2 state: AccumulatorState
3 swaps: SwapResult[] // one per allocation
4 quotes: SwapQuote[] // one per allocation
5 swap: SwapResult | null // first swap (backward compat)
6 quote: SwapQuote | null // first quote (backward compat)
7}>

AccumulatorState

types.ts
typescript
1interface AccumulatorState {
2 balanceSol: number // Buff wallet SOL balance
3 balanceUsd: number // In USD (real-time price)
4 roundUpCount: number // Round-ups since last swap
5 thresholdReached: boolean
6 thresholdUsd: number // Configured threshold
7 solPriceUsd: number // Current SOL price
8}

SwapResult (when threshold reached)

types.ts
typescript
1interface SwapResult {
2 txSignature: string // Solana tx signature
3 inputSol: number // SOL amount swapped
4 asset: SupportedAsset // Target asset (BTC, ETH, etc.)
5 timestamp: number // Unix timestamp
6}

Example

invest.ts
typescript
1// Call after each wrap()
2const { state, swaps, quotes } = await buff.checkAndInvest()
3
4if (!state.thresholdReached) {
5 console.log("Accumulated: $" + state.balanceUsd.toFixed(2))
6 console.log("Need: $" + (state.thresholdUsd - state.balanceUsd).toFixed(2))
7 return
8}
9
10// Multi-asset: one swap per allocation
11for (let i = 0; i < swaps.length; i++) {
12 console.log(swaps[i].inputSol + " SOL → " + swaps[i].asset)
13 console.log("Route: " + quotes[i]?.route)
14}
15// e.g. with allocations [{BTC:60},{ETH:40}]:
16// 0.030 SOL → BTC
17// 0.020 SOL → ETH
Note
A 0.01 SOL reserve is kept in the Buff wallet for future transaction fees. The swap is executed via Jupiter with automatic route finding.

Preview Without Executing

quote.ts
typescript
1// Get a quote without swapping
2const quote = await buff.getQuote()
3
4if (quote) {
5 console.log("Would swap:", quote.inputSol, "SOL")
6 console.log("Expected output:", quote.expectedOutput)
7 console.log("Route:", quote.route)
8}