@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.
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,126 @@
1
+
2
+
3
+ import { fail } from "../index.js";
4
+ import { U128_MAX } from "./wide.js";
5
+ import {
6
+ Undo, ckHeld, ckMoney, cloneBook, fClaims, fOwn, feesAt, feesNow, MAX_WALK_STEPS, moneyHeld, tokensHeld, walkDownAcc, walkUpFrom,
7
+ } from "./book.js";
8
+
9
+ import { FeeAcc, LEG_BACKING, cheaper, feeTotal, netFromGross } from "./fees.js";
10
+ import { relay } from "./relay.js";
11
+
12
+ import { split } from "./split.js";
13
+
14
+ import { sCirc } from "./supply.js";
15
+
16
+ const noSplit = () => ({ dF: 0n, ckAdd: 0n, ckTake: 0n, ms: 0n, booked: [0n, 0n, 0n, 0n], sqrtFNew: 0n });
17
+ const noRelay = () => ({ nC: 0, nK: 0, laidC: 0n, laidK: 0n, atF: 0n, kept: 0 });
18
+ const add = (a , b ) => (a + b > U128_MAX ? fail("Overflow") : a + b);
19
+
20
+ function commit(dst , src ) {
21
+ Object.assign(dst, src);
22
+ }
23
+
24
+ function buyOn(book , side , gross , minTokens , walkSteps ) {
25
+ const ppm = feesNow(book);
26
+ const budget = netFromGross(gross, ppm);
27
+ if (budget === 0n) fail("NothingToDo");
28
+ const w = cloneBook(book);
29
+ const u = new Undo();
30
+
31
+ const acc = new FeeAcc(ppm);
32
+ let walked = walkUpFrom(w, budget, { money: 0n, tokens: 0n, fromResale: 0n, fromFirst: 0n, steps: 0, budgetHit: false, graduatedNow: false }, acc, walkSteps, u);
33
+ const c = cheaper(ppm, feesAt(w, w.sqrtP));
34
+ if (c.some((x, i) => x !== ppm[i]) && walked.tokens > 0n) {
35
+ const spent = walked.money + feeTotal(acc.legs());
36
+ if (spent > U128_MAX || spent > gross) fail("Overflow");
37
+ const budget2 = netFromGross(gross - spent, c);
38
+ if (budget2 > 0n) walked = walkUpFrom(w, budget2, walked, acc, walkSteps, u);
39
+ }
40
+ if (walked.tokens === 0n) fail(tokensHeld(w) === 0n ? "BookEmpty" : "NothingToDo");
41
+ if (walked.tokens < minTokens) fail("SlippageExceeded");
42
+ const fees = acc.legs();
43
+ const paid = walked.money + feeTotal(fees);
44
+ if (paid > U128_MAX || paid > gross) fail("Overflow");
45
+ w.floor = add(w.floor, fees[LEG_BACKING]);
46
+ let sp = noSplit();
47
+ let rl = noRelay();
48
+ if (w.graduated && side !== null) {
49
+
50
+ const sNew = sCirc(w, side.sup);
51
+
52
+ if (u.bought.length > 0) {
53
+ sp = split(side.row, w.ranges.slice(0, w.nRanges), {
54
+ sqrtF: w.sqrtF, sqrtP1: w.sqrtP, fTotal: fOwn(w), fClaims: fClaims(w), ck0: ckMoney(w), held: ckHeld(w), sNew,
55
+ }, u.bought);
56
+ w.floor = add(w.floor, sp.dF);
57
+ w.sqrtF = sp.sqrtFNew;
58
+ }
59
+ rl = relay(w, side.row, sp.ckAdd, sNew, sp.ckTake);
60
+ }
61
+ const r = {
62
+ gross, net: walked.money, refund: gross - paid, tokens: walked.tokens, fees, fromResale: walked.fromResale, fromFirst: walked.fromFirst,
63
+ steps: walked.steps, budgetHit: walked.budgetHit, graduatedNow: walked.graduatedNow, sqrtBefore: book.sqrtP, sqrtAfter: w.sqrtP,
64
+ split: sp, relay: rl,
65
+ };
66
+ commit(book, w);
67
+ return r;
68
+ }
69
+
70
+ function sellOn(book , side , tokens , minOut ) {
71
+ if (tokens === 0n) fail("NothingToDo");
72
+ const ppm = feesNow(book);
73
+ const w = cloneBook(book);
74
+
75
+ const acc = new FeeAcc(ppm);
76
+ const walked = walkDownAcc(w, tokens, acc);
77
+ const fees = acc.legs();
78
+ const total = feeTotal(fees);
79
+ if (walked.tokens === 0n || walked.money <= total) fail(walked.money === 0n && moneyHeld(w) === 0n ? "BookEmpty" : "NothingToDo");
80
+ const out = walked.money - total;
81
+ if (out < minOut) fail("SlippageExceeded");
82
+ w.floor = add(w.floor, fees[LEG_BACKING]);
83
+
84
+ const rl = w.graduated && side !== null ? relay(w, side.row, 0n, sCirc(w, side.sup)) : noRelay();
85
+ const r = {
86
+ tokensIn: tokens, unfilled: tokens - walked.tokens, gross: walked.money, fees, out, steps: walked.steps, sqrtBefore: book.sqrtP, sqrtAfter: w.sqrtP,
87
+ fromF: walked.fromF, fromK: walked.fromK, fromC: walked.fromC, relay: rl,
88
+ };
89
+ commit(book, w);
90
+ return r;
91
+ }
92
+
93
+ export function buy(book , row , sup , gross , minTokens , opts = {}) {
94
+ return buyOn(book, { row, sup }, gross, minTokens, opts.walkSteps ?? MAX_WALK_STEPS);
95
+ }
96
+
97
+ export function sell(book , row , sup , tokens , minOut ) {
98
+ return sellOn(book, { row, sup }, tokens, minOut);
99
+ }
100
+
101
+ export const quoteBuy = (book , row , sup , gross , walkSteps = MAX_WALK_STEPS) =>
102
+ buy(cloneBook(book), row, sup, gross, 0n, { walkSteps });
103
+ export const quoteSell = (book , row , sup , tokens ) => sell(cloneBook(book), row, sup, tokens, 0n);
104
+
105
+ export function buyMinTokens(b , row , sup , gross , slippageBps , walkSteps = MAX_WALK_STEPS) {
106
+ const less = buyLessIn(gross, slippageBps);
107
+
108
+ return slippageBps === 10_000 ? 0n : buy(cloneBook(b), row, sup, less, 0n, { walkSteps }).tokens;
109
+ }
110
+
111
+ export function buyLessIn(gross , slippageBps ) {
112
+
113
+ if (!Number.isInteger(slippageBps) || slippageBps < 0 || slippageBps > 10_000) fail("VaultShareOutOfRange");
114
+ return (gross * BigInt(10_000 - slippageBps)) / 10_000n;
115
+ }
116
+
117
+ export function bookBuy(book , gross , minTokens , walkSteps = MAX_WALK_STEPS) {
118
+ return buyOn(book, null, gross, minTokens, walkSteps);
119
+ }
120
+
121
+ export function bookSell(book , tokens , minOut ) {
122
+ return sellOn(book, null, tokens, minOut);
123
+ }
124
+
125
+ export const quoteBookBuy = (book , gross , walkSteps = MAX_WALK_STEPS) => bookBuy(cloneBook(book), gross, 0n, walkSteps);
126
+ export const quoteBookSell = (book , tokens ) => bookSell(cloneBook(book), tokens, 0n);
@@ -0,0 +1,51 @@
1
+
2
+
3
+ export const DOWN = "down";
4
+ export const UP = "up";
5
+
6
+ export const U64_MAX = (1n << 64n) - 1n;
7
+ export const U128_MAX = (1n << 128n) - 1n;
8
+ const W3_LIMIT = 1n << 384n;
9
+ export const U256_LIMIT = 1n << 256n;
10
+
11
+ export function shlW(w , k ) {
12
+ const v = w << BigInt(k);
13
+ return v >= W3_LIMIT ? null : v;
14
+ }
15
+
16
+ export function shrW(w , k , r ) {
17
+ const kk = BigInt(k);
18
+ const q = w >> kk;
19
+ return r === UP && q << kk !== w ? q + 1n : q;
20
+ }
21
+
22
+ export function divW(w , d , r ) {
23
+ if (d === 0n) return null;
24
+ const q = w / d;
25
+ if (q >= U256_LIMIT) return null;
26
+ if (r === UP && q * d !== w) return q + 1n >= U256_LIMIT ? null : q + 1n;
27
+ return q;
28
+ }
29
+
30
+ export const toU128 = (x ) => (x === null || x > U128_MAX ? null : x);
31
+ export const wU128 = toU128;
32
+
33
+ export const mulDiv = (a , b , d , r ) => toU128(divW(a * b, d, r));
34
+
35
+ export const mulShr = (a , b , k , r ) => toU128(shrW(a * b, k, r));
36
+
37
+ export function shlDiv(x , k , d , r ) {
38
+ const s = shlW(x, k);
39
+ return s === null ? null : divW(s, d, r);
40
+ }
41
+
42
+ export function isqrt(n ) {
43
+ if (n < 0n) throw new RangeError("isqrt of a negative");
44
+ if (n < 2n) return n;
45
+ let x = 1n << BigInt((n.toString(2).length + 1) >> 1);
46
+ for (;;) {
47
+ const y = (x + n / x) >> 1n;
48
+ if (y >= x) return x;
49
+ x = y;
50
+ }
51
+ }
package/dist/quote.js ADDED
@@ -0,0 +1,6 @@
1
+
2
+
3
+ export { FloorError, FloorQuoteError, FLOOR_ERROR_CODES, decodeProgramError, decodeSolanaProgramError, decodeEvmProgramError, isFloorError } from "./errors.js";
4
+
5
+ export { createFloorClient, DEFAULT_SLIPPAGE_BPS } from "./client/index.js";
6
+
@@ -0,0 +1,23 @@
1
+
2
+
3
+ export const REGISTRY_PRICE_MAX_AGE_S = 86_400n;
4
+
5
+ export class StaleRegistryPriceError extends Error {
6
+ ageS ;
7
+ constructor(ageS ) {
8
+ super(`the quote's registry price is ${ageS} s old; a create takes one at most ${REGISTRY_PRICE_MAX_AGE_S} s (24 h) old`);
9
+ this.name = "RegistryPriceStale";
10
+ this.ageS = ageS;
11
+ }
12
+ }
13
+
14
+ export const registryPriceAge = (updatedAt , now ) => (now > updatedAt ? now - updatedAt : 0n);
15
+
16
+ export const registryPriceFresh = (updatedAt , now ) => registryPriceAge(updatedAt, now) <= REGISTRY_PRICE_MAX_AGE_S;
17
+
18
+ export function freshRegistryPrice(row , now ) {
19
+ if (row.usdCentsX64 <= 0n) throw new RangeError("the registry row has no price");
20
+ const age = registryPriceAge(row.updatedAt, now);
21
+ if (age > REGISTRY_PRICE_MAX_AGE_S) throw new StaleRegistryPriceError(age);
22
+ return row.usdCentsX64;
23
+ }
@@ -0,0 +1,42 @@
1
+
2
+
3
+ import { base58Decode } from "../codec.js";
4
+
5
+ export function jupiterSwapInstructionsBody(p ) {
6
+ return JSON.stringify({ quoteResponse: p.quote, userPublicKey: p.trader, wrapAndUnwrapSol: true, dynamicComputeUnitLimit: true, ...(p.destinationTokenAccount ? { destinationTokenAccount: p.destinationTokenAccount } : {}) });
7
+ }
8
+
9
+ function base64Decode(s ) {
10
+ const b = atob(s);
11
+ const out = new Uint8Array(b.length);
12
+ for (let i = 0; i < b.length; i++) out[i] = b.charCodeAt(i);
13
+ return out;
14
+ }
15
+
16
+ export function fromJupiter(j ) {
17
+ base58Decode(j.programId);
18
+ return {
19
+ programAddress: j.programId,
20
+ accounts: j.accounts.map((a) => ({ address: a.pubkey, role: ((a.isSigner ? 2 : 0) | (a.isWritable ? 1 : 0)) })),
21
+ data: base64Decode(j.data),
22
+ };
23
+ }
24
+
25
+ export function jupiterLeg(j ) {
26
+ return [...(j.setupInstructions ?? []), j.swapInstruction, ...(j.cleanupInstruction ? [j.cleanupInstruction] : []), ...(j.otherInstructions ?? [])].map(fromJupiter);
27
+ }
28
+
29
+ export function jupiterEdgeLeg(j ) {
30
+ const jb = (j.computeBudgetInstructions ?? []).map(fromJupiter);
31
+ const limit = jb.find((ix) => ix.data[0] === 2 && ix.data.length >= 5);
32
+ const price = jb.find((ix) => ix.data[0] === 3 && ix.data.length >= 9);
33
+ const dv = (ix ) => new DataView(ix.data.buffer, ix.data.byteOffset);
34
+ return {
35
+ setup: (j.setupInstructions ?? []).map(fromJupiter),
36
+ swap: [fromJupiter(j.swapInstruction)],
37
+ cleanup: [...(j.cleanupInstruction ? [j.cleanupInstruction] : []), ...(j.otherInstructions ?? [])].map(fromJupiter),
38
+ lookupTableAddresses: [...(j.addressLookupTableAddresses ?? [])],
39
+ ...(limit ? { computeUnits: dv(limit).getUint32(1, true) } : {}),
40
+ ...(price ? { computeUnitPrice: dv(price).getBigUint64(1, true) } : {}),
41
+ };
42
+ }
@@ -0,0 +1,11 @@
1
+
2
+
3
+ import { COMPUTE_BUDGET_PROGRAM_ID } from "../solana/instruction.js";
4
+
5
+ export const EDGE_UNKNOWN_CU = 400_000;
6
+
7
+ export function edgeLegInstructions(leg ) {
8
+ const ixs = [...leg.setup, ...leg.swap, ...leg.cleanup];
9
+ for (const ix of ixs) if (ix.programAddress === COMPUTE_BUDGET_PROGRAM_ID) throw new RangeError("a compute budget instruction outside the budget slot");
10
+ return ixs;
11
+ }
@@ -0,0 +1,51 @@
1
+
2
+
3
+ export const SOL_NATIVE = "SOL";
4
+
5
+ export const EVM_NATIVE = "0x0000000000000000000000000000000000000000";
6
+
7
+ export class NoRouteError extends Error {
8
+ reason ;
9
+ constructor(reason , detail ) {
10
+ super(`NoRoute (${reason}): ${detail}`);
11
+ this.name = "NoRouteError";
12
+ this.reason = reason;
13
+ }
14
+ }
15
+
16
+ export const coinKey = (a ) => (a.startsWith("0x") ? a.toLowerCase() : a);
17
+
18
+ export const MAX_QUOTE_CHAIN = 8;
19
+
20
+ export async function quoteGraph(p ) {
21
+ const g = { pools: new Map(), edge: new Set(p.edge.map(coinKey)) };
22
+ const seen = new Set ();
23
+ for (const t of p.tokens) {
24
+ let coin = t;
25
+ for (let depth = 0; ; depth++) {
26
+ const k = coinKey(coin);
27
+ if (g.edge.has(k) || seen.has(k)) break;
28
+ if (depth > MAX_QUOTE_CHAIN) throw new NoRouteError("QuoteCycle", `${t}'s quote chain passes ${MAX_QUOTE_CHAIN} pools`);
29
+ seen.add(k);
30
+ const pool = await p.readPool(coin);
31
+ if (pool === null) break;
32
+ if (pool.chain !== p.chain) throw new NoRouteError("Mixed", `${coin} is a ${pool.chain} launch on a ${p.chain} graph`);
33
+ g.pools.set(k, pool);
34
+ coin = pool.quote;
35
+ }
36
+ }
37
+ return g;
38
+ }
39
+
40
+ export function quoteChain(g , token ) {
41
+ const pools = [];
42
+ let coin = token;
43
+ for (;;) {
44
+ const k = coinKey(coin);
45
+ const pool = g.edge.has(k) ? undefined : g.pools.get(k);
46
+ if (!pool) return { pools, base: coin };
47
+ if (pools.length >= MAX_QUOTE_CHAIN || pools.includes(pool)) throw new NoRouteError("QuoteCycle", `${token}'s quote chain does not end`);
48
+ pools.push(pool);
49
+ coin = pool.quote;
50
+ }
51
+ }
@@ -0,0 +1,75 @@
1
+
2
+
3
+ import { WSOL_MINT } from "../solana/pda.js";
4
+ import { EVM_NATIVE, NoRouteError, SOL_NATIVE, coinKey, quoteChain } from "./graph.js";
5
+
6
+  
7
+
8
+  
9
+
10
+  
11
+
12
+  
13
+
14
+ export function findRoute(g , from , to ) {
15
+ if (coinKey(from) === coinKey(to)) throw new NoRouteError("Same", `${from} → itself`);
16
+ const a = quoteChain(g, from);
17
+ const b = quoteChain(g, to);
18
+ const chain = (a.pools[0] ?? b.pools[0])?.chain;
19
+ if (chain === undefined) throw new NoRouteError("NoFloorHop", `${from} → ${to} crosses no FLOOR pool`);
20
+ if ([...a.pools, ...b.pools].some((x) => x.chain !== chain)) throw new NoRouteError("Mixed", `${from} → ${to} mixes chains`);
21
+
22
+ const coinsA = [from, ...a.pools.map((x) => x.quote)].map(coinKey);
23
+ const coinsB = [to, ...b.pools.map((x) => x.quote)].map(coinKey);
24
+ let ia = -1;
25
+ let ib = -1;
26
+ for (let i = 0; i < coinsA.length && ia < 0; i++) {
27
+ const j = coinsB.indexOf(coinsA[i]);
28
+ if (j >= 0) [ia, ib] = [i, j];
29
+ }
30
+ const hops = [];
31
+ if (ia >= 0) {
32
+ for (const pool of a.pools.slice(0, ia)) hops.push({ kind: "sell", pool });
33
+ for (const pool of b.pools.slice(0, ib).reverse()) hops.push({ kind: "buy", pool });
34
+ } else {
35
+ for (const pool of a.pools) hops.push({ kind: "sell", pool });
36
+ hops.push({ kind: "edge", from: a.base, to: b.base });
37
+ for (const pool of [...b.pools].reverse()) hops.push({ kind: "buy", pool });
38
+ }
39
+ if (!hops.some((h) => h.kind !== "edge")) throw new NoRouteError("NoFloorHop", `${from} → ${to} crosses no FLOOR pool`);
40
+ return { chain, from, to, hops };
41
+ }
42
+
43
+ export function isWrapHop(h ) {
44
+ return h.kind === "edge" && ((h.from === SOL_NATIVE && h.to === WSOL_MINT) || (h.from === WSOL_MINT && h.to === SOL_NATIVE));
45
+ }
46
+
47
+ export function hopIn(h ) {
48
+ return h.kind === "edge" ? h.from : h.kind === "buy" ? h.pool.quote : h.pool.token;
49
+ }
50
+ export function hopOut(h ) {
51
+ return h.kind === "edge" ? h.to : h.kind === "buy" ? h.pool.token : h.pool.quote;
52
+ }
53
+
54
+ const USDC_SOLANA = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
55
+ const KNOWN = { [SOL_NATIVE]: "SOL", [WSOL_MINT]: "wSOL", [USDC_SOLANA]: "USDC" };
56
+
57
+ export function describeRoute(path , names = {}, edgeName ) {
58
+ const byKey = new Map(Object.entries(names).map(([k, v]) => [coinKey(k), v]));
59
+ const pools = new Map(path.hops.filter((h) => h.kind !== "edge").map((h) => [coinKey((h ).pool.token), (h ).pool]));
60
+ const name = (c ) => {
61
+ const n = byKey.get(coinKey(c));
62
+ if (n !== undefined) return n;
63
+ const p = pools.get(coinKey(c));
64
+ if (p) return p.symbol.startsWith("$") ? p.symbol : `$${p.symbol}`;
65
+ if (KNOWN[c]) return KNOWN[c];
66
+ if (path.chain === "evm" && coinKey(c) === EVM_NATIVE) return "native";
67
+ return c.length > 10 ? `${c.slice(0, 4)}…${c.slice(-4)}` : c;
68
+ };
69
+ let s = name(path.from);
70
+ for (const h of path.hops) {
71
+ s += ` → ${name(hopOut(h))}`;
72
+ if (h.kind === "edge") s += isWrapHop(h) ? (h.from === SOL_NATIVE ? " (wrap)" : " (unwrap)") : ` (${edgeName ?? (path.chain === "solana" ? "Jupiter" : "DEX")})`;
73
+ }
74
+ return s;
75
+ }
@@ -0,0 +1,15 @@
1
+
2
+
3
+ import { hopIn } from "./path.js";
4
+
5
+  
6
+
7
+  
8
+
9
+    
10
+
11
+  
12
+
13
+ export function routeInputs(q ) {
14
+ return q.path.hops.map(hopIn);
15
+ }
@@ -0,0 +1,35 @@
1
+
2
+
3
+ import { WSOL_MINT } from "./solana/pda.js";
4
+ import { SOL_NATIVE } from "./route/graph.js";
5
+ import { isWrapHop } from "./route/path.js";
6
+
7
+ import { jupiterSwapInstructionsBody } from "./route/edge-jupiter.js";
8
+
9
+ export const JUPITER_API = "https://lite-api.jup.ag/swap/v1";
10
+
11
+ export function jupiterQuoteUrl(p ) {
12
+ if (p.lamports <= 0n) throw new RangeError("lamports must be > 0");
13
+ if (!Number.isInteger(p.slippageBps) || p.slippageBps < 0 || p.slippageBps > 10_000) throw new RangeError("slippageBps in 0..10,000");
14
+ const q = new URLSearchParams({ inputMint: p.inputMint ?? WSOL_MINT, outputMint: p.quoteMint, amount: p.lamports.toString(), slippageBps: String(p.slippageBps), swapMode: "ExactIn" });
15
+ return `${p.api ?? JUPITER_API}/quote?${q.toString()}`;
16
+ }
17
+
18
+ const jupMint = (c ) => (c === SOL_NATIVE ? WSOL_MINT : c);
19
+
20
+ export function jupiterEdgeQuoter(fetchJson , opts = {}) {
21
+ return async ({ from, to, amountIn, slippageBps }) => {
22
+ const raw = (await fetchJson(jupiterQuoteUrl({ inputMint: jupMint(from), quoteMint: jupMint(to), lamports: amountIn, slippageBps, api: opts.api }))) ;
23
+ return { inAmount: BigInt(raw.inAmount), outAmount: BigInt(raw.outAmount), minOut: BigInt(raw.otherAmountThreshold), raw };
24
+ };
25
+ }
26
+
27
+ export function jupiterSwapRequests(q , trader , opts = {}) {
28
+ const out = [];
29
+ q.hops.forEach((h, i) => {
30
+ if (h.hop.kind !== "edge" || isWrapHop(h.hop)) return;
31
+ if (!h.edge) throw new RangeError(`hop ${i} has no edge quote`);
32
+ out.push({ hop: i, url: `${opts.api ?? JUPITER_API}/swap-instructions`, body: jupiterSwapInstructionsBody({ quote: h.edge.raw , trader }) });
33
+ });
34
+ return out;
35
+ }
package/dist/route.js ADDED
@@ -0,0 +1,8 @@
1
+
2
+
3
+ export * from "./route/graph.js";
4
+ export * from "./route/path.js";
5
+ export * from "./route/quote.js";
6
+ export * from "./route/edge.js";
7
+ export * from "./route/edge-jupiter.js";
8
+ export * from "./route-jupiter.js";
package/dist/sha256.js ADDED
@@ -0,0 +1,61 @@
1
+
2
+
3
+ const K = new Uint32Array([
4
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
5
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
6
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
7
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
8
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
9
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
10
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
11
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
12
+ ]);
13
+
14
+ const rotr = (x , n ) => (x >>> n) | (x << (32 - n));
15
+
16
+ export function sha256(msg ) {
17
+ const bitLen = BigInt(msg.length) * 8n;
18
+ const padded = new Uint8Array(Math.ceil((msg.length + 9) / 64) * 64);
19
+ padded.set(msg);
20
+ padded[msg.length] = 0x80;
21
+ for (let i = 0; i < 8; i++) padded[padded.length - 1 - i] = Number((bitLen >> BigInt(8 * i)) & 0xffn);
22
+ const h = new Uint32Array([0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19]);
23
+ const w = new Uint32Array(64);
24
+ for (let off = 0; off < padded.length; off += 64) {
25
+ for (let i = 0; i < 16; i++) {
26
+ const j = off + 4 * i;
27
+ w[i] = (padded[j] << 24) | (padded[j + 1] << 16) | (padded[j + 2] << 8) | padded[j + 3];
28
+ }
29
+ for (let i = 16; i < 64; i++) {
30
+ const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3);
31
+ const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10);
32
+ w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
33
+ }
34
+ let [a, b, c, d, e, f, g, hh] = h;
35
+ for (let i = 0; i < 64; i++) {
36
+ const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25);
37
+ const ch = (e & f) ^ (~e & g);
38
+ const t1 = (hh + S1 + ch + K[i] + w[i]) | 0;
39
+ const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22);
40
+ const maj = (a & b) ^ (a & c) ^ (b & c);
41
+ const t2 = (S0 + maj) | 0;
42
+ hh = g;
43
+ g = f;
44
+ f = e;
45
+ e = (d + t1) | 0;
46
+ d = c;
47
+ c = b;
48
+ b = a;
49
+ a = (t1 + t2) | 0;
50
+ }
51
+ h[0] += a; h[1] += b; h[2] += c; h[3] += d; h[4] += e; h[5] += f; h[6] += g; h[7] += hh;
52
+ }
53
+ const out = new Uint8Array(32);
54
+ for (let i = 0; i < 8; i++) {
55
+ out[4 * i] = h[i] >>> 24;
56
+ out[4 * i + 1] = (h[i] >>> 16) & 0xff;
57
+ out[4 * i + 2] = (h[i] >>> 8) & 0xff;
58
+ out[4 * i + 3] = h[i] & 0xff;
59
+ }
60
+ return out;
61
+ }
@@ -0,0 +1,18 @@
1
+
2
+
3
+ import { base58Encode, readUintLE } from "../codec.js";
4
+
5
+ export const LOOKUP_TABLE_META_SIZE = 56;
6
+
7
+ export function decodeLookupTable(address , data ) {
8
+ if (data.length < LOOKUP_TABLE_META_SIZE || readUintLE(data, 0, 4) !== 1n || (data.length - LOOKUP_TABLE_META_SIZE) % 32 !== 0) throw new RangeError("not a lookup table account");
9
+ const addresses = [];
10
+ for (let o = LOOKUP_TABLE_META_SIZE; o < data.length; o += 32) addresses.push(base58Encode(data.slice(o, o + 32)));
11
+ return {
12
+ address,
13
+ addresses,
14
+ deactivationSlot: readUintLE(data, 4, 8),
15
+ lastExtendedSlot: readUintLE(data, 12, 8),
16
+ authority: data[21] === 1 ? base58Encode(data.slice(22, 54)) : null,
17
+ };
18
+ }