@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,251 @@
1
+
2
+
3
+ import { decodeEvmLogs, decodeSolanaEventCpi } from "../events/index.ts";
4
+ import type { EvmEvent, FloorEvent, SolanaEvent } from "../events/index.ts";
5
+ import type { Log } from "../evm/items.ts";
6
+ import type { FeeLegs, FloorFamily } from "../client/types.ts";
7
+ import { base58Decode } from "../codec.ts";
8
+ import { FLOOR_AMM_PROGRAM_ID } from "../solana/floor/program.ts";
9
+
10
+ export interface EvmLandedTx {
11
+ logs: readonly Log[];
12
+ }
13
+ export interface SolanaLandedTx {
14
+ innerInstructions: readonly { programId: string; data: Uint8Array | string }[];
15
+ }
16
+ export type LandedTx = EvmLandedTx | SolanaLandedTx;
17
+
18
+ export interface ReceiptScope {
19
+
20
+ floorAmm?: string | readonly string[];
21
+
22
+ programId?: string;
23
+ }
24
+
25
+ const isEvm = (t: LandedTx): t is EvmLandedTx => "logs" in t;
26
+
27
+ const same = (chain: FloorFamily, a: string, b: string): boolean => (chain === "evm" ? a.toLowerCase() === b.toLowerCase() : a === b);
28
+
29
+ export function eventsOf(landed: LandedTx, scope: ReceiptScope = {}): FloorEvent[] {
30
+ if (isEvm(landed)) {
31
+ const from = scope.floorAmm === undefined ? undefined : typeof scope.floorAmm === "string" ? [scope.floorAmm] : scope.floorAmm;
32
+ return decodeEvmLogs(landed.logs, from);
33
+ }
34
+ const pid = scope.programId ?? FLOOR_AMM_PROGRAM_ID;
35
+ const out: SolanaEvent[] = [];
36
+ for (const ix of landed.innerInstructions) {
37
+ if (ix.programId !== pid) continue;
38
+ const e = decodeSolanaEventCpi(typeof ix.data === "string" ? base58Decode(ix.data) : ix.data);
39
+ if (e) out.push(e);
40
+ }
41
+ return out;
42
+ }
43
+
44
+ const launchOf = (e: FloorEvent): string => (e.chain === "evm" ? String((e.fields as { launchId: bigint }).launchId) : (e.fields as { launch: string }).launch);
45
+ const launchKey = (l: string | bigint | undefined): string | undefined => (l === undefined ? undefined : String(l));
46
+ const pick = <N extends string>(es: FloorEvent[], name: N): Extract<FloorEvent, { name: N }>[] => es.filter((e): e is Extract<FloorEvent, { name: N }> => e.name === name);
47
+
48
+ export interface ReceiptFill {
49
+ family: FloorFamily;
50
+ side: "buy" | "sell";
51
+
52
+ launch: string;
53
+ trader: string;
54
+ recipient: string;
55
+
56
+ amountIn: bigint;
57
+
58
+ amountOut: bigint;
59
+
60
+ refund: bigint;
61
+ quoteGross: bigint;
62
+ quoteNet: bigint;
63
+
64
+ fees: FeeLegs;
65
+
66
+ kind: number;
67
+ sqrtBefore: bigint;
68
+ sqrtAfter: bigint;
69
+ }
70
+
71
+ export function fillOf(side: "buy" | "sell", landed: LandedTx, p: { trader?: string; launch?: string | bigint } & ReceiptScope = {}): ReceiptFill | null {
72
+ const want = launchKey(p.launch);
73
+ for (const e of pick(eventsOf(landed, p), "Trade")) {
74
+ const f = e.fields;
75
+ if (f.isBuy !== (side === "buy")) continue;
76
+ if (p.trader !== undefined && !same(e.chain, f.trader, p.trader)) continue;
77
+ const launch = launchOf(e);
78
+ if (want !== undefined && launch !== want) continue;
79
+ const [backing, team, platform, floor, holder] = f.fees;
80
+ return {
81
+ family: e.chain, side, launch, trader: f.trader, recipient: f.recipient,
82
+ amountIn: f.isBuy ? f.quoteGross - f.refund : f.tokens, amountOut: f.isBuy ? f.tokens : f.quoteNet, refund: f.isBuy ? f.refund : f.unfilled,
83
+ quoteGross: f.quoteGross, quoteNet: f.quoteNet,
84
+ fees: { backing, team, platform, floor, holder, total: backing + team + platform + floor + holder },
85
+ kind: Number(f.kind), sqrtBefore: f.sqrtBefore, sqrtAfter: f.sqrtAfter,
86
+ };
87
+ }
88
+ return null;
89
+ }
90
+
91
+ export interface ReceiptClaimed {
92
+ quote: string;
93
+ launch: string | null;
94
+
95
+ types: number;
96
+ creator: bigint;
97
+ creatorRef: bigint;
98
+ teamSlot: bigint;
99
+ traderRef: bigint;
100
+ holder: bigint;
101
+ extra: bigint;
102
+
103
+ amount: bigint;
104
+ }
105
+
106
+ export interface ReceiptPayout {
107
+ quote: string;
108
+ to: string;
109
+ amount: bigint;
110
+ }
111
+
112
+ export interface ReceiptTokensPaid {
113
+ launch: string;
114
+ kind: "teamBuy" | "shareBought" | "teamBuyUnlocked";
115
+ tokens: bigint;
116
+ }
117
+ export interface ReceiptPaid {
118
+ claims: ReceiptClaimed[];
119
+ payouts: ReceiptPayout[];
120
+ tokens: ReceiptTokensPaid[];
121
+
122
+ byQuote: Record<string, bigint>;
123
+
124
+ tokensTotal: bigint;
125
+ }
126
+
127
+ export function paidOf(landed: LandedTx, p: { wallet: string } & ReceiptScope): ReceiptPaid {
128
+ const out: ReceiptPaid = { claims: [], payouts: [], tokens: [], byQuote: {}, tokensTotal: 0n };
129
+ const add = (q: string, a: bigint) => {
130
+ out.byQuote[q] = (out.byQuote[q] ?? 0n) + a;
131
+ };
132
+ const tok = (launch: string, kind: ReceiptTokensPaid["kind"], tokens: bigint | null) => {
133
+ if (tokens === null || tokens === 0n) return;
134
+ out.tokens.push({ launch, kind, tokens });
135
+ out.tokensTotal += tokens;
136
+ };
137
+ for (const e of eventsOf(landed, p)) {
138
+ if (e.chain === "solana") {
139
+ if (e.name === "Claimed" && e.fields.wallet === p.wallet) {
140
+ const f = e.fields;
141
+ out.claims.push({
142
+ quote: f.quoteMint, launch: f.launch, types: f.types | ((f.typesHi ?? 0) << 8), creator: f.creator, creatorRef: f.creatorRef, teamSlot: f.teamSlot,
143
+ traderRef: f.traderRef, holder: 0n, extra: f.extra, amount: f.amount,
144
+ });
145
+ add(f.quoteMint, f.amount);
146
+ tok(f.launch, "teamBuy", f.teamBuy);
147
+ tok(f.launch, "shareBought", f.shareBought);
148
+ } else if (e.name === "CreatorBuyUnlocked" && e.fields.creator === p.wallet) tok(e.fields.launch, "teamBuyUnlocked", e.fields.paid ?? e.fields.tokens);
149
+ continue;
150
+ }
151
+ evmPaid(e, p.wallet, out, add, tok);
152
+ }
153
+ return out;
154
+ }
155
+
156
+ function evmPaid(e: EvmEvent, wallet: string, out: ReceiptPaid, add: (q: string, a: bigint) => void, tok: (l: string, k: ReceiptTokensPaid["kind"], t: bigint) => void): void {
157
+ switch (e.name) {
158
+ case "Claimed": {
159
+ const f = e.fields;
160
+ if (!same("evm", f.wallet, wallet)) return;
161
+ 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 });
162
+ add(f.quote, f.amount);
163
+ return;
164
+ }
165
+ case "PayoutClaimed":
166
+ if (!same("evm", e.fields.wallet, wallet)) return;
167
+ out.payouts.push({ quote: e.fields.quote, to: e.fields.to, amount: e.fields.amount });
168
+ add(e.fields.quote, e.fields.amount);
169
+ return;
170
+ case "TeamBuyClaimed":
171
+ if (same("evm", e.fields.wallet, wallet)) tok(String(e.fields.launchId), "teamBuy", e.fields.tokens);
172
+ return;
173
+ case "TeamShareBoughtClaimed":
174
+ if (same("evm", e.fields.wallet, wallet)) tok(String(e.fields.launchId), "shareBought", e.fields.tokens);
175
+ return;
176
+ case "CreatorBuyUnlocked":
177
+ if (same("evm", e.fields.creator, wallet)) tok(String(e.fields.launchId), "teamBuyUnlocked", e.fields.paid);
178
+ return;
179
+ }
180
+ }
181
+
182
+ export interface ReceiptCreated {
183
+ family: FloorFamily;
184
+
185
+ launch: string;
186
+ launchId: bigint;
187
+
188
+ token: string;
189
+ quote: string;
190
+ creator: string;
191
+ presetId: number;
192
+ creatorBuyTokens: bigint;
193
+ createFee: bigint;
194
+ nativeCreateFee: bigint;
195
+ name: string;
196
+ symbol: string;
197
+ listed: boolean;
198
+ }
199
+
200
+ const text = (b: Uint8Array): string => {
201
+ let n = b.length;
202
+ while (n > 0 && b[n - 1] === 0) n--;
203
+ return new TextDecoder().decode(b.slice(0, n));
204
+ };
205
+
206
+ export function createdOf(landed: LandedTx, scope: ReceiptScope = {}): ReceiptCreated | null {
207
+ for (const e of eventsOf(landed, scope)) {
208
+ if (e.chain === "solana" && (e.name === "LaunchCreatedV2" || e.name === "LaunchCreated")) {
209
+ const f = e.fields;
210
+ return {
211
+ family: "solana", launch: f.launch, launchId: f.id, token: f.mint, quote: f.quoteMint, creator: f.creator, presetId: f.presetId, creatorBuyTokens: f.creatorBuyTokens,
212
+ createFee: f.createFee, nativeCreateFee: f.nativeCreateFee, name: text(f.name), symbol: text(f.symbol), listed: f.listed,
213
+ };
214
+ }
215
+ if (e.chain === "evm" && e.name === "LaunchCreated") {
216
+ const f = e.fields;
217
+ return {
218
+ family: "evm", launch: String(f.launchId), launchId: f.launchId, token: f.token, quote: f.quote, creator: f.creator, presetId: Number(f.presetId),
219
+ creatorBuyTokens: f.creatorBuyTokens, createFee: f.createFee, nativeCreateFee: f.nativeCreateFee, name: f.name, symbol: f.symbol, listed: f.listed,
220
+ };
221
+ }
222
+ }
223
+ return null;
224
+ }
225
+
226
+ export interface ReceiptBurned {
227
+ launch: string;
228
+ fromTeamBuy: bigint;
229
+ fromWallet: bigint;
230
+ fromShareBought: bigint;
231
+ total: bigint;
232
+
233
+ supply: bigint;
234
+ sOut: bigint;
235
+ floorAfterX64: bigint;
236
+ }
237
+
238
+ export function burnedOf(landed: LandedTx, p: { wallet: string } & ReceiptScope): ReceiptBurned[] {
239
+ const out: ReceiptBurned[] = [];
240
+ for (const e of eventsOf(landed, p)) {
241
+ if (!((e.chain === "solana" && e.name === "TokensBurned") || (e.chain === "evm" && e.name === "TokensBurnedV2"))) continue;
242
+ const f = e.fields as { wallet: string; fromTeamBuy: bigint; fromWallet: bigint; fromShareBought: bigint | null; supply: bigint; sOut: bigint; floorAfterX64: bigint };
243
+ if (!same(e.chain, f.wallet, p.wallet)) continue;
244
+ const share = f.fromShareBought ?? 0n;
245
+ out.push({
246
+ launch: launchOf(e), fromTeamBuy: f.fromTeamBuy, fromWallet: f.fromWallet, fromShareBought: share, total: f.fromTeamBuy + f.fromWallet + share,
247
+ supply: f.supply, sOut: f.sOut, floorAfterX64: f.floorAfterX64,
248
+ });
249
+ }
250
+ return out;
251
+ }
@@ -0,0 +1,114 @@
1
+
2
+
3
+ import { addressBytes, concatBytes } from "../codec.ts";
4
+ import { coinKey } from "../route/graph.ts";
5
+ import { AccountRole } from "../solana/instruction.ts";
6
+ import type { Instruction } from "../solana/instruction.ts";
7
+ import { ADDRESS_LOOKUP_TABLE_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, findProgramAddress } from "../solana/pda.ts";
8
+ import { createAssociatedTokenAccountIdempotentInstruction } from "../solana/tx.ts";
9
+ import { FLOOR_AMM_PROGRAM_ID } from "../solana/floor/program.ts";
10
+ import { ATA_CREATE_CU, routeTradeAccountsV2 } from "./route.ts";
11
+ import type { HopQuoteV2, PoolV2, RouteQuoteV2 } from "./route.ts";
12
+ import { buyFromBalanceInstructionV2, buyInstructionV2, openHolderPositionInstruction, sellFromBalanceInstructionV2, sellInstructionV2 } from "./solana.ts";
13
+
14
+ export const MAX_TABLE_ADDRESSES_V2 = 256;
15
+
16
+ export const MAX_EXTEND_ADDRESSES_V2 = 30;
17
+
18
+ const PROBES = ["kVwEj4fW3BriC9M17tRS88VResDomcntxJAgL1kJmDb", "BvhkBZ5kEP1dxJzfprm8kLk9dSgvT8cLziBFTHZef6r7"] as const;
19
+
20
+ function quoteProgramOf(pool: PoolV2, all: readonly PoolV2[]): string {
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: string, pool: PoolV2, all: readonly PoolV2[], programId: string): Instruction[] {
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: readonly PoolV2[], programId = FLOOR_AMM_PROGRAM_ID): string[] {
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: string[] = [];
49
+ const has = new Set<string>();
50
+ for (const x of seen[0].order) {
51
+ if (has.has(x) || !other.has(x) || seen[0].invoked.has(x) || (PROBES as readonly string[]).includes(x)) continue;
52
+ has.add(x);
53
+ out.push(x);
54
+ }
55
+ return out;
56
+ }
57
+
58
+ export function createRouteTableInstructionV2(p: { authority: string; payer: string; recentSlot: bigint }): { instruction: Instruction; table: string } {
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: { table: string; authority: string; payer: string; addresses: readonly string[] }): Instruction[] {
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: Instruction[] = [];
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]] as const),
98
+ sell: Object.freeze([[1, 68_471], [10, 122_930], [24, 216_856]] as const),
99
+ ata: ATA_CREATE_CU,
100
+ });
101
+
102
+ export function hopComputeUnitsV2(side: "buy" | "sell", crossings: number): number | undefined {
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<L>(q: RouteQuoteV2<L>, read: (hq: HopQuoteV2<L>, i: number) => { crossings: number } | null): (number | undefined)[] {
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
+ }