Earn
TheuseEarn hook moves part of a private balance into an ERC-4626 yield
vault and back without leaving the privacy pool.
A deposit spends a private note of the vault’s asset and credits the vault
shares as a new private note. A redeem spends shares and credits the asset. If
the vault call can’t deliver the signed minimum when it executes, the input is
refunded to your private balance instead. The Gateway fee replaces the
ordinary unshield fee and is charged on top of the amount, in the input token.
Earn explains how these operations settle.
useEarn
Earn runs in the key vault bound to the root SDK’s chain, so the hook always
uses the root client, whatever the provider’s
activeClient is. A deposit or
redeem unlocks first when the session is locked, like any other write.
Pick a Vault
The catalog carries what a vault picker shows: name, asset, APY, and whether each operation is available right now. APY values are fractions (0.05 is
5%).
vaults stays null
and error holds the failed read, but deposits and redemptions still work.
Build the vault list from the ERC-4626 capabilities in getRegisteredTokens()
instead; see Finding Vaults.
Deposit
amount is net: exactly the assets that enter the vault. The asset balance
has to cover amount plus the fee. Show the preview before the user confirms:
maxSlippageBps (default 100, or
1%), capped at the deployment’s limit. A vault can also stop taking deposits
while its route is enabled, for example at a supply cap. The TypeScript guide
shows how to check its capacity.
Redeem
Shares sit in the private balance under the vault address.amount is the
number of shares to redeem, and the fee is charged in shares on top of it, so
redeeming the whole balance needs room for the fee. See
Redeeming for the calculation.
Fees
EarnResult.fee reports the Gateway fee charged, in raw units of the input
token. To show it before submitting, call getGatewayFees on sdk.vault (or
from useSwap()) with the input token’s address, the executorAddress from
getGatewayConfig(), and the vault function’s selector: 0x6e553f65 for a
deposit, 0xba087652 for a redeem. See
useFees for turning the rates into an
amount.
Track the Operation
A deposit or redeem settles as an unshield followed by a credit. Follow itsrequestId with useUnshieldStatus: data.gateway.creditStatus reaches
'completed' once the output note exists, and data.gateway.settlement says
whether the vault call executed ('executed') or the input was refunded
('fallback').
creditStatus: 'rescued' is final too: the escrow went to the rescue
destination, so stop waiting for private credit. So is a failure, which never
reaches 'completed': settlement is 'failed_before_settlement' when the
request failed before it settled on-chain, 'reorged' when its settlement was
reorged out, and creditStatus is 'failed' when the credit didn’t go
through.
In history, an earn operation is two rows unless you fetch with
collapseGateway: true, which folds them into one row in
useTransactions().gatewayOperations.
Error Handling
A refused deposit or redeem carries a Gateway code.asGatewaySubmitErrorCode
reads it off the error. A refusal means nothing was submitted and the notes are
untouched, except GATEWAY_RECEIPT_REUSED: that exact request was accepted
before and may still settle.
Approval-Only Accounts
deposit and redeem refuse an approval-only account with INVALID_INPUT.
It proposes the operation with buildEarnApprovalPlan on the TypeScript client
(usePrivacyBoost()?.vault) instead; see
Approval-Only Accounts.