@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.
Files changed (292) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/LICENSE +201 -60
  3. package/README.md +45 -24
  4. package/dist/abi/evm-event-names.js +12 -0
  5. package/dist/abi/evm.gen.js +130 -0
  6. package/dist/abi/functions.gen.js +144 -0
  7. package/dist/abi/index.js +17 -0
  8. package/dist/abi/solana.gen.js +128 -0
  9. package/dist/burn.js +21 -0
  10. package/dist/client/evm/index.js +191 -0
  11. package/dist/client/index.js +19 -0
  12. package/dist/client/solana/index.js +259 -0
  13. package/dist/client/types.js +5 -0
  14. package/dist/codec.js +77 -0
  15. package/dist/config/codeHash.js +9 -0
  16. package/dist/config/generated.js +746 -0
  17. package/dist/config/index.js +49 -0
  18. package/dist/config/registry.gen.js +118 -0
  19. package/dist/config/solana-genesis.js +7 -0
  20. package/dist/ed25519.js +31 -0
  21. package/dist/errors.js +249 -0
  22. package/dist/events/index.js +40 -0
  23. package/dist/events/types.gen.js +3 -0
  24. package/dist/evm/abi.js +283 -0
  25. package/dist/evm/balances.js +26 -0
  26. package/dist/evm/chains.js +144 -0
  27. package/dist/evm/floor/abi.gen.js +195 -0
  28. package/dist/evm/floor/calls.js +230 -0
  29. package/dist/evm/floor/create.js +49 -0
  30. package/dist/evm/floor/events.js +41 -0
  31. package/dist/evm/floor/iface.js +44 -0
  32. package/dist/evm/items.js +209 -0
  33. package/dist/evm/keccak.js +118 -0
  34. package/dist/evm/multicall.js +30 -0
  35. package/dist/evm/router.js +184 -0
  36. package/dist/evm/zap.js +128 -0
  37. package/dist/fixtures.js +53 -0
  38. package/dist/generated/index.js +28 -0
  39. package/dist/generated/interface.gen.js +146 -0
  40. package/dist/handle.js +22 -0
  41. package/dist/index.js +91 -7
  42. package/dist/indexer/bodies.js +473 -0
  43. package/dist/indexer/client.js +104 -0
  44. package/dist/indexer/contract.js +349 -0
  45. package/dist/indexer/decode.js +97 -0
  46. package/dist/indexer/partners.js +100 -0
  47. package/dist/indexer/paths.js +127 -0
  48. package/dist/indexer/read.js +109 -0
  49. package/dist/indexer/stream.js +71 -0
  50. package/dist/lock.js +16 -0
  51. package/dist/math/fixed.js +154 -0
  52. package/dist/math/genesis.js +39 -0
  53. package/dist/math/index.js +20 -0
  54. package/dist/math/u256.js +49 -0
  55. package/dist/math/v2/ask.js +162 -0
  56. package/dist/math/v2/book.js +607 -0
  57. package/dist/math/v2/circulating.js +62 -0
  58. package/dist/math/v2/create.js +122 -0
  59. package/dist/math/v2/display.js +208 -0
  60. package/dist/math/v2/error.js +17 -0
  61. package/dist/math/v2/feasibility.js +174 -0
  62. package/dist/math/v2/fees.js +121 -0
  63. package/dist/math/v2/holders.js +105 -0
  64. package/dist/math/v2/milestones.js +40 -0
  65. package/dist/math/v2/pow.js +69 -0
  66. package/dist/math/v2/preset.js +151 -0
  67. package/dist/math/v2/receipt.js +108 -0
  68. package/dist/math/v2/relay.js +217 -0
  69. package/dist/math/v2/rows.js +365 -0
  70. package/dist/math/v2/split.js +111 -0
  71. package/dist/math/v2/sqrt.js +84 -0
  72. package/dist/math/v2/supply.js +21 -0
  73. package/dist/math/v2/team.js +75 -0
  74. package/dist/math/v2/trade.js +126 -0
  75. package/dist/math/v2/wide.js +51 -0
  76. package/dist/quote.js +6 -0
  77. package/dist/registryPrice.js +23 -0
  78. package/dist/route/edge-jupiter.js +42 -0
  79. package/dist/route/edge.js +11 -0
  80. package/dist/route/graph.js +51 -0
  81. package/dist/route/path.js +75 -0
  82. package/dist/route/quote.js +15 -0
  83. package/dist/route-jupiter.js +35 -0
  84. package/dist/route.js +8 -0
  85. package/dist/sha256.js +61 -0
  86. package/dist/solana/alt.js +18 -0
  87. package/dist/solana/floor/accounts.js +390 -0
  88. package/dist/solana/floor/borsh.js +66 -0
  89. package/dist/solana/floor/create.js +65 -0
  90. package/dist/solana/floor/events.js +140 -0
  91. package/dist/solana/floor/genesis.js +91 -0
  92. package/dist/solana/floor/genesisList.js +68 -0
  93. package/dist/solana/floor/instructions.js +435 -0
  94. package/dist/solana/floor/metadata.js +198 -0
  95. package/dist/solana/floor/mint.js +206 -0
  96. package/dist/solana/floor/program.js +64 -0
  97. package/dist/solana/floor/registry.js +83 -0
  98. package/dist/solana/instruction.js +49 -0
  99. package/dist/solana/pda.js +88 -0
  100. package/dist/solana/tx.js +138 -0
  101. package/dist/v2/burnPreview.js +35 -0
  102. package/dist/v2/circulating.js +11 -0
  103. package/dist/v2/claimAllEvm.js +70 -0
  104. package/dist/v2/claims-evm.js +120 -0
  105. package/dist/v2/claims.js +218 -0
  106. package/dist/v2/dead.js +21 -0
  107. package/dist/v2/devbuy.js +37 -0
  108. package/dist/v2/events.js +115 -0
  109. package/dist/v2/evm.js +299 -0
  110. package/dist/v2/evmReads.js +76 -0
  111. package/dist/v2/genesis.js +76 -0
  112. package/dist/v2/layouts.js +217 -0
  113. package/dist/v2/market-evm.js +74 -0
  114. package/dist/v2/presetRowEvm.js +45 -0
  115. package/dist/v2/press.js +106 -0
  116. package/dist/v2/quoteMint.js +52 -0
  117. package/dist/v2/receipt-tx.js +149 -0
  118. package/dist/v2/route-alt.js +114 -0
  119. package/dist/v2/route.js +389 -0
  120. package/dist/v2/solana.js +391 -0
  121. package/dist/v2/solanaSettle.js +97 -0
  122. package/dist/v2/tx/evm.js +126 -0
  123. package/dist/v2/tx/genesis.js +47 -0
  124. package/dist/v2/tx/index.js +5 -0
  125. package/dist/v2/tx/solana.js +118 -0
  126. package/dist/v2/types.js +13 -0
  127. package/docs/aggregator.md +61 -0
  128. package/docs/bot.md +50 -0
  129. package/docs/frontend.md +29 -0
  130. package/docs/wallet.md +54 -0
  131. package/package.json +428 -32
  132. package/src/abi/evm-event-names.ts +12 -0
  133. package/src/abi/evm.gen.ts +139 -0
  134. package/src/abi/functions.gen.ts +154 -0
  135. package/src/abi/index.ts +30 -0
  136. package/src/abi/solana.gen.ts +138 -0
  137. package/src/burn.ts +37 -0
  138. package/src/client/evm/index.ts +199 -0
  139. package/src/client/index.ts +23 -0
  140. package/src/client/solana/index.ts +260 -0
  141. package/src/client/types.ts +162 -0
  142. package/src/codec.ts +77 -0
  143. package/src/config/codeHash.ts +9 -0
  144. package/src/config/generated.ts +746 -0
  145. package/src/config/index.ts +91 -0
  146. package/src/config/registry.gen.ts +118 -0
  147. package/src/config/solana-genesis.ts +7 -0
  148. package/src/ed25519.ts +31 -0
  149. package/src/errors.ts +270 -0
  150. package/src/events/index.ts +44 -0
  151. package/src/events/types.gen.ts +831 -0
  152. package/src/evm/abi.ts +292 -0
  153. package/src/evm/balances.ts +26 -0
  154. package/src/evm/chains.ts +167 -0
  155. package/src/evm/floor/abi.gen.ts +195 -0
  156. package/src/evm/floor/calls.ts +247 -0
  157. package/src/evm/floor/create.ts +68 -0
  158. package/src/evm/floor/events.ts +51 -0
  159. package/src/evm/floor/iface.ts +45 -0
  160. package/src/evm/items.ts +252 -0
  161. package/src/evm/keccak.ts +118 -0
  162. package/src/evm/multicall.ts +43 -0
  163. package/src/evm/router.ts +209 -0
  164. package/src/evm/zap.ts +152 -0
  165. package/src/fixtures.ts +53 -0
  166. package/src/generated/index.ts +29 -0
  167. package/src/generated/interface.gen.ts +153 -0
  168. package/src/handle.ts +22 -0
  169. package/src/index.ts +91 -6
  170. package/src/indexer/bodies.ts +528 -0
  171. package/src/indexer/client.ts +178 -0
  172. package/src/indexer/contract.ts +410 -0
  173. package/src/indexer/decode.ts +109 -0
  174. package/src/indexer/partners.ts +109 -0
  175. package/src/indexer/paths.ts +190 -0
  176. package/src/indexer/read.ts +119 -0
  177. package/src/indexer/stream.ts +83 -0
  178. package/src/lock.ts +16 -0
  179. package/src/math/fixed.ts +154 -0
  180. package/src/math/genesis.ts +39 -0
  181. package/src/math/index.ts +24 -0
  182. package/src/math/u256.ts +49 -0
  183. package/src/math/v2/ask.ts +218 -0
  184. package/src/math/v2/book.ts +660 -0
  185. package/src/math/v2/circulating.ts +69 -0
  186. package/src/math/v2/create.ts +139 -0
  187. package/src/math/v2/display.ts +250 -0
  188. package/src/math/v2/error.ts +19 -0
  189. package/src/math/v2/feasibility.ts +200 -0
  190. package/src/math/v2/fees.ts +121 -0
  191. package/src/math/v2/holders.ts +124 -0
  192. package/src/math/v2/milestones.ts +51 -0
  193. package/src/math/v2/pow.ts +71 -0
  194. package/src/math/v2/preset.ts +189 -0
  195. package/src/math/v2/receipt.ts +162 -0
  196. package/src/math/v2/relay.ts +235 -0
  197. package/src/math/v2/rows.ts +412 -0
  198. package/src/math/v2/split.ts +160 -0
  199. package/src/math/v2/sqrt.ts +86 -0
  200. package/src/math/v2/supply.ts +27 -0
  201. package/src/math/v2/team.ts +75 -0
  202. package/src/math/v2/trade.ts +145 -0
  203. package/src/math/v2/wide.ts +52 -0
  204. package/src/quote.ts +6 -0
  205. package/src/registryPrice.ts +28 -0
  206. package/src/route/edge-jupiter.ts +71 -0
  207. package/src/route/edge.ts +22 -0
  208. package/src/route/graph.ts +77 -0
  209. package/src/route/path.ts +84 -0
  210. package/src/route/quote.ts +46 -0
  211. package/src/route-jupiter.ts +36 -0
  212. package/src/route.ts +8 -0
  213. package/src/sha256.ts +61 -0
  214. package/src/solana/alt.ts +25 -0
  215. package/src/solana/floor/accounts.ts +393 -0
  216. package/src/solana/floor/borsh.ts +66 -0
  217. package/src/solana/floor/create.ts +89 -0
  218. package/src/solana/floor/events.ts +147 -0
  219. package/src/solana/floor/genesis.ts +111 -0
  220. package/src/solana/floor/genesisList.ts +75 -0
  221. package/src/solana/floor/instructions.ts +491 -0
  222. package/src/solana/floor/metadata.ts +246 -0
  223. package/src/solana/floor/mint.ts +242 -0
  224. package/src/solana/floor/program.ts +64 -0
  225. package/src/solana/floor/registry.ts +96 -0
  226. package/src/solana/instruction.ts +61 -0
  227. package/src/solana/pda.ts +91 -0
  228. package/src/solana/tx.ts +143 -0
  229. package/src/v2/burnPreview.ts +52 -0
  230. package/src/v2/circulating.ts +22 -0
  231. package/src/v2/claimAllEvm.ts +97 -0
  232. package/src/v2/claims-evm.ts +179 -0
  233. package/src/v2/claims.ts +288 -0
  234. package/src/v2/dead.ts +21 -0
  235. package/src/v2/devbuy.ts +55 -0
  236. package/src/v2/events.ts +131 -0
  237. package/src/v2/evm.ts +349 -0
  238. package/src/v2/evmReads.ts +121 -0
  239. package/src/v2/genesis.ts +80 -0
  240. package/src/v2/layouts.ts +219 -0
  241. package/src/v2/market-evm.ts +95 -0
  242. package/src/v2/presetRowEvm.ts +54 -0
  243. package/src/v2/press.ts +124 -0
  244. package/src/v2/quoteMint.ts +63 -0
  245. package/src/v2/receipt-tx.ts +251 -0
  246. package/src/v2/route-alt.ts +114 -0
  247. package/src/v2/route.ts +494 -0
  248. package/src/v2/solana.ts +439 -0
  249. package/src/v2/solanaSettle.ts +117 -0
  250. package/src/v2/tx/evm.ts +152 -0
  251. package/src/v2/tx/genesis.ts +66 -0
  252. package/src/v2/tx/index.ts +5 -0
  253. package/src/v2/tx/solana.ts +140 -0
  254. package/src/v2/types.ts +284 -0
  255. package/dist/adapter.d.ts +0 -28
  256. package/dist/adapter.d.ts.map +0 -1
  257. package/dist/adapter.js +0 -2
  258. package/dist/adapter.js.map +0 -1
  259. package/dist/chain.d.ts +0 -23
  260. package/dist/chain.d.ts.map +0 -1
  261. package/dist/chain.js +0 -25
  262. package/dist/chain.js.map +0 -1
  263. package/dist/client.d.ts +0 -26
  264. package/dist/client.d.ts.map +0 -1
  265. package/dist/client.js +0 -43
  266. package/dist/client.js.map +0 -1
  267. package/dist/dex-adapter.d.ts +0 -228
  268. package/dist/dex-adapter.d.ts.map +0 -1
  269. package/dist/dex-adapter.js +0 -407
  270. package/dist/dex-adapter.js.map +0 -1
  271. package/dist/index.d.ts +0 -7
  272. package/dist/index.d.ts.map +0 -1
  273. package/dist/index.js.map +0 -1
  274. package/dist/quotes.d.ts +0 -12
  275. package/dist/quotes.d.ts.map +0 -1
  276. package/dist/quotes.gen.d.ts +0 -8
  277. package/dist/quotes.gen.d.ts.map +0 -1
  278. package/dist/quotes.gen.js +0 -2170
  279. package/dist/quotes.gen.js.map +0 -1
  280. package/dist/quotes.js +0 -24
  281. package/dist/quotes.js.map +0 -1
  282. package/dist/types.d.ts +0 -111
  283. package/dist/types.d.ts.map +0 -1
  284. package/dist/types.js +0 -2
  285. package/dist/types.js.map +0 -1
  286. package/src/adapter.ts +0 -20
  287. package/src/chain.ts +0 -39
  288. package/src/client.ts +0 -41
  289. package/src/dex-adapter.ts +0 -477
  290. package/src/quotes.gen.ts +0 -2170
  291. package/src/quotes.ts +0 -29
  292. package/src/types.ts +0 -115
