@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,105 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { U64_MAX } from "./wide.js";
|
|
4
|
+
import { v2Fail } from "./error.js";
|
|
5
|
+
|
|
6
|
+
const Q64 = 64n;
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
export const newPosition = () => ({ amount: 0n, owed: 0n, indexSnap: 0n, optedOut: false });
|
|
11
|
+
|
|
12
|
+
export const optedOutPosition = () => ({ ...newPosition(), optedOut: true });
|
|
13
|
+
|
|
14
|
+
function u64(x ) {
|
|
15
|
+
if (x < 0n || x > U64_MAX) throw new RangeError("holders: u64 overflow");
|
|
16
|
+
return x;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function earned(amount , dIndex ) {
|
|
20
|
+
|
|
21
|
+
const hi = dIndex >> Q64;
|
|
22
|
+
const lo = dIndex & U64_MAX;
|
|
23
|
+
return BigInt.asUintN(128, amount * hi + ((amount * lo) >> Q64));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export const newHolderBook = () => ({ index: 0n, w: 0n, carry: 0n, bookedTotal: 0n, paidTotal: 0n });
|
|
27
|
+
|
|
28
|
+
export const cloneHolderBook = (b ) => ({ ...b });
|
|
29
|
+
|
|
30
|
+
export function holderAddReward(b , amount , sCirc ) {
|
|
31
|
+
const booked = b.bookedTotal + amount;
|
|
32
|
+
if (booked > U64_MAX) v2Fail("Overflow");
|
|
33
|
+
b.bookedTotal = booked;
|
|
34
|
+
const total = b.carry + amount;
|
|
35
|
+
if (total > U64_MAX) v2Fail("Overflow");
|
|
36
|
+
const w = b.w;
|
|
37
|
+
if (w === 0n || w > sCirc || total === 0n) {
|
|
38
|
+
b.carry = total;
|
|
39
|
+
return "Carried";
|
|
40
|
+
}
|
|
41
|
+
const step = (total << Q64) / w;
|
|
42
|
+
|
|
43
|
+
const out = (step * w + (1n << Q64) - 1n) >> Q64;
|
|
44
|
+
const index = b.index + step;
|
|
45
|
+
if (index >= 1n << 128n) v2Fail("Overflow");
|
|
46
|
+
b.index = index;
|
|
47
|
+
b.carry = total - (out < total ? out : total);
|
|
48
|
+
return "Indexed";
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function holderSettle(b , p ) {
|
|
52
|
+
if (!p.optedOut) p.owed = u64(p.owed + BigInt.asUintN(64, earned(p.amount, b.index - p.indexSnap)));
|
|
53
|
+
p.indexSnap = b.index;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function holderAdd(b , p , amount ) {
|
|
57
|
+
holderSettle(b, p);
|
|
58
|
+
const a = p.amount + amount;
|
|
59
|
+
if (a > U64_MAX) v2Fail("Overflow");
|
|
60
|
+
p.amount = a;
|
|
61
|
+
if (!p.optedOut) {
|
|
62
|
+
const w = b.w + amount;
|
|
63
|
+
if (w > U64_MAX) v2Fail("Overflow");
|
|
64
|
+
b.w = w;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function holderSub(b , p , amount ) {
|
|
69
|
+
if (amount > p.amount) v2Fail("NotHeld");
|
|
70
|
+
holderSettle(b, p);
|
|
71
|
+
p.amount -= amount;
|
|
72
|
+
if (!p.optedOut) b.w = u64(b.w - amount);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function holderSync(b , p , real ) {
|
|
76
|
+
if (real > p.amount) return holderAdd(b, p, real - p.amount);
|
|
77
|
+
if (real === p.amount) return;
|
|
78
|
+
if (!p.optedOut) {
|
|
79
|
+
const d = b.index - p.indexSnap;
|
|
80
|
+
const all = earned(p.amount, d);
|
|
81
|
+
const kept = earned(real, d);
|
|
82
|
+
p.owed = u64(p.owed + BigInt.asUintN(64, kept));
|
|
83
|
+
const carry = b.carry + BigInt.asUintN(64, all - kept);
|
|
84
|
+
if (carry > U64_MAX) v2Fail("Overflow");
|
|
85
|
+
b.carry = carry;
|
|
86
|
+
b.w = u64(b.w - (p.amount - real));
|
|
87
|
+
}
|
|
88
|
+
p.indexSnap = b.index;
|
|
89
|
+
p.amount = real;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function holderOptIn(b , p ) {
|
|
93
|
+
if (!p.optedOut) return;
|
|
94
|
+
p.optedOut = false;
|
|
95
|
+
p.indexSnap = b.index;
|
|
96
|
+
b.w = u64(b.w + p.amount);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function holderTake(b , p ) {
|
|
100
|
+
holderSettle(b, p);
|
|
101
|
+
const x = p.owed;
|
|
102
|
+
p.owed = 0n;
|
|
103
|
+
b.paidTotal = u64(b.paidTotal + x);
|
|
104
|
+
return x;
|
|
105
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { U128_MAX } from "./wide.js";
|
|
4
|
+
import { v2Fail } from "./error.js";
|
|
5
|
+
|
|
6
|
+
const u128 = (x ) => (x > U128_MAX ? v2Fail("Overflow") : x);
|
|
7
|
+
|
|
8
|
+
export function bookedFromRemainder(r , thetaBps ) {
|
|
9
|
+
const keep = 10_000n - BigInt(thetaBps);
|
|
10
|
+
if (keep < 0n) v2Fail("Overflow");
|
|
11
|
+
return u128(r * keep) / 10_000n;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function splitByRow(amount , platformBps , floorBps ) {
|
|
15
|
+
if (platformBps + floorBps > 10_000) v2Fail("RowInvalid");
|
|
16
|
+
const part = (b ) => u128(amount * BigInt(b)) / 10_000n;
|
|
17
|
+
const platform = part(platformBps);
|
|
18
|
+
const floorToken = part(floorBps);
|
|
19
|
+
return { platform, floorToken, class: amount - platform - floorToken };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function teamSplit(amount , levelsPresent ) {
|
|
23
|
+
const tenth = amount / 10n;
|
|
24
|
+
const w = [90n, 9n, 1n];
|
|
25
|
+
const referrers = [0n, 0n, 0n];
|
|
26
|
+
let paid = 0n;
|
|
27
|
+
for (let i = 0; i < 3; i++) {
|
|
28
|
+
if (levelsPresent[i]) {
|
|
29
|
+
referrers[i] = (tenth * w[i] ) / 100n;
|
|
30
|
+
paid += referrers[i];
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return { referrers, slot: amount - paid };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export function slotSplit(slot , sharesBps ) {
|
|
37
|
+
const parts = sharesBps.map((s) => (slot * BigInt(s)) / 10_000n);
|
|
38
|
+
const paid = parts.reduce((a, b) => a + b, 0n);
|
|
39
|
+
return { parts, dust: slot - paid };
|
|
40
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
const SPLIT = 134217729;
|
|
4
|
+
|
|
5
|
+
function twoSum(a , b ) {
|
|
6
|
+
const s = a + b;
|
|
7
|
+
const bb = s - a;
|
|
8
|
+
return [s, (a - (s - bb)) + (b - bb)];
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function twoProd(a , b ) {
|
|
12
|
+
const p = a * b;
|
|
13
|
+
let t = SPLIT * a;
|
|
14
|
+
const ah = t - (t - a);
|
|
15
|
+
const al = a - ah;
|
|
16
|
+
t = SPLIT * b;
|
|
17
|
+
const bh = t - (t - b);
|
|
18
|
+
const bl = b - bh;
|
|
19
|
+
return [p, ((ah * bh - p) + ah * bl + al * bh) + al * bl];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function add(x , y ) {
|
|
23
|
+
const [s, e] = twoSum(x[0], y[0]);
|
|
24
|
+
return twoSum(s, e + x[1] + y[1]);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function mul(x , y ) {
|
|
28
|
+
const [p, e] = twoProd(x[0], y[0]);
|
|
29
|
+
return twoSum(p, e + (x[0] * y[1] + x[1] * y[0]));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function divN(x , n ) {
|
|
33
|
+
const q = x[0] / n;
|
|
34
|
+
const [p, e] = twoProd(q, n);
|
|
35
|
+
return twoSum(q, (x[0] - p - e + x[1]) / n);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const LN2 = [0.6931471805599453, 2.3190468138462996e-17];
|
|
39
|
+
|
|
40
|
+
function expDD(x ) {
|
|
41
|
+
const n = Math.round(x[0] / LN2[0]);
|
|
42
|
+
let r = add(x, mul(LN2, [-n, 0]));
|
|
43
|
+
r = [r[0] / 1024, r[1] / 1024];
|
|
44
|
+
let sum = [1, 0];
|
|
45
|
+
let term = [1, 0];
|
|
46
|
+
for (let i = 1; i <= 14; i++) {
|
|
47
|
+
term = divN(mul(term, r), i);
|
|
48
|
+
sum = add(sum, term);
|
|
49
|
+
}
|
|
50
|
+
for (let i = 0; i < 10; i++) sum = mul(sum, sum);
|
|
51
|
+
const s = Math.pow(2, n);
|
|
52
|
+
return [sum[0] * s, sum[1] * s];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function lnDD(x ) {
|
|
56
|
+
let y = [Math.log(x), 0];
|
|
57
|
+
for (let i = 0; i < 2; i++) {
|
|
58
|
+
const e = expDD([-y[0], -y[1]]);
|
|
59
|
+
y = add(y, add(mul([x, 0], e), [-1, 0]));
|
|
60
|
+
}
|
|
61
|
+
return y;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function pow(x , y ) {
|
|
65
|
+
if (y === 0 || x === 1) return 1;
|
|
66
|
+
if (!(x > 0) || !Number.isFinite(x) || !Number.isFinite(y)) return Math.pow(x, y);
|
|
67
|
+
const r = expDD(mul(lnDD(x), [y, 0]));
|
|
68
|
+
return r[0] + r[1];
|
|
69
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { AccountDecodeError } from "../../solana/floor/accounts.js";
|
|
4
|
+
import { MAX_BANDS } from "./rows.js";
|
|
5
|
+
import { RANGES_MAX } from "./book.js";
|
|
6
|
+
|
|
7
|
+
export const ROW_GLOBALS = 112;
|
|
8
|
+
export const RANGE_ROW = 48;
|
|
9
|
+
|
|
10
|
+
export { RANGES_MAX };
|
|
11
|
+
|
|
12
|
+
export const ROW_LEN = ROW_GLOBALS + RANGES_MAX * RANGE_ROW;
|
|
13
|
+
export const ROW_VERSION = 2;
|
|
14
|
+
|
|
15
|
+
export const MS_CAP_OFF = 42;
|
|
16
|
+
export const RANGE_PAD_OFF = 46;
|
|
17
|
+
|
|
18
|
+
export const OFF = {
|
|
19
|
+
VERSION: 0,
|
|
20
|
+
MS_CLASS: 1,
|
|
21
|
+
N_RANGES: 2,
|
|
22
|
+
N_BANDS: 3,
|
|
23
|
+
FLAGS: 4,
|
|
24
|
+
TEAM_BUY_RULE: 5,
|
|
25
|
+
PHI: 6,
|
|
26
|
+
WC: 8,
|
|
27
|
+
PSI_TAIL: 10,
|
|
28
|
+
TEAM_BUY_SHARE: 12,
|
|
29
|
+
TEAM_SLOT_MAX: 14,
|
|
30
|
+
PLATFORM_MS: 16,
|
|
31
|
+
FLOOR_MS: 18,
|
|
32
|
+
FEE_PPM: 20,
|
|
33
|
+
BANDS: 40,
|
|
34
|
+
RAISE_USD_CENTS: 56,
|
|
35
|
+
LAUNCH_CAP_USD_CENTS: 64,
|
|
36
|
+
PAD_CK: 72,
|
|
37
|
+
CK_TARGET_BPS: 74,
|
|
38
|
+
FEE_START: 76,
|
|
39
|
+
FEE_A: 96,
|
|
40
|
+
FEE_B: 104,
|
|
41
|
+
} ;
|
|
42
|
+
|
|
43
|
+
const u128At = (v , o ) => v.getBigUint64(o, true) | (v.getBigUint64(o + 8, true) << 64n);
|
|
44
|
+
|
|
45
|
+
export function decodeRowV2(b ) {
|
|
46
|
+
if (b.length !== ROW_LEN) throw new AccountDecodeError(`RowV2: ${b.length} bytes, want ${ROW_LEN}`);
|
|
47
|
+
const v = new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
48
|
+
const u16 = (o ) => v.getUint16(o, true);
|
|
49
|
+
const u32 = (o ) => v.getUint32(o, true);
|
|
50
|
+
const u64 = (o ) => v.getBigUint64(o, true);
|
|
51
|
+
if (b[OFF.VERSION] !== ROW_VERSION) throw new AccountDecodeError(`RowV2: version ${b[OFF.VERSION]}`);
|
|
52
|
+
const n = b[OFF.N_RANGES];
|
|
53
|
+
const nb = b[OFF.N_BANDS];
|
|
54
|
+
if (n > RANGES_MAX) throw new AccountDecodeError(`RowV2: ${n} ranges`);
|
|
55
|
+
if (nb > MAX_BANDS) throw new AccountDecodeError(`RowV2: ${nb} bands`);
|
|
56
|
+
if (b[OFF.PAD_CK] !== 0 || b[OFF.PAD_CK + 1] !== 0) throw new AccountDecodeError("RowV2: pad not zero");
|
|
57
|
+
if (b.subarray(OFF.BANDS + 4 * nb, OFF.BANDS + 4 * MAX_BANDS).some((x) => x !== 0)) throw new AccountDecodeError("RowV2: a band past n_bands not zero");
|
|
58
|
+
const ranges = [];
|
|
59
|
+
for (let i = 0; i < RANGES_MAX; i++) {
|
|
60
|
+
const o = ROW_GLOBALS + i * RANGE_ROW;
|
|
61
|
+
if (i >= n) {
|
|
62
|
+
if (b.subarray(o, o + RANGE_ROW).some((x) => x !== 0)) throw new AccountDecodeError(`RowV2: unused range ${i} not zero`);
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
if (b.subarray(o + RANGE_PAD_OFF, o + RANGE_ROW).some((x) => x !== 0)) throw new AccountDecodeError(`RowV2: range ${i} pad not zero`);
|
|
66
|
+
ranges.push({
|
|
67
|
+
sqrtARel: u128At(v, o),
|
|
68
|
+
sqrtBRel: u128At(v, o + 16),
|
|
69
|
+
sharePpb: u32(o + 32),
|
|
70
|
+
thetaBps: u16(o + 36),
|
|
71
|
+
psiBps: u16(o + 38),
|
|
72
|
+
kind: b[o + 40],
|
|
73
|
+
class: b[o + 41],
|
|
74
|
+
msCapPpm: u32(o + MS_CAP_OFF),
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
const legs = (o ) => [0, 1, 2, 3, 4].map((i) => u32(o + 4 * i));
|
|
78
|
+
return {
|
|
79
|
+
version: b[OFF.VERSION],
|
|
80
|
+
msClass: b[OFF.MS_CLASS],
|
|
81
|
+
nBands: nb,
|
|
82
|
+
flags: b[OFF.FLAGS],
|
|
83
|
+
teamBuyRule: b[OFF.TEAM_BUY_RULE],
|
|
84
|
+
phiBps: u16(OFF.PHI),
|
|
85
|
+
wcBps: u16(OFF.WC),
|
|
86
|
+
psiTailBps: u16(OFF.PSI_TAIL),
|
|
87
|
+
teamBuyShareBps: u16(OFF.TEAM_BUY_SHARE),
|
|
88
|
+
teamSlotMaxBps: u16(OFF.TEAM_SLOT_MAX),
|
|
89
|
+
platformMsBps: u16(OFF.PLATFORM_MS),
|
|
90
|
+
floorMsBps: u16(OFF.FLOOR_MS),
|
|
91
|
+
feePpm: legs(OFF.FEE_PPM),
|
|
92
|
+
bands: Array.from({ length: MAX_BANDS }, (_, i) => ({ widthBps: u16(OFF.BANDS + 4 * i), shareBps: u16(OFF.BANDS + 2 + 4 * i) })),
|
|
93
|
+
raiseUsdCents: u64(OFF.RAISE_USD_CENTS),
|
|
94
|
+
launchCapUsdCents: u64(OFF.LAUNCH_CAP_USD_CENTS),
|
|
95
|
+
ckTargetBps: u16(OFF.CK_TARGET_BPS),
|
|
96
|
+
feeStartPpm: legs(OFF.FEE_START),
|
|
97
|
+
feeARel: u64(OFF.FEE_A),
|
|
98
|
+
feeBRel: u64(OFF.FEE_B),
|
|
99
|
+
ranges,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function encodeRowV2(r ) {
|
|
104
|
+
if (r.ranges.length > RANGES_MAX) throw new RangeError(`${r.ranges.length} ranges > ${RANGES_MAX}`);
|
|
105
|
+
if (r.bands.length > MAX_BANDS) throw new RangeError(`${r.bands.length} bands > ${MAX_BANDS}`);
|
|
106
|
+
const b = new Uint8Array(ROW_LEN);
|
|
107
|
+
const v = new DataView(b.buffer);
|
|
108
|
+
const u16 = (o , x ) => v.setUint16(o, x, true);
|
|
109
|
+
const u32 = (o , x ) => v.setUint32(o, x, true);
|
|
110
|
+
const u64 = (o , x ) => v.setBigUint64(o, BigInt.asUintN(64, x), true);
|
|
111
|
+
const u128 = (o , x ) => {
|
|
112
|
+
u64(o, x & ((1n << 64n) - 1n));
|
|
113
|
+
u64(o + 8, x >> 64n);
|
|
114
|
+
};
|
|
115
|
+
b[OFF.VERSION] = ROW_VERSION;
|
|
116
|
+
b[OFF.MS_CLASS] = r.msClass;
|
|
117
|
+
b[OFF.N_RANGES] = r.ranges.length;
|
|
118
|
+
b[OFF.N_BANDS] = r.bands.length;
|
|
119
|
+
b[OFF.FLAGS] = r.flags;
|
|
120
|
+
b[OFF.TEAM_BUY_RULE] = r.teamBuyRule;
|
|
121
|
+
u16(OFF.PHI, r.phiBps);
|
|
122
|
+
u16(OFF.WC, r.wcBps);
|
|
123
|
+
u16(OFF.PSI_TAIL, r.psiTailBps);
|
|
124
|
+
u16(OFF.TEAM_BUY_SHARE, r.teamBuyShareBps);
|
|
125
|
+
u16(OFF.TEAM_SLOT_MAX, r.teamSlotMaxBps);
|
|
126
|
+
u16(OFF.PLATFORM_MS, r.platformMsBps);
|
|
127
|
+
u16(OFF.FLOOR_MS, r.floorMsBps);
|
|
128
|
+
r.feePpm.forEach((x, i) => u32(OFF.FEE_PPM + 4 * i, x));
|
|
129
|
+
r.bands.forEach((x, i) => {
|
|
130
|
+
u16(OFF.BANDS + 4 * i, x.widthBps);
|
|
131
|
+
u16(OFF.BANDS + 2 + 4 * i, x.shareBps);
|
|
132
|
+
});
|
|
133
|
+
u64(OFF.RAISE_USD_CENTS, r.raiseUsdCents);
|
|
134
|
+
u64(OFF.LAUNCH_CAP_USD_CENTS, r.launchCapUsdCents);
|
|
135
|
+
u16(OFF.CK_TARGET_BPS, r.ckTargetBps ?? 0);
|
|
136
|
+
(r.feeStartPpm ?? [0, 0, 0, 0, 0]).forEach((x, i) => u32(OFF.FEE_START + 4 * i, x));
|
|
137
|
+
u64(OFF.FEE_A, r.feeARel ?? 0n);
|
|
138
|
+
u64(OFF.FEE_B, r.feeBRel ?? 0n);
|
|
139
|
+
r.ranges.forEach((g, i) => {
|
|
140
|
+
const o = ROW_GLOBALS + i * RANGE_ROW;
|
|
141
|
+
u128(o, g.sqrtARel);
|
|
142
|
+
u128(o + 16, g.sqrtBRel);
|
|
143
|
+
u32(o + 32, g.sharePpb);
|
|
144
|
+
u16(o + 36, g.thetaBps);
|
|
145
|
+
u16(o + 38, g.psiBps);
|
|
146
|
+
b[o + 40] = g.kind;
|
|
147
|
+
b[o + 41] = g.class;
|
|
148
|
+
u32(o + MS_CAP_OFF, g.msCapPpm ?? 0);
|
|
149
|
+
});
|
|
150
|
+
return b;
|
|
151
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { MAX_WALK_STEPS, ckMoney, cloneBook } from "./book.js";
|
|
4
|
+
|
|
5
|
+
import { bestBidPriceX64V2, dexLiquidityV2, nextBuyPriceX64, stackViewV2 } from "./display.js";
|
|
6
|
+
|
|
7
|
+
import { FEE_LEGS, LEG_BACKING, LEG_FLOOR, LEG_HOLDER, LEG_PLATFORM, LEG_TEAM, feeTotal } from "./fees.js";
|
|
8
|
+
import { creatorLegsV2, platformLegsV2, splitMembersV2 } from "./team.js";
|
|
9
|
+
import { bookBuy, bookSell } from "./trade.js";
|
|
10
|
+
|
|
11
|
+
import { U64_MAX, U128_MAX } from "./wide.js";
|
|
12
|
+
|
|
13
|
+
const PPM = 1_000_000n;
|
|
14
|
+
|
|
15
|
+
export function priceImpactV2(spotX64 , money , tokens , side ) {
|
|
16
|
+
const avgX64 = tokens > 0n ? (money << 64n) / tokens : 0n;
|
|
17
|
+
let ppm = null;
|
|
18
|
+
if (spotX64 > 0n && tokens > 0n) {
|
|
19
|
+
const at = spotX64 * tokens;
|
|
20
|
+
const d = side === "buy" ? (money << 64n) - at : at - (money << 64n);
|
|
21
|
+
ppm = (d * PPM) / at;
|
|
22
|
+
}
|
|
23
|
+
return { spotX64, avgX64, ppm };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const dropOf = (before , after ) => (before === 0n ? 0n : ((before - after) * PPM) / before);
|
|
27
|
+
|
|
28
|
+
export function quoteBuyImpactV2(b , gross , walkSteps = MAX_WALK_STEPS) {
|
|
29
|
+
const beforeX64 = nextBuyPriceX64(b);
|
|
30
|
+
const after = cloneBook(b);
|
|
31
|
+
const quote = bookBuy(after, gross, 0n, walkSteps);
|
|
32
|
+
const afterX64 = nextBuyPriceX64(after);
|
|
33
|
+
return { quote, impact: { ...priceImpactV2(beforeX64, quote.net, quote.tokens, "buy"), beforeX64, afterX64, dropPpm: dropOf(beforeX64, afterX64) } };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const SELL_FLAG_DROP_PPM = 100_000n;
|
|
37
|
+
|
|
38
|
+
export function quoteSellImpactV2(b , tokens , circulating ) {
|
|
39
|
+
const beforeX64 = nextBuyPriceX64(b);
|
|
40
|
+
const bid = bestBidPriceX64V2(b) ?? 0n;
|
|
41
|
+
const after = cloneBook(b);
|
|
42
|
+
const quote = bookSell(after, tokens, 0n);
|
|
43
|
+
const afterX64 = nextBuyPriceX64(after);
|
|
44
|
+
const filled = quote.tokensIn - quote.unfilled;
|
|
45
|
+
const moneyBidding = dexLiquidityV2(b);
|
|
46
|
+
const cushion = ckMoney(b);
|
|
47
|
+
const stage = stackViewV2(b).stage;
|
|
48
|
+
const dropPpm = dropOf(beforeX64, afterX64);
|
|
49
|
+
const small = (circulating > 0n && filled * 100n <= circulating && dropPpm >= SELL_FLAG_DROP_PPM) || (quote.gross <= cushion && dropPpm > SELL_FLAG_DROP_PPM);
|
|
50
|
+
return {
|
|
51
|
+
quote,
|
|
52
|
+
impact: {
|
|
53
|
+
...priceImpactV2(bid, quote.gross, filled, "sell"), beforeX64, afterX64, dropPpm,
|
|
54
|
+
circulating, shareOfCirculatingPpm: circulating > 0n ? (filled * PPM) / circulating : null,
|
|
55
|
+
moneyBidding, shareOfBiddingPpm: moneyBidding > 0n ? (quote.gross * PPM) / moneyBidding : null,
|
|
56
|
+
cushion, stage, outOfProportion: stage === "stack" && small,
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const FEE_NAMES_V2 = ["Backing Fee", "Team Fee", "Platform fee", "$FLOOR fee", "Holder Fee"];
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
export function feePartsV2(fees , opts = {}) {
|
|
70
|
+
if (fees.length !== FEE_LEGS) throw new RangeError(`fees: ${fees.length} legs, want ${FEE_LEGS}`);
|
|
71
|
+
const none = [false, false, false];
|
|
72
|
+
const team = fees[LEG_TEAM];
|
|
73
|
+
const platform = fees[LEG_PLATFORM];
|
|
74
|
+
const [teamCreatorRefs, teamRest] = creatorLegsV2(team, opts.creatorRefs ?? none);
|
|
75
|
+
const [platformTraderRefs, platformBucket] = platformLegsV2(platform, opts.traderRefs ?? none);
|
|
76
|
+
const w = opts.slotWeights;
|
|
77
|
+
const split = w !== undefined && w.some((x) => x !== 0);
|
|
78
|
+
const [teamMembers, teamDust] = split ? splitMembersV2(teamRest, w) : [Array(w?.length ?? 10).fill(0n) , 0n];
|
|
79
|
+
return {
|
|
80
|
+
backing: fees[LEG_BACKING], backingKept: fees[LEG_BACKING], backingCut: 0n,
|
|
81
|
+
team, teamCreatorRefs, teamCreator: split ? 0n : teamRest, teamMembers, teamDust,
|
|
82
|
+
platform, platformTraderRefs, platformBucket,
|
|
83
|
+
floorToken: fees[LEG_FLOOR], holder: fees[LEG_HOLDER], total: feeTotal(fees),
|
|
84
|
+
parts: FEE_NAMES_V2.map((name, i) => ({ name, amount: fees[i] })),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const partsOf = (p ) => ({
|
|
89
|
+
team: p[0], holder: p[1], platform: p[2], floorToken: p[3], total: p[0] + p[1] + p[2] + p[3],
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
export function buyMilestonesV2(r ) {
|
|
93
|
+
const booked = partsOf(r.split.booked);
|
|
94
|
+
return { booked, paid: booked };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const EVM_TOKEN_SCALE = 1_000_000_000_000n;
|
|
98
|
+
|
|
99
|
+
export function maxLegV2(chain , side ) {
|
|
100
|
+
if (chain === "solana") return U64_MAX;
|
|
101
|
+
return side === "buy" ? U128_MAX : U64_MAX * EVM_TOKEN_SCALE;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function checkLegV2(amount , chain , side ) {
|
|
105
|
+
const max = maxLegV2(chain, side);
|
|
106
|
+
if (amount < 1n || amount > max) throw new RangeError(`${chain} ${side}: ${amount} outside 1 ..= ${max}`);
|
|
107
|
+
return amount;
|
|
108
|
+
}
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { fail } from "../index.js";
|
|
4
|
+
import { BIDS_MAX, KIND_BONDING, rangeIsFlat, rangeLiquidity } from "./book.js";
|
|
5
|
+
|
|
6
|
+
import { FRAC, flatMoneyForTokens, flatTokensForMoney, liquidityForMoney, moneyBetween, sqrtAfterTokens, tokensBetween } from "./sqrt.js";
|
|
7
|
+
|
|
8
|
+
import { DOWN, UP, U128_MAX, U64_MAX, divW, isqrt, mulDiv, shlDiv } from "./wide.js";
|
|
9
|
+
|
|
10
|
+
const CUT_NUDGES = 8;
|
|
11
|
+
|
|
12
|
+
const ov = (x ) => (x === null ? fail("Overflow") : x);
|
|
13
|
+
const u128 = (x ) => (x > U128_MAX ? fail("Overflow") : x);
|
|
14
|
+
const u64 = (x ) => (x > U64_MAX ? fail("Overflow") : x);
|
|
15
|
+
|
|
16
|
+
function slice(l , a , hi , sqrtF ) {
|
|
17
|
+
const money = ov(moneyBetween(l, a, hi, DOWN));
|
|
18
|
+
const tok = ov(tokensBetween(l, a, hi, UP));
|
|
19
|
+
const f = ov(flatMoneyForTokens(sqrtF, tok, UP));
|
|
20
|
+
return { money, tok, fIn: f < money ? f : money };
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function cutRoot(l , hi , sqrtF , left ) {
|
|
24
|
+
const f2 = sqrtF * sqrtF;
|
|
25
|
+
const fHi = divW(f2, hi, UP);
|
|
26
|
+
if (fHi === null || fHi > U128_MAX) return null;
|
|
27
|
+
const lt = shlDiv(left, FRAC, l, DOWN);
|
|
28
|
+
if (lt === null || lt > U128_MAX) return null;
|
|
29
|
+
const sum = hi + fHi;
|
|
30
|
+
if (sum > U128_MAX) return null;
|
|
31
|
+
const beta = sum - lt;
|
|
32
|
+
if (beta < 0n) return null;
|
|
33
|
+
const disc = beta * beta - 4n * f2;
|
|
34
|
+
const sq = beta + isqrt(disc < 0n ? 0n : disc);
|
|
35
|
+
if (sq > U128_MAX) return null;
|
|
36
|
+
return sq / 2n + (sq & 1n);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function absorb(sqrtF , edge , left ) {
|
|
40
|
+
if (edge <= sqrtF) return { lo: 0n, hi: 0n, money: 0n, fIn: 0n };
|
|
41
|
+
const money = ov(mulDiv(left, edge, edge - sqrtF, DOWN));
|
|
42
|
+
const f = ov(mulDiv(money, sqrtF, edge, UP));
|
|
43
|
+
return { lo: sqrtF, hi: edge, money, fIn: f < money ? f : money };
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function mirror(ranges , nRanges , sqrtF , top0 , premium , tokCap , segMax = BIDS_MAX, absorbLast = false) {
|
|
47
|
+
const segs = [];
|
|
48
|
+
let left = premium;
|
|
49
|
+
let tokLeft = tokCap;
|
|
50
|
+
let top = top0;
|
|
51
|
+
if (left === 0n || tokLeft === 0n || segMax <= 0 || top <= sqrtF) return { segs, tokens: 0n, left };
|
|
52
|
+
let cutDone = false;
|
|
53
|
+
for (let i = nRanges - 1; i >= 0; i--) {
|
|
54
|
+
const r = ranges[i];
|
|
55
|
+
if (r.kind === KIND_BONDING || rangeIsFlat(r) || r.sqrtA >= top) continue;
|
|
56
|
+
const hi = top < r.sqrtB ? top : r.sqrtB;
|
|
57
|
+
const lo = r.sqrtA > sqrtF ? r.sqrtA : sqrtF;
|
|
58
|
+
if (hi <= lo) continue;
|
|
59
|
+
const l = ov(rangeLiquidity(r));
|
|
60
|
+
if (l === 0n) continue;
|
|
61
|
+
if (absorbLast && segs.length + 1 === segMax) break;
|
|
62
|
+
const w = slice(l, lo, hi, sqrtF);
|
|
63
|
+
if (w.money - w.fIn <= left && w.tok <= tokLeft) {
|
|
64
|
+
segs.push({ lo, hi, money: w.money, fIn: w.fIn });
|
|
65
|
+
left -= w.money - w.fIn;
|
|
66
|
+
tokLeft -= w.tok;
|
|
67
|
+
top = lo;
|
|
68
|
+
if (left === 0n || tokLeft === 0n || segs.length === segMax) break;
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
const at = sqrtAfterTokens(l, hi, tokLeft) ?? lo;
|
|
72
|
+
const ap = cutRoot(l, hi, sqrtF, left) ?? lo;
|
|
73
|
+
let a = ap > at ? ap : at;
|
|
74
|
+
if (a < lo) a = lo;
|
|
75
|
+
if (a > hi) a = hi;
|
|
76
|
+
for (let k = 0; k < CUT_NUDGES + 1 && a < hi; k++, a++) {
|
|
77
|
+
const c = slice(l, a, hi, sqrtF);
|
|
78
|
+
if (c.money - c.fIn <= left && c.tok <= tokLeft) {
|
|
79
|
+
segs.push({ lo: a, hi, money: c.money, fIn: c.fIn });
|
|
80
|
+
left -= c.money - c.fIn;
|
|
81
|
+
tokLeft -= c.tok;
|
|
82
|
+
top = a;
|
|
83
|
+
break;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
cutDone = true;
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (absorbLast && !cutDone && left > 0n && tokLeft > 0n && segs.length + 1 === segMax && top > sqrtF) {
|
|
91
|
+
const g = absorb(sqrtF, top, left);
|
|
92
|
+
|
|
93
|
+
const tok = g.money === 0n ? 0n : ov(tokensBetween(ov(liquidityForMoney(g.lo, g.hi, g.money)), g.lo, g.hi, UP));
|
|
94
|
+
if (g.money > 0n && tok <= tokLeft) {
|
|
95
|
+
tokLeft -= tok;
|
|
96
|
+
left -= g.money - g.fIn;
|
|
97
|
+
segs.push(g);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return { segs, tokens: tokCap - tokLeft, left };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function relay(b , row , extraCk , circ , ckOut = 0n) {
|
|
104
|
+
const p = b.sqrtP;
|
|
105
|
+
const f = b.sqrtF;
|
|
106
|
+
const zero = () => ({ lo: 0n, hi: 0n, money: 0n, fIn: 0n });
|
|
107
|
+
|
|
108
|
+
let pool = u128(b.ckAtF + extraCk);
|
|
109
|
+
let floor = b.floor - b.ckAtF;
|
|
110
|
+
const edge = Array(BIDS_MAX + 1).fill(0n);
|
|
111
|
+
const segs = Array.from({ length: BIDS_MAX }, zero);
|
|
112
|
+
const tok = Array(BIDS_MAX).fill(0n);
|
|
113
|
+
const claim = Array(BIDS_MAX).fill(0n);
|
|
114
|
+
const cflag = Array(BIDS_MAX).fill(false);
|
|
115
|
+
let n = 0;
|
|
116
|
+
let kept = 0;
|
|
117
|
+
for (let i = 0; i < b.nBands; i++) {
|
|
118
|
+
const lo = b.bandEdge[i + 1];
|
|
119
|
+
const hi = b.bandEdge[i];
|
|
120
|
+
|
|
121
|
+
const fi = b.bandF[i] < b.bandMoney[i] ? b.bandF[i] : b.bandMoney[i];
|
|
122
|
+
pool = u128(pool + b.bandMoney[i] - fi);
|
|
123
|
+
floor = u128(floor + fi);
|
|
124
|
+
const klo = lo > p ? lo : p;
|
|
125
|
+
if (hi > klo && (b.bandTokens[i] > 0n || b.bandFClaim[i] > 0n)) {
|
|
126
|
+
|
|
127
|
+
if (n > 0 && edge[n] !== hi) {
|
|
128
|
+
segs[n] = { lo: hi, hi: edge[n], money: 0n, fIn: 0n };
|
|
129
|
+
edge[n + 1] = hi;
|
|
130
|
+
n++;
|
|
131
|
+
}
|
|
132
|
+
edge[n] = hi;
|
|
133
|
+
segs[n] = { lo: klo, hi, money: 0n, fIn: 0n };
|
|
134
|
+
tok[n] = b.bandTokens[i];
|
|
135
|
+
claim[n] = b.bandFClaim[i];
|
|
136
|
+
cflag[n] = i < b.nC;
|
|
137
|
+
edge[n + 1] = klo;
|
|
138
|
+
n++;
|
|
139
|
+
kept++;
|
|
140
|
+
} else if (b.bandTokens[i] > 0n) {
|
|
141
|
+
|
|
142
|
+
const t = b.bandTokens[i];
|
|
143
|
+
let mine = t;
|
|
144
|
+
if (f !== 0n) {
|
|
145
|
+
const c = ov(flatTokensForMoney(f, b.bandFClaim[i], UP));
|
|
146
|
+
mine = c < t ? c : t;
|
|
147
|
+
}
|
|
148
|
+
b.floorTokens = u64(b.floorTokens + t);
|
|
149
|
+
b.ckFloorTokens = u64(b.ckFloorTokens + t - mine);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (ckOut > pool) fail("Overflow");
|
|
153
|
+
pool -= ckOut;
|
|
154
|
+
const room = p > f && f > 0n;
|
|
155
|
+
if (room && n > 0 && edge[n] > p) {
|
|
156
|
+
segs[n] = { lo: p, hi: edge[n], money: 0n, fIn: 0n };
|
|
157
|
+
edge[n + 1] = p;
|
|
158
|
+
n++;
|
|
159
|
+
}
|
|
160
|
+
const nTop = n;
|
|
161
|
+
const r = { nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kept };
|
|
162
|
+
let lk = pool;
|
|
163
|
+
if (room && n + 1 < BIDS_MAX) {
|
|
164
|
+
|
|
165
|
+
const cp = ov(mulDiv(pool, BigInt(row.cShareBps), 10_000n, DOWN));
|
|
166
|
+
const backed = ov(flatTokensForMoney(f, floor, DOWN));
|
|
167
|
+
let cap = circ;
|
|
168
|
+
let laid ;
|
|
169
|
+
let nc ;
|
|
170
|
+
for (;;) {
|
|
171
|
+
const c = mirror(b.ranges, b.nRanges, f, p, cp, cap, BIDS_MAX - 1 - n, false);
|
|
172
|
+
nc = c.segs.length;
|
|
173
|
+
const ktop = nc > 0 ? c.segs[nc - 1].lo : p;
|
|
174
|
+
const k = mirror(b.ranges, b.nRanges, f, ktop, pool - cp + c.left, cap - c.tokens, BIDS_MAX - n - nc, true);
|
|
175
|
+
laid = [...c.segs, ...k.segs];
|
|
176
|
+
let fi = 0n;
|
|
177
|
+
for (const g of laid) fi += g.fIn;
|
|
178
|
+
if (fi <= floor || cap === 0n) {
|
|
179
|
+
lk = k.left;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (cap > backed) cap = backed;
|
|
184
|
+
else {
|
|
185
|
+
const short = ov(flatTokensForMoney(f, fi - floor, UP));
|
|
186
|
+
const dropped = short + BigInt(laid.length) + 1n;
|
|
187
|
+
cap = cap > dropped ? cap - dropped : 0n;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
r.nC = nc;
|
|
191
|
+
r.nK = laid.length - nc;
|
|
192
|
+
if (n === 0 && laid.length > 0) edge[0] = p;
|
|
193
|
+
laid.forEach((g, j) => {
|
|
194
|
+
segs[n + j] = g;
|
|
195
|
+
edge[n + j + 1] = g.lo;
|
|
196
|
+
cflag[n + j] = j < nc;
|
|
197
|
+
if (j < nc) r.laidC += g.money - g.fIn;
|
|
198
|
+
else r.laidK += g.money - g.fIn;
|
|
199
|
+
});
|
|
200
|
+
n += laid.length;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let fIn = 0n;
|
|
204
|
+
for (let i = nTop; i < n; i++) fIn += segs[i].fIn;
|
|
205
|
+
if (floor < fIn) fail("Overflow");
|
|
206
|
+
b.floor = floor - fIn + lk;
|
|
207
|
+
b.ckAtF = lk;
|
|
208
|
+
b.nBands = n;
|
|
209
|
+
b.nC = cflag.slice(0, n).filter((c) => c).length;
|
|
210
|
+
b.bandEdge = Array.from({ length: BIDS_MAX + 1 }, (_, i) => (n > 0 && i <= n ? edge[i] : 0n));
|
|
211
|
+
b.bandMoney = segs.map((g, i) => (i < n ? g.money : 0n));
|
|
212
|
+
b.bandF = segs.map((g, i) => (i < n ? g.fIn : 0n));
|
|
213
|
+
b.bandTokens = tok.map((t, i) => (i < n ? t : 0n));
|
|
214
|
+
b.bandFClaim = claim.map((c, i) => (i < n ? c : 0n));
|
|
215
|
+
r.atF = lk;
|
|
216
|
+
return r;
|
|
217
|
+
}
|