Skip to main content

withdraw()

withdraw() is a function from privacycash-evm used to withdraw ETH or a supported ERC-20 token from Privacy Cash to an EVM recipient address.

Parameters

ParameterTypeDescription
withdrawAmountInputnumberTotal amount to withdraw. Fees are deducted from this value.
recipientstringThe EVM address that will receive the withdrawn funds.
keyBasePathstringBase path for the circuit zkey (e.g. '/circuit').
signaturestringWallet signature of the Privacy Cash sign-in message.
addressstringThe withdrawer’s EVM wallet address.
token'eth' | 'usdc' | 'usdt'Optional. Defaults to 'eth'.
networkNetworkConfig | numberOptional. Pass BASE_NETWORK, ETH_NETWORK, or a supported chain ID.

Example Usage

'use client'
import { BASE_NETWORK, ETH_NETWORK, withdraw } from 'privacycash-evm'
import { useAccount, useChainId } from 'wagmi'

export function WithdrawButton() {
    const { address } = useAccount()
    const chainId = useChainId()

    const handleWithdraw = async () => {
        if (!address) return
        const network = chainId === 1 ? ETH_NETWORK : chainId === 8453 ? BASE_NETWORK : undefined
        if (!network) {
            alert('Switch to Base or Ethereum mainnet.')
            return
        }

        const signature = localStorage.getItem(`evm_sign_${address}`)
        if (!signature) {
            alert('Please sign in first.')
            return
        }

        const recipient = '0xRECIPIENT_ADDRESS'

        const txHash = await withdraw({
            withdrawAmountInput: 0.05,  // Total including fees
            recipient,
            keyBasePath: '/circuit',
            signature,
            address,
            token: 'eth',
            network,
        })

        console.log('Withdraw tx:', txHash)
    }

    return <button onClick={handleWithdraw}>Withdraw 0.05 ETH</button>
}


Fee Estimation

To display the estimated received amount before the user submits:
const RENT_FEE = 0.00025
const FEE_RATE = 35 // basis points, 0.35%

function estimateReceived(withdrawAmount: number): number {
    const flatFee = RENT_FEE                          // 0.00025 ETH
    const rateFee = (withdrawAmount * FEE_RATE) / 10000 // 0.35%
    return withdrawAmount - flatFee - rateFee
}

console.log(estimateReceived(0.1))  // ~0.09940 ETH

Notes

  • withdrawAmountInput is the total amount deducted from your private balance; the recipient receives less after fees.
  • Always validate that withdrawAmountInput <= privateBalance before calling withdraw().
  • The recipient must be a valid EVM address (0x + 40 hex characters).
  • Current token support is Base ETH/USDC and Ethereum ETH/USDT.