# Twinpool Seat bot

Every Twinpool pool on Elysium has one Seat. Whoever holds it trades the pool with no LP fee through TwinRouter, and pays
an hourly rent that streams to the pool's in-range LPs. This bot runs a Seat for you, start to finish:

1. Preflight: reads your balances, the pool's floor rent, the standing rent (holder or pending bid) and the bond size.
   It refuses before sending anything if your rent would not beat the standing one by 10%, if you lack gas, TWIN for
   the bond, or the quote token for the deposit.
2. Bond: locks `bondPerSeat()` $TWIN (0.25% of supply, about 2.5M TWIN) if you have not already locked enough.
3. Bid: deposits `rent x hours` of the quote token (at least 6 hours) and bids.
4. Waits 5 minutes for the bid to activate, then settles it: you hold the Seat.
5. Trades: every `--interval` seconds it reads the HyperCore order book (best bid and ask) and the pool price. When the
   pool sits outside the book's touch by more than `--min-gap-bps` (plus a 10 bps margin), it swaps through TwinRouter
   in the closing direction. The swap's price limit is the book price, so it can never push the pool past the book.
   Input is capped by `--max-trade` and your wallet balance; `minOut` is the HyperCore value of the input minus
   `--slippage-bps`. TwinRouter pokes the hook first, so each trade also donates the rent accrued so far to LPs.
   Every trade is checked against the hook's `TwinSwap` event: fee 0 and `seat = true`, otherwise the bot stops.
6. Duty: a holder can be slashed (10% of one bond) if two truth-lane snapshots 60 s apart both show the pool outside
   the pool's duty band on the same side. When the latest snapshot of your tenure is outside the band, the bot closes
   the gap first. If it cannot (no inventory), it leaves the Seat before a second snapshot can land.
7. Exit: when the deposit runs dry the Seat ends on its own. Ctrl-C leaves the Seat and withdraws what is left.

The target price always comes from the HyperCore API. TwinBook's reference is only used for the duty check.

## Run

```sh
cd services/seat-bot
npm install
export SEAT_BOT_PRIVATE_KEY=0x...          # a key used only for this bot
node seat.mjs --pool MONTRA/USDC --rent 0.5 --hours 6 --max-trade 50
```

| flag | meaning |
| --- | --- |
| `--pool` | `MONTRA/USDC`, `KNTQ/USDC` or `WHYPE/USDC` |
| `--rent` | rent per hour, in the quote token (USDC), e.g. `0.5` |
| `--hours` | hours of rent to deposit, at least 6 (default 6) |
| `--max-trade` | largest single trade, in quote units (USDC); the base side is converted at the book mid |
| `--min-gap-bps` | gap from the book mid before trading (default 30) |
| `--slippage-bps` | `minOut` margin under the HyperCore value (default 20) |
| `--interval` | seconds between checks (default 15) |
| `--once` | one check and at most one trade, then exit while keeping the Seat |
| `--leave` | leave the Seat now and withdraw the deposit left |
| `--unlock` | free the bond, once 24 h have passed since your last Seat or bid |

Env: `SEAT_BOT_PRIVATE_KEY` (required), `ELYSIUM_RPC` (default `https://rpc.elysium.kinetiq.xyz`),
`SEAT_BOT_MAX_BASE_FEE_GWEI` (default 3; no transaction is sent above it), `HYPERCORE_API`.

Output is one JSON line per event, with the tx hash, gas used and cost of every transaction.

## What you need

- $TWIN for the bond: `bondPerSeat()` per Seat held or bid, about 2.5M TWIN today.
- The quote token (USDC) for the deposit: `rent x hours`. The floor rent is 0.01 USDC per hour.
- Inventory on both sides of the pool (USDC and the base token) so the bot can close gaps in both directions.
  With only one side it closes gaps one way and warns you.
- HYPE on Elysium for gas: the bot refuses below 0.005 HYPE. A full cycle (bond, bid, settle, a few trades, leave,
  withdraw, unlock) uses about 1.5M gas, under 0.001 HYPE at Elysium's usual 0.5 gwei base fee.

## Risks, read before running

- **The bond is locked.** It stays locked while you hold or bid for any Seat and for 24 hours after the last one ends.
  A duty slash takes 10% of one Seat bond.
- **Rent is paid whether or not you trade.** It streams per second from your deposit from the moment the Seat is
  yours. If the pool stays in line with the book, you pay rent and make no trades.
- **A pending bid cannot be cancelled.** Ctrl-C during the 5-minute wait keeps the bot running until the bid
  activates, then it leaves and withdraws. A second Ctrl-C exits at once; run `--leave` after activation.
- **Inventory risk.** Each trade swaps one token for the other at a price at least as good as the HyperCore book. The
  bot does not hedge on HyperCore: you end up holding whatever the trades leave you with, and its value moves with
  the market.
- **The bot leaves on trouble.** If a transaction reverts, or it cannot read the chain or the book for about two
  minutes, it stops trading and leaves the Seat (an unattended Seat can be slashed). If leaving fails too, it prints
  the commands below.
- **Outbid.** Anyone can take the Seat with a bid 10% above yours, 5 minutes after placing it. Your deposit left is
  credited to you; the bot withdraws it and exits.

## Exit commands

```sh
node seat.mjs --pool MONTRA/USDC --leave     # leave the Seat now, withdraw the deposit left, print when the bond frees
node seat.mjs --pool MONTRA/USDC --unlock    # 24 h after your last Seat or bid: return the bond
```

Both only need `SEAT_BOT_PRIVATE_KEY`. They work with every Twinpool keeper stopped: `leave`, `withdraw` and
`unlockBond` are plain SeatAuction calls.

## Contracts (Elysium, chain 99901)

| | |
| --- | --- |
| SeatAuction | `0xa7181466f1cdfbd2461eba49e7b6270243891691` |
| TwinHook | `0xa93F33BEF2192D54b57865Db6b43Fe18424aE088` |
| TwinRouter | `0xD5603f25835C4962BDc1E530428a83140332C684` |
| TwinBook | `0x391e9e07cb6c7dd56fa592b9440641078fd5749b` |
| StateView | `0x27eD73aE0652fdCf71d6793412A7525f32bE9dfE` |
| $TWIN | `0xbe20f4f461763d5779cc057240d14eeb67116775` |

## Tests

`npm test` runs the pure parts (price math, trade planning and sizing, duty guard, argument checks). The full flow was
proven on an anvil fork of Elysium mainnet: bond, bid, activation, a zero-fee Seat trade, rent donated on poke, a duty
exit, leave, withdraw and unlock.