@@ -0,0 +1,121 @@
1
+
2
+
3
+ import { log2X64 } from "../fixed.ts";
4
+ import { DOWN, U128_MAX, UP, mulDiv } from "./wide.ts";
5
+
6
+ export const FEE_LEGS = 5;
7
+ export const FEE_DEFAULT_PPM = 100;
8
+ export const DEFAULT_FEE_PPM: readonly number[] = [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: bigint, ppm: number): bigint => mulDiv(amount, BigInt(ppm), PPM, UP) ?? amount;
17
+ export const feeLegs = (amount: bigint, ppm: readonly number[]): bigint[] => ppm.map((p) => feeLeg(amount, p));
18
+
19
+ export const feeTotal = (f: readonly bigint[]): bigint => f.reduce((a, b) => (a + b > U128_MAX ? U128_MAX : a + b), 0n);
20
+
21
+ export function ppmAt(end: readonly number[], start: readonly number[], sqrtA: bigint, sqrtB: bigint, sqrtP: bigint): number[] {
22
+ if (sqrtB === 0n || sqrtP >= sqrtB) return [...end];
23
+ if (sqrtP <= sqrtA) return [...start];
24
+ const lg = (x: bigint): bigint => 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: readonly number[] = [0, 0, 4_000, 1_000, 0];
39
+
40
+ export function ratesAt(graduated: boolean, own: boolean, end: readonly number[], start: readonly number[], sqrtA: bigint, sqrtB: bigint, sqrtP: bigint): number[] {
41
+ if (!graduated && !own) return [...PREGRAD_PPM];
42
+ return ppmAt(end, start, sqrtA, sqrtB, sqrtP);
43
+ }
44
+
45
+ export const cheaper = (a: readonly number[], b: readonly number[]): number[] => a.map((x, i) => Math.min(x, b[i]));
46
+
47
+ export class FeeAcc {
48
+ readonly pre: number[];
49
+ whole: bigint[] = [0n, 0n, 0n, 0n, 0n];
50
+ rem: bigint[] = [0n, 0n, 0n, 0n, 0n];
51
+ constructor(pre: readonly number[]) {
52
+ this.pre = [...pre];
53
+ }
54
+
55
+ add(q: bigint, at: readonly number[]): boolean {
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(): bigint[] {
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: bigint, ppm: readonly number[]): bigint | null {
82
+ const g = net + feeTotal(feeLegs(net, ppm));
83
+ return g > U128_MAX ? null : g;
84
+ }
85
+
86
+ export function netFromGross(gross: bigint, ppm: readonly number[]): bigint {
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: readonly [number, number, number] = [3_000, 2_000, 1_000];
105
+
106
+ export const CREATOR_REF_BPS: readonly [number, number, number] = [900, 90, 10];
107
+
108
+ export function slices(fee: bigint, present: readonly boolean[], bps: readonly number[]): [bigint[], bigint] {
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: bigint, traderPresent: readonly boolean[]): [bigint[], bigint] => slices(platform, traderPresent, TRADER_REF_BPS);
121
+ export const creatorLegs = (creator: bigint, creatorPresent: readonly boolean[]): [bigint[], bigint] => slices(creator, creatorPresent, CREATOR_REF_BPS);
@@ -0,0 +1,124 @@
1
+
2
+
3
+ import { U64_MAX } from "./wide.ts";
4
+ import { v2Fail } from "./error.ts";
5
+
6
+ const Q64 = 64n;
7
+
8
+ export interface HolderBook {
9
+
10
+ index: bigint;
11
+
12
+ w: bigint;
13
+
14
+ carry: bigint;
15
+ bookedTotal: bigint;
16
+ paidTotal: bigint;
17
+ }
18
+
19
+ export interface Position {
20
+ amount: bigint;
21
+ owed: bigint;
22
+ indexSnap: bigint;
23
+
24
+ optedOut: boolean;
25
+ }
26
+
27
+ export const newPosition = (): Position => ({ amount: 0n, owed: 0n, indexSnap: 0n, optedOut: false });
28
+
29
+ export const optedOutPosition = (): Position => ({ ...newPosition(), optedOut: true });
30
+
31
+ export type Payout = "Indexed" | "Carried";
32
+
33
+ function u64(x: bigint): bigint {
34
+ if (x < 0n || x > U64_MAX) throw new RangeError("holders: u64 overflow");
35
+ return x;
36
+ }
37
+
38
+ function earned(amount: bigint, dIndex: bigint): bigint {
39
+
40
+ const hi = dIndex >> Q64;
41
+ const lo = dIndex & U64_MAX;
42
+ return BigInt.asUintN(128, amount * hi + ((amount * lo) >> Q64));
43
+ }
44
+
45
+ export const newHolderBook = (): HolderBook => ({ index: 0n, w: 0n, carry: 0n, bookedTotal: 0n, paidTotal: 0n });
46
+
47
+ export const cloneHolderBook = (b: HolderBook): HolderBook => ({ ...b });
48
+
49
+ export function holderAddReward(b: HolderBook, amount: bigint, sCirc: bigint): Payout {
50
+ const booked = b.bookedTotal + amount;
51
+ if (booked > U64_MAX) v2Fail("Overflow");
52
+ b.bookedTotal = booked;
53
+ const total = b.carry + amount;
54
+ if (total > U64_MAX) v2Fail("Overflow");
55
+ const w = b.w;
56
+ if (w === 0n || w > sCirc || total === 0n) {
57
+ b.carry = total;
58
+ return "Carried";
59
+ }
60
+ const step = (total << Q64) / w;
61
+
62
+ const out = (step * w + (1n << Q64) - 1n) >> Q64;
63
+ const index = b.index + step;
64
+ if (index >= 1n << 128n) v2Fail("Overflow");
65
+ b.index = index;
66
+ b.carry = total - (out < total ? out : total);
67
+ return "Indexed";
68
+ }
69
+
70
+ export function holderSettle(b: HolderBook, p: Position): void {
71
+ if (!p.optedOut) p.owed = u64(p.owed + BigInt.asUintN(64, earned(p.amount, b.index - p.indexSnap)));
72
+ p.indexSnap = b.index;
73
+ }
74
+
75
+ export function holderAdd(b: HolderBook, p: Position, amount: bigint): void {
76
+ holderSettle(b, p);
77
+ const a = p.amount + amount;
78
+ if (a > U64_MAX) v2Fail("Overflow");
79
+ p.amount = a;
80
+ if (!p.optedOut) {
81
+ const w = b.w + amount;
82
+ if (w > U64_MAX) v2Fail("Overflow");
83
+ b.w = w;
84
+ }
85
+ }
86
+
87
+ export function holderSub(b: HolderBook, p: Position, amount: bigint): void {
88
+ if (amount > p.amount) v2Fail("NotHeld");
89
+ holderSettle(b, p);
90
+ p.amount -= amount;
91
+ if (!p.optedOut) b.w = u64(b.w - amount);
92
+ }
93
+
94
+ export function holderSync(b: HolderBook, p: Position, real: bigint): void {
95
+ if (real > p.amount) return holderAdd(b, p, real - p.amount);
96
+ if (real === p.amount) return;
97
+ if (!p.optedOut) {
98
+ const d = b.index - p.indexSnap;
99
+ const all = earned(p.amount, d);
100
+ const kept = earned(real, d);
101
+ p.owed = u64(p.owed + BigInt.asUintN(64, kept));
102
+ const carry = b.carry + BigInt.asUintN(64, all - kept);
103
+ if (carry > U64_MAX) v2Fail("Overflow");
104
+ b.carry = carry;
105
+ b.w = u64(b.w - (p.amount - real));
106
+ }
107
+ p.indexSnap = b.index;
108
+ p.amount = real;
109
+ }
110
+
111
+ export function holderOptIn(b: HolderBook, p: Position): void {
112
+ if (!p.optedOut) return;
113
+ p.optedOut = false;
114
+ p.indexSnap = b.index;
115
+ b.w = u64(b.w + p.amount);
116
+ }
117
+
118
+ export function holderTake(b: HolderBook, p: Position): bigint {
119
+ holderSettle(b, p);
120
+ const x = p.owed;
121
+ p.owed = 0n;
122
+ b.paidTotal = u64(b.paidTotal + x);
123
+ return x;
124
+ }
@@ -0,0 +1,51 @@
1
+
2
+
3
+ import { U128_MAX } from "./wide.ts";
4
+ import { v2Fail } from "./error.ts";
5
+
6
+ const u128 = (x: bigint): bigint => (x > U128_MAX ? v2Fail("Overflow") : x);
7
+
8
+ export function bookedFromRemainder(r: bigint, thetaBps: number): bigint {
9
+ const keep = 10_000n - BigInt(thetaBps);
10
+ if (keep < 0n) v2Fail("Overflow");
11
+ return u128(r * keep) / 10_000n;
12
+ }
13
+
14
+ export interface ClassSplit {
15
+ platform: bigint;
16
+ floorToken: bigint;
17
+ class: bigint;
18
+ }
19
+
20
+ export function splitByRow(amount: bigint, platformBps: number, floorBps: number): ClassSplit {
21
+ if (platformBps + floorBps > 10_000) v2Fail("RowInvalid");
22
+ const part = (b: number): bigint => u128(amount * BigInt(b)) / 10_000n;
23
+ const platform = part(platformBps);
24
+ const floorToken = part(floorBps);
25
+ return { platform, floorToken, class: amount - platform - floorToken };
26
+ }
27
+
28
+ export interface TeamSplit {
29
+ referrers: [bigint, bigint, bigint];
30
+ slot: bigint;
31
+ }
32
+
33
+ export function teamSplit(amount: bigint, levelsPresent: readonly [boolean, boolean, boolean]): TeamSplit {
34
+ const tenth = amount / 10n;
35
+ const w = [90n, 9n, 1n];
36
+ const referrers: [bigint, bigint, bigint] = [0n, 0n, 0n];
37
+ let paid = 0n;
38
+ for (let i = 0; i < 3; i++) {
39
+ if (levelsPresent[i]) {
40
+ referrers[i] = (tenth * w[i]!) / 100n;
41
+ paid += referrers[i];
42
+ }
43
+ }
44
+ return { referrers, slot: amount - paid };
45
+ }
46
+
47
+ export function slotSplit(slot: bigint, sharesBps: readonly number[]): { parts: bigint[]; dust: bigint } {
48
+ const parts = sharesBps.map((s) => (slot * BigInt(s)) / 10_000n);
49
+ const paid = parts.reduce((a, b) => a + b, 0n);
50
+ return { parts, dust: slot - paid };
51
+ }
@@ -0,0 +1,71 @@
1
+
2
+
3
+ type DD = [number, number];
4
+
5
+ const SPLIT = 134217729;
6
+
7
+ function twoSum(a: number, b: number): DD {
8
+ const s = a + b;
9
+ const bb = s - a;
10
+ return [s, (a - (s - bb)) + (b - bb)];
11
+ }
12
+
13
+ function twoProd(a: number, b: number): DD {
14
+ const p = a * b;
15
+ let t = SPLIT * a;
16
+ const ah = t - (t - a);
17
+ const al = a - ah;
18
+ t = SPLIT * b;
19
+ const bh = t - (t - b);
20
+ const bl = b - bh;
21
+ return [p, ((ah * bh - p) + ah * bl + al * bh) + al * bl];
22
+ }
23
+
24
+ function add(x: DD, y: DD): DD {
25
+ const [s, e] = twoSum(x[0], y[0]);
26
+ return twoSum(s, e + x[1] + y[1]);
27
+ }
28
+
29
+ function mul(x: DD, y: DD): DD {
30
+ const [p, e] = twoProd(x[0], y[0]);
31
+ return twoSum(p, e + (x[0] * y[1] + x[1] * y[0]));
32
+ }
33
+
34
+ function divN(x: DD, n: number): DD {
35
+ const q = x[0] / n;
36
+ const [p, e] = twoProd(q, n);
37
+ return twoSum(q, (x[0] - p - e + x[1]) / n);
38
+ }
39
+
40
+ const LN2: DD = [0.6931471805599453, 2.3190468138462996e-17];
41
+
42
+ function expDD(x: DD): DD {
43
+ const n = Math.round(x[0] / LN2[0]);
44
+ let r = add(x, mul(LN2, [-n, 0]));
45
+ r = [r[0] / 1024, r[1] / 1024];
46
+ let sum: DD = [1, 0];
47
+ let term: DD = [1, 0];
48
+ for (let i = 1; i <= 14; i++) {
49
+ term = divN(mul(term, r), i);
50
+ sum = add(sum, term);
51
+ }
52
+ for (let i = 0; i < 10; i++) sum = mul(sum, sum);
53
+ const s = Math.pow(2, n);
54
+ return [sum[0] * s, sum[1] * s];
55
+ }
56
+
57
+ function lnDD(x: number): DD {
58
+ let y: DD = [Math.log(x), 0];
59
+ for (let i = 0; i < 2; i++) {
60
+ const e = expDD([-y[0], -y[1]]);
61
+ y = add(y, add(mul([x, 0], e), [-1, 0]));
62
+ }
63
+ return y;
64
+ }
65
+
66
+ export function pow(x: number, y: number): number {
67
+ if (y === 0 || x === 1) return 1;
68
+ if (!(x > 0) || !Number.isFinite(x) || !Number.isFinite(y)) return Math.pow(x, y);
69
+ const r = expDD(mul(lnDD(x), [y, 0]));
70
+ return r[0] + r[1];
71
+ }
@@ -0,0 +1,189 @@
1
+
2
+
3
+ import { AccountDecodeError } from "../../solana/floor/accounts.ts";
4
+ import { MAX_BANDS } from "./rows.ts";
5
+ import { RANGES_MAX } from "./book.ts";
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
+ } as const;
42
+
43
+ export interface RangeRowV2 {
44
+ sqrtARel: bigint;
45
+ sqrtBRel: bigint;
46
+ sharePpb: number;
47
+ thetaBps: number;
48
+ psiBps: number;
49
+ kind: number;
50
+ class: number;
51
+
52
+ msCapPpm: number;
53
+ }
54
+
55
+ export interface RowV2 {
56
+ version: number;
57
+ msClass: number;
58
+ nBands: number;
59
+ flags: number;
60
+ teamBuyRule: number;
61
+ phiBps: number;
62
+ wcBps: number;
63
+ psiTailBps: number;
64
+ teamBuyShareBps: number;
65
+ teamSlotMaxBps: number;
66
+ platformMsBps: number;
67
+ floorMsBps: number;
68
+ feePpm: number[];
69
+ bands: { widthBps: number; shareBps: number }[];
70
+ raiseUsdCents: bigint;
71
+ launchCapUsdCents: bigint;
72
+
73
+ ckTargetBps: number;
74
+
75
+ feeStartPpm: number[];
76
+ feeARel: bigint;
77
+ feeBRel: bigint;
78
+ ranges: RangeRowV2[];
79
+ }
80
+
81
+ const u128At = (v: DataView, o: number) => v.getBigUint64(o, true) | (v.getBigUint64(o + 8, true) << 64n);
82
+
83
+ export function decodeRowV2(b: Uint8Array): RowV2 {
84
+ if (b.length !== ROW_LEN) throw new AccountDecodeError(`RowV2: ${b.length} bytes, want ${ROW_LEN}`);
85
+ const v = new DataView(b.buffer, b.byteOffset, b.byteLength);
86
+ const u16 = (o: number) => v.getUint16(o, true);
87
+ const u32 = (o: number) => v.getUint32(o, true);
88
+ const u64 = (o: number) => v.getBigUint64(o, true);
89
+ if (b[OFF.VERSION] !== ROW_VERSION) throw new AccountDecodeError(`RowV2: version ${b[OFF.VERSION]}`);
90
+ const n = b[OFF.N_RANGES];
91
+ const nb = b[OFF.N_BANDS];
92
+ if (n > RANGES_MAX) throw new AccountDecodeError(`RowV2: ${n} ranges`);
93
+ if (nb > MAX_BANDS) throw new AccountDecodeError(`RowV2: ${nb} bands`);
94
+ if (b[OFF.PAD_CK] !== 0 || b[OFF.PAD_CK + 1] !== 0) throw new AccountDecodeError("RowV2: pad not zero");
95
+ 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");
96
+ const ranges: RangeRowV2[] = [];
97
+ for (let i = 0; i < RANGES_MAX; i++) {
98
+ const o = ROW_GLOBALS + i * RANGE_ROW;
99
+ if (i >= n) {
100
+ if (b.subarray(o, o + RANGE_ROW).some((x) => x !== 0)) throw new AccountDecodeError(`RowV2: unused range ${i} not zero`);
101
+ continue;
102
+ }
103
+ if (b.subarray(o + RANGE_PAD_OFF, o + RANGE_ROW).some((x) => x !== 0)) throw new AccountDecodeError(`RowV2: range ${i} pad not zero`);
104
+ ranges.push({
105
+ sqrtARel: u128At(v, o),
106
+ sqrtBRel: u128At(v, o + 16),
107
+ sharePpb: u32(o + 32),
108
+ thetaBps: u16(o + 36),
109
+ psiBps: u16(o + 38),
110
+ kind: b[o + 40],
111
+ class: b[o + 41],
112
+ msCapPpm: u32(o + MS_CAP_OFF),
113
+ });
114
+ }
115
+ const legs = (o: number) => [0, 1, 2, 3, 4].map((i) => u32(o + 4 * i));
116
+ return {
117
+ version: b[OFF.VERSION],
118
+ msClass: b[OFF.MS_CLASS],
119
+ nBands: nb,
120
+ flags: b[OFF.FLAGS],
121
+ teamBuyRule: b[OFF.TEAM_BUY_RULE],
122
+ phiBps: u16(OFF.PHI),
123
+ wcBps: u16(OFF.WC),
124
+ psiTailBps: u16(OFF.PSI_TAIL),
125
+ teamBuyShareBps: u16(OFF.TEAM_BUY_SHARE),
126
+ teamSlotMaxBps: u16(OFF.TEAM_SLOT_MAX),
127
+ platformMsBps: u16(OFF.PLATFORM_MS),
128
+ floorMsBps: u16(OFF.FLOOR_MS),
129
+ feePpm: legs(OFF.FEE_PPM),
130
+ bands: Array.from({ length: MAX_BANDS }, (_, i) => ({ widthBps: u16(OFF.BANDS + 4 * i), shareBps: u16(OFF.BANDS + 2 + 4 * i) })),
131
+ raiseUsdCents: u64(OFF.RAISE_USD_CENTS),
132
+ launchCapUsdCents: u64(OFF.LAUNCH_CAP_USD_CENTS),
133
+ ckTargetBps: u16(OFF.CK_TARGET_BPS),
134
+ feeStartPpm: legs(OFF.FEE_START),
135
+ feeARel: u64(OFF.FEE_A),
136
+ feeBRel: u64(OFF.FEE_B),
137
+ ranges,
138
+ };
139
+ }
140
+
141
+ export function encodeRowV2(r: Omit<RowV2, "nBands" | "ckTargetBps" | "feeStartPpm" | "feeARel" | "feeBRel"> & Partial<RowV2>): Uint8Array {
142
+ if (r.ranges.length > RANGES_MAX) throw new RangeError(`${r.ranges.length} ranges > ${RANGES_MAX}`);
143
+ if (r.bands.length > MAX_BANDS) throw new RangeError(`${r.bands.length} bands > ${MAX_BANDS}`);
144
+ const b = new Uint8Array(ROW_LEN);
145
+ const v = new DataView(b.buffer);
146
+ const u16 = (o: number, x: number) => v.setUint16(o, x, true);
147
+ const u32 = (o: number, x: number) => v.setUint32(o, x, true);
148
+ const u64 = (o: number, x: bigint) => v.setBigUint64(o, BigInt.asUintN(64, x), true);
149
+ const u128 = (o: number, x: bigint) => {
150
+ u64(o, x & ((1n << 64n) - 1n));
151
+ u64(o + 8, x >> 64n);
152
+ };
153
+ b[OFF.VERSION] = ROW_VERSION;
154
+ b[OFF.MS_CLASS] = r.msClass;
155
+ b[OFF.N_RANGES] = r.ranges.length;
156
+ b[OFF.N_BANDS] = r.bands.length;
157
+ b[OFF.FLAGS] = r.flags;
158
+ b[OFF.TEAM_BUY_RULE] = r.teamBuyRule;
159
+ u16(OFF.PHI, r.phiBps);
160
+ u16(OFF.WC, r.wcBps);
161
+ u16(OFF.PSI_TAIL, r.psiTailBps);
162
+ u16(OFF.TEAM_BUY_SHARE, r.teamBuyShareBps);
163
+ u16(OFF.TEAM_SLOT_MAX, r.teamSlotMaxBps);
164
+ u16(OFF.PLATFORM_MS, r.platformMsBps);
165
+ u16(OFF.FLOOR_MS, r.floorMsBps);
166
+ r.feePpm.forEach((x, i) => u32(OFF.FEE_PPM + 4 * i, x));
167
+ r.bands.forEach((x, i) => {
168
+ u16(OFF.BANDS + 4 * i, x.widthBps);
169
+ u16(OFF.BANDS + 2 + 4 * i, x.shareBps);
170
+ });
171
+ u64(OFF.RAISE_USD_CENTS, r.raiseUsdCents);
172
+ u64(OFF.LAUNCH_CAP_USD_CENTS, r.launchCapUsdCents);
173
+ u16(OFF.CK_TARGET_BPS, r.ckTargetBps ?? 0);
174
+ (r.feeStartPpm ?? [0, 0, 0, 0, 0]).forEach((x, i) => u32(OFF.FEE_START + 4 * i, x));
175
+ u64(OFF.FEE_A, r.feeARel ?? 0n);
176
+ u64(OFF.FEE_B, r.feeBRel ?? 0n);
177
+ r.ranges.forEach((g, i) => {
178
+ const o = ROW_GLOBALS + i * RANGE_ROW;
179
+ u128(o, g.sqrtARel);
180
+ u128(o + 16, g.sqrtBRel);
181
+ u32(o + 32, g.sharePpb);
182
+ u16(o + 36, g.thetaBps);
183
+ u16(o + 38, g.psiBps);
184
+ b[o + 40] = g.kind;
185
+ b[o + 41] = g.class;
186
+ u32(o + MS_CAP_OFF, g.msCapPpm ?? 0);
187
+ });
188
+ return b;
189
+ }