# Provide liquidity

> Deposit stock and USDG into a BookVault, follow the daily UTC roll, then withdraw in kind and claim, with viem and cast examples.

Source: https://docs.degenhive.ai/pools/liquidity

This guide takes you through one full cycle in a `BookVault`, the LP book behind each stock/USDG pool: deposit, the daily roll, shares, withdrawal and claim.

**Prerequisites**
- A wallet on Robinhood Chain (4663) with ETH for gas, USDG, and the stock token of the pool (or USDG only, for a zap).
- The pool's book address. There is one `BookVault` proxy per pool. Addresses publish at launch on [Network](https://docs.degenhive.ai/network.md); the examples use `BOOK`.

> [!IMPORTANT]
> These contracts are not deployed yet and not externally audited.

## Step 1: Read the book and its terms

Each book reports its stock, USDG and the `ProtocolConfig` it reads. Fees come from one `BookTerms` record that applies to every book. No fee values are hard-coded; the council sets them within these caps:

| Term | Cap | Meaning |
|---|---|---|
| `mgmtFeeBps` | 300 | Per year, on the value of outside shares |
| `perfFeeBps` | 5,000 | On gain above the book's high-water mark |
| `withdrawFeeBps` | 100 | Kept by the book for holders who stay |
| `nftBps` | 2,000 | Share of every fee paid to the Stingvestors pot |
| `settleDelay` | 1 minute to 12 hours | Wait after 00:00 UTC before the roll |

Raising a fee, or any change to `nftBps`, `settleDelay`, `feeRecipient`, `nftPot` or `keeper`, waits the 2-day timelock. Cuts to the other fees apply at once.

**viem**

```ts
import { createPublicClient, createWalletClient, defineChain, http, parseAbi } from "viem";
import { privateKeyToAccount } from "viem/accounts";

export const robinhood = defineChain({
  id: 4663,
  name: "Robinhood Chain",
  nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
  rpcUrls: { default: { http: ["https://rpc.mainnet.chain.robinhood.com"] } },
});

export const BOOK = "0x..."; // addresses publish at launch
export const account = privateKeyToAccount(process.env.PK as `0x${string}`);
export const pub = createPublicClient({ chain: robinhood, transport: http() });
export const wallet = createWalletClient({ account, chain: robinhood, transport: http() });

export const bookAbi = parseAbi([
  "function stock() view returns (address)",
  "function usdg() view returns (address)",
  "function config() view returns (address)",
  "function value() view returns (uint256)",
  "function quote(uint256 stockAmount, uint256 usdgAmount) view returns (uint256)",
  "function requestDeposit(uint256 stockAmount, uint256 usdgAmount)",
  "function zapFloor(uint256 usdgIn) view returns (uint256)",
  "function zapDeposit(uint256 usdgIn, uint256 minStockOut) returns (uint256 stockIn, uint256 usdgQueued)",
  "function cancelDeposit() returns (uint256 stockAmount, uint256 usdgAmount)",
  "function roll()",
  "function realize(address account)",
  "function accountOf(address account) view returns ((uint256 shares, uint256 locked, uint256 assetPending, uint256 depositPending, uint256 assetOwed, uint256 owed))",
  "function balanceOf(address account) view returns (uint256)",
  "function requestWithdraw(uint256 shares)",
  "function cancelWithdraw() returns (uint256 shares)",
  "function claim() returns (uint256 stockPaid, uint256 usdgPaid)",
]);
export const erc20 = parseAbi([
  "function approve(address spender, uint256 amount) returns (bool)",
  "function decimals() view returns (uint8)",
]);
const configAbi = parseAbi([
  "function bookTerms() view returns ((uint16 mgmtFeeBps, uint16 perfFeeBps, uint16 withdrawFeeBps, uint16 nftBps, uint16 maxSlippageBps, uint32 settleDelay, address feeRecipient, address nftPot, address keeper))",
]);

const config = await pub.readContract({ address: BOOK, abi: bookAbi, functionName: "config" });
export const terms = await pub.readContract({ address: config, abi: configAbi, functionName: "bookTerms" });
```

**cast**

```bash
cast call $BOOK "stock()(address)" --rpc-url $RH_RPC
cast call $BOOK "usdg()(address)" --rpc-url $RH_RPC
CONFIG=$(cast call $BOOK "config()(address)" --rpc-url $RH_RPC)
cast call $CONFIG "bookTerms()((uint16,uint16,uint16,uint16,uint16,uint32,address,address,address))" --rpc-url $RH_RPC
```

