@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.
Files changed (298) hide show
  1. package/CHANGELOG.md +114 -0
  2. package/LICENSE +201 -60
  3. package/README.md +54 -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 +130 -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 +803 -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 +148 -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/squid.js +107 -0
  37. package/dist/evm/zap.js +128 -0
  38. package/dist/fixtures.js +53 -0
  39. package/dist/generated/index.js +28 -0
  40. package/dist/generated/interface.gen.js +146 -0
  41. package/dist/handle.js +22 -0
  42. package/dist/index.js +93 -7
  43. package/dist/indexer/bodies.js +473 -0
  44. package/dist/indexer/client.js +105 -0
  45. package/dist/indexer/contract.js +358 -0
  46. package/dist/indexer/decode.js +97 -0
  47. package/dist/indexer/partners.js +105 -0
  48. package/dist/indexer/paths.js +127 -0
  49. package/dist/indexer/read.js +109 -0
  50. package/dist/indexer/stream.js +71 -0
  51. package/dist/lock.js +16 -0
  52. package/dist/math/fixed.js +154 -0
  53. package/dist/math/genesis.js +39 -0
  54. package/dist/math/index.js +20 -0
  55. package/dist/math/u256.js +49 -0
  56. package/dist/math/v2/ask.js +162 -0
  57. package/dist/math/v2/book.js +607 -0
  58. package/dist/math/v2/circulating.js +62 -0
  59. package/dist/math/v2/create.js +122 -0
  60. package/dist/math/v2/display.js +208 -0
  61. package/dist/math/v2/error.js +17 -0
  62. package/dist/math/v2/feasibility.js +174 -0
  63. package/dist/math/v2/fees.js +121 -0
  64. package/dist/math/v2/holders.js +105 -0
  65. package/dist/math/v2/milestones.js +40 -0
  66. package/dist/math/v2/pow.js +69 -0
  67. package/dist/math/v2/preset.js +151 -0
  68. package/dist/math/v2/receipt.js +108 -0
  69. package/dist/math/v2/relay.js +217 -0
  70. package/dist/math/v2/rows.js +365 -0
  71. package/dist/math/v2/split.js +111 -0
  72. package/dist/math/v2/sqrt.js +84 -0
  73. package/dist/math/v2/supply.js +21 -0
  74. package/dist/math/v2/team.js +75 -0
  75. package/dist/math/v2/trade.js +126 -0
  76. package/dist/math/v2/wide.js +51 -0
  77. package/dist/quote.js +6 -0
  78. package/dist/registryPrice.js +23 -0
  79. package/dist/route/edge-jupiter.js +42 -0
  80. package/dist/route/edge.js +11 -0
  81. package/dist/route/graph.js +51 -0
  82. package/dist/route/path.js +75 -0
  83. package/dist/route/quote.js +15 -0
  84. package/dist/route-jupiter.js +35 -0
  85. package/dist/route.js +8 -0
  86. package/dist/sha256.js +61 -0
  87. package/dist/solana/alt.js +18 -0
  88. package/dist/solana/floor/accounts.js +392 -0
  89. package/dist/solana/floor/borsh.js +66 -0
  90. package/dist/solana/floor/create.js +65 -0
  91. package/dist/solana/floor/events.js +144 -0
  92. package/dist/solana/floor/genesis.js +91 -0
  93. package/dist/solana/floor/genesisList.js +68 -0
  94. package/dist/solana/floor/instructions.js +448 -0
  95. package/dist/solana/floor/metadata.js +198 -0
  96. package/dist/solana/floor/mint.js +206 -0
  97. package/dist/solana/floor/program.js +64 -0
  98. package/dist/solana/floor/registry.js +83 -0
  99. package/dist/solana/instruction.js +49 -0
  100. package/dist/solana/pda.js +88 -0
  101. package/dist/solana/tx.js +138 -0
  102. package/dist/v2/burnPreview.js +35 -0
  103. package/dist/v2/circulating.js +11 -0
  104. package/dist/v2/claimAllEvm.js +70 -0
  105. package/dist/v2/claims-evm.js +120 -0
  106. package/dist/v2/claims.js +218 -0
  107. package/dist/v2/dead.js +21 -0
  108. package/dist/v2/devbuy.js +37 -0
  109. package/dist/v2/events.js +115 -0
  110. package/dist/v2/evm.js +299 -0
  111. package/dist/v2/evmReads.js +76 -0
  112. package/dist/v2/genesis.js +76 -0
  113. package/dist/v2/layouts.js +217 -0
  114. package/dist/v2/market-evm.js +74 -0
  115. package/dist/v2/presetRowEvm.js +45 -0
  116. package/dist/v2/press.js +106 -0
  117. package/dist/v2/quoteMint.js +52 -0
  118. package/dist/v2/receipt-tx.js +149 -0
  119. package/dist/v2/route-alt.js +114 -0
  120. package/dist/v2/route.js +389 -0
  121. package/dist/v2/router.js +22 -0
  122. package/dist/v2/solana.js +391 -0
  123. package/dist/v2/solanaSettle.js +97 -0
  124. package/dist/v2/tx/evm.js +126 -0
  125. package/dist/v2/tx/genesis.js +47 -0
  126. package/dist/v2/tx/index.js +5 -0
  127. package/dist/v2/tx/solana.js +118 -0
  128. package/dist/v2/types.js +13 -0
  129. package/docs/aggregator.md +61 -0
  130. package/docs/bot.md +50 -0
  131. package/docs/floor-line.md +66 -0
  132. package/docs/frontend.md +29 -0
  133. package/docs/wallet.md +54 -0
  134. package/examples/floor-line.html +104 -0
  135. package/package.json +429 -32
  136. package/src/abi/evm-event-names.ts +12 -0
  137. package/src/abi/evm.gen.ts +139 -0
  138. package/src/abi/functions.gen.ts +154 -0
  139. package/src/abi/index.ts +30 -0
  140. package/src/abi/solana.gen.ts +140 -0
  141. package/src/burn.ts +37 -0
  142. package/src/client/evm/index.ts +199 -0
  143. package/src/client/index.ts +23 -0
  144. package/src/client/solana/index.ts +260 -0
  145. package/src/client/types.ts +162 -0
  146. package/src/codec.ts +77 -0
  147. package/src/config/codeHash.ts +9 -0
  148. package/src/config/generated.ts +803 -0
  149. package/src/config/index.ts +93 -0
  150. package/src/config/registry.gen.ts +118 -0
  151. package/src/config/solana-genesis.ts +7 -0
  152. package/src/ed25519.ts +31 -0
  153. package/src/errors.ts +270 -0
  154. package/src/events/index.ts +44 -0
  155. package/src/events/types.gen.ts +842 -0
  156. package/src/evm/abi.ts +292 -0
  157. package/src/evm/balances.ts +26 -0
  158. package/src/evm/chains.ts +171 -0
  159. package/src/evm/floor/abi.gen.ts +195 -0
  160. package/src/evm/floor/calls.ts +247 -0
  161. package/src/evm/floor/create.ts +68 -0
  162. package/src/evm/floor/events.ts +51 -0
  163. package/src/evm/floor/iface.ts +45 -0
  164. package/src/evm/items.ts +252 -0
  165. package/src/evm/keccak.ts +118 -0
  166. package/src/evm/multicall.ts +43 -0
  167. package/src/evm/router.ts +209 -0
  168. package/src/evm/squid.ts +138 -0
  169. package/src/evm/zap.ts +152 -0
  170. package/src/fixtures.ts +53 -0
  171. package/src/generated/index.ts +29 -0
  172. package/src/generated/interface.gen.ts +153 -0
  173. package/src/handle.ts +22 -0
  174. package/src/index.ts +93 -6
  175. package/src/indexer/bodies.ts +528 -0
  176. package/src/indexer/client.ts +180 -0
  177. package/src/indexer/contract.ts +419 -0
  178. package/src/indexer/decode.ts +109 -0
  179. package/src/indexer/partners.ts +115 -0
  180. package/src/indexer/paths.ts +190 -0
  181. package/src/indexer/read.ts +119 -0
  182. package/src/indexer/stream.ts +83 -0
  183. package/src/lock.ts +16 -0
  184. package/src/math/fixed.ts +154 -0
  185. package/src/math/genesis.ts +39 -0
  186. package/src/math/index.ts +24 -0
  187. package/src/math/u256.ts +49 -0
  188. package/src/math/v2/ask.ts +218 -0
  189. package/src/math/v2/book.ts +660 -0
  190. package/src/math/v2/circulating.ts +69 -0
  191. package/src/math/v2/create.ts +139 -0
  192. package/src/math/v2/display.ts +250 -0
  193. package/src/math/v2/error.ts +19 -0
  194. package/src/math/v2/feasibility.ts +200 -0
  195. package/src/math/v2/fees.ts +121 -0
  196. package/src/math/v2/holders.ts +124 -0
  197. package/src/math/v2/milestones.ts +51 -0
  198. package/src/math/v2/pow.ts +71 -0
  199. package/src/math/v2/preset.ts +189 -0
  200. package/src/math/v2/receipt.ts +162 -0
  201. package/src/math/v2/relay.ts +235 -0
  202. package/src/math/v2/rows.ts +412 -0
  203. package/src/math/v2/split.ts +160 -0
  204. package/src/math/v2/sqrt.ts +86 -0
  205. package/src/math/v2/supply.ts +27 -0
  206. package/src/math/v2/team.ts +75 -0
  207. package/src/math/v2/trade.ts +145 -0
  208. package/src/math/v2/wide.ts +52 -0
  209. package/src/quote.ts +6 -0
  210. package/src/registryPrice.ts +28 -0
  211. package/src/route/edge-jupiter.ts +71 -0
  212. package/src/route/edge.ts +22 -0
  213. package/src/route/graph.ts +77 -0
  214. package/src/route/path.ts +84 -0
  215. package/src/route/quote.ts +46 -0
  216. package/src/route-jupiter.ts +36 -0
  217. package/src/route.ts +8 -0
  218. package/src/sha256.ts +61 -0
  219. package/src/solana/alt.ts +25 -0
  220. package/src/solana/floor/accounts.ts +395 -0
  221. package/src/solana/floor/borsh.ts +66 -0
  222. package/src/solana/floor/create.ts +89 -0
  223. package/src/solana/floor/events.ts +151 -0
  224. package/src/solana/floor/genesis.ts +111 -0
  225. package/src/solana/floor/genesisList.ts +75 -0
  226. package/src/solana/floor/instructions.ts +504 -0
  227. package/src/solana/floor/metadata.ts +246 -0
  228. package/src/solana/floor/mint.ts +242 -0
  229. package/src/solana/floor/program.ts +64 -0
  230. package/src/solana/floor/registry.ts +96 -0
  231. package/src/solana/instruction.ts +61 -0
  232. package/src/solana/pda.ts +91 -0
  233. package/src/solana/tx.ts +143 -0
  234. package/src/v2/burnPreview.ts +52 -0
  235. package/src/v2/circulating.ts +22 -0
  236. package/src/v2/claimAllEvm.ts +97 -0
  237. package/src/v2/claims-evm.ts +179 -0
  238. package/src/v2/claims.ts +288 -0
  239. package/src/v2/dead.ts +21 -0
  240. package/src/v2/devbuy.ts +55 -0
  241. package/src/v2/events.ts +131 -0
  242. package/src/v2/evm.ts +349 -0
  243. package/src/v2/evmReads.ts +121 -0
  244. package/src/v2/genesis.ts +80 -0
  245. package/src/v2/layouts.ts +219 -0
  246. package/src/v2/market-evm.ts +95 -0
  247. package/src/v2/presetRowEvm.ts +54 -0
  248. package/src/v2/press.ts +124 -0
  249. package/src/v2/quoteMint.ts +63 -0
  250. package/src/v2/receipt-tx.ts +251 -0
  251. package/src/v2/route-alt.ts +114 -0
  252. package/src/v2/route.ts +494 -0
  253. package/src/v2/router.ts +22 -0
  254. package/src/v2/solana.ts +439 -0
  255. package/src/v2/solanaSettle.ts +117 -0
  256. package/src/v2/tx/evm.ts +152 -0
  257. package/src/v2/tx/genesis.ts +66 -0
  258. package/src/v2/tx/index.ts +5 -0
  259. package/src/v2/tx/solana.ts +140 -0
  260. package/src/v2/types.ts +284 -0
  261. package/dist/adapter.d.ts +0 -28
  262. package/dist/adapter.d.ts.map +0 -1
  263. package/dist/adapter.js +0 -2
  264. package/dist/adapter.js.map +0 -1
  265. package/dist/chain.d.ts +0 -23
  266. package/dist/chain.d.ts.map +0 -1
  267. package/dist/chain.js +0 -25
  268. package/dist/chain.js.map +0 -1
  269. package/dist/client.d.ts +0 -26
  270. package/dist/client.d.ts.map +0 -1
  271. package/dist/client.js +0 -43
  272. package/dist/client.js.map +0 -1
  273. package/dist/dex-adapter.d.ts +0 -228
  274. package/dist/dex-adapter.d.ts.map +0 -1
  275. package/dist/dex-adapter.js +0 -407
  276. package/dist/dex-adapter.js.map +0 -1
  277. package/dist/index.d.ts +0 -7
  278. package/dist/index.d.ts.map +0 -1
  279. package/dist/index.js.map +0 -1
  280. package/dist/quotes.d.ts +0 -12
  281. package/dist/quotes.d.ts.map +0 -1
  282. package/dist/quotes.gen.d.ts +0 -8
  283. package/dist/quotes.gen.d.ts.map +0 -1
  284. package/dist/quotes.gen.js +0 -2170
  285. package/dist/quotes.gen.js.map +0 -1
  286. package/dist/quotes.js +0 -24
  287. package/dist/quotes.js.map +0 -1
  288. package/dist/types.d.ts +0 -111
  289. package/dist/types.d.ts.map +0 -1
  290. package/dist/types.js +0 -2
  291. package/dist/types.js.map +0 -1
  292. package/src/adapter.ts +0 -20
  293. package/src/chain.ts +0 -39
  294. package/src/client.ts +0 -41
  295. package/src/dex-adapter.ts +0 -477
  296. package/src/quotes.gen.ts +0 -2170
  297. package/src/quotes.ts +0 -29
  298. package/src/types.ts +0 -115
