@floorlaunchpad/sdk 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (292) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/LICENSE +201 -60
  3. package/README.md +45 -24
  4. package/dist/abi/evm-event-names.js +12 -0
  5. package/dist/abi/evm.gen.js +130 -0
  6. package/dist/abi/functions.gen.js +144 -0
  7. package/dist/abi/index.js +17 -0
  8. package/dist/abi/solana.gen.js +128 -0
  9. package/dist/burn.js +21 -0
  10. package/dist/client/evm/index.js +191 -0
  11. package/dist/client/index.js +19 -0
  12. package/dist/client/solana/index.js +259 -0
  13. package/dist/client/types.js +5 -0
  14. package/dist/codec.js +77 -0
  15. package/dist/config/codeHash.js +9 -0
  16. package/dist/config/generated.js +746 -0
  17. package/dist/config/index.js +49 -0
  18. package/dist/config/registry.gen.js +118 -0
  19. package/dist/config/solana-genesis.js +7 -0
  20. package/dist/ed25519.js +31 -0
  21. package/dist/errors.js +249 -0
  22. package/dist/events/index.js +40 -0
  23. package/dist/events/types.gen.js +3 -0
  24. package/dist/evm/abi.js +283 -0
  25. package/dist/evm/balances.js +26 -0
  26. package/dist/evm/chains.js +144 -0
  27. package/dist/evm/floor/abi.gen.js +195 -0
  28. package/dist/evm/floor/calls.js +230 -0
  29. package/dist/evm/floor/create.js +49 -0
  30. package/dist/evm/floor/events.js +41 -0
  31. package/dist/evm/floor/iface.js +44 -0
  32. package/dist/evm/items.js +209 -0
  33. package/dist/evm/keccak.js +118 -0
  34. package/dist/evm/multicall.js +30 -0
  35. package/dist/evm/router.js +184 -0
  36. package/dist/evm/zap.js +128 -0
  37. package/dist/fixtures.js +53 -0
  38. package/dist/generated/index.js +28 -0
  39. package/dist/generated/interface.gen.js +146 -0
  40. package/dist/handle.js +22 -0
  41. package/dist/index.js +91 -7
  42. package/dist/indexer/bodies.js +473 -0
  43. package/dist/indexer/client.js +104 -0
  44. package/dist/indexer/contract.js +349 -0
  45. package/dist/indexer/decode.js +97 -0
  46. package/dist/indexer/partners.js +100 -0
  47. package/dist/indexer/paths.js +127 -0
  48. package/dist/indexer/read.js +109 -0
  49. package/dist/indexer/stream.js +71 -0
  50. package/dist/lock.js +16 -0
  51. package/dist/math/fixed.js +154 -0
  52. package/dist/math/genesis.js +39 -0
  53. package/dist/math/index.js +20 -0
  54. package/dist/math/u256.js +49 -0
  55. package/dist/math/v2/ask.js +162 -0
  56. package/dist/math/v2/book.js +607 -0
  57. package/dist/math/v2/circulating.js +62 -0
  58. package/dist/math/v2/create.js +122 -0
  59. package/dist/math/v2/display.js +208 -0
  60. package/dist/math/v2/error.js +17 -0
  61. package/dist/math/v2/feasibility.js +174 -0
  62. package/dist/math/v2/fees.js +121 -0
  63. package/dist/math/v2/holders.js +105 -0
  64. package/dist/math/v2/milestones.js +40 -0
  65. package/dist/math/v2/pow.js +69 -0
  66. package/dist/math/v2/preset.js +151 -0
  67. package/dist/math/v2/receipt.js +108 -0
  68. package/dist/math/v2/relay.js +217 -0
  69. package/dist/math/v2/rows.js +365 -0
  70. package/dist/math/v2/split.js +111 -0
  71. package/dist/math/v2/sqrt.js +84 -0
  72. package/dist/math/v2/supply.js +21 -0
  73. package/dist/math/v2/team.js +75 -0
  74. package/dist/math/v2/trade.js +126 -0
  75. package/dist/math/v2/wide.js +51 -0
  76. package/dist/quote.js +6 -0
  77. package/dist/registryPrice.js +23 -0
  78. package/dist/route/edge-jupiter.js +42 -0
  79. package/dist/route/edge.js +11 -0
  80. package/dist/route/graph.js +51 -0
  81. package/dist/route/path.js +75 -0
  82. package/dist/route/quote.js +15 -0
  83. package/dist/route-jupiter.js +35 -0
  84. package/dist/route.js +8 -0
  85. package/dist/sha256.js +61 -0
  86. package/dist/solana/alt.js +18 -0
  87. package/dist/solana/floor/accounts.js +390 -0
  88. package/dist/solana/floor/borsh.js +66 -0
  89. package/dist/solana/floor/create.js +65 -0
  90. package/dist/solana/floor/events.js +140 -0
  91. package/dist/solana/floor/genesis.js +91 -0
  92. package/dist/solana/floor/genesisList.js +68 -0
  93. package/dist/solana/floor/instructions.js +435 -0
  94. package/dist/solana/floor/metadata.js +198 -0
  95. package/dist/solana/floor/mint.js +206 -0
  96. package/dist/solana/floor/program.js +64 -0
  97. package/dist/solana/floor/registry.js +83 -0
  98. package/dist/solana/instruction.js +49 -0
  99. package/dist/solana/pda.js +88 -0
  100. package/dist/solana/tx.js +138 -0
  101. package/dist/v2/burnPreview.js +35 -0
  102. package/dist/v2/circulating.js +11 -0
  103. package/dist/v2/claimAllEvm.js +70 -0
  104. package/dist/v2/claims-evm.js +120 -0
  105. package/dist/v2/claims.js +218 -0
  106. package/dist/v2/dead.js +21 -0
  107. package/dist/v2/devbuy.js +37 -0
  108. package/dist/v2/events.js +115 -0
  109. package/dist/v2/evm.js +299 -0
  110. package/dist/v2/evmReads.js +76 -0
  111. package/dist/v2/genesis.js +76 -0
  112. package/dist/v2/layouts.js +217 -0
  113. package/dist/v2/market-evm.js +74 -0
  114. package/dist/v2/presetRowEvm.js +45 -0
  115. package/dist/v2/press.js +106 -0
  116. package/dist/v2/quoteMint.js +52 -0
  117. package/dist/v2/receipt-tx.js +149 -0
  118. package/dist/v2/route-alt.js +114 -0
  119. package/dist/v2/route.js +389 -0
  120. package/dist/v2/solana.js +391 -0
  121. package/dist/v2/solanaSettle.js +97 -0
  122. package/dist/v2/tx/evm.js +126 -0
  123. package/dist/v2/tx/genesis.js +47 -0
  124. package/dist/v2/tx/index.js +5 -0
  125. package/dist/v2/tx/solana.js +118 -0
  126. package/dist/v2/types.js +13 -0
  127. package/docs/aggregator.md +61 -0
  128. package/docs/bot.md +50 -0
  129. package/docs/frontend.md +29 -0
  130. package/docs/wallet.md +54 -0
  131. package/package.json +428 -32
  132. package/src/abi/evm-event-names.ts +12 -0
  133. package/src/abi/evm.gen.ts +139 -0
  134. package/src/abi/functions.gen.ts +154 -0
  135. package/src/abi/index.ts +30 -0
  136. package/src/abi/solana.gen.ts +138 -0
  137. package/src/burn.ts +37 -0
  138. package/src/client/evm/index.ts +199 -0
  139. package/src/client/index.ts +23 -0
  140. package/src/client/solana/index.ts +260 -0
  141. package/src/client/types.ts +162 -0
  142. package/src/codec.ts +77 -0
  143. package/src/config/codeHash.ts +9 -0
  144. package/src/config/generated.ts +746 -0
  145. package/src/config/index.ts +91 -0
  146. package/src/config/registry.gen.ts +118 -0
  147. package/src/config/solana-genesis.ts +7 -0
  148. package/src/ed25519.ts +31 -0
  149. package/src/errors.ts +270 -0
  150. package/src/events/index.ts +44 -0
  151. package/src/events/types.gen.ts +831 -0
  152. package/src/evm/abi.ts +292 -0
  153. package/src/evm/balances.ts +26 -0
  154. package/src/evm/chains.ts +167 -0
  155. package/src/evm/floor/abi.gen.ts +195 -0
  156. package/src/evm/floor/calls.ts +247 -0
  157. package/src/evm/floor/create.ts +68 -0
  158. package/src/evm/floor/events.ts +51 -0
  159. package/src/evm/floor/iface.ts +45 -0
  160. package/src/evm/items.ts +252 -0
  161. package/src/evm/keccak.ts +118 -0
  162. package/src/evm/multicall.ts +43 -0
  163. package/src/evm/router.ts +209 -0
  164. package/src/evm/zap.ts +152 -0
  165. package/src/fixtures.ts +53 -0
  166. package/src/generated/index.ts +29 -0
  167. package/src/generated/interface.gen.ts +153 -0
  168. package/src/handle.ts +22 -0
  169. package/src/index.ts +91 -6
  170. package/src/indexer/bodies.ts +528 -0
  171. package/src/indexer/client.ts +178 -0
  172. package/src/indexer/contract.ts +410 -0
  173. package/src/indexer/decode.ts +109 -0
  174. package/src/indexer/partners.ts +109 -0
  175. package/src/indexer/paths.ts +190 -0
  176. package/src/indexer/read.ts +119 -0
  177. package/src/indexer/stream.ts +83 -0
  178. package/src/lock.ts +16 -0
  179. package/src/math/fixed.ts +154 -0
  180. package/src/math/genesis.ts +39 -0
  181. package/src/math/index.ts +24 -0
  182. package/src/math/u256.ts +49 -0
  183. package/src/math/v2/ask.ts +218 -0
  184. package/src/math/v2/book.ts +660 -0
  185. package/src/math/v2/circulating.ts +69 -0
  186. package/src/math/v2/create.ts +139 -0
  187. package/src/math/v2/display.ts +250 -0
  188. package/src/math/v2/error.ts +19 -0
  189. package/src/math/v2/feasibility.ts +200 -0
  190. package/src/math/v2/fees.ts +121 -0
  191. package/src/math/v2/holders.ts +124 -0
  192. package/src/math/v2/milestones.ts +51 -0
  193. package/src/math/v2/pow.ts +71 -0
  194. package/src/math/v2/preset.ts +189 -0
  195. package/src/math/v2/receipt.ts +162 -0
  196. package/src/math/v2/relay.ts +235 -0
  197. package/src/math/v2/rows.ts +412 -0
  198. package/src/math/v2/split.ts +160 -0
  199. package/src/math/v2/sqrt.ts +86 -0
  200. package/src/math/v2/supply.ts +27 -0
  201. package/src/math/v2/team.ts +75 -0
  202. package/src/math/v2/trade.ts +145 -0
  203. package/src/math/v2/wide.ts +52 -0
  204. package/src/quote.ts +6 -0
  205. package/src/registryPrice.ts +28 -0
  206. package/src/route/edge-jupiter.ts +71 -0
  207. package/src/route/edge.ts +22 -0
  208. package/src/route/graph.ts +77 -0
  209. package/src/route/path.ts +84 -0
  210. package/src/route/quote.ts +46 -0
  211. package/src/route-jupiter.ts +36 -0
  212. package/src/route.ts +8 -0
  213. package/src/sha256.ts +61 -0
  214. package/src/solana/alt.ts +25 -0
  215. package/src/solana/floor/accounts.ts +393 -0
  216. package/src/solana/floor/borsh.ts +66 -0
  217. package/src/solana/floor/create.ts +89 -0
  218. package/src/solana/floor/events.ts +147 -0
  219. package/src/solana/floor/genesis.ts +111 -0
  220. package/src/solana/floor/genesisList.ts +75 -0
  221. package/src/solana/floor/instructions.ts +491 -0
  222. package/src/solana/floor/metadata.ts +246 -0
  223. package/src/solana/floor/mint.ts +242 -0
  224. package/src/solana/floor/program.ts +64 -0
  225. package/src/solana/floor/registry.ts +96 -0
  226. package/src/solana/instruction.ts +61 -0
  227. package/src/solana/pda.ts +91 -0
  228. package/src/solana/tx.ts +143 -0
  229. package/src/v2/burnPreview.ts +52 -0
  230. package/src/v2/circulating.ts +22 -0
  231. package/src/v2/claimAllEvm.ts +97 -0
  232. package/src/v2/claims-evm.ts +179 -0
  233. package/src/v2/claims.ts +288 -0
  234. package/src/v2/dead.ts +21 -0
  235. package/src/v2/devbuy.ts +55 -0
  236. package/src/v2/events.ts +131 -0
  237. package/src/v2/evm.ts +349 -0
  238. package/src/v2/evmReads.ts +121 -0
  239. package/src/v2/genesis.ts +80 -0
  240. package/src/v2/layouts.ts +219 -0
  241. package/src/v2/market-evm.ts +95 -0
  242. package/src/v2/presetRowEvm.ts +54 -0
  243. package/src/v2/press.ts +124 -0
  244. package/src/v2/quoteMint.ts +63 -0
  245. package/src/v2/receipt-tx.ts +251 -0
  246. package/src/v2/route-alt.ts +114 -0
  247. package/src/v2/route.ts +494 -0
  248. package/src/v2/solana.ts +439 -0
  249. package/src/v2/solanaSettle.ts +117 -0
  250. package/src/v2/tx/evm.ts +152 -0
  251. package/src/v2/tx/genesis.ts +66 -0
  252. package/src/v2/tx/index.ts +5 -0
  253. package/src/v2/tx/solana.ts +140 -0
  254. package/src/v2/types.ts +284 -0
  255. package/dist/adapter.d.ts +0 -28
  256. package/dist/adapter.d.ts.map +0 -1
  257. package/dist/adapter.js +0 -2
  258. package/dist/adapter.js.map +0 -1
  259. package/dist/chain.d.ts +0 -23
  260. package/dist/chain.d.ts.map +0 -1
  261. package/dist/chain.js +0 -25
  262. package/dist/chain.js.map +0 -1
  263. package/dist/client.d.ts +0 -26
  264. package/dist/client.d.ts.map +0 -1
  265. package/dist/client.js +0 -43
  266. package/dist/client.js.map +0 -1
  267. package/dist/dex-adapter.d.ts +0 -228
  268. package/dist/dex-adapter.d.ts.map +0 -1
  269. package/dist/dex-adapter.js +0 -407
  270. package/dist/dex-adapter.js.map +0 -1
  271. package/dist/index.d.ts +0 -7
  272. package/dist/index.d.ts.map +0 -1
  273. package/dist/index.js.map +0 -1
  274. package/dist/quotes.d.ts +0 -12
  275. package/dist/quotes.d.ts.map +0 -1
  276. package/dist/quotes.gen.d.ts +0 -8
  277. package/dist/quotes.gen.d.ts.map +0 -1
  278. package/dist/quotes.gen.js +0 -2170
  279. package/dist/quotes.gen.js.map +0 -1
  280. package/dist/quotes.js +0 -24
  281. package/dist/quotes.js.map +0 -1
  282. package/dist/types.d.ts +0 -111
  283. package/dist/types.d.ts.map +0 -1
  284. package/dist/types.js +0 -2
  285. package/dist/types.js.map +0 -1
  286. package/src/adapter.ts +0 -20
  287. package/src/chain.ts +0 -39
  288. package/src/client.ts +0 -41
  289. package/src/dex-adapter.ts +0 -477
  290. package/src/quotes.gen.ts +0 -2170
  291. package/src/quotes.ts +0 -29
  292. package/src/types.ts +0 -115
