@floorlaunchpad/sdk 0.1.0 → 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,69 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { U128_MAX } from "./wide.ts";
|
|
4
|
+
import { v2Fail } from "./error.ts";
|
|
5
|
+
import type { Book } from "./book.ts";
|
|
6
|
+
|
|
7
|
+
export interface BidHoldings {
|
|
8
|
+
c: bigint;
|
|
9
|
+
k: bigint;
|
|
10
|
+
f: bigint;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function circulatingS(nLive: bigint, forSale: bigint, held: BidHoldings, dead: bigint): bigint {
|
|
14
|
+
let out = forSale;
|
|
15
|
+
for (const x of [held.c, held.k, held.f, dead]) {
|
|
16
|
+
out += x;
|
|
17
|
+
if (out > U128_MAX) v2Fail("Overflow");
|
|
18
|
+
}
|
|
19
|
+
if (nLive < out) v2Fail("Overflow");
|
|
20
|
+
return nLive - out;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function sweepIfEmpty(s: bigint, f: bigint, c: bigint, k: bigint): [bigint, [bigint, bigint, bigint]] {
|
|
24
|
+
if (s > 0n) return [0n, [f, c, k]];
|
|
25
|
+
const rest = f + c + k;
|
|
26
|
+
if (f + c > U128_MAX || rest > U128_MAX) v2Fail("Overflow");
|
|
27
|
+
return [rest, [0n, 0n, 0n]];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function sweepUnsettled(b: Book): bigint {
|
|
31
|
+
let moved = 0n;
|
|
32
|
+
for (let i = 0; i < b.nRanges; i++) {
|
|
33
|
+
moved += b.ranges[i].place;
|
|
34
|
+
if (moved > U128_MAX) v2Fail("Overflow");
|
|
35
|
+
}
|
|
36
|
+
if (moved === 0n) return 0n;
|
|
37
|
+
for (let i = 0; i < b.nRanges; i++) b.ranges[i].place = 0n;
|
|
38
|
+
if (b.sqrtP > b.sqrtLMax) b.sqrtLMax = b.sqrtP;
|
|
39
|
+
return moved;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const bytes = (n: number, tail: number[]): Uint8Array => {
|
|
43
|
+
const a = new Uint8Array(n);
|
|
44
|
+
a.set(tail, n - tail.length);
|
|
45
|
+
return a;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
export const EVM_DEAD: readonly Uint8Array[] = [
|
|
49
|
+
new Uint8Array(20),
|
|
50
|
+
bytes(20, [0xde, 0xad]),
|
|
51
|
+
bytes(20, [1]),
|
|
52
|
+
Uint8Array.from([0xde, 0xad, 0, 0, 0, 0, 0, 0, 0, 0, 0x42, 0x06, 0x94, 0x20, 0x69, 0x42, 0x06, 0x94, 0x20, 0x69]),
|
|
53
|
+
];
|
|
54
|
+
|
|
55
|
+
const eq = (a: Uint8Array, b: Uint8Array): boolean => a.length === b.length && a.every((x, i) => x === b[i]);
|
|
56
|
+
|
|
57
|
+
export function isEvmDead(a: Uint8Array): boolean {
|
|
58
|
+
return a.length === 20 && EVM_DEAD.some((d) => eq(d, a));
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const SOLANA_INCINERATOR = Uint8Array.from([
|
|
62
|
+
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,
|
|
63
|
+
0xe1, 0x00, 0x00, 0x00, 0x00,
|
|
64
|
+
]);
|
|
65
|
+
export const SOLANA_SYSTEM_PROGRAM = new Uint8Array(32);
|
|
66
|
+
|
|
67
|
+
export function isSolanaDeadOwner(owner: Uint8Array): boolean {
|
|
68
|
+
return eq(owner, SOLANA_INCINERATOR) || eq(owner, SOLANA_SYSTEM_PROGRAM);
|
|
69
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { fail } from "../index.ts";
|
|
4
|
+
import { KIND_TAIL, checkBook, emptyBook, pushRange } from "./book.ts";
|
|
5
|
+
import type { Book } from "./book.ts";
|
|
6
|
+
import { FRAC, SQRT_TAIL_END, flatMoneyForTokens, liquidityForTokens, moneyBetween } from "./sqrt.ts";
|
|
7
|
+
import type { Sqrt } from "./sqrt.ts";
|
|
8
|
+
import { bookBuy } from "./trade.ts";
|
|
9
|
+
import type { BookBuy } from "./trade.ts";
|
|
10
|
+
import { DOWN, U128_MAX, U256_LIMIT, UP, divW, isqrt, mulShr, shlW } from "./wide.ts";
|
|
11
|
+
import type { RangeRowV2, RowV2 } from "./preset.ts";
|
|
12
|
+
|
|
13
|
+
export const N0 = 1_000_000_000_000_000n;
|
|
14
|
+
|
|
15
|
+
export const TOKENS_PER_PPB = N0 / 1_000_000_000n;
|
|
16
|
+
export const TOKEN_DECIMALS_MIN = 6;
|
|
17
|
+
export const TOKEN_DECIMALS_MAX = 24;
|
|
18
|
+
export const QUOTE_DECIMALS_MAX = 30;
|
|
19
|
+
|
|
20
|
+
export interface CreateCtx {
|
|
21
|
+
quoteDecimals: number;
|
|
22
|
+
|
|
23
|
+
quoteUsdCentsX64: bigint;
|
|
24
|
+
|
|
25
|
+
tokenDecimals: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function sqrtP0(launchCapUsdCents: bigint, quoteUsdCentsX64: bigint, quoteDecimals: number): Sqrt {
|
|
29
|
+
if (quoteUsdCentsX64 === 0n || quoteDecimals > QUOTE_DECIMALS_MAX || launchCapUsdCents === 0n) fail("QuoteRowCannotOpen");
|
|
30
|
+
const num = shlW(launchCapUsdCents * 10n ** BigInt(quoteDecimals), 64 + 2 * FRAC);
|
|
31
|
+
if (num === null) fail("QuoteRowCannotOpen");
|
|
32
|
+
const den = quoteUsdCentsX64 * N0;
|
|
33
|
+
if (den >= U256_LIMIT) fail("QuoteRowCannotOpen");
|
|
34
|
+
const q = divW(num, den, DOWN);
|
|
35
|
+
if (q === null) fail("QuoteRowCannotOpen");
|
|
36
|
+
const s = isqrt(q);
|
|
37
|
+
if (s === 0n || s > SQRT_TAIL_END) fail("QuoteRowCannotOpen");
|
|
38
|
+
return s;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function usdCentsToUnits(cents: bigint, ctx: CreateCtx): bigint {
|
|
42
|
+
if (ctx.quoteUsdCentsX64 === 0n) fail("Zero");
|
|
43
|
+
if (ctx.quoteDecimals > 30) fail("Overflow");
|
|
44
|
+
const n = (cents * 10n ** BigInt(ctx.quoteDecimals)) << 64n;
|
|
45
|
+
if (n >= U256_LIMIT) fail("Overflow");
|
|
46
|
+
const q = n / ctx.quoteUsdCentsX64;
|
|
47
|
+
if (q > U128_MAX) fail("Overflow");
|
|
48
|
+
return q;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function teamBuyBounds(row: Pick<RowV2, "raiseUsdCents" | "teamBuyShareBps">, ctx: CreateCtx): [bigint, bigint] {
|
|
52
|
+
const cents = (row.raiseUsdCents * BigInt(row.teamBuyShareBps)) / 10_000n;
|
|
53
|
+
if (cents > 0xffff_ffff_ffff_ffffn) fail("Overflow");
|
|
54
|
+
return [0n, usdCentsToUnits(cents, ctx)];
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function edges(s0: Sqrt, g: RangeRowV2): [Sqrt, Sqrt] {
|
|
58
|
+
const at = (rel: bigint): Sqrt => {
|
|
59
|
+
const s = mulShr(s0, rel, 64, DOWN);
|
|
60
|
+
if (s === null || s === 0n || s >= SQRT_TAIL_END) fail("QuoteRowCannotOpen");
|
|
61
|
+
return s;
|
|
62
|
+
};
|
|
63
|
+
const a = at(g.sqrtARel);
|
|
64
|
+
const b = g.kind === KIND_TAIL ? SQRT_TAIL_END : g.sqrtBRel === g.sqrtARel ? a : at(g.sqrtBRel);
|
|
65
|
+
if (g.sqrtBRel !== g.sqrtARel && b <= a) fail("QuoteRowCannotOpen");
|
|
66
|
+
return [a, b];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export function createBook(row: Pick<RowV2, "feePpm" | "ranges"> & Partial<Pick<RowV2, "feeStartPpm" | "feeARel" | "feeBRel">>, s0: Sqrt): { book: Book; raise: bigint } {
|
|
70
|
+
const book = emptyBook(s0, row.feePpm);
|
|
71
|
+
|
|
72
|
+
book.feeStartPpm = [...(row.feeStartPpm ?? [0, 0, 0, 0, 0])];
|
|
73
|
+
const knot = (rel: bigint | undefined): Sqrt => mulShr(s0, rel ?? 0n, 32, DOWN) ?? 0n;
|
|
74
|
+
book.sqrtFeeA = knot(row.feeARel);
|
|
75
|
+
book.sqrtFeeB = knot(row.feeBRel);
|
|
76
|
+
let left = N0;
|
|
77
|
+
const n = row.ranges.length;
|
|
78
|
+
for (let i = 0; i < n; i++) {
|
|
79
|
+
const g = row.ranges[i];
|
|
80
|
+
const [a, b] = edges(s0, g);
|
|
81
|
+
let t = BigInt(g.sharePpb) * TOKENS_PER_PPB;
|
|
82
|
+
if (t > 0xffff_ffff_ffff_ffffn) fail("Overflow");
|
|
83
|
+
if (t > left) t = left;
|
|
84
|
+
if (g.kind === KIND_TAIL) {
|
|
85
|
+
t = left;
|
|
86
|
+
}
|
|
87
|
+
if (a !== b && t > 0n) {
|
|
88
|
+
|
|
89
|
+
const l = liquidityForTokens(a, b, t);
|
|
90
|
+
if (l === null) fail("QuoteRowCannotOpen");
|
|
91
|
+
if (g.kind !== KIND_TAIL && moneyBetween(l, a, b, UP) === null) fail("QuoteRowCannotOpen");
|
|
92
|
+
}
|
|
93
|
+
pushRange(book, a, b, t, g.kind, g.class);
|
|
94
|
+
const r = book.ranges[i];
|
|
95
|
+
r.thetaBps = g.thetaBps;
|
|
96
|
+
r.psiBps = g.psiBps;
|
|
97
|
+
r.msCapPpm = g.msCapPpm ?? 0;
|
|
98
|
+
left -= t;
|
|
99
|
+
}
|
|
100
|
+
if (n === 0 || book.ranges[n - 1].kind !== KIND_TAIL || left !== 0n) fail("RowTail");
|
|
101
|
+
checkBook(book);
|
|
102
|
+
const r0 = book.ranges[0];
|
|
103
|
+
if (r0.sqrtA === r0.sqrtB && flatMoneyForTokens(r0.sqrtA, r0.tokens, DOWN) === 0n) fail("QuoteRowCannotOpen");
|
|
104
|
+
const raise = flatMoneyForTokens(r0.sqrtA, r0.tokens, UP);
|
|
105
|
+
if (raise === null) fail("QuoteRowCannotOpen");
|
|
106
|
+
return { book, raise };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function quoteOpensV2(row: Pick<RowV2, "feePpm" | "ranges" | "launchCapUsdCents"> & Partial<Pick<RowV2, "feeStartPpm" | "feeARel" | "feeBRel">>, ctx: CreateCtx): void {
|
|
110
|
+
if (ctx.tokenDecimals < TOKEN_DECIMALS_MIN || ctx.tokenDecimals > TOKEN_DECIMALS_MAX) fail("QuoteRowCannotOpen");
|
|
111
|
+
createBook(row, sqrtP0(row.launchCapUsdCents, ctx.quoteUsdCentsX64, ctx.quoteDecimals));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface CreatedV2 {
|
|
115
|
+
book: Book;
|
|
116
|
+
sqrtP0: Sqrt;
|
|
117
|
+
|
|
118
|
+
raise: bigint;
|
|
119
|
+
teamBuyMax: bigint;
|
|
120
|
+
|
|
121
|
+
supply: bigint;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function createLaunchV2(row: RowV2, ctx: CreateCtx): CreatedV2 {
|
|
125
|
+
quoteOpensV2(row, ctx);
|
|
126
|
+
const s0 = sqrtP0(row.launchCapUsdCents, ctx.quoteUsdCentsX64, ctx.quoteDecimals);
|
|
127
|
+
const { book, raise } = createBook(row, s0);
|
|
128
|
+
return { book, sqrtP0: s0, raise, teamBuyMax: teamBuyBounds(row, ctx)[1], supply: N0 };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export function teamBuyV2(book: Book, gross: bigint, max: bigint): BookBuy {
|
|
132
|
+
const take = gross < max ? gross : max;
|
|
133
|
+
if (take === 0n) {
|
|
134
|
+
return { gross, net: 0n, refund: gross, tokens: 0n, fees: [0n, 0n, 0n, 0n, 0n], fromResale: 0n, fromFirst: 0n, steps: 0, budgetHit: false, graduatedNow: false, sqrtBefore: book.sqrtP, sqrtAfter: book.sqrtP,
|
|
135
|
+
split: { dF: 0n, ckAdd: 0n, ckTake: 0n, ms: 0n, booked: [0n, 0n, 0n, 0n], sqrtFNew: 0n }, relay: { nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kept: 0 } };
|
|
136
|
+
}
|
|
137
|
+
const r = bookBuy(book, take, 0n);
|
|
138
|
+
return { ...r, refund: r.refund + gross - take, gross };
|
|
139
|
+
}
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { FloorMathError } from "../index.ts";
|
|
4
|
+
import { KIND_BONDING, KIND_TAIL, MAX_WALK_STEPS, bidMoneyOf, feesAt, feesNow, nextAskSqrt, nextBidSqrt, placeMoney, rangeHeld, vaultV2 } from "./book.ts";
|
|
5
|
+
|
|
6
|
+
export { bidMoneyOf, placeMoney, vaultV2 };
|
|
7
|
+
import type { Book } from "./book.ts";
|
|
8
|
+
import { LEG_BACKING, LEG_FLOOR, LEG_HOLDER, LEG_PLATFORM, LEG_TEAM } from "./fees.ts";
|
|
9
|
+
import { FRAC, priceX64 } from "./sqrt.ts";
|
|
10
|
+
import type { Sqrt } from "./sqrt.ts";
|
|
11
|
+
import { quoteBookBuy, quoteBookSell } from "./trade.ts";
|
|
12
|
+
import type { BookBuy, BookSell } from "./trade.ts";
|
|
13
|
+
import { U64_MAX, U128_MAX, isqrt } from "./wide.ts";
|
|
14
|
+
|
|
15
|
+
export const nextBuySqrt = (b: Book): Sqrt => nextAskSqrt(b) ?? b.sqrtP;
|
|
16
|
+
|
|
17
|
+
export const nextBuyPriceX64 = (b: Book): bigint => priceX64(nextBuySqrt(b));
|
|
18
|
+
|
|
19
|
+
export function marketCapV2(b: Book, liveSupply: bigint): bigint {
|
|
20
|
+
if (liveSupply < 0n || liveSupply > U64_MAX) throw new RangeError(`liveSupply ${liveSupply}: u64 math units`);
|
|
21
|
+
const s = nextBuySqrt(b);
|
|
22
|
+
return (s * s * liveSupply) >> BigInt(2 * FRAC);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function dexLiquidityV2(b: Book): bigint {
|
|
26
|
+
return dexLiquidityOfParts(bidMoneyOf(b), placeMoney(b));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function dexLiquidityOfParts(bidMoney: bigint, place: bigint): bigint {
|
|
30
|
+
if (bidMoney < 0n || place < 0n) throw new RangeError("liquidity parts are money: ≥ 0");
|
|
31
|
+
const s = bidMoney + place;
|
|
32
|
+
if (s > U128_MAX) throw new RangeError("liquidity past u128");
|
|
33
|
+
return s;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function bestBidPriceX64V2(b: Book): bigint | null {
|
|
37
|
+
const s = nextBidSqrt(b);
|
|
38
|
+
return s === null ? null : priceX64(s);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const isGraduatedV2 = (b: Book): boolean => b.graduated;
|
|
42
|
+
|
|
43
|
+
export interface FeeRates {
|
|
44
|
+
|
|
45
|
+
backing: number;
|
|
46
|
+
|
|
47
|
+
team: number;
|
|
48
|
+
platform: number;
|
|
49
|
+
|
|
50
|
+
floorToken: number;
|
|
51
|
+
|
|
52
|
+
holder: number;
|
|
53
|
+
}
|
|
54
|
+
const ratesOf = (ppm: readonly number[]): FeeRates => ({ backing: ppm[LEG_BACKING], team: ppm[LEG_TEAM], platform: ppm[LEG_PLATFORM], floorToken: ppm[LEG_FLOOR], holder: ppm[LEG_HOLDER] });
|
|
55
|
+
|
|
56
|
+
export interface CurveViewV2 {
|
|
57
|
+
curve: boolean;
|
|
58
|
+
progress: number;
|
|
59
|
+
rates: FeeRates;
|
|
60
|
+
|
|
61
|
+
start: FeeRates;
|
|
62
|
+
end: FeeRates;
|
|
63
|
+
}
|
|
64
|
+
export function curveViewV2(b: Book): CurveViewV2 {
|
|
65
|
+
const curve = b.sqrtFeeB !== 0n;
|
|
66
|
+
let progress = 1;
|
|
67
|
+
if (curve) {
|
|
68
|
+
if (b.sqrtP <= b.sqrtFeeA) progress = 0;
|
|
69
|
+
else if (b.sqrtP < b.sqrtFeeB) {
|
|
70
|
+
const lg = (x: bigint): number => Math.log2(Number(x));
|
|
71
|
+
const [la, lb] = [lg(b.sqrtFeeA), lg(b.sqrtFeeB)];
|
|
72
|
+
progress = lb > la ? Math.min(1, Math.max(0, (lg(b.sqrtP) - la) / (lb - la))) : 1;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return { curve, progress, rates: ratesOf(feesNow(b)), start: ratesOf(curve ? b.feeStartPpm : b.feePpm), end: ratesOf(b.feePpm) };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface FeesAtMarketCapV2 {
|
|
79
|
+
sqrtP: Sqrt;
|
|
80
|
+
ppm: number[];
|
|
81
|
+
rates: FeeRates;
|
|
82
|
+
}
|
|
83
|
+
export function feesAtMarketCapV2(b: Book, marketCap: bigint, liveSupply: bigint): FeesAtMarketCapV2 {
|
|
84
|
+
if (liveSupply <= 0n || liveSupply > U64_MAX) throw new RangeError(`liveSupply ${liveSupply}: 1 ..= u64 math units`);
|
|
85
|
+
if (marketCap < 0n) throw new RangeError("marketCap < 0");
|
|
86
|
+
const sqrtP = isqrt((marketCap << BigInt(2 * FRAC)) / liveSupply);
|
|
87
|
+
const ppm = feesAt(b, sqrtP);
|
|
88
|
+
return { sqrtP, ppm, rates: ratesOf(ppm) };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export type LaunchStageV2 = "window" | "stack" | "tail";
|
|
92
|
+
export interface StackViewV2 {
|
|
93
|
+
stage: LaunchStageV2;
|
|
94
|
+
|
|
95
|
+
range: number | null;
|
|
96
|
+
|
|
97
|
+
windowLeft: bigint;
|
|
98
|
+
stackTotal: bigint;
|
|
99
|
+
stackLeft: bigint;
|
|
100
|
+
stackSold: bigint;
|
|
101
|
+
|
|
102
|
+
stackSoldBps: number;
|
|
103
|
+
|
|
104
|
+
sqrtT0: Sqrt | null;
|
|
105
|
+
}
|
|
106
|
+
export function stackViewV2(b: Book): StackViewV2 {
|
|
107
|
+
const ask = nextAskSqrt(b);
|
|
108
|
+
let range: number | null = null;
|
|
109
|
+
let total = 0n, left = 0n;
|
|
110
|
+
let t0: Sqrt | null = null;
|
|
111
|
+
for (let i = 0; i < b.nRanges; i++) {
|
|
112
|
+
const r = b.ranges[i];
|
|
113
|
+
if (r.kind === KIND_TAIL) t0 ??= r.sqrtA;
|
|
114
|
+
else if (r.kind !== KIND_BONDING) {
|
|
115
|
+
total += r.tokens;
|
|
116
|
+
left += r.askLeft;
|
|
117
|
+
}
|
|
118
|
+
if (ask !== null && range === null && rangeHeld(r) > 0n && (r.sqrtA === r.sqrtB ? ask === r.sqrtA : r.sqrtA <= ask && ask < r.sqrtB)) {
|
|
119
|
+
|
|
120
|
+
if (!(b.sqrtF !== 0n && b.sqrtF === ask && b.floorTokens > 0n)) range = i;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
const kind = range === null ? null : b.ranges[range].kind;
|
|
124
|
+
const stage: LaunchStageV2 = !b.graduated ? "window" : kind === KIND_TAIL || (t0 !== null && ask !== null && ask >= t0) ? "tail" : "stack";
|
|
125
|
+
const sold = total > left ? total - left : 0n;
|
|
126
|
+
return {
|
|
127
|
+
stage, range, windowLeft: b.graduated || b.nRanges === 0 ? 0n : rangeHeld(b.ranges[0]),
|
|
128
|
+
stackTotal: total, stackLeft: left, stackSold: sold, stackSoldBps: total === 0n ? 0 : Number((sold * 10_000n) / total), sqrtT0: t0,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface LaunchDisplayV2 {
|
|
133
|
+
nextBuyPriceX64: bigint;
|
|
134
|
+
marketCap: bigint;
|
|
135
|
+
dexLiquidity: bigint;
|
|
136
|
+
graduated: boolean;
|
|
137
|
+
curve: CurveViewV2;
|
|
138
|
+
stack: StackViewV2;
|
|
139
|
+
}
|
|
140
|
+
export function launchDisplayV2(b: Book, liveSupply: bigint): LaunchDisplayV2 {
|
|
141
|
+
return { nextBuyPriceX64: nextBuyPriceX64(b), marketCap: marketCapV2(b, liveSupply), dexLiquidity: dexLiquidityV2(b), graduated: b.graduated, curve: curveViewV2(b), stack: stackViewV2(b) };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export type ExactOutErrorCode = "Zero" | "Overflow" | "OutOfReach";
|
|
145
|
+
export class ExactOutError extends Error {
|
|
146
|
+
readonly code: ExactOutErrorCode;
|
|
147
|
+
constructor(code: ExactOutErrorCode, detail?: string) {
|
|
148
|
+
super(`floor exact-out: ${code}${detail ? ` (${detail})` : ""}`);
|
|
149
|
+
this.name = "ExactOutError";
|
|
150
|
+
this.code = code;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export const EXACT_OUT_SCAN = 16n;
|
|
155
|
+
|
|
156
|
+
const nothing = (e: unknown): boolean => e instanceof FloorMathError && (e.code === "NothingToDo" || e.code === "BookEmpty");
|
|
157
|
+
|
|
158
|
+
function least(ok: (v: bigint) => boolean, lo: bigint, hi: bigint): bigint {
|
|
159
|
+
while (hi - lo > 1n) {
|
|
160
|
+
const m = (lo + hi) / 2n;
|
|
161
|
+
if (ok(m)) hi = m;
|
|
162
|
+
else lo = m;
|
|
163
|
+
}
|
|
164
|
+
let best = hi;
|
|
165
|
+
for (let v = hi - 1n; v >= 1n && v >= hi - EXACT_OUT_SCAN; v--) if (ok(v)) best = v;
|
|
166
|
+
return best;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function quoteBuyExactOutV2(b: Book, tokensOut: bigint, walkSteps: number = MAX_WALK_STEPS): { gross: bigint; quote: BookBuy; quotes: number } {
|
|
170
|
+
if (tokensOut <= 0n) throw new ExactOutError("Zero");
|
|
171
|
+
if (tokensOut > U64_MAX) throw new ExactOutError("Overflow");
|
|
172
|
+
let quotes = 0;
|
|
173
|
+
const memo = new Map<bigint, BookBuy | null>();
|
|
174
|
+
const at = (g: bigint): BookBuy | null => {
|
|
175
|
+
if (memo.has(g)) return memo.get(g)!;
|
|
176
|
+
quotes++;
|
|
177
|
+
let r: BookBuy | null = null;
|
|
178
|
+
try {
|
|
179
|
+
r = quoteBookBuy(b, g, walkSteps);
|
|
180
|
+
} catch (e) {
|
|
181
|
+
if (!nothing(e)) throw e;
|
|
182
|
+
}
|
|
183
|
+
memo.set(g, r);
|
|
184
|
+
return r;
|
|
185
|
+
};
|
|
186
|
+
const ok = (g: bigint): boolean => (at(g)?.tokens ?? 0n) >= tokensOut;
|
|
187
|
+
|
|
188
|
+
const s = nextBuySqrt(b);
|
|
189
|
+
let hi = (s * s * tokensOut) >> BigInt(2 * FRAC);
|
|
190
|
+
if (hi < 1n) hi = 1n;
|
|
191
|
+
let lo = 0n;
|
|
192
|
+
let prev = -1n;
|
|
193
|
+
let flat = 0;
|
|
194
|
+
while (!ok(hi)) {
|
|
195
|
+
const r = at(hi);
|
|
196
|
+
const got = r?.tokens ?? 0n;
|
|
197
|
+
|
|
198
|
+
if (r !== null && r.refund > 0n && got === prev) {
|
|
199
|
+
if (++flat >= 2) throw new ExactOutError("OutOfReach");
|
|
200
|
+
} else flat = 0;
|
|
201
|
+
prev = got;
|
|
202
|
+
lo = hi;
|
|
203
|
+
if (hi === U128_MAX) throw new ExactOutError("OutOfReach");
|
|
204
|
+
hi = hi * 2n > U128_MAX ? U128_MAX : hi * 2n;
|
|
205
|
+
}
|
|
206
|
+
const gross = least(ok, lo, hi);
|
|
207
|
+
return { gross, quote: at(gross)!, quotes };
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
export function quoteSellExactOutV2(b: Book, quoteOut: bigint): { tokensIn: bigint; quote: BookSell; quotes: number } {
|
|
211
|
+
if (quoteOut <= 0n) throw new ExactOutError("Zero");
|
|
212
|
+
if (quoteOut > U128_MAX) throw new ExactOutError("Overflow");
|
|
213
|
+
let quotes = 0;
|
|
214
|
+
const memo = new Map<bigint, BookSell | null>();
|
|
215
|
+
const at = (t: bigint): BookSell | null => {
|
|
216
|
+
if (memo.has(t)) return memo.get(t)!;
|
|
217
|
+
quotes++;
|
|
218
|
+
let r: BookSell | null = null;
|
|
219
|
+
try {
|
|
220
|
+
r = quoteBookSell(b, t);
|
|
221
|
+
} catch (e) {
|
|
222
|
+
if (!nothing(e)) throw e;
|
|
223
|
+
}
|
|
224
|
+
memo.set(t, r);
|
|
225
|
+
return r;
|
|
226
|
+
};
|
|
227
|
+
const ok = (t: bigint): boolean => {
|
|
228
|
+
const r = at(t);
|
|
229
|
+
return r !== null && r.unfilled === 0n && r.out >= quoteOut;
|
|
230
|
+
};
|
|
231
|
+
let hi = 1n;
|
|
232
|
+
let lo = 0n;
|
|
233
|
+
while (!ok(hi)) {
|
|
234
|
+
const r = at(hi);
|
|
235
|
+
if (r !== null && r.unfilled > 0n) {
|
|
236
|
+
|
|
237
|
+
const filled = hi - r.unfilled;
|
|
238
|
+
if (filled > lo && ok(filled)) {
|
|
239
|
+
hi = filled;
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
throw new ExactOutError("OutOfReach");
|
|
243
|
+
}
|
|
244
|
+
if (hi === U64_MAX) throw new ExactOutError("OutOfReach");
|
|
245
|
+
lo = hi;
|
|
246
|
+
hi = hi * 2n > U64_MAX ? U64_MAX : hi * 2n;
|
|
247
|
+
}
|
|
248
|
+
const tokensIn = least(ok, lo, hi);
|
|
249
|
+
return { tokensIn, quote: at(tokensIn)!, quotes };
|
|
250
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
export type V2ErrorCode = "RowInvalid" | "Zero" | "Overflow" | "SupplyShort" | "TargetUnreachable" | "NotOwner" | "WExceedsSupply" | "NotHeld";
|
|
4
|
+
|
|
5
|
+
export class V2MathError extends Error {
|
|
6
|
+
readonly code: V2ErrorCode;
|
|
7
|
+
|
|
8
|
+
readonly stackIndex: number | undefined;
|
|
9
|
+
constructor(code: V2ErrorCode, stack?: number) {
|
|
10
|
+
super(`floor-math v2: ${code}${stack === undefined ? "" : `(${stack})`}`);
|
|
11
|
+
this.name = "V2MathError";
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.stackIndex = stack;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function v2Fail(code: V2ErrorCode, stack?: number): never {
|
|
18
|
+
throw new V2MathError(code, stack);
|
|
19
|
+
}
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { build, milestonesTotal } from "./ask.ts";
|
|
4
|
+
import type { Ask } from "./ask.ts";
|
|
5
|
+
import { cloneRow, f64ToU128, rowWcBps } from "./rows.ts";
|
|
6
|
+
import type { Row } from "./rows.ts";
|
|
7
|
+
import { V2MathError } from "./error.ts";
|
|
8
|
+
|
|
9
|
+
export type Violation =
|
|
10
|
+
| { kind: "Structure" }
|
|
11
|
+
|
|
12
|
+
| { kind: "SupplyShort"; excess: number }
|
|
13
|
+
|
|
14
|
+
| { kind: "FloorBelowRatio"; segment: number }
|
|
15
|
+
|
|
16
|
+
| { kind: "EmptyStretch" }
|
|
17
|
+
|
|
18
|
+
| { kind: "TargetUnreachable"; stack: number }
|
|
19
|
+
|
|
20
|
+
| { kind: "TargetShort"; stack: number; short: number }
|
|
21
|
+
|
|
22
|
+
| { kind: "RangeCover"; segment: number; cover: number }
|
|
23
|
+
|
|
24
|
+
| { kind: "OrderClamp"; segment: number };
|
|
25
|
+
|
|
26
|
+
export function r1Cover(phi: number, sA: number, x: number, a: number, b: number): number {
|
|
27
|
+
const u = Math.sqrt(b / a);
|
|
28
|
+
if (phi * u >= 1.0) return 0.0;
|
|
29
|
+
const need = (phi * sA * (u * u - 1.0)) / (u * (1.0 - phi * u));
|
|
30
|
+
return need <= 0.0 ? Infinity : x / need;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function askRules(row: Row, a: Ask, v: Violation[]): void {
|
|
34
|
+
const phi = row.phiBps / 1e4;
|
|
35
|
+
const clamp = (1.0 - row.wBps / 1e4) * (1.0 - rowWcBps(row) / 1e4);
|
|
36
|
+
let s = 0.0;
|
|
37
|
+
let bigF = 0.0;
|
|
38
|
+
let f = 0.0;
|
|
39
|
+
for (let i = 0; i < a.segs.length; i++) {
|
|
40
|
+
const g = a.segs[i]!;
|
|
41
|
+
const ranged = (g.kind === "Climb" || g.kind === "Sale") && g.b > g.a * (1.0 + 1e-12);
|
|
42
|
+
if (ranged && g.a < a.tailStart * (1.0 - 1e-12)) {
|
|
43
|
+
const c = r1Cover(phi, s, g.tokens, g.a, g.b);
|
|
44
|
+
if (c < 1.0 - 1e-9) v.push({ kind: "RangeCover", segment: i, cover: c });
|
|
45
|
+
}
|
|
46
|
+
if (g.kind === "Tail") break;
|
|
47
|
+
s += g.tokens;
|
|
48
|
+
bigF += g.toFloor;
|
|
49
|
+
const fNew = s > 0.0 ? fmax(f, bigF / s) : f;
|
|
50
|
+
if (g.kind !== "Bonding" && fNew > f * (1.0 + 1e-12) && fNew > clamp * g.b * (1.0 + 1e-9)) {
|
|
51
|
+
v.push({ kind: "OrderClamp", segment: i });
|
|
52
|
+
}
|
|
53
|
+
f = fNew;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const fmax = (a: number, b: number): number => (Number.isNaN(a) ? b : Number.isNaN(b) ? a : a >= b ? a : b);
|
|
58
|
+
|
|
59
|
+
export type Fix =
|
|
60
|
+
|
|
61
|
+
| { kind: "TailBps"; bps: number }
|
|
62
|
+
|
|
63
|
+
| { kind: "ScaleMilestones"; factor: number }
|
|
64
|
+
|
|
65
|
+
| { kind: "TopLevel"; level: bigint };
|
|
66
|
+
|
|
67
|
+
export interface Report {
|
|
68
|
+
ok: boolean;
|
|
69
|
+
violations: Violation[];
|
|
70
|
+
|
|
71
|
+
sold: number;
|
|
72
|
+
allowed: number;
|
|
73
|
+
|
|
74
|
+
roomTopLevel: bigint | null;
|
|
75
|
+
fix: Fix | null;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function tryBuild(row: Row): Ask | V2MathError {
|
|
79
|
+
try {
|
|
80
|
+
return build(row);
|
|
81
|
+
} catch (e) {
|
|
82
|
+
if (e instanceof V2MathError) return e;
|
|
83
|
+
throw e;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function violationsOf(row: Row): [Violation[], Ask | null] {
|
|
88
|
+
const a = tryBuild(row);
|
|
89
|
+
if (a instanceof V2MathError) {
|
|
90
|
+
if (a.code === "TargetUnreachable") return [[{ kind: "TargetUnreachable", stack: a.stackIndex! }], null];
|
|
91
|
+
return [[{ kind: "Structure" }], null];
|
|
92
|
+
}
|
|
93
|
+
const v: Violation[] = [];
|
|
94
|
+
if (a.supplyExcess > 1e-9) v.push({ kind: "SupplyShort", excess: a.supplyExcess });
|
|
95
|
+
if (a.floorBreak !== null) v.push({ kind: "FloorBelowRatio", segment: a.floorBreak });
|
|
96
|
+
if (!a.contiguous) v.push({ kind: "EmptyStretch" });
|
|
97
|
+
a.stacks.forEach((s, i) => {
|
|
98
|
+
if (!s.skipped && s.short > 1e-6 * Math.max(s.milestone + s.short, 1.0)) v.push({ kind: "TargetShort", stack: i, short: s.short });
|
|
99
|
+
});
|
|
100
|
+
askRules(row, a, v);
|
|
101
|
+
return [v, a];
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const passes = (row: Row): boolean => violationsOf(row)[0].length === 0;
|
|
105
|
+
|
|
106
|
+
export function withTopLevel(row: Row, level: bigint): Row {
|
|
107
|
+
const r = cloneRow(row);
|
|
108
|
+
const t = r.stacks[r.stacks.length - 1];
|
|
109
|
+
if (t !== undefined) t.level = level;
|
|
110
|
+
return r;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function scaled(row: Row, k: number): Row {
|
|
114
|
+
const r = cloneRow(row);
|
|
115
|
+
for (const s of r.stacks) s.milestone = f64ToU128(Math.floor(Number(s.milestone) * k));
|
|
116
|
+
return r;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function room(row: Row): bigint | null {
|
|
120
|
+
const last = row.stacks[row.stacks.length - 1];
|
|
121
|
+
if (last === undefined) return null;
|
|
122
|
+
const lvl = Number(last.level);
|
|
123
|
+
let lo = lvl;
|
|
124
|
+
let hi = lvl * 1e6;
|
|
125
|
+
if (passes(withTopLevel(row, f64ToU128(hi)))) return f64ToU128(hi);
|
|
126
|
+
for (let i = 0; i < 80; i++) {
|
|
127
|
+
const mid = Math.sqrt(lo * hi);
|
|
128
|
+
if (passes(withTopLevel(row, f64ToU128(mid)))) lo = mid;
|
|
129
|
+
else hi = mid;
|
|
130
|
+
}
|
|
131
|
+
return f64ToU128(lo);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function check(row: Row): Report {
|
|
135
|
+
const [violations, ask] = violationsOf(row);
|
|
136
|
+
const allowed = 1.0 - row.tailBps / 1e4;
|
|
137
|
+
const sold = ask === null ? 0.0 : ask.supplyAfterStacks;
|
|
138
|
+
const ok = violations.length === 0;
|
|
139
|
+
const rep: Report = { ok, violations, sold, allowed, roomTopLevel: null, fix: null };
|
|
140
|
+
if (ok) {
|
|
141
|
+
rep.roomTopLevel = room(row);
|
|
142
|
+
return rep;
|
|
143
|
+
}
|
|
144
|
+
if (rep.violations.some((v) => v.kind === "SupplyShort")) {
|
|
145
|
+
|
|
146
|
+
let best: [number, Fix] | null = null;
|
|
147
|
+
const offer = (rel: number, f: Fix): void => {
|
|
148
|
+
if (Number.isFinite(rel) && (best === null || rel < best[0])) best = [rel, f];
|
|
149
|
+
};
|
|
150
|
+
const tailNew = Math.max(10_000.0 - Math.ceil(sold * 1e4), 0.0);
|
|
151
|
+
if (tailNew >= 1.0) {
|
|
152
|
+
const t = cloneRow(row);
|
|
153
|
+
t.tailBps = Math.min(Math.trunc(tailNew), 65_535);
|
|
154
|
+
if (passes(t)) offer(1.0 - tailNew / Math.max(row.tailBps, 1), { kind: "TailBps", bps: t.tailBps });
|
|
155
|
+
}
|
|
156
|
+
let lo = 0.0;
|
|
157
|
+
let hi = 1.0;
|
|
158
|
+
if (passes(scaled(row, 0.0))) {
|
|
159
|
+
for (let i = 0; i < 60; i++) {
|
|
160
|
+
const mid = (lo + hi) / 2.0;
|
|
161
|
+
if (passes(scaled(row, mid))) lo = mid;
|
|
162
|
+
else hi = mid;
|
|
163
|
+
}
|
|
164
|
+
offer(1.0 - lo, { kind: "ScaleMilestones", factor: lo });
|
|
165
|
+
}
|
|
166
|
+
if (row.stacks.length > 1) {
|
|
167
|
+
const prev = Number(row.stacks[row.stacks.length - 2]!.level);
|
|
168
|
+
const top = Number(row.stacks[row.stacks.length - 1]!.level);
|
|
169
|
+
let lo = prev * 1.0001;
|
|
170
|
+
let hi = top;
|
|
171
|
+
if (passes(withTopLevel(row, f64ToU128(lo)))) {
|
|
172
|
+
for (let i = 0; i < 80; i++) {
|
|
173
|
+
const mid = Math.sqrt(lo * hi);
|
|
174
|
+
if (passes(withTopLevel(row, f64ToU128(mid)))) lo = mid;
|
|
175
|
+
else hi = mid;
|
|
176
|
+
}
|
|
177
|
+
offer(1.0 - lo / top, { kind: "TopLevel", level: f64ToU128(lo) });
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
rep.fix = best === null ? null : (best as [number, Fix])[1];
|
|
181
|
+
}
|
|
182
|
+
return rep;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
export function fitStacks(row: Row): Row {
|
|
186
|
+
let best: [number, Row] | null = null;
|
|
187
|
+
for (let n = row.stacks.length; n >= 1; n--) {
|
|
188
|
+
const r = cloneRow(row);
|
|
189
|
+
r.stacks.length = n;
|
|
190
|
+
const a = passes(r) ? tryBuild(r) : null;
|
|
191
|
+
if (a !== null && !(a instanceof V2MathError)) {
|
|
192
|
+
const t = milestonesTotal(a);
|
|
193
|
+
if (best === null || t > best[0]) best = [t, r];
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
if (best !== null) return (best as [number, Row])[1];
|
|
197
|
+
const r = cloneRow(row);
|
|
198
|
+
r.stacks.length = Math.min(r.stacks.length, 1);
|
|
199
|
+
return r;
|
|
200
|
+
}
|