Skip to main content

getBalanceFromUtxos()

getBalanceFromUtxos() is a utility function from privacycash/utils used to calculate the total balance from a set of SOL UTXOs (Unspent Transaction Outputs).

Parameters

ParameterTypeDescription
utxosUtxo[]An array of UTXO objects, typically retrieved using getUtxos().

Return Value

The function returns an object containing the balance information:
PropertyTypeDescription
lamportsnumberThe total balance in lamports.

Example Usage

import { getUtxos, getBalanceFromUtxos } from "privacycash/utils"

// 1. Fetch user UTXOs
const myValidUtxos = await getUtxos({
    connection,
    publicKey,
    storage: localStorage,
    encryptionService,
});

// 2. Calculate balance
const balanceInfo = getBalanceFromUtxos(myValidUtxos);
console.log("Total SOL Balance (lamports):", balanceInfo.lamports);

getUtxos()

getUtxos() is used to fetch all valid private UTXOs for a given user.

Parameters

ParameterTypeDescription
connectionConnectionSolana web3 connection object.
publicKeyPublicKeyThe user’s Solana public key.
storageStorageA storage object (e.g., localStorage).
encryptionServiceEncryptionServiceThe encryption service to decrypt UTXO data.
offsetnumber(optional) utxo fetch offset

Example Usage

const myValidUtxos = await getUtxos({
    connection,
    publicKey,
    storage: localStorage,
    encryptionService,
});

getBalanceFromUtxosSPL()

getBalanceFromUtxosSPL() calculates the total balance from a set of SPL token UTXOs.

Parameters

ParameterTypeDescription
utxosUtxo[]An array of UTXO objects, retrieved using getUtxosSPL().

Return Value

PropertyTypeDescription
base_unitsnumberThe total balance in base units.

getUtxosSPL()

getUtxosSPL() is used to fetch all valid private SPL token UTXOs for a given user and specific token mint.

Parameters

ParameterTypeDescription
connectionConnectionSolana web3 connection object.
publicKeyPublicKeyThe user’s Solana public key.
storageStorageA storage object (e.g., localStorage).
encryptionServiceEncryptionServiceThe encryption service.
mintAddressPublicKey or stringThe mint address of the SPL token.
offsetnumber(optional) utxo fetch offset

Example Usage

import { getUtxosSPL, getBalanceFromUtxosSPL } from "privacycash/utils"

const myValidUtxos = await getUtxosSPL({
    connection,
    publicKey,
    storage: localStorage,
    encryptionService,
    mintAddress: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v' // USDC
});

const balanceInfo = getBalanceFromUtxosSPL(myValidUtxos);
console.log("Total USDC Balance (base units):", balanceInfo.base_units);