@@ -0,0 +1,288 @@
1
+
2
+
3
+ import { addressBytes, base58Encode } from "../codec.ts";
4
+ import { computeUnitLimitInstruction, computeUnitPriceInstruction } from "../solana/instruction.ts";
5
+ import type { Instruction } from "../solana/instruction.ts";
6
+ import { PACKET_DATA_SIZE, compileV0Message, transactionSize } from "../solana/tx.ts";
7
+ import type { LookupTable } from "../solana/tx.ts";
8
+ import { decodeTeamSlot } from "../solana/floor/accounts.ts";
9
+ import { CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_SHARE_BOUGHT, CLAIM_TEAM_BUY, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF } from "../solana/floor/instructions.ts";
10
+ import { FLOOR_AMM_PROGRAM_ID, configPda, refOwedPda, refOwedVault } from "../solana/floor/program.ts";
11
+ import { TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, WSOL_MINT, associatedTokenAddress } from "../solana/pda.ts";
12
+ import { CREATOR_BUY_LOCK_SECS, MAX_MEMBERS, splitTeamBuyV2 } from "../math/v2/team.ts";
13
+ import { holderSync, holderTake } from "../math/v2/holders.ts";
14
+ import { FloorMathError } from "../math/index.ts";
15
+ import { decodeHolderBook, decodeHolderPosition, decodeLaunchV2 } from "./layouts.ts";
16
+ import { CLAIM_HOLDER, claimInstructionV2 } from "./solana.ts";
17
+ import type { ClaimLaunchV2, Ctx } from "./solana.ts";
18
+
19
+ export const MAX_TX_ACCOUNT_LOCKS_V2 = 64;
20
+
21
+ export const CLAIM_COMPUTE_UNIT_LIMIT_V2 = 1_400_000;
22
+
23
+ const MEMBER_IN = 2;
24
+ const KIND_WALLET = 0;
25
+
26
+ export function teamBuyDueV2(createdAt: bigint, now: bigint): boolean {
27
+ const c = createdAt < 0n ? 0n : createdAt;
28
+ const due = c + CREATOR_BUY_LOCK_SECS > 0xffff_ffff_ffff_ffffn ? 0xffff_ffff_ffff_ffffn : c + CREATOR_BUY_LOCK_SECS;
29
+ return now >= due;
30
+ }
31
+
32
+ export function holderOwedNowV2(p: { positionData: Uint8Array | null; bookData: Uint8Array; balance: bigint }): bigint {
33
+ if (!p.positionData) return 0n;
34
+ const pos = decodeHolderPosition(p.positionData) as any;
35
+ const book = decodeHolderBook(p.bookData) as any;
36
+ if (pos.launch !== book.launch) throw new RangeError("this HolderPosition is not the book's launch");
37
+ const b = { index: book.index as bigint, w: book.w as bigint, carry: book.carry as bigint, bookedTotal: book.bookedTotal as bigint, paidTotal: book.paidTotal as bigint };
38
+ const q = { amount: pos.amount as bigint, owed: pos.owed as bigint, indexSnap: pos.indexSnap as bigint, optedOut: false };
39
+ holderSync(b, q, p.balance);
40
+ return holderTake(b, q);
41
+ }
42
+
43
+ export interface OwedV2 {
44
+
45
+ creator: bigint;
46
+
47
+ creatorRef: bigint;
48
+
49
+ teamSlot: bigint;
50
+
51
+ teamBuy: bigint;
52
+
53
+ shareBought: bigint;
54
+
55
+ holder: bigint;
56
+ }
57
+ export interface LaunchOwedV2 {
58
+ launch: string;
59
+ quoteMint: string;
60
+ quoteTokenProgram: string;
61
+ quoteVault: string;
62
+ baseMint: string;
63
+ tokenVault: string;
64
+ teamSlot: string | null;
65
+ owed: OwedV2;
66
+ }
67
+
68
+ export const claimBitsV2 = (o: OwedV2): number =>
69
+ (o.creator > 0n ? CLAIM_CREATOR : 0) | (o.creatorRef > 0n ? CLAIM_CREATOR_REF : 0) | (o.teamSlot > 0n ? CLAIM_TEAM_SLOT : 0) |
70
+ (o.teamBuy > 0n ? CLAIM_TEAM_BUY : 0) | (o.shareBought > 0n ? CLAIM_SHARE_BOUGHT : 0) | (o.holder > 0n ? CLAIM_HOLDER : 0);
71
+ const quoteOf = (o: OwedV2): bigint => o.creator + o.creatorRef + o.teamSlot + o.holder;
72
+
73
+ export function launchOwedV2(p: {
74
+ wallet: string; launch: string; launchData: Uint8Array; teamSlot?: { address: string; data: Uint8Array } | null; now: bigint; holderOwed?: bigint;
75
+ }): LaunchOwedV2 {
76
+ const a = decodeLaunchV2(p.launchData);
77
+ const owed: OwedV2 = { creator: 0n, creatorRef: 0n, teamSlot: 0n, teamBuy: 0n, shareBought: 0n, holder: p.holderOwed ?? 0n };
78
+ const places: boolean[] = Array(MAX_MEMBERS).fill(false);
79
+ if (a.creator === p.wallet) {
80
+ owed.creator = a.creatorOwed;
81
+ places[0] = true;
82
+ }
83
+ (a.creatorRef as string[]).forEach((r, i) => {
84
+ if (r === p.wallet) owed.creatorRef += a.creatorRefOwed[i];
85
+ });
86
+ if (p.teamSlot) {
87
+ const t = decodeTeamSlot(p.teamSlot.data);
88
+ if (t.launch !== p.launch) throw new RangeError("this TeamSlot is not the launch's");
89
+ const k = addressBytes(p.wallet);
90
+ const i = (t.members as any[]).findIndex((m) => m.state === MEMBER_IN && m.kind === KIND_WALLET && (m.key as Uint8Array).every((x, j) => x === k[j]));
91
+ if (i >= 0) {
92
+ owed.teamSlot = a.slotOwed[i];
93
+ places[i] = true;
94
+ }
95
+ }
96
+ const createdAt = a.createdAt < 0n ? 0n : (a.createdAt as bigint);
97
+ let buy: bigint[] = a.teamBuyOwed.slice();
98
+ const due = teamBuyDueV2(a.createdAt, p.now);
99
+ if (a.creatorLocked > 0n) {
100
+ try {
101
+ const split = splitTeamBuyV2(a.creatorLocked, a.slotWeightPpb, createdAt, p.now);
102
+ buy = buy.map((x, i) => x + split[i]);
103
+ } catch (e) {
104
+ if (!(e instanceof FloorMathError && e.code === "NotDue")) throw e;
105
+ }
106
+ }
107
+ for (let i = 0; i < MAX_MEMBERS; i++) {
108
+ if (!places[i]) continue;
109
+ if (due) owed.teamBuy += buy[i];
110
+ owed.shareBought += a.slotBoughtOwed[i];
111
+ }
112
+ return {
113
+ launch: p.launch, quoteMint: a.quoteMint, quoteTokenProgram: a.quoteTokenProgram, quoteVault: a.quoteVault, baseMint: a.baseMint,
114
+ tokenVault: a.tokenVault, teamSlot: p.teamSlot?.address ?? null, owed,
115
+ };
116
+ }
117
+
118
+ export interface ClaimTxV2 {
119
+ quoteMint: string;
120
+ instructions: Instruction[];
121
+ message: Uint8Array;
122
+ size: number;
123
+ locks: number;
124
+ launches: string[];
125
+
126
+ refOwed: boolean;
127
+ types: number;
128
+
129
+ unwrap: boolean;
130
+
131
+ amount: bigint;
132
+
133
+ tokens: bigint;
134
+ }
135
+ export interface ClaimPlanV2 {
136
+ transactions: ClaimTxV2[];
137
+
138
+ skipped: string[];
139
+ }
140
+
141
+ function locksOf(payer: string, ixs: Instruction[]): number {
142
+ const s = new Set<string>([payer]);
143
+ for (const ix of ixs) {
144
+ s.add(ix.programAddress);
145
+ for (const a of ix.accounts) s.add(a.address);
146
+ }
147
+ return s.size;
148
+ }
149
+
150
+ export function planClaimAllV2(p: {
151
+ wallet: string;
152
+ sender?: string;
153
+ launches: readonly LaunchOwedV2[];
154
+
155
+ refOwed?: readonly { quoteMint: string; quoteTokenProgram?: string; owed: bigint }[];
156
+ recentBlockhash: string;
157
+ lookupTables?: LookupTable[];
158
+ microLamports?: bigint;
159
+ computeUnitLimit?: number;
160
+
161
+ unwrapSol?: boolean;
162
+
163
+ launchQuotes?: readonly string[];
164
+ } & Ctx): ClaimPlanV2 {
165
+ const sender = p.sender ?? p.wallet;
166
+ const skipped: string[] = [];
167
+ type Group = { tp: string; classes: Map<number, LaunchOwedV2[]>; refs: bigint };
168
+ const byQuote = new Map<string, Group>();
169
+ const group = (q: string, tp: string): Group => {
170
+ let g = byQuote.get(q);
171
+ if (!g) byQuote.set(q, (g = { tp, classes: new Map(), refs: 0n }));
172
+ return g;
173
+ };
174
+ const classOf = (o: OwedV2): number => (o.teamBuy > 0n || o.shareBought > 0n ? 1 : 0) | (o.holder > 0n ? 2 : 0);
175
+ for (const l of p.launches) {
176
+ if (claimBitsV2(l.owed) === 0) {
177
+ skipped.push(l.launch);
178
+ continue;
179
+ }
180
+ const g = group(l.quoteMint, l.quoteTokenProgram);
181
+ const c = classOf(l.owed);
182
+ if (!g.classes.has(c)) g.classes.set(c, []);
183
+ g.classes.get(c)!.push(l);
184
+ }
185
+ for (const r of p.refOwed ?? []) {
186
+ if (r.owed === 0n) {
187
+ skipped.push(`ref_owed:${r.quoteMint}`);
188
+ continue;
189
+ }
190
+ const known = byQuote.get(r.quoteMint);
191
+ if (!known && !r.quoteTokenProgram) throw new RangeError(`the RefOwed in ${r.quoteMint} needs its quote's token program`);
192
+ group(r.quoteMint, known?.tp ?? r.quoteTokenProgram!).refs = r.owed;
193
+ }
194
+ const pid = p.programId ?? FLOOR_AMM_PROGRAM_ID;
195
+ const budget = (): Instruction[] => [computeUnitLimitInstruction(p.computeUnitLimit ?? CLAIM_COMPUTE_UNIT_LIMIT_V2), ...(p.microLamports !== undefined ? [computeUnitPriceInstruction(p.microLamports)] : [])];
196
+ const fits = (t: ClaimTxV2): boolean => t.size <= PACKET_DATA_SIZE && t.locks <= MAX_TX_ACCOUNT_LOCKS_V2;
197
+ const txs: ClaimTxV2[] = [];
198
+ for (const [quoteMint, g] of byQuote) {
199
+ const unwrap = (p.unwrapSol ?? true) && quoteMint === WSOL_MINT && g.tp === TOKEN_PROGRAM_ID;
200
+ const build = (ls: LaunchOwedV2[], refs: bigint): ClaimTxV2 => {
201
+ const types = ls.reduce((a, l) => a | claimBitsV2(l.owed), 0) | (refs > 0n ? CLAIM_TRADER_REF : 0);
202
+ const tok = (types & (CLAIM_TEAM_BUY | CLAIM_SHARE_BOUGHT)) !== 0;
203
+ const ro = refOwedPda(p.wallet, quoteMint, pid)[0];
204
+ const claim = claimInstructionV2({
205
+ sender, wallet: p.wallet, quoteMint, quoteTokenProgram: g.tp, destination: associatedTokenAddress(p.wallet, quoteMint, g.tp),
206
+ config: configPda(pid)[0], refOwed: refs > 0n ? ro : null, refOwedVault: refs > 0n ? refOwedVault(ro, quoteMint, g.tp) : null, types,
207
+ launches: ls.map((l): ClaimLaunchV2 => ({
208
+ launch: l.launch, quoteVault: l.quoteVault, teamSlot: l.teamSlot, baseMint: l.baseMint,
209
+ ...(tok ? { teamBuy: { baseMint: l.baseMint, tokenVault: l.tokenVault, walletBase: associatedTokenAddress(p.wallet, l.baseMint, TOKEN_2022_PROGRAM_ID) } } : {}),
210
+ })),
211
+ unwrap, quoteIsLaunch: p.launchQuotes?.includes(quoteMint) ?? false, programId: p.programId, hookProgramId: p.hookProgramId,
212
+ });
213
+ const instructions = [...budget(), claim];
214
+ const message = compileV0Message({ payer: sender, instructions, recentBlockhash: p.recentBlockhash, lookupTables: p.lookupTables });
215
+ return {
216
+ quoteMint, instructions, message, size: transactionSize(message), locks: locksOf(sender, instructions), launches: ls.map((l) => l.launch),
217
+ refOwed: refs > 0n, types, unwrap, amount: ls.reduce((a, l) => a + quoteOf(l.owed), 0n) + refs, tokens: ls.reduce((a, l) => a + l.owed.teamBuy + l.owed.shareBought, 0n),
218
+ };
219
+ };
220
+ const alone = (ls: LaunchOwedV2[], refs: bigint, what: string): ClaimTxV2 => {
221
+ const t = build(ls, refs);
222
+ if (!fits(t)) throw new RangeError(`${what} alone does not fit one transaction`);
223
+ return t;
224
+ };
225
+ let refs = g.refs;
226
+ for (const c of [...g.classes.keys()].sort()) {
227
+ let cur: LaunchOwedV2[] = [];
228
+ let last: ClaimTxV2 | null = null;
229
+ for (const l of g.classes.get(c)!) {
230
+ const t = build([...cur, l], refs);
231
+ if (fits(t)) {
232
+ cur.push(l);
233
+ last = t;
234
+ continue;
235
+ }
236
+ if (cur.length === 0 && refs > 0n) {
237
+
238
+ txs.push(alone([], refs, "the referral claim"));
239
+ refs = 0n;
240
+ } else if (cur.length === 0) throw new RangeError(`launch ${l.launch} alone does not fit one transaction`);
241
+ else {
242
+ txs.push(last!);
243
+ refs = 0n;
244
+ }
245
+ cur = [l];
246
+ last = alone(cur, 0n, `launch ${l.launch}`);
247
+ }
248
+ if (last) {
249
+ txs.push(last);
250
+ refs = 0n;
251
+ }
252
+ }
253
+ if (refs > 0n) txs.push(alone([], refs, "the referral claim"));
254
+ }
255
+ return { transactions: txs, skipped };
256
+ }
257
+
258
+ export interface ClaimLaunchesPerTxV2 {
259
+
260
+ plain: number;
261
+
262
+ tokens: number;
263
+
264
+ holder: number;
265
+
266
+ both: number;
267
+ }
268
+
269
+ export function claimLaunchesPerTxV2(p: { quoteMint?: string; quoteTokenProgram?: string; lookupTables?: LookupTable[]; microLamports?: bigint } & Ctx = {}): ClaimLaunchesPerTxV2 {
270
+ const key = (i: number): string => {
271
+ const b = new Uint8Array(32);
272
+ b[0] = 0xf1;
273
+ b[1] = i & 0xff;
274
+ b[2] = i >> 8;
275
+ return base58Encode(b);
276
+ };
277
+ const quoteMint = p.quoteMint ?? key(0xfff0);
278
+ const quoteTokenProgram = p.quoteTokenProgram ?? TOKEN_PROGRAM_ID;
279
+ const measure = (o: Partial<OwedV2>): number => {
280
+ const launches = Array.from({ length: 64 }, (_, i): LaunchOwedV2 => ({
281
+ launch: key(i), quoteMint, quoteTokenProgram, quoteVault: key(0x100 + i), baseMint: key(0x200 + i), tokenVault: key(0x300 + i), teamSlot: key(0x400 + i),
282
+ owed: { creator: 0n, creatorRef: 0n, teamSlot: 0n, teamBuy: 0n, shareBought: 0n, holder: 0n, ...o },
283
+ }));
284
+ const plan = planClaimAllV2({ wallet: key(0xfff1), launches, recentBlockhash: key(0xfff2), lookupTables: p.lookupTables, microLamports: p.microLamports, programId: p.programId, hookProgramId: p.hookProgramId });
285
+ return plan.transactions[0].launches.length;
286
+ };
287
+ return { plain: measure({ creator: 1n, teamSlot: 1n }), tokens: measure({ creator: 1n, teamBuy: 1n }), holder: measure({ holder: 1n }), both: measure({ teamBuy: 1n, holder: 1n }) };
288
+ }
package/src/v2/dead.ts ADDED
@@ -0,0 +1,21 @@
1
+
2
+
3
+ export const SOLANA_DEAD_OWNERS: readonly string[] = [
4
+ "1nc1nerator11111111111111111111111111111111",
5
+ "11111111111111111111111111111111",
6
+ ];
7
+
8
+ export const EVM_DEAD_ADDRESSES: readonly string[] = [
9
+ "0x0000000000000000000000000000000000000000",
10
+ "0x000000000000000000000000000000000000dead",
11
+ "0x0000000000000000000000000000000000000001",
12
+ "0xdead000000000000000042069420694206942069",
13
+ ];
14
+
15
+ export function isSolanaDeadOwner(owner: string): boolean {
16
+ return SOLANA_DEAD_OWNERS.includes(owner);
17
+ }
18
+
19
+ export function isEvmDead(address: string): boolean {
20
+ return EVM_DEAD_ADDRESSES.includes(address.toLowerCase());
21
+ }
@@ -0,0 +1,55 @@
1
+
2
+
3
+ import { base58Encode } from "../codec.ts";
4
+ import { SLOT_ACTIVE } from "../solana/floor/accounts.ts";
5
+ import { TEAM_SLOT_WALLET } from "../solana/floor/instructions.ts";
6
+ import { priceX64 } from "../math/v2/sqrt.ts";
7
+ import { TRADE_KIND_TEAM_BUY, TRADE_KIND_TEAM_SHARE_BUY } from "./events.ts";
8
+ import type { TradeEventV2 } from "./types.ts";
9
+
10
+ export type DevBuyRole = "creator" | "team_member" | "team_share_buy" | "team_buy";
11
+
12
+ export interface DevBuy {
13
+ launch: string;
14
+ trader: string;
15
+ role: DevBuyRole;
16
+
17
+ quoteIn: bigint;
18
+
19
+ tokens: bigint;
20
+
21
+ priceBeforeX64: bigint;
22
+ priceAfterX64: bigint;
23
+
24
+ impactBps: bigint;
25
+ slot: bigint;
26
+ }
27
+
28
+ export interface DevTeam {
29
+ creator: string;
30
+ teamSlot?: { members: { key: Uint8Array; kind: number; state: number }[] } | null;
31
+ }
32
+
33
+ export function teamSlotWallets(slot: DevTeam["teamSlot"]): string[] {
34
+ return (slot?.members ?? []).filter((m) => m.kind === TEAM_SLOT_WALLET && m.state === SLOT_ACTIVE).map((m) => base58Encode(m.key));
35
+ }
36
+
37
+ export const impactBps = (before: bigint, after: bigint): bigint => (before === 0n ? 0n : ((after - before) * 10_000n) / before);
38
+
39
+ export function devBuyFromTrade(t: TradeEventV2, team: DevTeam, ctx: { teamBuy?: boolean; teamShareBuy?: boolean } = {}): DevBuy | null {
40
+ if (!t.isBuy) return null;
41
+ let role: DevBuyRole | null = null;
42
+ if (t.kind === TRADE_KIND_TEAM_BUY || ctx.teamBuy) role = "team_buy";
43
+ else if (t.kind === TRADE_KIND_TEAM_SHARE_BUY || ctx.teamShareBuy) role = "team_share_buy";
44
+ else if (t.trader === team.creator) role = "creator";
45
+ else if (teamSlotWallets(team.teamSlot).includes(t.trader)) role = "team_member";
46
+ if (role === null) return null;
47
+ const priceBeforeX64 = priceX64(t.sqrtBefore);
48
+ const priceAfterX64 = priceX64(t.sqrtAfter);
49
+ return {
50
+ launch: t.launch, trader: t.trader, role, quoteIn: t.quoteGross - t.refund, tokens: t.tokens, priceBeforeX64, priceAfterX64,
51
+ impactBps: impactBps(priceBeforeX64, priceAfterX64), slot: t.slot,
52
+ };
53
+ }
54
+
55
+ export const devBuyChangeSinceBps = (d: DevBuy, sqrtNow: bigint): bigint => impactBps(d.priceAfterX64, priceX64(sqrtNow));
@@ -0,0 +1,131 @@
1
+
2
+
3
+ import { hex } from "../codec.ts";
4
+ import { EVENT_IX_TAG_LE, eventDiscriminator } from "../solana/instruction.ts";
5
+ import { Reader, Writer } from "../solana/floor/borsh.ts";
6
+
7
+ type F = "pubkey" | "bool" | "u8" | "u32" | "u64" | "i64" | "u128" | "u64[4]" | "u64[5]" | "bytes:10" | "bytes:32";
8
+ const S = (...f: [string, F][]): [string, F][] => f;
9
+
10
+ export const EVENTS_V2: Record<string, [string, F][]> = {
11
+
12
+ LaunchCreatedV2: S(["launch", "pubkey"], ["mint", "pubkey"], ["quoteMint", "pubkey"], ["creator", "pubkey"], ["id", "u64"], ["presetId", "u8"], ["presetVersion", "u32"], ["presetHash", "bytes:32"],
13
+ ["p0X64", "u128"], ["raise", "u64"], ["windowTokens", "u64"], ["creatorBuyTokens", "u64"], ["createFee", "u64"], ["name", "bytes:32"], ["symbol", "bytes:10"], ["slot", "u64"], ["ts", "i64"],
14
+ ["listed", "bool"], ["quoteUsdCentsX64", "u128"], ["nativeCreateFee", "u64"], ["rowVersion", "u8"], ["sqrtP0", "u128"]),
15
+ Trade: S(["launch", "pubkey"], ["trader", "pubkey"], ["isBuy", "bool"], ["quoteGross", "u64"], ["quoteNet", "u64"], ["refund", "u64"], ["tokens", "u64"], ["fees", "u64[5]"], ["traderRef", "u64"], ["fromResale", "u64"], ["fromFirst", "u64"], ["unfilled", "u64"], ["sqrtBefore", "u128"], ["sqrtAfter", "u128"], ["sqrtF", "u128"], ["floor", "u64"], ["cushion", "u64"], ["sCirc", "u64"], ["steps", "u8"], ["budgetHit", "bool"], ["slot", "u64"], ["ts", "i64"],
16
+
17
+ ["kind", "u8"],
18
+
19
+ ["creatorRef", "u64"],
20
+
21
+ ["recipient", "pubkey"],
22
+
23
+ ["bidMoney", "u64"], ["msPaid", "u64[4]"]),
24
+ HolderPositionOpened: S(["launch", "pubkey"], ["owner", "pubkey"], ["amount", "u64"]),
25
+ HolderPositionClosed: S(["launch", "pubkey"], ["owner", "pubkey"], ["amount", "u64"], ["paid", "u64"]),
26
+ HolderIndexRolled: S(["launch", "pubkey"], ["index", "u128"], ["wMatured", "u64"], ["wPending", "u64"], ["carry", "u64"]),
27
+ DeadSynced: S(["launch", "pubkey"], ["added", "u64"], ["deadTokens", "u64"], ["sCirc", "u64"]),
28
+ };
29
+
30
+ EVENTS_V2.GenesisRaiseBought = EVENTS_V2.Trade;
31
+
32
+ export const TRADE_KIND_ORDINARY = 0;
33
+ export const TRADE_KIND_TEAM_BUY = 1;
34
+ export const TRADE_KIND_TEAM_SHARE_BUY = 2;
35
+ export const TRADE_KIND_GENESIS_RAISE = 3;
36
+ export const TRADE_KIND_GENESIS_SHARE = 4;
37
+ export const TRADE_KIND_GENESIS_OVERFLOW = 5;
38
+ export const TRADE_KIND_GENESIS_OVERFLOW_SHARE = 6;
39
+
40
+ export const isPersonTradeV2 = (kind: number | undefined): boolean => kind !== TRADE_KIND_GENESIS_RAISE && kind !== TRADE_KIND_GENESIS_OVERFLOW;
41
+
42
+ export const movesBookV2 = (kind: number | undefined): boolean => kind !== TRADE_KIND_GENESIS_SHARE && kind !== TRADE_KIND_GENESIS_OVERFLOW_SHARE;
43
+
44
+ export const FEE_LEGS_V2 = ["backing", "team", "platform", "floor", "holder"] as const;
45
+ export const MS_CLASSES_V2 = ["team", "holder", "platform", "floor"] as const;
46
+
47
+ let disc: Map<string, string> | undefined;
48
+
49
+ const PRE_CUTOVER: [string, string][] = [["TradeV2", "Trade"]];
50
+ const DISC = (): Map<string, string> =>
51
+ (disc ??= new Map([...Object.keys(EVENTS_V2).map((n): [string, string] => [hex(eventDiscriminator(n)), n]), ...PRE_CUTOVER.map(([a, n]): [string, string] => [hex(eventDiscriminator(a)), n])]));
52
+
53
+ function read(r: Reader, t: F): unknown {
54
+ const bm = /^bytes:(\d+)$/.exec(t);
55
+ if (bm) return r.bytes(Number(bm[1]));
56
+ const m = /^u64\[(\d)\]$/.exec(t);
57
+ if (m) return Array.from({ length: Number(m[1]) }, () => r.u64());
58
+ return (r as any)[t]();
59
+ }
60
+ function write(w: Writer, t: F, v: any): void {
61
+ const bm = /^bytes:(\d+)$/.exec(t);
62
+ if (bm) {
63
+ if (v.length !== Number(bm[1])) throw new RangeError(`${t}: ${v.length} bytes`);
64
+ w.bytes(v);
65
+ return;
66
+ }
67
+ const m = /^u64\[(\d)\]$/.exec(t);
68
+ if (m) {
69
+ if (v.length !== Number(m[1])) throw new RangeError(`${t}: ${v.length} values`);
70
+ for (const x of v) w.u64(x);
71
+ } else if (t === "i64") w.uint(BigInt.asUintN(64, v), 8);
72
+ else (w as any)[t](v);
73
+ }
74
+
75
+ export interface FloorEventV2 {
76
+ name: string;
77
+ fields: Record<string, any>;
78
+ }
79
+
80
+ export function decodeEventDataV2(data: Uint8Array): FloorEventV2 | null {
81
+ const name = DISC().get(hex(data.slice(0, 8)));
82
+ if (!name) return null;
83
+ const r = new Reader(data.slice(8));
84
+ const fields: Record<string, any> = {};
85
+ for (const [f, t] of EVENTS_V2[name]) fields[f] = read(r, t);
86
+ if (r.o !== r.b.length) throw new RangeError(`${name}: ${r.b.length} body bytes, the schema reads ${r.o}`);
87
+ return { name, fields };
88
+ }
89
+
90
+ export function encodeEventDataV2(e: FloorEventV2): Uint8Array {
91
+ const w = new Writer().bytes(eventDiscriminator(e.name));
92
+ for (const [f, t] of EVENTS_V2[e.name] ?? []) write(w, t, e.fields[f]);
93
+ return w.done();
94
+ }
95
+
96
+ export function decodeEventCpiV2(ixData: Uint8Array): FloorEventV2 | null {
97
+ if (ixData.length < 16 || hex(ixData.slice(0, 8)) !== hex(EVENT_IX_TAG_LE)) return null;
98
+ return decodeEventDataV2(ixData.slice(8));
99
+ }
100
+
101
+ export interface PlatformWithdrawalV2 {
102
+ launch: string;
103
+ platform: bigint;
104
+ cut: bigint;
105
+
106
+ paid: bigint;
107
+ platformOwedAfter: bigint;
108
+ cutOwedAfter: bigint;
109
+ }
110
+
111
+ export function platformWithdrawalsV2(events: readonly { name: string; fields: Record<string, any> }[]): PlatformWithdrawalV2[] {
112
+ const by = new Map<string, PlatformWithdrawalV2>();
113
+ const row = (launch: string) => {
114
+ let r = by.get(launch);
115
+ if (!r) by.set(launch, (r = { launch, platform: 0n, cut: 0n, paid: 0n, platformOwedAfter: 0n, cutOwedAfter: 0n }));
116
+ return r;
117
+ };
118
+ for (const e of events) {
119
+ if (e.name === "PlatformWithdrawn") {
120
+ const r = row(e.fields.launch);
121
+ r.platform += e.fields.amount;
122
+ r.platformOwedAfter = e.fields.owedAfter;
123
+ } else if (e.name === "CutWithdrawn") {
124
+ const r = row(e.fields.launch);
125
+ r.cut += e.fields.amount;
126
+ r.cutOwedAfter = e.fields.cutOwedAfter;
127
+ }
128
+ }
129
+ for (const r of by.values()) r.paid = r.platform + r.cut;
130
+ return [...by.values()];
131
+ }