@floorlaunchpad/sdk 0.1.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +84 -0
- package/LICENSE +201 -60
- package/README.md +45 -24
- package/dist/abi/evm-event-names.js +12 -0
- package/dist/abi/evm.gen.js +130 -0
- package/dist/abi/functions.gen.js +144 -0
- package/dist/abi/index.js +17 -0
- package/dist/abi/solana.gen.js +128 -0
- package/dist/burn.js +21 -0
- package/dist/client/evm/index.js +191 -0
- package/dist/client/index.js +19 -0
- package/dist/client/solana/index.js +259 -0
- package/dist/client/types.js +5 -0
- package/dist/codec.js +77 -0
- package/dist/config/codeHash.js +9 -0
- package/dist/config/generated.js +746 -0
- package/dist/config/index.js +49 -0
- package/dist/config/registry.gen.js +118 -0
- package/dist/config/solana-genesis.js +7 -0
- package/dist/ed25519.js +31 -0
- package/dist/errors.js +249 -0
- package/dist/events/index.js +40 -0
- package/dist/events/types.gen.js +3 -0
- package/dist/evm/abi.js +283 -0
- package/dist/evm/balances.js +26 -0
- package/dist/evm/chains.js +144 -0
- package/dist/evm/floor/abi.gen.js +195 -0
- package/dist/evm/floor/calls.js +230 -0
- package/dist/evm/floor/create.js +49 -0
- package/dist/evm/floor/events.js +41 -0
- package/dist/evm/floor/iface.js +44 -0
- package/dist/evm/items.js +209 -0
- package/dist/evm/keccak.js +118 -0
- package/dist/evm/multicall.js +30 -0
- package/dist/evm/router.js +184 -0
- package/dist/evm/zap.js +128 -0
- package/dist/fixtures.js +53 -0
- package/dist/generated/index.js +28 -0
- package/dist/generated/interface.gen.js +146 -0
- package/dist/handle.js +22 -0
- package/dist/index.js +91 -7
- package/dist/indexer/bodies.js +473 -0
- package/dist/indexer/client.js +104 -0
- package/dist/indexer/contract.js +349 -0
- package/dist/indexer/decode.js +97 -0
- package/dist/indexer/partners.js +100 -0
- package/dist/indexer/paths.js +127 -0
- package/dist/indexer/read.js +109 -0
- package/dist/indexer/stream.js +71 -0
- package/dist/lock.js +16 -0
- package/dist/math/fixed.js +154 -0
- package/dist/math/genesis.js +39 -0
- package/dist/math/index.js +20 -0
- package/dist/math/u256.js +49 -0
- package/dist/math/v2/ask.js +162 -0
- package/dist/math/v2/book.js +607 -0
- package/dist/math/v2/circulating.js +62 -0
- package/dist/math/v2/create.js +122 -0
- package/dist/math/v2/display.js +208 -0
- package/dist/math/v2/error.js +17 -0
- package/dist/math/v2/feasibility.js +174 -0
- package/dist/math/v2/fees.js +121 -0
- package/dist/math/v2/holders.js +105 -0
- package/dist/math/v2/milestones.js +40 -0
- package/dist/math/v2/pow.js +69 -0
- package/dist/math/v2/preset.js +151 -0
- package/dist/math/v2/receipt.js +108 -0
- package/dist/math/v2/relay.js +217 -0
- package/dist/math/v2/rows.js +365 -0
- package/dist/math/v2/split.js +111 -0
- package/dist/math/v2/sqrt.js +84 -0
- package/dist/math/v2/supply.js +21 -0
- package/dist/math/v2/team.js +75 -0
- package/dist/math/v2/trade.js +126 -0
- package/dist/math/v2/wide.js +51 -0
- package/dist/quote.js +6 -0
- package/dist/registryPrice.js +23 -0
- package/dist/route/edge-jupiter.js +42 -0
- package/dist/route/edge.js +11 -0
- package/dist/route/graph.js +51 -0
- package/dist/route/path.js +75 -0
- package/dist/route/quote.js +15 -0
- package/dist/route-jupiter.js +35 -0
- package/dist/route.js +8 -0
- package/dist/sha256.js +61 -0
- package/dist/solana/alt.js +18 -0
- package/dist/solana/floor/accounts.js +390 -0
- package/dist/solana/floor/borsh.js +66 -0
- package/dist/solana/floor/create.js +65 -0
- package/dist/solana/floor/events.js +140 -0
- package/dist/solana/floor/genesis.js +91 -0
- package/dist/solana/floor/genesisList.js +68 -0
- package/dist/solana/floor/instructions.js +435 -0
- package/dist/solana/floor/metadata.js +198 -0
- package/dist/solana/floor/mint.js +206 -0
- package/dist/solana/floor/program.js +64 -0
- package/dist/solana/floor/registry.js +83 -0
- package/dist/solana/instruction.js +49 -0
- package/dist/solana/pda.js +88 -0
- package/dist/solana/tx.js +138 -0
- package/dist/v2/burnPreview.js +35 -0
- package/dist/v2/circulating.js +11 -0
- package/dist/v2/claimAllEvm.js +70 -0
- package/dist/v2/claims-evm.js +120 -0
- package/dist/v2/claims.js +218 -0
- package/dist/v2/dead.js +21 -0
- package/dist/v2/devbuy.js +37 -0
- package/dist/v2/events.js +115 -0
- package/dist/v2/evm.js +299 -0
- package/dist/v2/evmReads.js +76 -0
- package/dist/v2/genesis.js +76 -0
- package/dist/v2/layouts.js +217 -0
- package/dist/v2/market-evm.js +74 -0
- package/dist/v2/presetRowEvm.js +45 -0
- package/dist/v2/press.js +106 -0
- package/dist/v2/quoteMint.js +52 -0
- package/dist/v2/receipt-tx.js +149 -0
- package/dist/v2/route-alt.js +114 -0
- package/dist/v2/route.js +389 -0
- package/dist/v2/solana.js +391 -0
- package/dist/v2/solanaSettle.js +97 -0
- package/dist/v2/tx/evm.js +126 -0
- package/dist/v2/tx/genesis.js +47 -0
- package/dist/v2/tx/index.js +5 -0
- package/dist/v2/tx/solana.js +118 -0
- package/dist/v2/types.js +13 -0
- package/docs/aggregator.md +61 -0
- package/docs/bot.md +50 -0
- package/docs/frontend.md +29 -0
- package/docs/wallet.md +54 -0
- package/package.json +428 -32
- package/src/abi/evm-event-names.ts +12 -0
- package/src/abi/evm.gen.ts +139 -0
- package/src/abi/functions.gen.ts +154 -0
- package/src/abi/index.ts +30 -0
- package/src/abi/solana.gen.ts +138 -0
- package/src/burn.ts +37 -0
- package/src/client/evm/index.ts +199 -0
- package/src/client/index.ts +23 -0
- package/src/client/solana/index.ts +260 -0
- package/src/client/types.ts +162 -0
- package/src/codec.ts +77 -0
- package/src/config/codeHash.ts +9 -0
- package/src/config/generated.ts +746 -0
- package/src/config/index.ts +91 -0
- package/src/config/registry.gen.ts +118 -0
- package/src/config/solana-genesis.ts +7 -0
- package/src/ed25519.ts +31 -0
- package/src/errors.ts +270 -0
- package/src/events/index.ts +44 -0
- package/src/events/types.gen.ts +831 -0
- package/src/evm/abi.ts +292 -0
- package/src/evm/balances.ts +26 -0
- package/src/evm/chains.ts +167 -0
- package/src/evm/floor/abi.gen.ts +195 -0
- package/src/evm/floor/calls.ts +247 -0
- package/src/evm/floor/create.ts +68 -0
- package/src/evm/floor/events.ts +51 -0
- package/src/evm/floor/iface.ts +45 -0
- package/src/evm/items.ts +252 -0
- package/src/evm/keccak.ts +118 -0
- package/src/evm/multicall.ts +43 -0
- package/src/evm/router.ts +209 -0
- package/src/evm/zap.ts +152 -0
- package/src/fixtures.ts +53 -0
- package/src/generated/index.ts +29 -0
- package/src/generated/interface.gen.ts +153 -0
- package/src/handle.ts +22 -0
- package/src/index.ts +91 -6
- package/src/indexer/bodies.ts +528 -0
- package/src/indexer/client.ts +178 -0
- package/src/indexer/contract.ts +410 -0
- package/src/indexer/decode.ts +109 -0
- package/src/indexer/partners.ts +109 -0
- package/src/indexer/paths.ts +190 -0
- package/src/indexer/read.ts +119 -0
- package/src/indexer/stream.ts +83 -0
- package/src/lock.ts +16 -0
- package/src/math/fixed.ts +154 -0
- package/src/math/genesis.ts +39 -0
- package/src/math/index.ts +24 -0
- package/src/math/u256.ts +49 -0
- package/src/math/v2/ask.ts +218 -0
- package/src/math/v2/book.ts +660 -0
- package/src/math/v2/circulating.ts +69 -0
- package/src/math/v2/create.ts +139 -0
- package/src/math/v2/display.ts +250 -0
- package/src/math/v2/error.ts +19 -0
- package/src/math/v2/feasibility.ts +200 -0
- package/src/math/v2/fees.ts +121 -0
- package/src/math/v2/holders.ts +124 -0
- package/src/math/v2/milestones.ts +51 -0
- package/src/math/v2/pow.ts +71 -0
- package/src/math/v2/preset.ts +189 -0
- package/src/math/v2/receipt.ts +162 -0
- package/src/math/v2/relay.ts +235 -0
- package/src/math/v2/rows.ts +412 -0
- package/src/math/v2/split.ts +160 -0
- package/src/math/v2/sqrt.ts +86 -0
- package/src/math/v2/supply.ts +27 -0
- package/src/math/v2/team.ts +75 -0
- package/src/math/v2/trade.ts +145 -0
- package/src/math/v2/wide.ts +52 -0
- package/src/quote.ts +6 -0
- package/src/registryPrice.ts +28 -0
- package/src/route/edge-jupiter.ts +71 -0
- package/src/route/edge.ts +22 -0
- package/src/route/graph.ts +77 -0
- package/src/route/path.ts +84 -0
- package/src/route/quote.ts +46 -0
- package/src/route-jupiter.ts +36 -0
- package/src/route.ts +8 -0
- package/src/sha256.ts +61 -0
- package/src/solana/alt.ts +25 -0
- package/src/solana/floor/accounts.ts +393 -0
- package/src/solana/floor/borsh.ts +66 -0
- package/src/solana/floor/create.ts +89 -0
- package/src/solana/floor/events.ts +147 -0
- package/src/solana/floor/genesis.ts +111 -0
- package/src/solana/floor/genesisList.ts +75 -0
- package/src/solana/floor/instructions.ts +491 -0
- package/src/solana/floor/metadata.ts +246 -0
- package/src/solana/floor/mint.ts +242 -0
- package/src/solana/floor/program.ts +64 -0
- package/src/solana/floor/registry.ts +96 -0
- package/src/solana/instruction.ts +61 -0
- package/src/solana/pda.ts +91 -0
- package/src/solana/tx.ts +143 -0
- package/src/v2/burnPreview.ts +52 -0
- package/src/v2/circulating.ts +22 -0
- package/src/v2/claimAllEvm.ts +97 -0
- package/src/v2/claims-evm.ts +179 -0
- package/src/v2/claims.ts +288 -0
- package/src/v2/dead.ts +21 -0
- package/src/v2/devbuy.ts +55 -0
- package/src/v2/events.ts +131 -0
- package/src/v2/evm.ts +349 -0
- package/src/v2/evmReads.ts +121 -0
- package/src/v2/genesis.ts +80 -0
- package/src/v2/layouts.ts +219 -0
- package/src/v2/market-evm.ts +95 -0
- package/src/v2/presetRowEvm.ts +54 -0
- package/src/v2/press.ts +124 -0
- package/src/v2/quoteMint.ts +63 -0
- package/src/v2/receipt-tx.ts +251 -0
- package/src/v2/route-alt.ts +114 -0
- package/src/v2/route.ts +494 -0
- package/src/v2/solana.ts +439 -0
- package/src/v2/solanaSettle.ts +117 -0
- package/src/v2/tx/evm.ts +152 -0
- package/src/v2/tx/genesis.ts +66 -0
- package/src/v2/tx/index.ts +5 -0
- package/src/v2/tx/solana.ts +140 -0
- package/src/v2/types.ts +284 -0
- package/dist/adapter.d.ts +0 -28
- package/dist/adapter.d.ts.map +0 -1
- package/dist/adapter.js +0 -2
- package/dist/adapter.js.map +0 -1
- package/dist/chain.d.ts +0 -23
- package/dist/chain.d.ts.map +0 -1
- package/dist/chain.js +0 -25
- package/dist/chain.js.map +0 -1
- package/dist/client.d.ts +0 -26
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -43
- package/dist/client.js.map +0 -1
- package/dist/dex-adapter.d.ts +0 -228
- package/dist/dex-adapter.d.ts.map +0 -1
- package/dist/dex-adapter.js +0 -407
- package/dist/dex-adapter.js.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/quotes.d.ts +0 -12
- package/dist/quotes.d.ts.map +0 -1
- package/dist/quotes.gen.d.ts +0 -8
- package/dist/quotes.gen.d.ts.map +0 -1
- package/dist/quotes.gen.js +0 -2170
- package/dist/quotes.gen.js.map +0 -1
- package/dist/quotes.js +0 -24
- package/dist/quotes.js.map +0 -1
- package/dist/types.d.ts +0 -111
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/src/adapter.ts +0 -20
- package/src/chain.ts +0 -39
- package/src/client.ts +0 -41
- package/src/dex-adapter.ts +0 -477
- package/src/quotes.gen.ts +0 -2170
- package/src/quotes.ts +0 -29
- package/src/types.ts +0 -115
|
@@ -0,0 +1,607 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { fail } from "../index.js";
|
|
4
|
+
import { FeeAcc, ratesAt } from "./fees.js";
|
|
5
|
+
import { DOWN, UP, U64_MAX, U128_MAX } from "./wide.js";
|
|
6
|
+
import {
|
|
7
|
+
SQRT_TAIL_END, flatMoneyForTokens, flatTokensForMoney, liquidityForMoney, liquidityForTokens, moneyBetween,
|
|
8
|
+
sqrtAfterMoney, sqrtAfterTokens, tokensBetween,
|
|
9
|
+
} from "./sqrt.js";
|
|
10
|
+
|
|
11
|
+
export const RANGES_MAX = 48;
|
|
12
|
+
|
|
13
|
+
export const BIDS_MAX = 24;
|
|
14
|
+
export const MAX_WALK_STEPS = 24;
|
|
15
|
+
|
|
16
|
+
export const PIECES_MAX = MAX_WALK_STEPS * 2;
|
|
17
|
+
|
|
18
|
+
export const SOL_BUY_WALK_STEPS = 16;
|
|
19
|
+
|
|
20
|
+
export const KIND_BONDING = 0;
|
|
21
|
+
export const KIND_CLIMB = 1;
|
|
22
|
+
export const KIND_SALE_BAND = 2;
|
|
23
|
+
|
|
24
|
+
export const KIND_RETIRED_3 = 3;
|
|
25
|
+
export const KIND_UNUSED = 4;
|
|
26
|
+
export const KIND_TAIL = 5;
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
const ov = (x ) => (x === null ? fail("Overflow") : x);
|
|
35
|
+
const u64 = (x ) => (x < 0n || x > U64_MAX ? fail("Overflow") : x);
|
|
36
|
+
const u128 = (x ) => (x < 0n || x > U128_MAX ? fail("Overflow") : x);
|
|
37
|
+
|
|
38
|
+
export const UNDO_MAX = MAX_WALK_STEPS + 1;
|
|
39
|
+
|
|
40
|
+
export class Undo {
|
|
41
|
+
seen = new Set ();
|
|
42
|
+
|
|
43
|
+
bought = [];
|
|
44
|
+
touch(i ) {
|
|
45
|
+
if (this.seen.has(i)) return;
|
|
46
|
+
if (this.seen.size === UNDO_MAX) fail("StepBudget");
|
|
47
|
+
this.seen.add(i);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
buyFirst(i , lo , hi , money , tokens ) {
|
|
51
|
+
const n = this.bought.length;
|
|
52
|
+
if (n > 0 && this.bought[n - 1].range === i) {
|
|
53
|
+
const p = this.bought[n - 1];
|
|
54
|
+
p.sqrtHi = hi;
|
|
55
|
+
p.money = u128(p.money + money);
|
|
56
|
+
p.tokens = u64(p.tokens + tokens);
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
if (n === PIECES_MAX) fail("StepBudget");
|
|
60
|
+
this.bought.push({ sqrtLo: lo, sqrtHi: hi, money, tokens, range: i });
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const min = (a , b ) => (a < b ? a : b);
|
|
64
|
+
const max = (a , b ) => (a > b ? a : b);
|
|
65
|
+
|
|
66
|
+
export const emptyRange = () => ({ sqrtA: 0n, sqrtB: 0n, tokens: 0n, askLeft: 0n, back: 0n, place: 0n, thetaBps: 0, psiBps: 0, kind: 0, class: 0, msCapPpm: 0 });
|
|
67
|
+
export const rangeIsFlat = (r ) => r.sqrtA === r.sqrtB;
|
|
68
|
+
export const rangeLiquidity = (r ) => liquidityForTokens(r.sqrtA, r.sqrtB, r.tokens);
|
|
69
|
+
export const rangeHeld = (r ) => r.askLeft + r.back;
|
|
70
|
+
|
|
71
|
+
export function emptyBook(sqrtP , feePpm ) {
|
|
72
|
+
return {
|
|
73
|
+
sqrtP, sqrtF: 0n, sqrtLMax: 0n, floor: 0n, floorTokens: 0n, nBands: 0, nC: 0,
|
|
74
|
+
bandEdge: new Array(BIDS_MAX + 1).fill(0n), bandMoney: new Array(BIDS_MAX).fill(0n), bandTokens: new Array(BIDS_MAX).fill(0n),
|
|
75
|
+
bandF: new Array(BIDS_MAX).fill(0n), bandFClaim: new Array(BIDS_MAX).fill(0n), ckAtF: 0n, ckFloorTokens: 0n, feePpm: [...feePpm], feeStartPpm: [0, 0, 0, 0, 0], sqrtFeeA: 0n, sqrtFeeB: 0n, graduated: false, nRanges: 0, ranges: Array.from({ length: RANGES_MAX }, emptyRange),
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const feesNow = (b ) => feesAt(b, b.sqrtP);
|
|
80
|
+
|
|
81
|
+
export const feesAt = (b , sqrtP ) => ratesAt(b.graduated, b.ownFees === true, b.feePpm, b.feeStartPpm, b.sqrtFeeA, b.sqrtFeeB, sqrtP);
|
|
82
|
+
|
|
83
|
+
function charge(b , acc , q ) {
|
|
84
|
+
if (q > 0n && !acc.add(q, feesAt(b, b.sqrtP))) fail("Overflow");
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function cloneBook(b ) {
|
|
88
|
+
return { ...b, bandEdge: [...b.bandEdge], bandMoney: [...b.bandMoney], bandTokens: [...b.bandTokens], bandF: [...b.bandF], bandFClaim: [...b.bandFClaim], feePpm: [...b.feePpm], feeStartPpm: [...b.feeStartPpm], ranges: b.ranges.map((r) => ({ ...r })) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function pushRange(b , sqrtA , sqrtB , tokens , kind , cls ) {
|
|
92
|
+
const n = b.nRanges;
|
|
93
|
+
if (n === RANGES_MAX) fail("RowTooManyRanges");
|
|
94
|
+
if (sqrtA === 0n || sqrtA > sqrtB || sqrtB > SQRT_TAIL_END || (n > 0 && b.ranges[n - 1].sqrtB !== sqrtA)) fail("SqrtRange");
|
|
95
|
+
b.ranges[n] = { ...emptyRange(), sqrtA, sqrtB, tokens: u64(tokens), askLeft: tokens, kind, class: cls };
|
|
96
|
+
b.nRanges++;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function cpUp(l , p , hi , rem , held , allAtTop ) {
|
|
100
|
+
if (held === 0n || p >= hi || l === 0n) return [0n, 0n, p];
|
|
101
|
+
|
|
102
|
+
const qFull = moneyBetween(l, p, hi, UP);
|
|
103
|
+
let p2 , q ;
|
|
104
|
+
if (qFull !== null && rem >= qFull) [p2, q] = [hi, qFull];
|
|
105
|
+
else {
|
|
106
|
+
p2 = min(ov(sqrtAfterMoney(l, p, rem)), hi);
|
|
107
|
+
q = ov(moneyBetween(l, p, p2, UP));
|
|
108
|
+
}
|
|
109
|
+
if (p2 <= p) return [0n, 0n, p];
|
|
110
|
+
let dt = min(ov(tokensBetween(l, p, p2, DOWN)), held);
|
|
111
|
+
if (allAtTop && p2 === hi) dt = held;
|
|
112
|
+
if (dt === 0n) return [0n, 0n, p];
|
|
113
|
+
return [dt, q, p2];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function cpDown(l , lo , p , rem , held ) {
|
|
117
|
+
if (held === 0n || p <= lo || l === 0n) return [0n, 0n, p];
|
|
118
|
+
const xFull = ov(tokensBetween(l, lo, p, UP));
|
|
119
|
+
let p2 , dx ;
|
|
120
|
+
if (rem >= xFull) [p2, dx] = [lo, xFull & U64_MAX];
|
|
121
|
+
else {
|
|
122
|
+
p2 = max(ov(sqrtAfterTokens(l, p, rem)), lo);
|
|
123
|
+
if (p2 >= p) return [0n, 0n, p];
|
|
124
|
+
dx = min(ov(tokensBetween(l, p2, p, UP)), rem);
|
|
125
|
+
}
|
|
126
|
+
const q = min(ov(moneyBetween(l, p2, p, DOWN)), held);
|
|
127
|
+
if (q === 0n) return [0n, 0n, p];
|
|
128
|
+
return [dx, q, p2];
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function flatCanBuy(s , money ) {
|
|
132
|
+
if (money === 0n) return false;
|
|
133
|
+
const t = flatTokensForMoney(s, money, DOWN);
|
|
134
|
+
if (t === null || t < 1n) return false;
|
|
135
|
+
const q = flatMoneyForTokens(s, t, DOWN);
|
|
136
|
+
return q !== null && q > 0n;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const rangeLo = (b , r ) => max(r.sqrtA, b.sqrtLMax);
|
|
140
|
+
|
|
141
|
+
function cpEdges(b , pc ) {
|
|
142
|
+
switch (pc.t) {
|
|
143
|
+
case "range": return [rangeLo(b, b.ranges[pc.i]), b.ranges[pc.i].sqrtB];
|
|
144
|
+
case "band": return [b.bandEdge[pc.i + 1], b.bandEdge[pc.i]];
|
|
145
|
+
case "floor": return [b.sqrtF, b.sqrtF];
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function tokensOf(b , pc ) {
|
|
149
|
+
switch (pc.t) {
|
|
150
|
+
case "range": return rangeHeld(b.ranges[pc.i]);
|
|
151
|
+
case "band": return b.bandTokens[pc.i];
|
|
152
|
+
case "floor": return b.floorTokens;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function moneyOf(b , pc ) {
|
|
156
|
+
switch (pc.t) {
|
|
157
|
+
case "range": return b.ranges[pc.i].place;
|
|
158
|
+
case "band": return b.bandMoney[pc.i];
|
|
159
|
+
case "floor": return b.floor;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const isFlat = (b , pc ) => (pc.t === "range" ? rangeIsFlat(b.ranges[pc.i]) : pc.t === "floor");
|
|
163
|
+
|
|
164
|
+
function flatBids(b , i ) {
|
|
165
|
+
const r = b.ranges[i];
|
|
166
|
+
return rangeIsFlat(r) && flatCanBuy(r.sqrtA, r.place);
|
|
167
|
+
}
|
|
168
|
+
const floorBids = (b ) => b.sqrtF !== 0n && flatCanBuy(b.sqrtF, b.floor);
|
|
169
|
+
const flatS = (b , pc ) => (pc.t === "range" ? b.ranges[pc.i].sqrtA : b.sqrtF);
|
|
170
|
+
|
|
171
|
+
function pieces(b ) {
|
|
172
|
+
const out = [];
|
|
173
|
+
for (let i = 0; i < b.nRanges; i++) out.push({ t: "range", i });
|
|
174
|
+
for (let i = 0; i < b.nBands; i++) out.push({ t: "band", i });
|
|
175
|
+
if (b.sqrtF !== 0n) out.push({ t: "floor" });
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const sum = (xs , n ) => {
|
|
180
|
+
let s = 0n;
|
|
181
|
+
for (let i = 0; i < n; i++) s += xs[i];
|
|
182
|
+
return s;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export function moneyHeld(b ) {
|
|
186
|
+
let t = bidMoneySum(b);
|
|
187
|
+
for (let i = 0; i < b.nRanges; i++) t += b.ranges[i].place;
|
|
188
|
+
return t;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
export const fOwn = (b ) => b.floor - b.ckAtF + sum(b.bandF, b.nBands);
|
|
192
|
+
|
|
193
|
+
export const fClaims = (b ) => sum(b.bandFClaim, b.nBands);
|
|
194
|
+
|
|
195
|
+
export const ckMoney = (b ) => sum(b.bandMoney, b.nBands) - sum(b.bandF, b.nBands) + b.ckAtF;
|
|
196
|
+
|
|
197
|
+
export const ckHeld = (b ) => sum(b.bandTokens, b.nBands) + b.ckFloorTokens;
|
|
198
|
+
|
|
199
|
+
const bidMoneySum = (b ) => b.floor + sum(b.bandMoney, b.nBands);
|
|
200
|
+
|
|
201
|
+
export const placeMoney = (b ) => {
|
|
202
|
+
let t = 0n;
|
|
203
|
+
for (let i = 0; i < b.nRanges; i++) t += b.ranges[i].place;
|
|
204
|
+
return t;
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export function bidMoneyOf(b ) {
|
|
208
|
+
const s = bidMoneySum(b);
|
|
209
|
+
if (s > U128_MAX) throw new RangeError("bid money past u128");
|
|
210
|
+
return s;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export function vaultV2(b ) {
|
|
214
|
+
const s = fOwn(b) + placeMoney(b);
|
|
215
|
+
if (s > U128_MAX) throw new RangeError("bid money past u128");
|
|
216
|
+
return s;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export function sqrtT0(b ) {
|
|
220
|
+
for (let i = b.nRanges - 1; i >= 0; i--) if (b.ranges[i].kind === KIND_TAIL) return b.ranges[i].sqrtA;
|
|
221
|
+
return SQRT_TAIL_END;
|
|
222
|
+
}
|
|
223
|
+
export function tokensHeld(b ) {
|
|
224
|
+
let t = b.floorTokens;
|
|
225
|
+
for (let i = 0; i < b.nBands; i++) t += b.bandTokens[i];
|
|
226
|
+
for (let i = 0; i < b.nRanges; i++) t += rangeHeld(b.ranges[i]);
|
|
227
|
+
return t;
|
|
228
|
+
}
|
|
229
|
+
export function forSale(b ) {
|
|
230
|
+
let t = 0n;
|
|
231
|
+
for (let i = 0; i < b.nRanges; i++) t += b.ranges[i].askLeft;
|
|
232
|
+
return u64(t);
|
|
233
|
+
}
|
|
234
|
+
export function boughtBack(b ) {
|
|
235
|
+
let t = b.floorTokens;
|
|
236
|
+
for (let i = 0; i < b.nRanges; i++) t += b.ranges[i].back;
|
|
237
|
+
for (let i = 0; i < b.nBands; i++) t += b.bandTokens[i];
|
|
238
|
+
return u64(t);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
export function checkBook(b ) {
|
|
242
|
+
const n = b.nRanges;
|
|
243
|
+
if (n === 0 || n > RANGES_MAX || b.nBands > BIDS_MAX || b.nC > b.nBands) fail("BookEmpty");
|
|
244
|
+
if (b.sqrtP === 0n || b.sqrtP > SQRT_TAIL_END) fail("SqrtRange");
|
|
245
|
+
for (let i = 0; i < n; i++) {
|
|
246
|
+
const r = b.ranges[i];
|
|
247
|
+
if (r.sqrtA === 0n || r.sqrtA > r.sqrtB || (i > 0 && b.ranges[i - 1].sqrtB !== r.sqrtA)) fail("SqrtRange");
|
|
248
|
+
if (r.askLeft + r.back > U64_MAX) fail("Overflow");
|
|
249
|
+
if (r.kind !== KIND_BONDING && (r.place !== 0n || r.back !== 0n)) fail("Overflow");
|
|
250
|
+
if (r.kind === KIND_BONDING && i !== 0) fail("SqrtRange");
|
|
251
|
+
}
|
|
252
|
+
if (b.ranges[n - 1].sqrtB !== SQRT_TAIL_END || rangeIsFlat(b.ranges[n - 1])) fail("SqrtRange");
|
|
253
|
+
const nb = b.nBands;
|
|
254
|
+
if (nb > 0) {
|
|
255
|
+
if (b.bandEdge[0] > b.sqrtLMax || b.bandEdge[nb] < b.sqrtF) fail("SqrtRange");
|
|
256
|
+
for (let i = 0; i < nb; i++) {
|
|
257
|
+
if (b.bandEdge[i + 1] > b.bandEdge[i]) fail("SqrtRange");
|
|
258
|
+
if (b.bandF[i] > b.bandMoney[i]) fail("Overflow");
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
if (b.ckAtF > b.floor || b.ckFloorTokens > b.floorTokens) fail("Overflow");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function flatUp(b ) {
|
|
265
|
+
const p = b.sqrtP;
|
|
266
|
+
if (b.sqrtF !== 0n && b.sqrtF === p && b.floorTokens > 0n) return { t: "floor" };
|
|
267
|
+
for (let i = 0; i < b.nRanges; i++) {
|
|
268
|
+
const r = b.ranges[i];
|
|
269
|
+
if (rangeIsFlat(r) && r.sqrtA === p && rangeHeld(r) > 0n) return { t: "range", i };
|
|
270
|
+
}
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
function flatDown(b ) {
|
|
274
|
+
const p = b.sqrtP;
|
|
275
|
+
for (let i = b.nRanges - 1; i >= 0; i--) if (b.ranges[i].sqrtA === p && flatBids(b, i)) return { t: "range", i };
|
|
276
|
+
if (b.sqrtF === p && floorBids(b)) return { t: "floor" };
|
|
277
|
+
return null;
|
|
278
|
+
}
|
|
279
|
+
function cpPieceUp(b ) {
|
|
280
|
+
const p = b.sqrtP;
|
|
281
|
+
for (const pc of pieces(b)) {
|
|
282
|
+
const [lo, hi] = cpEdges(b, pc);
|
|
283
|
+
if (!isFlat(b, pc) && lo < hi && lo <= p && p < hi && tokensOf(b, pc) > 0n) return pc;
|
|
284
|
+
}
|
|
285
|
+
return null;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function bandDown(b ) {
|
|
289
|
+
const p = b.sqrtP;
|
|
290
|
+
for (let j = 0; j < b.nBands; j++) {
|
|
291
|
+
const [lo, hi] = [b.bandEdge[j + 1], b.bandEdge[j]];
|
|
292
|
+
if (lo < hi && lo < p && p <= hi && b.bandMoney[j] > 0n) return j;
|
|
293
|
+
}
|
|
294
|
+
return null;
|
|
295
|
+
}
|
|
296
|
+
function nextUp(b ) {
|
|
297
|
+
let best = null;
|
|
298
|
+
for (const pc of pieces(b)) {
|
|
299
|
+
if (tokensOf(b, pc) === 0n) continue;
|
|
300
|
+
let s ;
|
|
301
|
+
if (isFlat(b, pc)) s = flatS(b, pc);
|
|
302
|
+
else {
|
|
303
|
+
const [lo, hi] = cpEdges(b, pc);
|
|
304
|
+
if (!(lo < hi)) continue;
|
|
305
|
+
s = lo;
|
|
306
|
+
}
|
|
307
|
+
if (s > b.sqrtP && (best === null || s < best)) best = s;
|
|
308
|
+
}
|
|
309
|
+
return best;
|
|
310
|
+
}
|
|
311
|
+
function nextDown(b ) {
|
|
312
|
+
let best = null;
|
|
313
|
+
for (const pc of pieces(b)) {
|
|
314
|
+
let s ;
|
|
315
|
+
if (pc.t === "range" && rangeIsFlat(b.ranges[pc.i])) {
|
|
316
|
+
if (!flatBids(b, pc.i)) continue;
|
|
317
|
+
s = b.ranges[pc.i].sqrtA;
|
|
318
|
+
} else if (pc.t === "floor") {
|
|
319
|
+
if (!floorBids(b)) continue;
|
|
320
|
+
s = b.sqrtF;
|
|
321
|
+
} else if (pc.t === "band") {
|
|
322
|
+
const [lo, hi] = cpEdges(b, pc);
|
|
323
|
+
if (!(lo < hi && b.bandMoney[pc.i] > 0n)) continue;
|
|
324
|
+
s = hi;
|
|
325
|
+
} else continue;
|
|
326
|
+
if (s < b.sqrtP && (best === null || s > best)) best = s;
|
|
327
|
+
}
|
|
328
|
+
return best;
|
|
329
|
+
}
|
|
330
|
+
const lUp = (b , pc , hi ) => (pc.t === "range" ? ov(rangeLiquidity(b.ranges[pc.i])) : ov(liquidityForTokens(b.sqrtP, hi, tokensOf(b, pc))));
|
|
331
|
+
|
|
332
|
+
function graduate(b ) {
|
|
333
|
+
const r = b.ranges[0];
|
|
334
|
+
b.floor = u128(b.floor + r.place);
|
|
335
|
+
r.place = 0n;
|
|
336
|
+
b.sqrtF = r.sqrtA;
|
|
337
|
+
b.sqrtLMax = max(b.sqrtLMax, r.sqrtA);
|
|
338
|
+
b.graduated = true;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function floorRebuy(b , take , cost ) {
|
|
342
|
+
const [x0, xe] = [b.floorTokens, b.ckFloorTokens];
|
|
343
|
+
if (xe > 0n) {
|
|
344
|
+
const back = (cost * xe) / x0;
|
|
345
|
+
const gone = min((take * xe + x0 - 1n) / x0, xe);
|
|
346
|
+
b.ckAtF = u128(b.ckAtF + back);
|
|
347
|
+
b.ckFloorTokens -= gone;
|
|
348
|
+
}
|
|
349
|
+
b.floorTokens -= take;
|
|
350
|
+
b.floor = u128(b.floor + cost);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
function bandRebuy(b , j , dt , q ) {
|
|
354
|
+
const claim = b.bandFClaim[j];
|
|
355
|
+
const t0 = b.bandTokens[j];
|
|
356
|
+
const r = min(min((claim * dt + t0 - 1n) / t0, claim), q);
|
|
357
|
+
b.bandTokens[j] -= dt;
|
|
358
|
+
b.bandMoney[j] = u128(b.bandMoney[j] + q);
|
|
359
|
+
b.bandF[j] += r;
|
|
360
|
+
b.bandFClaim[j] -= r;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
function rangeUp(b , i , l , hi , st , w , acc , u ) {
|
|
364
|
+
const p = b.sqrtP;
|
|
365
|
+
const [dt, q, p2] = cpUp(l, p, hi, st.rem, b.ranges[i].askLeft, false);
|
|
366
|
+
if (dt === 0n) return false;
|
|
367
|
+
b.ranges[i].askLeft -= dt;
|
|
368
|
+
b.sqrtP = p2;
|
|
369
|
+
b.sqrtLMax = max(b.sqrtLMax, p2);
|
|
370
|
+
u.buyFirst(i, p, p2, q, dt);
|
|
371
|
+
st.rem -= q;
|
|
372
|
+
w.money += q; w.tokens += dt; w.fromFirst += dt;
|
|
373
|
+
charge(b, acc, q);
|
|
374
|
+
return true;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
export function nextAskSqrt(b ) {
|
|
378
|
+
const c = { ...b };
|
|
379
|
+
for (let i = 0; i <= 2 * (RANGES_MAX + BIDS_MAX + 2); i++) {
|
|
380
|
+
if (flatUp(c)) return c.sqrtP;
|
|
381
|
+
const pc = cpPieceUp(c);
|
|
382
|
+
if (pc) {
|
|
383
|
+
const [, hi] = cpEdges(c, pc);
|
|
384
|
+
const l = pc.t === "range" ? rangeLiquidity(c.ranges[pc.i]) : liquidityForTokens(c.sqrtP, hi, tokensOf(c, pc));
|
|
385
|
+
const t = l === null ? null : tokensBetween(l, c.sqrtP, hi, DOWN);
|
|
386
|
+
if (t !== 0n) return c.sqrtP;
|
|
387
|
+
c.sqrtP = hi;
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
const s = nextUp(c);
|
|
391
|
+
if (s === null) return null;
|
|
392
|
+
c.sqrtP = s;
|
|
393
|
+
}
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
export function nextBidSqrt(b ) {
|
|
398
|
+
const c = { ...b };
|
|
399
|
+
for (let i = 0; i <= 2 * (RANGES_MAX + BIDS_MAX + 2); i++) {
|
|
400
|
+
if (flatDown(c)) return c.sqrtP;
|
|
401
|
+
const j = bandDown(c);
|
|
402
|
+
if (j !== null) {
|
|
403
|
+
const lo = c.bandEdge[j + 1];
|
|
404
|
+
const l = liquidityForMoney(lo, c.sqrtP, c.bandMoney[j]);
|
|
405
|
+
const q = l === null ? null : moneyBetween(l, lo, c.sqrtP, DOWN);
|
|
406
|
+
if (q !== 0n) return c.sqrtP;
|
|
407
|
+
}
|
|
408
|
+
const s = nextDown(c);
|
|
409
|
+
if (s === null) return null;
|
|
410
|
+
c.sqrtP = s;
|
|
411
|
+
}
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
export function walkUp(b , budget ) {
|
|
416
|
+
return walkUpFrom(b, budget, { money: 0n, tokens: 0n, fromResale: 0n, fromFirst: 0n, steps: 0, budgetHit: false, graduatedNow: false }, new FeeAcc(feesNow(b)));
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
export function walkUpFrom(b , budget , w0 , acc , maxSteps = MAX_WALK_STEPS, u = new Undo()) {
|
|
420
|
+
const st = { rem: budget };
|
|
421
|
+
const w = { ...w0, budgetHit: false };
|
|
422
|
+
while (st.rem > 0n) {
|
|
423
|
+
if (w.steps >= Math.min(maxSteps, MAX_WALK_STEPS)) {
|
|
424
|
+
w.budgetHit = true;
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
w.steps++;
|
|
428
|
+
const fp = flatUp(b);
|
|
429
|
+
if (fp) {
|
|
430
|
+
const s = flatS(b, fp);
|
|
431
|
+
const can = ov(flatTokensForMoney(s, st.rem, DOWN));
|
|
432
|
+
const take = min(can, tokensOf(b, fp));
|
|
433
|
+
if (take === 0n) break;
|
|
434
|
+
const cost = ov(flatMoneyForTokens(s, take, UP));
|
|
435
|
+
st.rem -= cost;
|
|
436
|
+
w.money += cost; w.tokens += take;
|
|
437
|
+
charge(b, acc, cost);
|
|
438
|
+
if (fp.t === "floor") {
|
|
439
|
+
floorRebuy(b, take, cost);
|
|
440
|
+
w.fromResale += take;
|
|
441
|
+
} else {
|
|
442
|
+
u.touch(fp.i);
|
|
443
|
+
const r = b.ranges[fp.i];
|
|
444
|
+
const bonding = r.kind === KIND_BONDING;
|
|
445
|
+
const tb = min(take, r.back);
|
|
446
|
+
r.back -= tb;
|
|
447
|
+
r.askLeft -= take - tb;
|
|
448
|
+
w.fromResale += tb;
|
|
449
|
+
w.fromFirst += take - tb;
|
|
450
|
+
if (bonding) {
|
|
451
|
+
r.place = u128(r.place + cost);
|
|
452
|
+
if (rangeHeld(r) === 0n && !b.graduated) {
|
|
453
|
+
graduate(b);
|
|
454
|
+
w.graduatedNow = true;
|
|
455
|
+
}
|
|
456
|
+
} else {
|
|
457
|
+
|
|
458
|
+
b.sqrtLMax = max(b.sqrtLMax, s);
|
|
459
|
+
u.buyFirst(fp.i, s, s, cost, take);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
if (tokensOf(b, fp) > 0n) break;
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
const pc = cpPieceUp(b);
|
|
466
|
+
if (pc) {
|
|
467
|
+
const [, hi] = cpEdges(b, pc);
|
|
468
|
+
const l = lUp(b, pc, hi);
|
|
469
|
+
const p = b.sqrtP;
|
|
470
|
+
let moved ;
|
|
471
|
+
if (pc.t === "range") {
|
|
472
|
+
u.touch(pc.i);
|
|
473
|
+
moved = rangeUp(b, pc.i, l, hi, st, w, acc, u);
|
|
474
|
+
} else if (pc.t === "band") {
|
|
475
|
+
const [dt, q, p2] = cpUp(l, p, hi, st.rem, b.bandTokens[pc.i], true);
|
|
476
|
+
if (dt > 0n) {
|
|
477
|
+
bandRebuy(b, pc.i, dt, q);
|
|
478
|
+
b.sqrtP = p2;
|
|
479
|
+
st.rem -= q;
|
|
480
|
+
w.money += q; w.tokens += dt; w.fromResale += dt;
|
|
481
|
+
charge(b, acc, q);
|
|
482
|
+
}
|
|
483
|
+
moved = dt > 0n;
|
|
484
|
+
} else throw new Error("unreachable");
|
|
485
|
+
if (b.sqrtP === hi) continue;
|
|
486
|
+
if (!moved && st.rem > 0n && ov(tokensBetween(l, b.sqrtP, hi, DOWN)) === 0n) {
|
|
487
|
+
|
|
488
|
+
if (pc.t === "range") {
|
|
489
|
+
const q = ov(moneyBetween(l, b.sqrtP, hi, UP));
|
|
490
|
+
if (q > st.rem) break;
|
|
491
|
+
u.buyFirst(pc.i, b.sqrtP, hi, q, 0n);
|
|
492
|
+
st.rem -= q;
|
|
493
|
+
w.money += q;
|
|
494
|
+
charge(b, acc, q);
|
|
495
|
+
b.sqrtLMax = max(b.sqrtLMax, hi);
|
|
496
|
+
}
|
|
497
|
+
b.sqrtP = hi;
|
|
498
|
+
continue;
|
|
499
|
+
}
|
|
500
|
+
if (!moved || st.rem === 0n || tokensOf(b, pc) > 0n) break;
|
|
501
|
+
continue;
|
|
502
|
+
}
|
|
503
|
+
const s = nextUp(b);
|
|
504
|
+
if (s === null) break;
|
|
505
|
+
b.sqrtP = s;
|
|
506
|
+
}
|
|
507
|
+
w.tokens = u64(w.tokens);
|
|
508
|
+
return w;
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
export function walkDown(b , tokens ) {
|
|
512
|
+
return walkDownAcc(b, tokens, new FeeAcc(feesNow(b)));
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
export function walkDownAcc(b , tokens , acc , u = new Undo()) {
|
|
516
|
+
let rem = tokens;
|
|
517
|
+
const w = { tokens: 0n, money: 0n, fromF: 0n, fromK: 0n, fromC: 0n, steps: 0, budgetHit: false };
|
|
518
|
+
while (rem > 0n) {
|
|
519
|
+
if (w.steps === MAX_WALK_STEPS) {
|
|
520
|
+
w.budgetHit = true;
|
|
521
|
+
break;
|
|
522
|
+
}
|
|
523
|
+
w.steps++;
|
|
524
|
+
const fp = flatDown(b);
|
|
525
|
+
if (fp) {
|
|
526
|
+
const s = flatS(b, fp);
|
|
527
|
+
const money = moneyOf(b, fp);
|
|
528
|
+
const can = ov(flatTokensForMoney(s, money, DOWN));
|
|
529
|
+
const take = min(can, rem);
|
|
530
|
+
const q = take === 0n ? 0n : min(ov(flatMoneyForTokens(s, take, DOWN)), money);
|
|
531
|
+
if (q === 0n) break;
|
|
532
|
+
rem -= take;
|
|
533
|
+
w.tokens += take; w.money += q;
|
|
534
|
+
charge(b, acc, q);
|
|
535
|
+
if (fp.t === "floor") {
|
|
536
|
+
|
|
537
|
+
const ce = min(q, b.ckAtF);
|
|
538
|
+
if (ce > 0n) {
|
|
539
|
+
const xe = min(ov(flatTokensForMoney(s, ce, DOWN)), take);
|
|
540
|
+
b.ckAtF -= ce;
|
|
541
|
+
b.ckFloorTokens = u64(b.ckFloorTokens + xe);
|
|
542
|
+
}
|
|
543
|
+
b.floor -= q;
|
|
544
|
+
b.floorTokens = u64(b.floorTokens + take);
|
|
545
|
+
w.fromF += q;
|
|
546
|
+
} else {
|
|
547
|
+
u.touch(fp.i);
|
|
548
|
+
const r = b.ranges[fp.i];
|
|
549
|
+
r.place -= q;
|
|
550
|
+
r.back = u64(r.back + take);
|
|
551
|
+
}
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
const j = bandDown(b);
|
|
555
|
+
if (j !== null) {
|
|
556
|
+
const lo = b.bandEdge[j + 1];
|
|
557
|
+
const l = ov(liquidityForMoney(lo, b.sqrtP, b.bandMoney[j]));
|
|
558
|
+
const [dx, q0, p2] = cpDown(l, lo, b.sqrtP, rem, b.bandMoney[j]);
|
|
559
|
+
let q = q0;
|
|
560
|
+
if (dx === 0n) {
|
|
561
|
+
|
|
562
|
+
if (ov(moneyBetween(l, lo, b.sqrtP, DOWN)) === 0n) {
|
|
563
|
+
const s = nextDown(b);
|
|
564
|
+
if (s === null) break;
|
|
565
|
+
b.sqrtP = s;
|
|
566
|
+
continue;
|
|
567
|
+
}
|
|
568
|
+
break;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const dF = min(min(ov(flatMoneyForTokens(b.sqrtF, dx, DOWN)), b.bandF[j]), q);
|
|
572
|
+
|
|
573
|
+
q = min(q, b.bandMoney[j] - b.bandF[j] + dF);
|
|
574
|
+
b.bandF[j] -= dF;
|
|
575
|
+
b.bandFClaim[j] = u128(b.bandFClaim[j] + dF);
|
|
576
|
+
b.bandMoney[j] -= q;
|
|
577
|
+
b.bandTokens[j] = u64(b.bandTokens[j] + dx);
|
|
578
|
+
if (j < b.nC) w.fromC += q;
|
|
579
|
+
else w.fromK += q;
|
|
580
|
+
b.sqrtP = p2;
|
|
581
|
+
rem -= dx;
|
|
582
|
+
w.tokens += dx; w.money += q;
|
|
583
|
+
charge(b, acc, q);
|
|
584
|
+
if (p2 === lo && b.bandMoney[j] > 0n) {
|
|
585
|
+
|
|
586
|
+
b.floor = u128(b.floor + b.bandMoney[j]);
|
|
587
|
+
b.ckAtF = u128(b.ckAtF + b.bandMoney[j] - b.bandF[j]);
|
|
588
|
+
b.bandMoney[j] = 0n;
|
|
589
|
+
b.bandF[j] = 0n;
|
|
590
|
+
}
|
|
591
|
+
if (p2 === lo || b.bandMoney[j] === 0n) {
|
|
592
|
+
if (p2 !== lo) {
|
|
593
|
+
|
|
594
|
+
const s = nextDown(b);
|
|
595
|
+
if (s === null) break;
|
|
596
|
+
b.sqrtP = s;
|
|
597
|
+
}
|
|
598
|
+
continue;
|
|
599
|
+
}
|
|
600
|
+
break;
|
|
601
|
+
}
|
|
602
|
+
const s = nextDown(b);
|
|
603
|
+
if (s === null) break;
|
|
604
|
+
b.sqrtP = s;
|
|
605
|
+
}
|
|
606
|
+
return w;
|
|
607
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { U128_MAX } from "./wide.js";
|
|
4
|
+
import { v2Fail } from "./error.js";
|
|
5
|
+
|
|
6
|
+
export function circulatingS(nLive , forSale , held , dead ) {
|
|
7
|
+
let out = forSale;
|
|
8
|
+
for (const x of [held.c, held.k, held.f, dead]) {
|
|
9
|
+
out += x;
|
|
10
|
+
if (out > U128_MAX) v2Fail("Overflow");
|
|
11
|
+
}
|
|
12
|
+
if (nLive < out) v2Fail("Overflow");
|
|
13
|
+
return nLive - out;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function sweepIfEmpty(s , f , c , k ) {
|
|
17
|
+
if (s > 0n) return [0n, [f, c, k]];
|
|
18
|
+
const rest = f + c + k;
|
|
19
|
+
if (f + c > U128_MAX || rest > U128_MAX) v2Fail("Overflow");
|
|
20
|
+
return [rest, [0n, 0n, 0n]];
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function sweepUnsettled(b ) {
|
|
24
|
+
let moved = 0n;
|
|
25
|
+
for (let i = 0; i < b.nRanges; i++) {
|
|
26
|
+
moved += b.ranges[i].place;
|
|
27
|
+
if (moved > U128_MAX) v2Fail("Overflow");
|
|
28
|
+
}
|
|
29
|
+
if (moved === 0n) return 0n;
|
|
30
|
+
for (let i = 0; i < b.nRanges; i++) b.ranges[i].place = 0n;
|
|
31
|
+
if (b.sqrtP > b.sqrtLMax) b.sqrtLMax = b.sqrtP;
|
|
32
|
+
return moved;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const bytes = (n , tail ) => {
|
|
36
|
+
const a = new Uint8Array(n);
|
|
37
|
+
a.set(tail, n - tail.length);
|
|
38
|
+
return a;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export const EVM_DEAD = [
|
|
42
|
+
new Uint8Array(20),
|
|
43
|
+
bytes(20, [0xde, 0xad]),
|
|
44
|
+
bytes(20, [1]),
|
|
45
|
+
Uint8Array.from([0xde, 0xad, 0, 0, 0, 0, 0, 0, 0, 0, 0x42, 0x06, 0x94, 0x20, 0x69, 0x42, 0x06, 0x94, 0x20, 0x69]),
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
const eq = (a , b ) => a.length === b.length && a.every((x, i) => x === b[i]);
|
|
49
|
+
|
|
50
|
+
export function isEvmDead(a ) {
|
|
51
|
+
return a.length === 20 && EVM_DEAD.some((d) => eq(d, a));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const SOLANA_INCINERATOR = Uint8Array.from([
|
|
55
|
+
0x00, 0x33, 0x90, 0x72, 0x8d, 0x34, 0x11, 0x60, 0x79, 0xbd, 0xc9, 0x11, 0xbf, 0xff, 0x00, 0xdb, 0xd4, 0x4d, 0x2e, 0xcd, 0xcc, 0xf7, 0x9c, 0xa6, 0xe1, 0x00, 0x38,
|
|
56
|
+
0xe1, 0x00, 0x00, 0x00, 0x00,
|
|
57
|
+
]);
|
|
58
|
+
export const SOLANA_SYSTEM_PROGRAM = new Uint8Array(32);
|
|
59
|
+
|
|
60
|
+
export function isSolanaDeadOwner(owner ) {
|
|
61
|
+
return eq(owner, SOLANA_INCINERATOR) || eq(owner, SOLANA_SYSTEM_PROGRAM);
|
|
62
|
+
}
|