## Step 2: Queue a deposit in kind

Approve the book for both tokens, then call `requestDeposit(stockAmount, usdgAmount)`. The book pulls the tokens now and queues them for the next roll. Several requests on one UTC day merge into one. Use `quote(stockAmount, usdgAmount)` to see the value the roll will credit: stock at the lower of TWAP and spot, plus USDG at par.

Reverts with `DomainError` while the `ENTRY` domain is paused, and with `ZeroAmount` if both amounts are zero.

**viem**

```ts
import { parseUnits } from "viem";
import { BOOK, account, bookAbi, erc20, pub, wallet } from "./setup";

const [stock, usdg] = await Promise.all([
  pub.readContract({ address: BOOK, abi: bookAbi, functionName: "stock" }),
  pub.readContract({ address: BOOK, abi: bookAbi, functionName: "usdg" }),
]);
const stockDec = await pub.readContract({ address: stock, abi: erc20, functionName: "decimals" });
const stockAmount = parseUnits("2", stockDec);
const usdgAmount = parseUnits("500", 6);

await wallet.writeContract({ address: stock, abi: erc20, functionName: "approve", args: [BOOK, stockAmount] });
await wallet.writeContract({ address: usdg, abi: erc20, functionName: "approve", args: [BOOK, usdgAmount] });
const hash = await wallet.writeContract({
  address: BOOK, abi: bookAbi, functionName: "requestDeposit", args: [stockAmount, usdgAmount],
});
await pub.waitForTransactionReceipt({ hash });
```

**cast**

```bash
cast send $STOCK "approve(address,uint256)" $BOOK $STOCK_AMOUNT --private-key $PK --rpc-url $RH_RPC
cast send $USDG "approve(address,uint256)" $BOOK 500000000 --private-key $PK --rpc-url $RH_RPC
cast send $BOOK "requestDeposit(uint256,uint256)" $STOCK_AMOUNT 500000000 --private-key $PK --rpc-url $RH_RPC
```

## Step 3: Or zap in from USDG only

`zapDeposit(usdgIn, minStockOut)` buys the stock share of your USDG through `MakerHook.swap` in the same transaction, at the pool's current mix, then queues the stock and the leftover USDG. The swap pays the pool's normal fee, including the closed-hours floor. Read `zapFloor(usdgIn)` for a fair `minStockOut`: it prices the stock at the higher of spot and TWAP, less `maxSlippageBps`.

`zapFloor` reverts with `BookOffTwap` when spot sits more than `maxSlippageBps` ticks from TWAP; wait for the pool to settle. The zap reverts with `Slippage` if the swap buys less than `minStockOut`, and with `DomainError` while paused.

**viem**

```ts
const usdgIn = parseUnits("1000", 6);
const minStockOut = await pub.readContract({ address: BOOK, abi: bookAbi, functionName: "zapFloor", args: [usdgIn] });

await wallet.writeContract({ address: usdg, abi: erc20, functionName: "approve", args: [BOOK, usdgIn] });
await wallet.writeContract({ address: BOOK, abi: bookAbi, functionName: "zapDeposit", args: [usdgIn, minStockOut] });
```

## Step 4: Cancel the same day if you change your mind

The cutoff is 00:00 UTC. Until then, `cancelDeposit()` returns today's whole request in kind. After midnight the request belongs to the next roll and cannot be cancelled. Reverts with `NothingQueued` if you have no request for today.

**viem**

```ts
await wallet.writeContract({ address: BOOK, abi: bookAbi, functionName: "cancelDeposit" });
```

## Step 5: Roll the day

`roll()` runs once per UTC day, from `settleDelay` after 00:00 UTC. For the first hour only `BookTerms.keeper` may call it; after that anyone can. It settles every day up to yesterday in one call. Reverts with `RollNotDue` before the window opens or once the day is rolled.

> **Figure: A UTC day in a BookVault.** A Robinhood book takes deposits in kind, rolls once a day after settleDelay, pays its fees in USDG and settles deposits and withdrawals in kind.
>
> Left, during UTC day D: requestDeposit takes the stock and USDG in kind, or zapDeposit takes USDG and buys the stock leg; requestWithdraw takes shares and can be cancelled the same day only. Above them: the roll opens at 00:00 UTC plus settleDelay, for the keeper during the first hour and then for anyone. Middle: roll(), once per UTC day, in five steps: 1. gain over holding, using the lower of TWAP and spot; 2. take the management fee and the performance fee above the alpha high-water mark; 3. harvest the fees into USDG, idle USDG first, then a pool slice; 4. pay the fees; 5. price shares and settle deposits and in-kind withdrawals. From step 4, nftBps of the fees go to the Stingvestors VestorPot and the rest to the fee recipient, the outbox Safe. From step 5: claim, where realize hands over shares and claim() pays USDG and stock.

