-
Notifications
You must be signed in to change notification settings - Fork 0
Fix stack accounting and order flow #28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
173713c
fix(state): ignore typed and data-bearing capacity cells
phroi 7025d3c
fix(protocol): tighten receipt and DAO readiness accounting
phroi 4e3822a
fix(order): prefer progressed and minted descendants
phroi 3b5f6b6
fix(sdk): derive pool maturity from live deposits
phroi 9cc4363
fix(interface): tighten preview selection and test coverage
phroi 07622ea
fix(bot): simplify rebalancing policy contract
phroi c14c72f
docs(stack): separate live behavior from future ideas
phroi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| # Current Bot Rebalancing Policy | ||
|
|
||
| This document describes the policy currently implemented in `apps/bot/src/policy.ts`. | ||
|
|
||
| ## Goal | ||
|
|
||
| The bot keeps enough liquid iCKB to keep matching and redemption paths responsive, while leaving as much capital as practical in CKB. | ||
|
|
||
| The live policy is intentionally small: | ||
|
|
||
| - keep a minimum iCKB inventory | ||
| - refill that inventory with one direct deposit when it gets too low | ||
| - request withdrawals from ready pool deposits when iCKB inventory drifts too high | ||
| - do nothing when output space or balances make the action unsafe | ||
|
|
||
| ## Inputs | ||
|
|
||
| `planRebalance(...)` decides from five inputs: | ||
|
|
||
| - `outputSlots`: how many transaction output slots remain before the bot would hit its DAO-safe output cap | ||
| - `ickbBalance`: currently available iCKB after pending order matches are applied | ||
| - `ckbBalance`: currently available CKB after pending order matches are applied | ||
| - `depositCapacity`: the current CKB capacity required for one standard iCKB deposit at the live exchange ratio | ||
| - `readyDeposits`: ready pool deposits that the bot can request for withdrawal now | ||
|
|
||
| ## Constants | ||
|
|
||
| The current policy is shaped by three constants in `apps/bot/src/policy.ts`: | ||
|
|
||
| - `CKB_RESERVE = 1000 CKB`: the bot keeps this much extra CKB after making a new deposit | ||
| - `MIN_ICKB_BALANCE = 2000 iCKB`: if iCKB falls below this line, the bot tries to replenish it | ||
| - `TARGET_ICKB_BALANCE = 100000 iCKB + 20000 iCKB`: if iCKB rises above this target band, the bot tries to convert excess iCKB back toward CKB through ready deposit withdrawals | ||
|
|
||
| The current withdrawal request cap is `30` deposits per transaction. | ||
|
|
||
| ## Decision Order | ||
|
|
||
| The policy is deliberately greedy and local. | ||
|
|
||
| 1. If fewer than two output slots remain, do nothing. | ||
| 2. If available iCKB is below `MIN_ICKB_BALANCE`: | ||
| - request one new deposit if available CKB is at least `depositCapacity + CKB_RESERVE` | ||
| - otherwise do nothing | ||
| 3. If available iCKB is at or above `MIN_ICKB_BALANCE`, compute `excessIckb = ickbBalance - TARGET_ICKB_BALANCE`. | ||
| 4. If `excessIckb <= 0`, do nothing. | ||
| 5. Otherwise, pick a bounded subset of ready deposits whose total `udtValue` stays within `excessIckb`, and request withdrawals for that subset. | ||
|
|
||
| ## Ready Deposit Selection | ||
|
|
||
| `selectReadyDeposits(...)` is intentionally simple. | ||
|
|
||
| - It walks the ready deposits in the order they were prepared by the bot state reader. | ||
| - It skips any deposit that would push the cumulative selected `udtValue` above the current excess target. | ||
| - It stops once it reaches the request limit. | ||
|
|
||
| This keeps the live policy predictable and cheap. It does not try to globally optimize pool shape. | ||
|
|
||
| ## Ownership Boundary | ||
|
|
||
| This file describes bot-owned operating policy only. | ||
|
|
||
| - The bot owns when to add one more deposit. | ||
| - The bot owns when to request ready withdrawals. | ||
| - `@ickb/sdk` owns UI-side maturity estimation from live stack state. | ||
| - The older pool snapshot idea is not part of the current runtime path. | ||
|
|
||
| ## Non-Goals | ||
|
|
||
| This policy does not try to: | ||
|
|
||
| - maintain a global optimal distribution of deposits over the full 180-epoch clock | ||
| - encode a snapshot summary for interface use | ||
| - predict or coordinate other bots' behavior beyond acting on current visible state | ||
|
|
||
| Those may still be useful research directions, but they are not the current live contract. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { ccc } from "@ckb-ccc/ccc"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { isPlainCapacityCell } from "@ickb/utils"; | ||
|
|
||
| function byte32FromByte(hexByte: string): `0x${string}` { | ||
| if (!/^[0-9a-f]{2}$/iu.test(hexByte)) { | ||
| throw new Error("Expected exactly one byte as two hex chars"); | ||
| } | ||
| return `0x${hexByte.repeat(32)}`; | ||
| } | ||
|
|
||
| function script(codeHashByte: string): ccc.Script { | ||
| return ccc.Script.from({ | ||
| codeHash: byte32FromByte(codeHashByte), | ||
| hashType: "type", | ||
| args: "0x", | ||
| }); | ||
| } | ||
|
|
||
| describe("isPlainCapacityCell", () => { | ||
| it("accepts only no-type empty-data cells", () => { | ||
| const plain = ccc.Cell.from({ | ||
| outPoint: { txHash: byte32FromByte("11"), index: 0n }, | ||
| cellOutput: { | ||
| capacity: ccc.fixedPointFrom(1000), | ||
| lock: script("22"), | ||
| }, | ||
| outputData: "0x", | ||
| }); | ||
| const typed = ccc.Cell.from({ | ||
| outPoint: { txHash: byte32FromByte("33"), index: 0n }, | ||
| cellOutput: { | ||
| capacity: ccc.fixedPointFrom(2000), | ||
| lock: script("22"), | ||
| type: script("44"), | ||
| }, | ||
| outputData: "0x", | ||
| }); | ||
| const dataCell = ccc.Cell.from({ | ||
| outPoint: { txHash: byte32FromByte("55"), index: 0n }, | ||
| cellOutput: { | ||
| capacity: ccc.fixedPointFrom(3000), | ||
| lock: script("22"), | ||
| }, | ||
| outputData: "0xab", | ||
| }); | ||
|
|
||
| expect(isPlainCapacityCell(plain)).toBe(true); | ||
| expect(isPlainCapacityCell(typed)).toBe(false); | ||
| expect(isPlainCapacityCell(dataCell)).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.