Withdrawing Tokens

The withdraw() method lets a user transfer shielded tokens from their private Shielder account back to a public address — using a relayer to maintain privacy.

When to Use

Use withdraw() when a user wants to exit the private set and receive tokens on a public Ethereum address — anonymously, via relayer.

async withdraw(
  token: Token,
  amount: bigint,
  quotedFees: QuotedFees,
  withdrawalAddress: Address,
  pocketMoney: bigint
): Promise<`0x${string}`>

What It Does

  • Fetches the latest state and constructs a withdrawal proof.

  • Sends a withdrawal request to the configured relayer backend.

  • The relayer pays gas and broadcasts the transaction to the network.

  • Funds arrive at the withdrawalAddress, minus relayer fees.

Example

src/App.tsx
...

import { initializeShielderClient } from "./shielder";
import { nativeToken } from "@cardinal-cryptography/shielder-sdk";

const amountToWithdraw = 500_000_000_000_000n; // 0.0005 ETH
const withdrawalAddress = "0x..." as const;

(async () => {
  const shielder = await initializeShielderClient();
  const token = nativeToken();

  // Sync to ensure up-to-date state
  await shielder.syncShielder();

  // Get fee quote from relayer for this withdrawal
  const quotedFees = await shielder.getWithdrawFees(token, 0n); // pocketMoney = 0n for native

  // Withdraw through relayer
  const txHash = await shielder.withdraw(
    token,
    amountToWithdraw,
    quotedFees,
    withdrawalAddress,
    0n // pocketMoney, usually 0 for native token
  );

  console.log("Withdraw tx hash:", txHash);
})();

...

Pocket Money (for ERC-20s only)

If the withdrawal token is not the native token, you may want to request a small native token payment from the relayer to cover gas for the recipient.

const pocketMoney = 100_000_000_000_000n; // 0.0001 ETH
const quotedFees = await shielder.getWithdrawFees(token, pocketMoney)

Tips

  • amountToWithdraw passed to withdraw() function includes the fee, more precisely withdrawalAddress will receive amountToWithdraw - quotedFees.fee_details.total_cost_fee_token

Last updated

Was this helpful?