@floorlaunchpad/sdk 0.1.1 → 0.3.1
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 +114 -0
- package/LICENSE +201 -60
- package/README.md +54 -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 +130 -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 +803 -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 +148 -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/squid.js +107 -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 +93 -7
- package/dist/indexer/bodies.js +473 -0
- package/dist/indexer/client.js +105 -0
- package/dist/indexer/contract.js +358 -0
- package/dist/indexer/decode.js +97 -0
- package/dist/indexer/partners.js +105 -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 +392 -0
- package/dist/solana/floor/borsh.js +66 -0
- package/dist/solana/floor/create.js +65 -0
- package/dist/solana/floor/events.js +144 -0
- package/dist/solana/floor/genesis.js +91 -0
- package/dist/solana/floor/genesisList.js +68 -0
- package/dist/solana/floor/instructions.js +448 -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/router.js +22 -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/floor-line.md +66 -0
- package/docs/frontend.md +29 -0
- package/docs/wallet.md +54 -0
- package/examples/floor-line.html +104 -0
- package/package.json +429 -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 +140 -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 +803 -0
- package/src/config/index.ts +93 -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 +842 -0
- package/src/evm/abi.ts +292 -0
- package/src/evm/balances.ts +26 -0
- package/src/evm/chains.ts +171 -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/squid.ts +138 -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 +93 -6
- package/src/indexer/bodies.ts +528 -0
- package/src/indexer/client.ts +180 -0
- package/src/indexer/contract.ts +419 -0
- package/src/indexer/decode.ts +109 -0
- package/src/indexer/partners.ts +115 -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 +395 -0
- package/src/solana/floor/borsh.ts +66 -0
- package/src/solana/floor/create.ts +89 -0
- package/src/solana/floor/events.ts +151 -0
- package/src/solana/floor/genesis.ts +111 -0
- package/src/solana/floor/genesisList.ts +75 -0
- package/src/solana/floor/instructions.ts +504 -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/router.ts +22 -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,122 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { fail } from "../index.js";
|
|
4
|
+
import { KIND_TAIL, checkBook, emptyBook, pushRange } from "./book.js";
|
|
5
|
+
|
|
6
|
+
import { FRAC, SQRT_TAIL_END, flatMoneyForTokens, liquidityForTokens, moneyBetween } from "./sqrt.js";
|
|
7
|
+
|
|
8
|
+
import { bookBuy } from "./trade.js";
|
|
9
|
+
|
|
10
|
+
import { DOWN, U128_MAX, U256_LIMIT, UP, divW, isqrt, mulShr, shlW } from "./wide.js";
|
|
11
|
+
|
|
12
|
+
export const N0 = 1_000_000_000_000_000n;
|
|
13
|
+
|
|
14
|
+
export const TOKENS_PER_PPB = N0 / 1_000_000_000n;
|
|
15
|
+
export const TOKEN_DECIMALS_MIN = 6;
|
|
16
|
+
export const TOKEN_DECIMALS_MAX = 24;
|
|
17
|
+
export const QUOTE_DECIMALS_MAX = 30;
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
export function sqrtP0(launchCapUsdCents , quoteUsdCentsX64 , quoteDecimals ) {
|
|
22
|
+
if (quoteUsdCentsX64 === 0n || quoteDecimals > QUOTE_DECIMALS_MAX || launchCapUsdCents === 0n) fail("QuoteRowCannotOpen");
|
|
23
|
+
const num = shlW(launchCapUsdCents * 10n ** BigInt(quoteDecimals), 64 + 2 * FRAC);
|
|
24
|
+
if (num === null) fail("QuoteRowCannotOpen");
|
|
25
|
+
const den = quoteUsdCentsX64 * N0;
|
|
26
|
+
if (den >= U256_LIMIT) fail("QuoteRowCannotOpen");
|
|
27
|
+
const q = divW(num, den, DOWN);
|
|
28
|
+
if (q === null) fail("QuoteRowCannotOpen");
|
|
29
|
+
const s = isqrt(q);
|
|
30
|
+
if (s === 0n || s > SQRT_TAIL_END) fail("QuoteRowCannotOpen");
|
|
31
|
+
return s;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function usdCentsToUnits(cents , ctx ) {
|
|
35
|
+
if (ctx.quoteUsdCentsX64 === 0n) fail("Zero");
|
|
36
|
+
if (ctx.quoteDecimals > 30) fail("Overflow");
|
|
37
|
+
const n = (cents * 10n ** BigInt(ctx.quoteDecimals)) << 64n;
|
|
38
|
+
if (n >= U256_LIMIT) fail("Overflow");
|
|
39
|
+
const q = n / ctx.quoteUsdCentsX64;
|
|
40
|
+
if (q > U128_MAX) fail("Overflow");
|
|
41
|
+
return q;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function teamBuyBounds(row , ctx ) {
|
|
45
|
+
const cents = (row.raiseUsdCents * BigInt(row.teamBuyShareBps)) / 10_000n;
|
|
46
|
+
if (cents > 0xffff_ffff_ffff_ffffn) fail("Overflow");
|
|
47
|
+
return [0n, usdCentsToUnits(cents, ctx)];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function edges(s0 , g ) {
|
|
51
|
+
const at = (rel ) => {
|
|
52
|
+
const s = mulShr(s0, rel, 64, DOWN);
|
|
53
|
+
if (s === null || s === 0n || s >= SQRT_TAIL_END) fail("QuoteRowCannotOpen");
|
|
54
|
+
return s;
|
|
55
|
+
};
|
|
56
|
+
const a = at(g.sqrtARel);
|
|
57
|
+
const b = g.kind === KIND_TAIL ? SQRT_TAIL_END : g.sqrtBRel === g.sqrtARel ? a : at(g.sqrtBRel);
|
|
58
|
+
if (g.sqrtBRel !== g.sqrtARel && b <= a) fail("QuoteRowCannotOpen");
|
|
59
|
+
return [a, b];
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function createBook(row , s0 ) {
|
|
63
|
+
const book = emptyBook(s0, row.feePpm);
|
|
64
|
+
|
|
65
|
+
book.feeStartPpm = [...(row.feeStartPpm ?? [0, 0, 0, 0, 0])];
|
|
66
|
+
const knot = (rel ) => mulShr(s0, rel ?? 0n, 32, DOWN) ?? 0n;
|
|
67
|
+
book.sqrtFeeA = knot(row.feeARel);
|
|
68
|
+
book.sqrtFeeB = knot(row.feeBRel);
|
|
69
|
+
let left = N0;
|
|
70
|
+
const n = row.ranges.length;
|
|
71
|
+
for (let i = 0; i < n; i++) {
|
|
72
|
+
const g = row.ranges[i];
|
|
73
|
+
const [a, b] = edges(s0, g);
|
|
74
|
+
let t = BigInt(g.sharePpb) * TOKENS_PER_PPB;
|
|
75
|
+
if (t > 0xffff_ffff_ffff_ffffn) fail("Overflow");
|
|
76
|
+
if (t > left) t = left;
|
|
77
|
+
if (g.kind === KIND_TAIL) {
|
|
78
|
+
t = left;
|
|
79
|
+
}
|
|
80
|
+
if (a !== b && t > 0n) {
|
|
81
|
+
|
|
82
|
+
const l = liquidityForTokens(a, b, t);
|
|
83
|
+
if (l === null) fail("QuoteRowCannotOpen");
|
|
84
|
+
if (g.kind !== KIND_TAIL && moneyBetween(l, a, b, UP) === null) fail("QuoteRowCannotOpen");
|
|
85
|
+
}
|
|
86
|
+
pushRange(book, a, b, t, g.kind, g.class);
|
|
87
|
+
const r = book.ranges[i];
|
|
88
|
+
r.thetaBps = g.thetaBps;
|
|
89
|
+
r.psiBps = g.psiBps;
|
|
90
|
+
r.msCapPpm = g.msCapPpm ?? 0;
|
|
91
|
+
left -= t;
|
|
92
|
+
}
|
|
93
|
+
if (n === 0 || book.ranges[n - 1].kind !== KIND_TAIL || left !== 0n) fail("RowTail");
|
|
94
|
+
checkBook(book);
|
|
95
|
+
const r0 = book.ranges[0];
|
|
96
|
+
if (r0.sqrtA === r0.sqrtB && flatMoneyForTokens(r0.sqrtA, r0.tokens, DOWN) === 0n) fail("QuoteRowCannotOpen");
|
|
97
|
+
const raise = flatMoneyForTokens(r0.sqrtA, r0.tokens, UP);
|
|
98
|
+
if (raise === null) fail("QuoteRowCannotOpen");
|
|
99
|
+
return { book, raise };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function quoteOpensV2(row , ctx ) {
|
|
103
|
+
if (ctx.tokenDecimals < TOKEN_DECIMALS_MIN || ctx.tokenDecimals > TOKEN_DECIMALS_MAX) fail("QuoteRowCannotOpen");
|
|
104
|
+
createBook(row, sqrtP0(row.launchCapUsdCents, ctx.quoteUsdCentsX64, ctx.quoteDecimals));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function createLaunchV2(row , ctx ) {
|
|
108
|
+
quoteOpensV2(row, ctx);
|
|
109
|
+
const s0 = sqrtP0(row.launchCapUsdCents, ctx.quoteUsdCentsX64, ctx.quoteDecimals);
|
|
110
|
+
const { book, raise } = createBook(row, s0);
|
|
111
|
+
return { book, sqrtP0: s0, raise, teamBuyMax: teamBuyBounds(row, ctx)[1], supply: N0 };
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function teamBuyV2(book , gross , max ) {
|
|
115
|
+
const take = gross < max ? gross : max;
|
|
116
|
+
if (take === 0n) {
|
|
117
|
+
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,
|
|
118
|
+
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 } };
|
|
119
|
+
}
|
|
120
|
+
const r = bookBuy(book, take, 0n);
|
|
121
|
+
return { ...r, refund: r.refund + gross - take, gross };
|
|
122
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { FloorMathError } from "../index.js";
|
|
4
|
+
import { KIND_BONDING, KIND_TAIL, MAX_WALK_STEPS, bidMoneyOf, feesAt, feesNow, nextAskSqrt, nextBidSqrt, placeMoney, rangeHeld, vaultV2 } from "./book.js";
|
|
5
|
+
|
|
6
|
+
export { bidMoneyOf, placeMoney, vaultV2 };
|
|
7
|
+
|
|
8
|
+
import { LEG_BACKING, LEG_FLOOR, LEG_HOLDER, LEG_PLATFORM, LEG_TEAM } from "./fees.js";
|
|
9
|
+
import { FRAC, priceX64 } from "./sqrt.js";
|
|
10
|
+
|
|
11
|
+
import { quoteBookBuy, quoteBookSell } from "./trade.js";
|
|
12
|
+
|
|
13
|
+
import { U64_MAX, U128_MAX, isqrt } from "./wide.js";
|
|
14
|
+
|
|
15
|
+
export const nextBuySqrt = (b ) => nextAskSqrt(b) ?? b.sqrtP;
|
|
16
|
+
|
|
17
|
+
export const nextBuyPriceX64 = (b ) => priceX64(nextBuySqrt(b));
|
|
18
|
+
|
|
19
|
+
export function marketCapV2(b , liveSupply ) {
|
|
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 ) {
|
|
26
|
+
return dexLiquidityOfParts(bidMoneyOf(b), placeMoney(b));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function dexLiquidityOfParts(bidMoney , place ) {
|
|
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 ) {
|
|
37
|
+
const s = nextBidSqrt(b);
|
|
38
|
+
return s === null ? null : priceX64(s);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const isGraduatedV2 = (b ) => b.graduated;
|
|
42
|
+
|
|
43
|
+
const ratesOf = (ppm ) => ({ backing: ppm[LEG_BACKING], team: ppm[LEG_TEAM], platform: ppm[LEG_PLATFORM], floorToken: ppm[LEG_FLOOR], holder: ppm[LEG_HOLDER] });
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
export function curveViewV2(b ) {
|
|
48
|
+
const curve = b.sqrtFeeB !== 0n;
|
|
49
|
+
let progress = 1;
|
|
50
|
+
if (curve) {
|
|
51
|
+
if (b.sqrtP <= b.sqrtFeeA) progress = 0;
|
|
52
|
+
else if (b.sqrtP < b.sqrtFeeB) {
|
|
53
|
+
const lg = (x ) => Math.log2(Number(x));
|
|
54
|
+
const [la, lb] = [lg(b.sqrtFeeA), lg(b.sqrtFeeB)];
|
|
55
|
+
progress = lb > la ? Math.min(1, Math.max(0, (lg(b.sqrtP) - la) / (lb - la))) : 1;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return { curve, progress, rates: ratesOf(feesNow(b)), start: ratesOf(curve ? b.feeStartPpm : b.feePpm), end: ratesOf(b.feePpm) };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function feesAtMarketCapV2(b , marketCap , liveSupply ) {
|
|
62
|
+
if (liveSupply <= 0n || liveSupply > U64_MAX) throw new RangeError(`liveSupply ${liveSupply}: 1 ..= u64 math units`);
|
|
63
|
+
if (marketCap < 0n) throw new RangeError("marketCap < 0");
|
|
64
|
+
const sqrtP = isqrt((marketCap << BigInt(2 * FRAC)) / liveSupply);
|
|
65
|
+
const ppm = feesAt(b, sqrtP);
|
|
66
|
+
return { sqrtP, ppm, rates: ratesOf(ppm) };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
export function stackViewV2(b ) {
|
|
74
|
+
const ask = nextAskSqrt(b);
|
|
75
|
+
let range = null;
|
|
76
|
+
let total = 0n, left = 0n;
|
|
77
|
+
let t0 = null;
|
|
78
|
+
for (let i = 0; i < b.nRanges; i++) {
|
|
79
|
+
const r = b.ranges[i];
|
|
80
|
+
if (r.kind === KIND_TAIL) t0 ??= r.sqrtA;
|
|
81
|
+
else if (r.kind !== KIND_BONDING) {
|
|
82
|
+
total += r.tokens;
|
|
83
|
+
left += r.askLeft;
|
|
84
|
+
}
|
|
85
|
+
if (ask !== null && range === null && rangeHeld(r) > 0n && (r.sqrtA === r.sqrtB ? ask === r.sqrtA : r.sqrtA <= ask && ask < r.sqrtB)) {
|
|
86
|
+
|
|
87
|
+
if (!(b.sqrtF !== 0n && b.sqrtF === ask && b.floorTokens > 0n)) range = i;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const kind = range === null ? null : b.ranges[range].kind;
|
|
91
|
+
const stage = !b.graduated ? "window" : kind === KIND_TAIL || (t0 !== null && ask !== null && ask >= t0) ? "tail" : "stack";
|
|
92
|
+
const sold = total > left ? total - left : 0n;
|
|
93
|
+
return {
|
|
94
|
+
stage, range, windowLeft: b.graduated || b.nRanges === 0 ? 0n : rangeHeld(b.ranges[0]),
|
|
95
|
+
stackTotal: total, stackLeft: left, stackSold: sold, stackSoldBps: total === 0n ? 0 : Number((sold * 10_000n) / total), sqrtT0: t0,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function launchDisplayV2(b , liveSupply ) {
|
|
100
|
+
return { nextBuyPriceX64: nextBuyPriceX64(b), marketCap: marketCapV2(b, liveSupply), dexLiquidity: dexLiquidityV2(b), graduated: b.graduated, curve: curveViewV2(b), stack: stackViewV2(b) };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export class ExactOutError extends Error {
|
|
104
|
+
code ;
|
|
105
|
+
constructor(code , detail ) {
|
|
106
|
+
super(`floor exact-out: ${code}${detail ? ` (${detail})` : ""}`);
|
|
107
|
+
this.name = "ExactOutError";
|
|
108
|
+
this.code = code;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export const EXACT_OUT_SCAN = 16n;
|
|
113
|
+
|
|
114
|
+
const nothing = (e ) => e instanceof FloorMathError && (e.code === "NothingToDo" || e.code === "BookEmpty");
|
|
115
|
+
|
|
116
|
+
function least(ok , lo , hi ) {
|
|
117
|
+
while (hi - lo > 1n) {
|
|
118
|
+
const m = (lo + hi) / 2n;
|
|
119
|
+
if (ok(m)) hi = m;
|
|
120
|
+
else lo = m;
|
|
121
|
+
}
|
|
122
|
+
let best = hi;
|
|
123
|
+
for (let v = hi - 1n; v >= 1n && v >= hi - EXACT_OUT_SCAN; v--) if (ok(v)) best = v;
|
|
124
|
+
return best;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function quoteBuyExactOutV2(b , tokensOut , walkSteps = MAX_WALK_STEPS) {
|
|
128
|
+
if (tokensOut <= 0n) throw new ExactOutError("Zero");
|
|
129
|
+
if (tokensOut > U64_MAX) throw new ExactOutError("Overflow");
|
|
130
|
+
let quotes = 0;
|
|
131
|
+
const memo = new Map ();
|
|
132
|
+
const at = (g ) => {
|
|
133
|
+
if (memo.has(g)) return memo.get(g) ;
|
|
134
|
+
quotes++;
|
|
135
|
+
let r = null;
|
|
136
|
+
try {
|
|
137
|
+
r = quoteBookBuy(b, g, walkSteps);
|
|
138
|
+
} catch (e) {
|
|
139
|
+
if (!nothing(e)) throw e;
|
|
140
|
+
}
|
|
141
|
+
memo.set(g, r);
|
|
142
|
+
return r;
|
|
143
|
+
};
|
|
144
|
+
const ok = (g ) => (at(g)?.tokens ?? 0n) >= tokensOut;
|
|
145
|
+
|
|
146
|
+
const s = nextBuySqrt(b);
|
|
147
|
+
let hi = (s * s * tokensOut) >> BigInt(2 * FRAC);
|
|
148
|
+
if (hi < 1n) hi = 1n;
|
|
149
|
+
let lo = 0n;
|
|
150
|
+
let prev = -1n;
|
|
151
|
+
let flat = 0;
|
|
152
|
+
while (!ok(hi)) {
|
|
153
|
+
const r = at(hi);
|
|
154
|
+
const got = r?.tokens ?? 0n;
|
|
155
|
+
|
|
156
|
+
if (r !== null && r.refund > 0n && got === prev) {
|
|
157
|
+
if (++flat >= 2) throw new ExactOutError("OutOfReach");
|
|
158
|
+
} else flat = 0;
|
|
159
|
+
prev = got;
|
|
160
|
+
lo = hi;
|
|
161
|
+
if (hi === U128_MAX) throw new ExactOutError("OutOfReach");
|
|
162
|
+
hi = hi * 2n > U128_MAX ? U128_MAX : hi * 2n;
|
|
163
|
+
}
|
|
164
|
+
const gross = least(ok, lo, hi);
|
|
165
|
+
return { gross, quote: at(gross) , quotes };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function quoteSellExactOutV2(b , quoteOut ) {
|
|
169
|
+
if (quoteOut <= 0n) throw new ExactOutError("Zero");
|
|
170
|
+
if (quoteOut > U128_MAX) throw new ExactOutError("Overflow");
|
|
171
|
+
let quotes = 0;
|
|
172
|
+
const memo = new Map ();
|
|
173
|
+
const at = (t ) => {
|
|
174
|
+
if (memo.has(t)) return memo.get(t) ;
|
|
175
|
+
quotes++;
|
|
176
|
+
let r = null;
|
|
177
|
+
try {
|
|
178
|
+
r = quoteBookSell(b, t);
|
|
179
|
+
} catch (e) {
|
|
180
|
+
if (!nothing(e)) throw e;
|
|
181
|
+
}
|
|
182
|
+
memo.set(t, r);
|
|
183
|
+
return r;
|
|
184
|
+
};
|
|
185
|
+
const ok = (t ) => {
|
|
186
|
+
const r = at(t);
|
|
187
|
+
return r !== null && r.unfilled === 0n && r.out >= quoteOut;
|
|
188
|
+
};
|
|
189
|
+
let hi = 1n;
|
|
190
|
+
let lo = 0n;
|
|
191
|
+
while (!ok(hi)) {
|
|
192
|
+
const r = at(hi);
|
|
193
|
+
if (r !== null && r.unfilled > 0n) {
|
|
194
|
+
|
|
195
|
+
const filled = hi - r.unfilled;
|
|
196
|
+
if (filled > lo && ok(filled)) {
|
|
197
|
+
hi = filled;
|
|
198
|
+
break;
|
|
199
|
+
}
|
|
200
|
+
throw new ExactOutError("OutOfReach");
|
|
201
|
+
}
|
|
202
|
+
if (hi === U64_MAX) throw new ExactOutError("OutOfReach");
|
|
203
|
+
lo = hi;
|
|
204
|
+
hi = hi * 2n > U64_MAX ? U64_MAX : hi * 2n;
|
|
205
|
+
}
|
|
206
|
+
const tokensIn = least(ok, lo, hi);
|
|
207
|
+
return { tokensIn, quote: at(tokensIn) , quotes };
|
|
208
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
export class V2MathError extends Error {
|
|
4
|
+
code ;
|
|
5
|
+
|
|
6
|
+
stackIndex ;
|
|
7
|
+
constructor(code , stack ) {
|
|
8
|
+
super(`floor-math v2: ${code}${stack === undefined ? "" : `(${stack})`}`);
|
|
9
|
+
this.name = "V2MathError";
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.stackIndex = stack;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function v2Fail(code , stack ) {
|
|
16
|
+
throw new V2MathError(code, stack);
|
|
17
|
+
}
|
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { build, milestonesTotal } from "./ask.js";
|
|
4
|
+
|
|
5
|
+
import { cloneRow, f64ToU128, rowWcBps } from "./rows.js";
|
|
6
|
+
|
|
7
|
+
import { V2MathError } from "./error.js";
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
export function r1Cover(phi , sA , x , a , b ) {
|
|
18
|
+
const u = Math.sqrt(b / a);
|
|
19
|
+
if (phi * u >= 1.0) return 0.0;
|
|
20
|
+
const need = (phi * sA * (u * u - 1.0)) / (u * (1.0 - phi * u));
|
|
21
|
+
return need <= 0.0 ? Infinity : x / need;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function askRules(row , a , v ) {
|
|
25
|
+
const phi = row.phiBps / 1e4;
|
|
26
|
+
const clamp = (1.0 - row.wBps / 1e4) * (1.0 - rowWcBps(row) / 1e4);
|
|
27
|
+
let s = 0.0;
|
|
28
|
+
let bigF = 0.0;
|
|
29
|
+
let f = 0.0;
|
|
30
|
+
for (let i = 0; i < a.segs.length; i++) {
|
|
31
|
+
const g = a.segs[i] ;
|
|
32
|
+
const ranged = (g.kind === "Climb" || g.kind === "Sale") && g.b > g.a * (1.0 + 1e-12);
|
|
33
|
+
if (ranged && g.a < a.tailStart * (1.0 - 1e-12)) {
|
|
34
|
+
const c = r1Cover(phi, s, g.tokens, g.a, g.b);
|
|
35
|
+
if (c < 1.0 - 1e-9) v.push({ kind: "RangeCover", segment: i, cover: c });
|
|
36
|
+
}
|
|
37
|
+
if (g.kind === "Tail") break;
|
|
38
|
+
s += g.tokens;
|
|
39
|
+
bigF += g.toFloor;
|
|
40
|
+
const fNew = s > 0.0 ? fmax(f, bigF / s) : f;
|
|
41
|
+
if (g.kind !== "Bonding" && fNew > f * (1.0 + 1e-12) && fNew > clamp * g.b * (1.0 + 1e-9)) {
|
|
42
|
+
v.push({ kind: "OrderClamp", segment: i });
|
|
43
|
+
}
|
|
44
|
+
f = fNew;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const fmax = (a , b ) => (Number.isNaN(a) ? b : Number.isNaN(b) ? a : a >= b ? a : b);
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
function tryBuild(row ) {
|
|
53
|
+
try {
|
|
54
|
+
return build(row);
|
|
55
|
+
} catch (e) {
|
|
56
|
+
if (e instanceof V2MathError) return e;
|
|
57
|
+
throw e;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function violationsOf(row ) {
|
|
62
|
+
const a = tryBuild(row);
|
|
63
|
+
if (a instanceof V2MathError) {
|
|
64
|
+
if (a.code === "TargetUnreachable") return [[{ kind: "TargetUnreachable", stack: a.stackIndex }], null];
|
|
65
|
+
return [[{ kind: "Structure" }], null];
|
|
66
|
+
}
|
|
67
|
+
const v = [];
|
|
68
|
+
if (a.supplyExcess > 1e-9) v.push({ kind: "SupplyShort", excess: a.supplyExcess });
|
|
69
|
+
if (a.floorBreak !== null) v.push({ kind: "FloorBelowRatio", segment: a.floorBreak });
|
|
70
|
+
if (!a.contiguous) v.push({ kind: "EmptyStretch" });
|
|
71
|
+
a.stacks.forEach((s, i) => {
|
|
72
|
+
if (!s.skipped && s.short > 1e-6 * Math.max(s.milestone + s.short, 1.0)) v.push({ kind: "TargetShort", stack: i, short: s.short });
|
|
73
|
+
});
|
|
74
|
+
askRules(row, a, v);
|
|
75
|
+
return [v, a];
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export const passes = (row ) => violationsOf(row)[0].length === 0;
|
|
79
|
+
|
|
80
|
+
export function withTopLevel(row , level ) {
|
|
81
|
+
const r = cloneRow(row);
|
|
82
|
+
const t = r.stacks[r.stacks.length - 1];
|
|
83
|
+
if (t !== undefined) t.level = level;
|
|
84
|
+
return r;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function scaled(row , k ) {
|
|
88
|
+
const r = cloneRow(row);
|
|
89
|
+
for (const s of r.stacks) s.milestone = f64ToU128(Math.floor(Number(s.milestone) * k));
|
|
90
|
+
return r;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function room(row ) {
|
|
94
|
+
const last = row.stacks[row.stacks.length - 1];
|
|
95
|
+
if (last === undefined) return null;
|
|
96
|
+
const lvl = Number(last.level);
|
|
97
|
+
let lo = lvl;
|
|
98
|
+
let hi = lvl * 1e6;
|
|
99
|
+
if (passes(withTopLevel(row, f64ToU128(hi)))) return f64ToU128(hi);
|
|
100
|
+
for (let i = 0; i < 80; i++) {
|
|
101
|
+
const mid = Math.sqrt(lo * hi);
|
|
102
|
+
if (passes(withTopLevel(row, f64ToU128(mid)))) lo = mid;
|
|
103
|
+
else hi = mid;
|
|
104
|
+
}
|
|
105
|
+
return f64ToU128(lo);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function check(row ) {
|
|
109
|
+
const [violations, ask] = violationsOf(row);
|
|
110
|
+
const allowed = 1.0 - row.tailBps / 1e4;
|
|
111
|
+
const sold = ask === null ? 0.0 : ask.supplyAfterStacks;
|
|
112
|
+
const ok = violations.length === 0;
|
|
113
|
+
const rep = { ok, violations, sold, allowed, roomTopLevel: null, fix: null };
|
|
114
|
+
if (ok) {
|
|
115
|
+
rep.roomTopLevel = room(row);
|
|
116
|
+
return rep;
|
|
117
|
+
}
|
|
118
|
+
if (rep.violations.some((v) => v.kind === "SupplyShort")) {
|
|
119
|
+
|
|
120
|
+
let best = null;
|
|
121
|
+
const offer = (rel , f ) => {
|
|
122
|
+
if (Number.isFinite(rel) && (best === null || rel < best[0])) best = [rel, f];
|
|
123
|
+
};
|
|
124
|
+
const tailNew = Math.max(10_000.0 - Math.ceil(sold * 1e4), 0.0);
|
|
125
|
+
if (tailNew >= 1.0) {
|
|
126
|
+
const t = cloneRow(row);
|
|
127
|
+
t.tailBps = Math.min(Math.trunc(tailNew), 65_535);
|
|
128
|
+
if (passes(t)) offer(1.0 - tailNew / Math.max(row.tailBps, 1), { kind: "TailBps", bps: t.tailBps });
|
|
129
|
+
}
|
|
130
|
+
let lo = 0.0;
|
|
131
|
+
let hi = 1.0;
|
|
132
|
+
if (passes(scaled(row, 0.0))) {
|
|
133
|
+
for (let i = 0; i < 60; i++) {
|
|
134
|
+
const mid = (lo + hi) / 2.0;
|
|
135
|
+
if (passes(scaled(row, mid))) lo = mid;
|
|
136
|
+
else hi = mid;
|
|
137
|
+
}
|
|
138
|
+
offer(1.0 - lo, { kind: "ScaleMilestones", factor: lo });
|
|
139
|
+
}
|
|
140
|
+
if (row.stacks.length > 1) {
|
|
141
|
+
const prev = Number(row.stacks[row.stacks.length - 2] .level);
|
|
142
|
+
const top = Number(row.stacks[row.stacks.length - 1] .level);
|
|
143
|
+
let lo = prev * 1.0001;
|
|
144
|
+
let hi = top;
|
|
145
|
+
if (passes(withTopLevel(row, f64ToU128(lo)))) {
|
|
146
|
+
for (let i = 0; i < 80; i++) {
|
|
147
|
+
const mid = Math.sqrt(lo * hi);
|
|
148
|
+
if (passes(withTopLevel(row, f64ToU128(mid)))) lo = mid;
|
|
149
|
+
else hi = mid;
|
|
150
|
+
}
|
|
151
|
+
offer(1.0 - lo / top, { kind: "TopLevel", level: f64ToU128(lo) });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
rep.fix = best === null ? null : (best )[1];
|
|
155
|
+
}
|
|
156
|
+
return rep;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function fitStacks(row ) {
|
|
160
|
+
let best = null;
|
|
161
|
+
for (let n = row.stacks.length; n >= 1; n--) {
|
|
162
|
+
const r = cloneRow(row);
|
|
163
|
+
r.stacks.length = n;
|
|
164
|
+
const a = passes(r) ? tryBuild(r) : null;
|
|
165
|
+
if (a !== null && !(a instanceof V2MathError)) {
|
|
166
|
+
const t = milestonesTotal(a);
|
|
167
|
+
if (best === null || t > best[0]) best = [t, r];
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (best !== null) return (best )[1];
|
|
171
|
+
const r = cloneRow(row);
|
|
172
|
+
r.stacks.length = Math.min(r.stacks.length, 1);
|
|
173
|
+
return r;
|
|
174
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { log2X64 } from "../fixed.js";
|
|
4
|
+
import { DOWN, U128_MAX, UP, mulDiv } from "./wide.js";
|
|
5
|
+
|
|
6
|
+
export const FEE_LEGS = 5;
|
|
7
|
+
export const FEE_DEFAULT_PPM = 100;
|
|
8
|
+
export const DEFAULT_FEE_PPM = [100, 100, 100, 100, 100];
|
|
9
|
+
export const LEG_BACKING = 0;
|
|
10
|
+
export const LEG_TEAM = 1;
|
|
11
|
+
export const LEG_PLATFORM = 2;
|
|
12
|
+
export const LEG_FLOOR = 3;
|
|
13
|
+
export const LEG_HOLDER = 4;
|
|
14
|
+
const PPM = 1_000_000n;
|
|
15
|
+
|
|
16
|
+
export const feeLeg = (amount , ppm ) => mulDiv(amount, BigInt(ppm), PPM, UP) ?? amount;
|
|
17
|
+
export const feeLegs = (amount , ppm ) => ppm.map((p) => feeLeg(amount, p));
|
|
18
|
+
|
|
19
|
+
export const feeTotal = (f ) => f.reduce((a, b) => (a + b > U128_MAX ? U128_MAX : a + b), 0n);
|
|
20
|
+
|
|
21
|
+
export function ppmAt(end , start , sqrtA , sqrtB , sqrtP ) {
|
|
22
|
+
if (sqrtB === 0n || sqrtP >= sqrtB) return [...end];
|
|
23
|
+
if (sqrtP <= sqrtA) return [...start];
|
|
24
|
+
const lg = (x ) => log2X64(x) ?? 0n;
|
|
25
|
+
const [la, lb, lp] = [lg(sqrtA), lg(sqrtB), lg(sqrtP)];
|
|
26
|
+
const one = 1n << 64n;
|
|
27
|
+
let w = 0n;
|
|
28
|
+
if (lb > la) {
|
|
29
|
+
const q = mulDiv(lb > lp ? lb - lp : 0n, one, lb - la, DOWN) ?? 0n;
|
|
30
|
+
w = q > one ? one : q;
|
|
31
|
+
}
|
|
32
|
+
return end.map((e, i) => {
|
|
33
|
+
const x = BigInt(start[i]) * w + BigInt(e) * (one - w);
|
|
34
|
+
return Number((x + one - 1n) / one);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const PREGRAD_PPM = [0, 0, 4_000, 1_000, 0];
|
|
39
|
+
|
|
40
|
+
export function ratesAt(graduated , own , end , start , sqrtA , sqrtB , sqrtP ) {
|
|
41
|
+
if (!graduated && !own) return [...PREGRAD_PPM];
|
|
42
|
+
return ppmAt(end, start, sqrtA, sqrtB, sqrtP);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const cheaper = (a , b ) => a.map((x, i) => Math.min(x, b[i]));
|
|
46
|
+
|
|
47
|
+
export class FeeAcc {
|
|
48
|
+
pre ;
|
|
49
|
+
whole = [0n, 0n, 0n, 0n, 0n];
|
|
50
|
+
rem = [0n, 0n, 0n, 0n, 0n];
|
|
51
|
+
constructor(pre ) {
|
|
52
|
+
this.pre = [...pre];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
add(q , at ) {
|
|
56
|
+
const [qh, ql] = [q / PPM, q % PPM];
|
|
57
|
+
const w = [...this.whole];
|
|
58
|
+
const r2 = [...this.rem];
|
|
59
|
+
for (let i = 0; i < FEE_LEGS; i++) {
|
|
60
|
+
const r = BigInt(Math.min(this.pre[i], at[i]));
|
|
61
|
+
const lo = ql * r;
|
|
62
|
+
const rem = r2[i] + (lo % PPM);
|
|
63
|
+
const x = w[i] + qh * r + lo / PPM + rem / PPM;
|
|
64
|
+
if (x > U128_MAX) return false;
|
|
65
|
+
w[i] = x;
|
|
66
|
+
r2[i] = rem % PPM;
|
|
67
|
+
}
|
|
68
|
+
this.whole = w;
|
|
69
|
+
this.rem = r2;
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
legs() {
|
|
74
|
+
return this.whole.map((x, i) => {
|
|
75
|
+
const v = x + (this.rem[i] > 0n ? 1n : 0n);
|
|
76
|
+
return v > U128_MAX ? U128_MAX : v;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function grossForNet(net , ppm ) {
|
|
82
|
+
const g = net + feeTotal(feeLegs(net, ppm));
|
|
83
|
+
return g > U128_MAX ? null : g;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function netFromGross(gross , ppm ) {
|
|
87
|
+
const sum = ppm.reduce((a, p) => a + BigInt(p), 0n);
|
|
88
|
+
let net = mulDiv(gross, PPM, PPM + sum, DOWN) ?? 0n;
|
|
89
|
+
for (;;) {
|
|
90
|
+
if (net >= gross) break;
|
|
91
|
+
const g = grossForNet(net + 1n, ppm);
|
|
92
|
+
if (g === null || g > gross) break;
|
|
93
|
+
net++;
|
|
94
|
+
}
|
|
95
|
+
for (;;) {
|
|
96
|
+
if (net <= 0n) break;
|
|
97
|
+
const g = grossForNet(net, ppm);
|
|
98
|
+
if (g !== null && g <= gross) break;
|
|
99
|
+
net--;
|
|
100
|
+
}
|
|
101
|
+
return net;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export const TRADER_REF_BPS = [3_000, 2_000, 1_000];
|
|
105
|
+
|
|
106
|
+
export const CREATOR_REF_BPS = [900, 90, 10];
|
|
107
|
+
|
|
108
|
+
export function slices(fee , present , bps ) {
|
|
109
|
+
const out = [0n, 0n, 0n];
|
|
110
|
+
let left = fee;
|
|
111
|
+
for (let i = 0; i < 3; i++) {
|
|
112
|
+
if (!present[i] || bps[i] === 0) continue;
|
|
113
|
+
let s = (fee * BigInt(bps[i])) / 10_000n;
|
|
114
|
+
if (s > left) s = left;
|
|
115
|
+
out[i] = s;
|
|
116
|
+
left -= s;
|
|
117
|
+
}
|
|
118
|
+
return [out, left];
|
|
119
|
+
}
|
|
120
|
+
export const platformLegs = (platform , traderPresent ) => slices(platform, traderPresent, TRADER_REF_BPS);
|
|
121
|
+
export const creatorLegs = (creator , creatorPresent ) => slices(creator, creatorPresent, CREATOR_REF_BPS);
|