The roll runs in this order:

1. It pokes the hook, which collects pool fees into idle and checks the breaker.
2. It measures gain over holding: the book's holdings now against what it held after the last roll, valued at both TWAP and spot, taking the lower.
3. It charges the management fee on outside shares for the days elapsed, and the performance fee on the part of the gain that lifts the book above its high-water mark. Losing days pay no performance fee.
4. It turns the fees into USDG: idle USDG first, then a pool slice sold through the hook with a TWAP-based floor. If the sale fails, the rest stays due for the next roll.
5. It pays `nftBps` of the fees to `BookTerms.nftPot`, the Stingvestors `VestorPot`, and the rest to `BookTerms.feeRecipient`, the council's outbox Safe. That Safe bridges the USDG to Hyperliquid and calls [`HyperRevenueRouter.notifyRevenue`](https://docs.degenhive.ai/revenue.md). A refused payment is deferred and retried each roll.
6. It prices shares, executes the queued deposits and then the withdrawals, and redeploys idle tokens.

Deposits are valued with stock at the lower of TWAP and spot. If they exceed the book's deposit cap, every outside request is cut pro rata and the rest is refunded in kind to your owed balance. A pool that refuses the add, for example while paused, refunds the same way.

> [!NOTE]
> The `HiveFund` is a depositor in the same queue. Once per UTC day anyone can call `HiveFund.allocate()`, which deposits protocol USDG into books under their target weight and withdraws at most `maxMoveBps` from books over it. The Fund pays no management fee and is not limited by the deposit cap.

**viem**

```ts
await wallet.writeContract({ address: BOOK, abi: bookAbi, functionName: "roll" });
```

**cast**

```bash
cast send $BOOK "roll()" --private-key $PK --rpc-url $RH_RPC
```

## Step 6: Realize your shares

Shares minted at the roll sit in the book's own entry until they are realized. Your next request, transfer or claim realizes them, or anyone can call `realize(account)`. `accountOf(account).shares` counts them before and after. Book shares are a normal ERC-20 once realized.

**viem**

```ts
await wallet.writeContract({ address: BOOK, abi: bookAbi, functionName: "realize", args: [account.address] });
const acct = await pub.readContract({ address: BOOK, abi: bookAbi, functionName: "accountOf", args: [account.address] });
console.log(acct.shares, acct.assetOwed, acct.owed);
```

## Step 7: Request a withdrawal

`requestWithdraw(shares)` locks free shares in the book for the next roll. It is never paused. `cancelWithdraw()` returns today's request before 00:00 UTC. At the roll you receive a pro-rata slice of the pool, the book's held stock and its idle USDG, in kind. `withdrawFeeBps` stays in the book for the holders who remain; it is waived when every share leaves.

Reverts with `SharesShort` if you ask for more than your free shares, and with `ZeroAmount` for zero.

**viem**

```ts
const shares = await pub.readContract({ address: BOOK, abi: bookAbi, functionName: "balanceOf", args: [account.address] });
await wallet.writeContract({ address: BOOK, abi: bookAbi, functionName: "requestWithdraw", args: [shares] });
```

## Step 8: Claim your tokens

After the roll, `claim()` pays your owed stock and USDG. USDG must transfer or the call reverts. If the stock token refuses the transfer, the stock stays owed and you can claim it later. Refunded deposits are claimed the same way.

**viem**

```ts
const { result } = await pub.simulateContract({ account, address: BOOK, abi: bookAbi, functionName: "claim" });
console.log("stock", result[0], "usdg", result[1]);
await wallet.writeContract({ address: BOOK, abi: bookAbi, functionName: "claim" });
```

**cast**

```bash
cast send $BOOK "claim()" --private-key $PK --rpc-url $RH_RPC
```

## Step 9: Where to go next

- [Stock pools](https://docs.degenhive.ai/pools.md) How the hook prices swaps and protects the book.
- [Swap and integrate](https://docs.degenhive.ai/pools/swapping.md) Quote and swap Stock Tokens against USDG.
- [NFTs](https://docs.degenhive.ai/nfts.md) How the `nftBps` share reaches Stingvestor holders.
- [Revenue](https://docs.degenhive.ai/revenue.md) What happens to the fee recipient's share on Hyperliquid.