@@ -0,0 +1,74 @@
1
+
2
+
3
+ import { MULTICALL3_ADDRESS, aggregate3Call, decodeAggregate3Result } from "../evm/multicall.js";
4
+ import { decodeCirculatingOf, decodeLaunchBook, decodeLaunchFeeCurve, decodeLaunchStateSupply, decodeBidMoney, evmSettleInputsV2, viewCall } from "./evm.js";
5
+
6
+ import { launchDisplayV2 } from "../math/v2/display.js";
7
+
8
+ export const EVM_TOKEN_SCALE = 1_000_000_000_000n;
9
+
10
+ export const LAUNCH_READS_V2 = ["book", "state", "feeCurve", "circulating", "bidMoney"] ;
11
+
12
+ const REQUIRED = new Set(["book", "state", "feeCurve"]);
13
+
14
+ export function launchReadCallsV2(floorAmm , launchId ) {
15
+ const v = (data ) => ({ to: floorAmm, data, value: 0n });
16
+ return {
17
+ book: v(viewCall("launchBook", [launchId])),
18
+ state: v(viewCall("launchState", [launchId])),
19
+ feeCurve: v(viewCall("launchFeeCurve", [launchId])),
20
+ circulating: v(viewCall("circulatingOf", [launchId])),
21
+ bidMoney: v(viewCall("bidMoney", [launchId])),
22
+ };
23
+ }
24
+
25
+ export function launchMulticallV2(floorAmm , launchId , multicall = MULTICALL3_ADDRESS) {
26
+ const c = launchReadCallsV2(floorAmm, launchId);
27
+ return aggregate3Call(LAUNCH_READS_V2.map((k) => ({ target: c[k].to, allowFailure: !REQUIRED.has(k), callData: c[k].data })), multicall);
28
+ }
29
+
30
+       
31
+
32
+ export function decodeLaunchReadsV2(r , opts = {}) {
33
+ if (r.book === null || r.state === null || r.feeCurve === null) throw new RangeError("launchBook / launchState / launchFeeCurve failed");
34
+ const book = decodeLaunchBook(r.book);
35
+
36
+ const supply = launchStateSupplyMath(decodeLaunchStateSupply(r.state).supply);
37
+ const curve = decodeLaunchFeeCurve(r.feeCurve);
38
+ const circulating = r.circulating === null ? null : decodeCirculatingOf(r.circulating);
39
+ const math = opts.row !== undefined && circulating !== null ? evmSettleInputsV2(book, opts.row, { live: supply, dead: circulating.dead }, opts.msCapPpm, curve) : null;
40
+
41
+ const displayBook = math?.book ?? evmSettleInputsV2(book, NO_ROW, { live: supply, dead: 0n }, opts.msCapPpm, curve).book;
42
+ return {
43
+ book, supply, curve, circulating, bidMoney: r.bidMoney === null ? null : decodeBidMoney(r.bidMoney),
44
+ math, display: launchDisplayV2(displayBook, supply),
45
+ };
46
+ }
47
+
48
+ export function launchStateSupplyMath(chain ) {
49
+ if (chain % EVM_TOKEN_SCALE !== 0n) throw new RangeError(`launchState.supply ${chain} is not a whole math unit (CHAIN = math × ${EVM_TOKEN_SCALE})`);
50
+ return chain / EVM_TOKEN_SCALE;
51
+ }
52
+
53
+ const NO_ROW = { phiBps: 0, ckBps: 0, wcBps: 0, cShareBps: 0, msClass: 0, platformMsBps: 0, floorMsBps: 0 };
54
+
55
+ export function decodeLaunchMulticallV2(data , opts = {}) {
56
+ const res = decodeAggregate3Result(data);
57
+ if (res.length !== LAUNCH_READS_V2.length) throw new RangeError(`aggregate3: ${res.length} results, ${LAUNCH_READS_V2.length} calls`);
58
+ const r = Object.fromEntries(LAUNCH_READS_V2.map((k, i) => [k, res[i].success ? res[i].returnData : null])) ;
59
+ return decodeLaunchReadsV2(r, opts);
60
+ }
61
+
62
+ export async function readLaunchV2(ethCall , floorAmm , launchId , opts = {}) {
63
+ if (opts.multicall !== null) return decodeLaunchMulticallV2(await ethCall(launchMulticallV2(floorAmm, launchId, opts.multicall ?? MULTICALL3_ADDRESS)), opts);
64
+ const c = launchReadCallsV2(floorAmm, launchId);
65
+ const got = await Promise.all(LAUNCH_READS_V2.map(async (k) => {
66
+ try {
67
+ return await ethCall(c[k]);
68
+ } catch (e) {
69
+ if (REQUIRED.has(k)) throw e;
70
+ return null;
71
+ }
72
+ }));
73
+ return decodeLaunchReadsV2(Object.fromEntries(LAUNCH_READS_V2.map((k, i) => [k, got[i]])) , opts);
74
+ }
@@ -0,0 +1,45 @@
1
+
2
+
3
+ import { bytesToHex, decodeAbi, encodeCall, hexToBytes } from "../evm/abi.js";
4
+
5
+ import { ROW_LEN, decodeRowV2 } from "../math/v2/preset.js";
6
+
7
+ import { evmSignature } from "../generated/index.js";
8
+
9
+ export const PRESET_ROW_VIEW_EVM = { sig: evmSignature("presetRow"), ret: ["(uint8,bool,bool,uint32,bytes32,bytes)"] } ;
10
+ export const NEXT_PRESET_ID_VIEW_EVM = { sig: evmSignature("nextPresetId"), ret: ["uint8"] } ;
11
+
12
+ const RESERVED_ID = 0;
13
+ const RETIRED_ID = 2;
14
+
15
+ export function presetRowCallV2(id ) {
16
+ if (!Number.isInteger(id) || id < 0 || id > 255) throw new RangeError(`preset id ${id}: a u8`);
17
+ return bytesToHex(encodeCall(PRESET_ROW_VIEW_EVM.sig, [BigInt(id)]));
18
+ }
19
+ export const nextPresetIdCallV2 = () => bytesToHex(encodeCall(NEXT_PRESET_ID_VIEW_EVM.sig, []));
20
+ export const decodeNextPresetIdEvmV2 = (data ) => Number(decodeAbi(NEXT_PRESET_ID_VIEW_EVM.ret, data)[0] );
21
+
22
+ export function decodePresetRowEvmV2(data ) {
23
+ const [t] = decodeAbi(PRESET_ROW_VIEW_EVM.ret, data) ;
24
+ const spec = hexToBytes(t[5] );
25
+ if (spec.length === 0) return null;
26
+ if (spec.length !== ROW_LEN) throw new RangeError(`preset row spec ${spec.length} bytes, want ${ROW_LEN}`);
27
+ return {
28
+ id: Number(t[0] ), bump: 0, checked: 0, genesisRow: 0, padVersion: new Uint8Array(2), enabled: t[1] ? 1 : 0, deleted: t[2] ? 1 : 0, version: Number(t[3] ),
29
+ rowHash: hexToBytes(t[4] ), row: spec, padEnd: new Uint8Array(0),
30
+ };
31
+ }
32
+
33
+ export function presetMenuV2(rows ) {
34
+ return rows
35
+ .filter((r) => r !== null && r.enabled !== 0 && r.deleted === 0 && r.id !== RESERVED_ID && r.id !== RETIRED_ID)
36
+ .sort((a, b) => a.id - b.id)
37
+ .map((r) => ({ id: r.id, version: r.version, rowHash: bytesToHex(r.rowHash), row: decodeRowV2(r.row) }));
38
+ }
39
+
40
+ export async function readPresetMenuEvmV2(rpc , amm ) {
41
+ const next = decodeNextPresetIdEvmV2(await rpc.ethCall(amm, nextPresetIdCallV2()));
42
+ const ids = Array.from({ length: Math.max(0, next - 1) }, (_, i) => i + 1).filter((id) => id !== RETIRED_ID);
43
+ const rows = await Promise.all(ids.map(async (id) => decodePresetRowEvmV2(await rpc.ethCall(amm, presetRowCallV2(id)))));
44
+ return presetMenuV2(rows);
45
+ }
@@ -0,0 +1,106 @@
1
+
2
+
3
+ import { FloorError } from "../errors.js";
4
+ import { DEFAULT_SLIPPAGE_BPS } from "../client/types.js";
5
+ import { keccak256 } from "../evm/keccak.js";
6
+ import { bytesToHex, checksumAddress, decodeAbi, encodeAbi, hexToBytes } from "../evm/abi.js";
7
+ import { deadlineArg, floorAmmCall, floorTokenCall } from "../evm/floor/calls.js";
8
+ import { EVM_TOKEN_SCALE } from "./market-evm.js";
9
+
10
+ export { DEFAULT_SLIPPAGE_BPS };
11
+
12
+ export const MAX_SLIPPAGE_BPS = 10_000;
13
+ const BPS = 10_000n;
14
+
15
+ const MAX_UNIX_SECONDS = 100_000_000_000n;
16
+
17
+ const bad = (name , message ) => new FloorError("BadArgument", message, { name });
18
+
19
+ export function minOutAt(quoted , slippageBps ) {
20
+ if (!Number.isInteger(slippageBps) || slippageBps < 0 || slippageBps > MAX_SLIPPAGE_BPS) {
21
+ throw bad("BadSlippage", `slippageBps ${slippageBps} is not a whole number of bps in 0..=${MAX_SLIPPAGE_BPS}`);
22
+ }
23
+ if (quoted <= 0n) throw bad("BadQuote", `a quote of ${quoted} has no minimum (the NO_MINIMUM rule)`);
24
+ const m = (quoted * (BPS - BigInt(slippageBps)) + BPS - 1n) / BPS;
25
+ return m > 0n ? m : 1n;
26
+ }
27
+
28
+ const seconds = (v , what ) => {
29
+ if (typeof v === "number" && !Number.isFinite(v)) throw bad("BadDeadline", `${what} ${v} is not a time`);
30
+ const s = typeof v === "bigint" ? v : BigInt(Math.floor(v));
31
+ if (s < 0n || s > MAX_UNIX_SECONDS) throw bad("BadDeadline", `${what} ${v} is not unix seconds (a millisecond clock reads as no deadline)`);
32
+ return s;
33
+ };
34
+
35
+ export function deadlineFrom(p ) {
36
+ const now = seconds(p.nowSec, "nowSec");
37
+ const head = p.headTs === undefined || p.headTs === null ? 0n : seconds(p.headTs, "headTs");
38
+ if (!Number.isInteger(p.windowSec) || p.windowSec <= 0) throw bad("BadDeadline", `windowSec ${p.windowSec} is not whole seconds > 0`);
39
+ return deadlineArg((now > head ? now : head) + BigInt(p.windowSec));
40
+ }
41
+
42
+ const utf8 = (s ) => new TextEncoder().encode(s);
43
+ const PERMIT_TYPE = "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)";
44
+ const DOMAIN_TYPE = "EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)";
45
+
46
+ export const PERMIT_TYPEHASH = bytesToHex(keccak256(utf8(PERMIT_TYPE)));
47
+
48
+ export const EIP712_DOMAIN_TYPEHASH = bytesToHex(keccak256(utf8(DOMAIN_TYPE)));
49
+
50
+ export const permitDomain = (p ) =>
51
+ ({ name: p.tokenName, version: p.version ?? "1", chainId: p.chainId, verifyingContract: checksumAddress(p.token) });
52
+
53
+ function sellTokens(tokensIn ) {
54
+ const t = tokensIn - (tokensIn % EVM_TOKEN_SCALE);
55
+ if (t <= 0n) throw new RangeError("tokensIn must be at least one TOKEN_SCALE (10^12)");
56
+ return t;
57
+ }
58
+
59
+ export function sellPermitTypedData(p
60
+
61
+ ) {
62
+ return {
63
+ domain: permitDomain(p),
64
+ types: {
65
+ EIP712Domain: [{ name: "name", type: "string" }, { name: "version", type: "string" }, { name: "chainId", type: "uint256" }, { name: "verifyingContract", type: "address" }],
66
+ Permit: [{ name: "owner", type: "address" }, { name: "spender", type: "address" }, { name: "value", type: "uint256" }, { name: "nonce", type: "uint256" }, { name: "deadline", type: "uint256" }],
67
+ },
68
+ primaryType: "Permit",
69
+ message: { owner: checksumAddress(p.owner), spender: checksumAddress(p.floorAmm), value: sellTokens(p.tokensIn), nonce: p.nonce, deadline: deadlineArg(p.permitDeadline) },
70
+ };
71
+ }
72
+
73
+ export const permitDomainSeparator = (d ) =>
74
+ bytesToHex(keccak256(encodeAbi(["bytes32", "bytes32", "bytes32", "uint256", "address"], [hexToBytes(EIP712_DOMAIN_TYPEHASH), keccak256(utf8(d.name)), keccak256(utf8(d.version)), BigInt(d.chainId), d.verifyingContract])));
75
+
76
+ export function permitDigest(td ) {
77
+ const m = td.message;
78
+ const struct = keccak256(encodeAbi(["bytes32", "address", "address", "uint256", "uint256", "uint256"], [hexToBytes(PERMIT_TYPEHASH), m.owner, m.spender, m.value, m.nonce, m.deadline]));
79
+ return bytesToHex(keccak256(new Uint8Array([0x19, 0x01, ...hexToBytes(permitDomainSeparator(td.domain)), ...struct])));
80
+ }
81
+
82
+ export function splitSignature(sig ) {
83
+ const b = hexToBytes(sig);
84
+ if (b.length !== 65) throw new RangeError(`a signature is 65 bytes, not ${b.length}`);
85
+ const v = b[64] < 27 ? b[64] + 27 : b[64];
86
+ if (v !== 27 && v !== 28) throw new RangeError(`signature v ${b[64]} is not 27 / 28 (or 0 / 1)`);
87
+ return { r: bytesToHex(b.slice(0, 32)), s: bytesToHex(b.slice(32, 64)), v };
88
+ }
89
+
90
+ export function sellWithPermitCall(p
91
+
92
+ ) {
93
+ if (p.minQuoteOut <= 0n) throw new RangeError("minQuoteOut must be > 0 (the NO_MINIMUM rule)");
94
+ const { r, s, v } = splitSignature(p.signature);
95
+ return floorAmmCall(p.floorAmm, "sellWithPermit", {
96
+ launchId: p.launchId, tokensIn: sellTokens(p.tokensIn), minQuoteOut: p.minQuoteOut, to: p.to, deadline: deadlineArg(p.deadline),
97
+ permitDeadline: deadlineArg(p.permitDeadline), v: BigInt(v), r, s,
98
+ });
99
+ }
100
+
101
+ export function sellPlanOf(p ) {
102
+ return p.allowance >= sellTokens(p.tokensIn) ? "sell" : "permit";
103
+ }
104
+
105
+ export const permitNonceCall = (token , owner ) => floorTokenCall(token, "nonces", { owner });
106
+ export const decodePermitNonce = (data ) => decodeAbi(["uint256"], data)[0] ;
@@ -0,0 +1,52 @@
1
+
2
+
3
+ export const EXT_TRANSFER_FEE = 1;
4
+
5
+ const U64_MAX = (1n << 64n) - 1n;
6
+
7
+ const u16 = (d , o ) => d[o] | (d[o + 1] << 8);
8
+ const u64 = (d , o ) => {
9
+ let x = 0n;
10
+ for (let i = 7; i >= 0; i--) x = (x << 8n) | BigInt(d[o + i]);
11
+ return x;
12
+ };
13
+
14
+ export function mintTlvV2(data ) {
15
+ const out = [];
16
+ if (data.length <= 166) return out;
17
+ for (let o = 166; o + 4 <= data.length; ) {
18
+ const t = u16(data, o);
19
+ const l = u16(data, o + 2);
20
+ if (t === 0 || o + 4 + l > data.length) break;
21
+ out.push({ type: t, value: data.subarray(o + 4, o + 4 + l) });
22
+ o += 4 + l;
23
+ }
24
+ return out;
25
+ }
26
+
27
+ export function decodeQuoteTransferFeeV2(data ) {
28
+ const ext = mintTlvV2(data).find((e) => e.type === EXT_TRANSFER_FEE);
29
+ if (!ext || ext.value.length < 108) return null;
30
+ const at = (o ) => ({ epoch: u64(ext.value, o), maximumFee: u64(ext.value, o + 8), basisPoints: u16(ext.value, o + 16) });
31
+ return { older: at(72), newer: at(90) };
32
+ }
33
+
34
+ export function quoteTransferFeeV2(fee , epoch , amount ) {
35
+ if (!fee) return 0n;
36
+ if (amount < 0n || amount > U64_MAX) throw new RangeError(`amount ${amount} is not a u64`);
37
+ const f = epoch >= fee.newer.epoch ? fee.newer : fee.older;
38
+ if (f.basisPoints === 0) return 0n;
39
+ const w = (amount * BigInt(f.basisPoints) + 9_999n) / 10_000n;
40
+ return w < f.maximumFee ? w : f.maximumFee;
41
+ }
42
+
43
+ export function quoteReceivedV2(fee , epoch , amount ) {
44
+ const w = quoteTransferFeeV2(fee, epoch, amount);
45
+ return amount - (w < amount ? w : amount);
46
+ }
47
+
48
+ export function quoteBuyNetInV2(fee , epoch , amount ) {
49
+ const w = quoteTransferFeeV2(fee, epoch, amount);
50
+ if (w >= amount) throw new RangeError(`QuoteTransferShort: the quote mint's transfer fee ${w} takes the whole amount ${amount}`);
51
+ return amount - w;
52
+ }
@@ -0,0 +1,149 @@
1
+
2
+
3
+ import { decodeEvmLogs, decodeSolanaEventCpi } from "../events/index.js";
4
+
5
+ import { base58Decode } from "../codec.js";
6
+ import { FLOOR_AMM_PROGRAM_ID } from "../solana/floor/program.js";
7
+
8
+ const isEvm = (t ) => "logs" in t;
9
+
10
+ const same = (chain , a , b ) => (chain === "evm" ? a.toLowerCase() === b.toLowerCase() : a === b);
11
+
12
+ export function eventsOf(landed , scope = {}) {
13
+ if (isEvm(landed)) {
14
+ const from = scope.floorAmm === undefined ? undefined : typeof scope.floorAmm === "string" ? [scope.floorAmm] : scope.floorAmm;
15
+ return decodeEvmLogs(landed.logs, from);
16
+ }
17
+ const pid = scope.programId ?? FLOOR_AMM_PROGRAM_ID;
18
+ const out = [];
19
+ for (const ix of landed.innerInstructions) {
20
+ if (ix.programId !== pid) continue;
21
+ const e = decodeSolanaEventCpi(typeof ix.data === "string" ? base58Decode(ix.data) : ix.data);
22
+ if (e) out.push(e);
23
+ }
24
+ return out;
25
+ }
26
+
27
+ const launchOf = (e ) => (e.chain === "evm" ? String((e.fields ).launchId) : (e.fields ).launch);
28
+ const launchKey = (l ) => (l === undefined ? undefined : String(l));
29
+ const pick = (es , name ) => es.filter((e) => e.name === name);
30
+
31
+  
32
+
33
+ export function fillOf(side , landed , p = {}) {
34
+ const want = launchKey(p.launch);
35
+ for (const e of pick(eventsOf(landed, p), "Trade")) {
36
+ const f = e.fields;
37
+ if (f.isBuy !== (side === "buy")) continue;
38
+ if (p.trader !== undefined && !same(e.chain, f.trader, p.trader)) continue;
39
+ const launch = launchOf(e);
40
+ if (want !== undefined && launch !== want) continue;
41
+ const [backing, team, platform, floor, holder] = f.fees;
42
+ return {
43
+ family: e.chain, side, launch, trader: f.trader, recipient: f.recipient,
44
+ amountIn: f.isBuy ? f.quoteGross - f.refund : f.tokens, amountOut: f.isBuy ? f.tokens : f.quoteNet, refund: f.isBuy ? f.refund : f.unfilled,
45
+ quoteGross: f.quoteGross, quoteNet: f.quoteNet,
46
+ fees: { backing, team, platform, floor, holder, total: backing + team + platform + floor + holder },
47
+ kind: Number(f.kind), sqrtBefore: f.sqrtBefore, sqrtAfter: f.sqrtAfter,
48
+ };
49
+ }
50
+ return null;
51
+ }
52
+
53
+  
54
+
55
+ export function paidOf(landed , p ) {
56
+ const out = { claims: [], payouts: [], tokens: [], byQuote: {}, tokensTotal: 0n };
57
+ const add = (q , a ) => {
58
+ out.byQuote[q] = (out.byQuote[q] ?? 0n) + a;
59
+ };
60
+ const tok = (launch , kind , tokens ) => {
61
+ if (tokens === null || tokens === 0n) return;
62
+ out.tokens.push({ launch, kind, tokens });
63
+ out.tokensTotal += tokens;
64
+ };
65
+ for (const e of eventsOf(landed, p)) {
66
+ if (e.chain === "solana") {
67
+ if (e.name === "Claimed" && e.fields.wallet === p.wallet) {
68
+ const f = e.fields;
69
+ out.claims.push({
70
+ quote: f.quoteMint, launch: f.launch, types: f.types | ((f.typesHi ?? 0) << 8), creator: f.creator, creatorRef: f.creatorRef, teamSlot: f.teamSlot,
71
+ traderRef: f.traderRef, holder: 0n, extra: f.extra, amount: f.amount,
72
+ });
73
+ add(f.quoteMint, f.amount);
74
+ tok(f.launch, "teamBuy", f.teamBuy);
75
+ tok(f.launch, "shareBought", f.shareBought);
76
+ } else if (e.name === "CreatorBuyUnlocked" && e.fields.creator === p.wallet) tok(e.fields.launch, "teamBuyUnlocked", e.fields.paid ?? e.fields.tokens);
77
+ continue;
78
+ }
79
+ evmPaid(e, p.wallet, out, add, tok);
80
+ }
81
+ return out;
82
+ }
83
+
84
+ function evmPaid(e , wallet , out , add , tok ) {
85
+ switch (e.name) {
86
+ case "Claimed": {
87
+ const f = e.fields;
88
+ if (!same("evm", f.wallet, wallet)) return;
89
+ out.claims.push({ quote: f.quote, launch: null, types: Number(f.types), creator: f.creator, creatorRef: f.creatorRef, teamSlot: f.teamSlot, traderRef: f.traderRef, holder: f.holder, extra: f.extra, amount: f.amount });
90
+ add(f.quote, f.amount);
91
+ return;
92
+ }
93
+ case "PayoutClaimed":
94
+ if (!same("evm", e.fields.wallet, wallet)) return;
95
+ out.payouts.push({ quote: e.fields.quote, to: e.fields.to, amount: e.fields.amount });
96
+ add(e.fields.quote, e.fields.amount);
97
+ return;
98
+ case "TeamBuyClaimed":
99
+ if (same("evm", e.fields.wallet, wallet)) tok(String(e.fields.launchId), "teamBuy", e.fields.tokens);
100
+ return;
101
+ case "TeamShareBoughtClaimed":
102
+ if (same("evm", e.fields.wallet, wallet)) tok(String(e.fields.launchId), "shareBought", e.fields.tokens);
103
+ return;
104
+ case "CreatorBuyUnlocked":
105
+ if (same("evm", e.fields.creator, wallet)) tok(String(e.fields.launchId), "teamBuyUnlocked", e.fields.paid);
106
+ return;
107
+ }
108
+ }
109
+
110
+ const text = (b ) => {
111
+ let n = b.length;
112
+ while (n > 0 && b[n - 1] === 0) n--;
113
+ return new TextDecoder().decode(b.slice(0, n));
114
+ };
115
+
116
+ export function createdOf(landed , scope = {}) {
117
+ for (const e of eventsOf(landed, scope)) {
118
+ if (e.chain === "solana" && (e.name === "LaunchCreatedV2" || e.name === "LaunchCreated")) {
119
+ const f = e.fields;
120
+ return {
121
+ family: "solana", launch: f.launch, launchId: f.id, token: f.mint, quote: f.quoteMint, creator: f.creator, presetId: f.presetId, creatorBuyTokens: f.creatorBuyTokens,
122
+ createFee: f.createFee, nativeCreateFee: f.nativeCreateFee, name: text(f.name), symbol: text(f.symbol), listed: f.listed,
123
+ };
124
+ }
125
+ if (e.chain === "evm" && e.name === "LaunchCreated") {
126
+ const f = e.fields;
127
+ return {
128
+ family: "evm", launch: String(f.launchId), launchId: f.launchId, token: f.token, quote: f.quote, creator: f.creator, presetId: Number(f.presetId),
129
+ creatorBuyTokens: f.creatorBuyTokens, createFee: f.createFee, nativeCreateFee: f.nativeCreateFee, name: f.name, symbol: f.symbol, listed: f.listed,
130
+ };
131
+ }
132
+ }
133
+ return null;
134
+ }
135
+
136
+ export function burnedOf(landed , p ) {
137
+ const out = [];
138
+ for (const e of eventsOf(landed, p)) {
139
+ if (!((e.chain === "solana" && e.name === "TokensBurned") || (e.chain === "evm" && e.name === "TokensBurnedV2"))) continue;
140
+ const f = e.fields ;
141
+ if (!same(e.chain, f.wallet, p.wallet)) continue;
142
+ const share = f.fromShareBought ?? 0n;
143
+ out.push({
144
+ launch: launchOf(e), fromTeamBuy: f.fromTeamBuy, fromWallet: f.fromWallet, fromShareBought: share, total: f.fromTeamBuy + f.fromWallet + share,
145
+ supply: f.supply, sOut: f.sOut, floorAfterX64: f.floorAfterX64,
146
+ });
147
+ }
148
+ return out;
149
+ }
@@ -0,0 +1,114 @@
1
+
2
+
3
+ import { addressBytes, concatBytes } from "../codec.js";
4
+ import { coinKey } from "../route/graph.js";
5
+ import { AccountRole } from "../solana/instruction.js";
6
+
7
+ import { ADDRESS_LOOKUP_TABLE_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, findProgramAddress } from "../solana/pda.js";
8
+ import { createAssociatedTokenAccountIdempotentInstruction } from "../solana/tx.js";
9
+ import { FLOOR_AMM_PROGRAM_ID } from "../solana/floor/program.js";
10
+ import { ATA_CREATE_CU, routeTradeAccountsV2 } from "./route.js";
11
+
12
+ import { buyFromBalanceInstructionV2, buyInstructionV2, openHolderPositionInstruction, sellFromBalanceInstructionV2, sellInstructionV2 } from "./solana.js";
13
+
14
+ export const MAX_TABLE_ADDRESSES_V2 = 256;
15
+
16
+ export const MAX_EXTEND_ADDRESSES_V2 = 30;
17
+
18
+ const PROBES = ["kVwEj4fW3BriC9M17tRS88VResDomcntxJAgL1kJmDb", "BvhkBZ5kEP1dxJzfprm8kLk9dSgvT8cLziBFTHZef6r7"] ;
19
+
20
+ function quoteProgramOf(pool , all ) {
21
+ if (pool.quoteTokenProgram) return pool.quoteTokenProgram;
22
+ if (all.some((x) => coinKey(x.token) === coinKey(pool.quote))) return TOKEN_2022_PROGRAM_ID;
23
+ throw new RangeError(`pool ${pool.symbol}: quoteTokenProgram is required`);
24
+ }
25
+
26
+ function hopForms(trader , pool , all , programId ) {
27
+ const qp = quoteProgramOf(pool, all);
28
+ const a = { ...routeTradeAccountsV2(trader, pool, qp, programId), quoteIsLaunch: pool.quoteIsLaunch ?? all.some((x) => coinKey(x.token) === coinKey(pool.quote)), programId };
29
+ return [
30
+ buyInstructionV2({ ...a, amountIn: 1n, minOut: 1n }),
31
+ buyFromBalanceInstructionV2({ ...a, keep: 0n, minOut: 1n }),
32
+ sellInstructionV2({ ...a, tokensIn: 1n, minOut: 1n }),
33
+ sellFromBalanceInstructionV2({ ...a, keep: 0n, minOut: 1n }),
34
+ openHolderPositionInstruction({ owner: trader, launch: a.launch, ownerBase: a.traderBase, programId }),
35
+ createAssociatedTokenAccountIdempotentInstruction({ payer: trader, owner: trader, mint: pool.token, tokenProgram: TOKEN_2022_PROGRAM_ID }),
36
+ createAssociatedTokenAccountIdempotentInstruction({ payer: trader, owner: trader, mint: pool.quote, tokenProgram: qp }),
37
+ ];
38
+ }
39
+
40
+ export function routeTableAddressesV2(pools , programId = FLOOR_AMM_PROGRAM_ID) {
41
+ if (pools.some((p) => p.chain !== "solana")) throw new RangeError("a lookup table is Solana's");
42
+ const seen = PROBES.map((w) => {
43
+ const ixs = pools.flatMap((p) => hopForms(w, p, pools, programId));
44
+ const invoked = new Set(ixs.map((i) => i.programAddress));
45
+ return { order: ixs.flatMap((i) => i.accounts.map((m) => m.address)), invoked };
46
+ });
47
+ const other = new Set(seen[1].order);
48
+ const out = [];
49
+ const has = new Set ();
50
+ for (const x of seen[0].order) {
51
+ if (has.has(x) || !other.has(x) || seen[0].invoked.has(x) || (PROBES ).includes(x)) continue;
52
+ has.add(x);
53
+ out.push(x);
54
+ }
55
+ return out;
56
+ }
57
+
58
+ export function createRouteTableInstructionV2(p ) {
59
+ if (p.recentSlot < 0n || p.recentSlot > 0xffff_ffff_ffff_ffffn) throw new RangeError("recentSlot: a u64");
60
+ const slot = new Uint8Array(8);
61
+ new DataView(slot.buffer).setBigUint64(0, p.recentSlot, true);
62
+ const [table, bump] = findProgramAddress([addressBytes(p.authority), slot], ADDRESS_LOOKUP_TABLE_PROGRAM_ID);
63
+ const data = concatBytes(Uint8Array.of(0, 0, 0, 0), slot, Uint8Array.of(bump));
64
+ return {
65
+ table,
66
+ instruction: {
67
+ programAddress: ADDRESS_LOOKUP_TABLE_PROGRAM_ID, data,
68
+ accounts: [
69
+ { address: table, role: AccountRole.WRITABLE }, { address: p.authority, role: AccountRole.READONLY },
70
+ { address: p.payer, role: AccountRole.WRITABLE_SIGNER }, { address: SYSTEM_PROGRAM_ID, role: AccountRole.READONLY },
71
+ ],
72
+ },
73
+ };
74
+ }
75
+
76
+ export function extendRouteTableInstructionsV2(p ) {
77
+ if (p.addresses.length === 0 || p.addresses.length > MAX_TABLE_ADDRESSES_V2) throw new RangeError(`addresses: 1 ..= ${MAX_TABLE_ADDRESSES_V2}`);
78
+ const out = [];
79
+ for (let i = 0; i < p.addresses.length; i += MAX_EXTEND_ADDRESSES_V2) {
80
+ const chunk = p.addresses.slice(i, i + MAX_EXTEND_ADDRESSES_V2);
81
+ const head = new Uint8Array(12);
82
+ const v = new DataView(head.buffer);
83
+ v.setUint32(0, 2, true);
84
+ v.setBigUint64(4, BigInt(chunk.length), true);
85
+ out.push({
86
+ programAddress: ADDRESS_LOOKUP_TABLE_PROGRAM_ID, data: concatBytes(head, ...chunk.map(addressBytes)),
87
+ accounts: [
88
+ { address: p.table, role: AccountRole.WRITABLE }, { address: p.authority, role: AccountRole.READONLY_SIGNER },
89
+ { address: p.payer, role: AccountRole.WRITABLE_SIGNER }, { address: SYSTEM_PROGRAM_ID, role: AccountRole.READONLY },
90
+ ],
91
+ });
92
+ }
93
+ return out;
94
+ }
95
+
96
+ export const ROUTE_CU_V2 = Object.freeze({
97
+ buy: Object.freeze([[1, 72_930], [10, 126_041], [24, 229_866]] ),
98
+ sell: Object.freeze([[1, 68_471], [10, 122_930], [24, 216_856]] ),
99
+ ata: ATA_CREATE_CU,
100
+ });
101
+
102
+ export function hopComputeUnitsV2(side , crossings ) {
103
+ if (!Number.isInteger(crossings) || crossings < 1) return undefined;
104
+ for (const [n, cu] of ROUTE_CU_V2[side]) if (crossings <= n) return cu;
105
+ return undefined;
106
+ }
107
+
108
+ export function routeComputeUnitsV2 (q , read ) {
109
+ return q.hops.map((hq, i) => {
110
+ if (hq.hop.kind === "edge") return undefined;
111
+ const r = read(hq, i);
112
+ return r === null ? undefined : hopComputeUnitsV2(hq.hop.kind, r.crossings);
113
+ });
114
+ }