Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.privacyboost.io/llms.txt

Use this file to discover all available pages before exploring further.

Deposits

This guide covers depositing tokens from your wallet into your private balance.

Basic Deposit

try {
    val result = withContext(Dispatchers.IO) {
        sdk.shield(
            tokenAddress = "0x...token-address",
            amount = "1000000000000000000" // 1 token (18 decimals)
        )
    }
    println("Transaction hash: ${result.txHash}")
    println("Commitment: ${result.commitment}")
} catch (e: SDKError) {
    println("Deposit failed: $e")
}

Deposit Parameters

ParameterTypeRequiredDescription
tokenAddressStringYesERC-20 token contract address
amountStringYesAmount in wei (smallest unit)

Deposit Steps

When a deposit is submitted, the SDK executes these steps in order:
StepDescription
WrappingWrapping ETH to WETH (if depositing ETH)
ApprovingApproving token spending on the shield contract
ShieldingExecuting the deposit transaction
RegisteringRegistering the deposit with the indexer
ComplianceWaiting for compliance verification

Deposit Result

data class DepositResult(
    val txHash: String,      // Main deposit transaction hash
    val commitment: String,  // Note commitment
    val fee: String          // Fee paid (in wei)
)

Depositing ETH

To deposit native ETH, use the zero address. The SDK automatically wraps ETH to WETH:
val ethAddress = "0x0000000000000000000000000000000000000000"

val result = withContext(Dispatchers.IO) {
    sdk.shield(
        tokenAddress = ethAddress,
        amount = "1000000000000000000" // 1 ETH
    )
}
println("Deposit tx: ${result.txHash}")
The SDK automatically:
  1. Wraps ETH to WETH
  2. Approves WETH spending
  3. Deposits WETH to the shield contract

Parsing Amounts

Use helper functions to convert between human-readable and wei formats:
// Parse human-readable amount to wei string
val weiAmount = sdk.parseAmount("1.5", decimals = 18u)
// Returns: "1500000000000000000"

// Format wei string to human-readable
val formatted = sdk.formatAmount("1500000000000000000", decimals = 18u)
// Returns: "1.5"

// For USDC (6 decimals)
val usdcWei = sdk.parseAmount("100.0", decimals = 6u)
// Returns: "100000000"

Error Handling

try {
    val result = withContext(Dispatchers.IO) {
        sdk.shield(tokenAddress = tokenAddress, amount = amount)
    }
} catch (e: SDKError) {
    when (e) {
        is SDKError.InsufficientBalance ->
            println("Not enough tokens in wallet")
        is SDKError.InvalidAmount ->
            println("Invalid amount format")
        is SDKError.WalletError ->
            println("Wallet error: ${e.message}")
        is SDKError.SignatureRejected ->
            println("User rejected the transaction")
        is SDKError.NetworkError ->
            println("Network error: ${e.message}")
        else ->
            println("Deposit error: $e")
    }
}

Best Practices

1. Validate Amounts Before Depositing

fun validateDepositAmount(amount: String, walletBalance: String) {
    val amountValue = amount.toBigIntegerOrNull()
        ?: throw SDKError.InvalidAmount
    require(amountValue > BigInteger.ZERO) { "Amount must be positive" }

    val balanceValue = walletBalance.toBigIntegerOrNull()
        ?: throw SDKError.InsufficientBalance
    require(amountValue <= balanceValue) { "Insufficient wallet balance" }
}

2. Refresh Balance After Deposit

val result = withContext(Dispatchers.IO) {
    sdk.shield(tokenAddress = tokenAddress, amount = amount)
}

// Refresh balance to reflect the deposit
val updatedBalance = withContext(Dispatchers.IO) {
    sdk.getBalance(tokenAddress = tokenAddress)
}
println("New shielded balance: ${updatedBalance.shieldedBalance}")

3. Always Use Background Dispatcher

SDK methods are blocking calls. Always wrap them in withContext(Dispatchers.IO) to avoid blocking the main thread:
// In a ViewModel
viewModelScope.launch {
    val result = withContext(Dispatchers.IO) {
        sdk.shield(tokenAddress = token, amount = amount)
    }
    // Update UI on main thread
    _depositState.value = DepositState.Success(result.txHash)
}

Next Steps