@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,391 @@
1
+
2
+
3
+ import { addressBytes } from "../codec.js";
4
+ import { isOnCurve } from "../ed25519.js";
5
+ import { R, RS, W, WS, instructionDiscriminator } from "../solana/instruction.js";
6
+
7
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, WSOL_MINT, associatedTokenAddress, findProgramAddress } from "../solana/pda.js";
8
+
9
+ import { FLOOR_AMM_ERRORS, FLOOR_AMM_PROGRAM_ID, INSTRUCTIONS_SYSVAR_ID, METADATA_PROGRAM_ID, configPda, floorEventAuthorityPda, genesisConvertBandPda, genesisRaisePda, launchPda, teamSlotPda, traderLinkPda, traderRefPda, unwrapPda } from "../solana/floor/program.js";
10
+ import { Writer } from "../solana/floor/borsh.js";
11
+ import { CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_EXTRA, CLAIM_KNOWN_WIDE, CLAIM_TEAM_BUY, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF, CLAIM_SHARE_BOUGHT, MAX_NAME_LEN, MAX_SYMBOL_LEN, createMintAccounts, withdrawPlatformManyInstruction } from "../solana/floor/instructions.js";
12
+
13
+ import { MEMBERS_MAX, ROW_LEN } from "./layouts.js";
14
+ import { freshRegistryPrice } from "../registryPrice.js";
15
+
16
+ import { CLAIM_BITS, CLAIM_KNOWN_MASKS, PROGRAM_IDS } from "../generated/index.js";
17
+
18
+ export const FLOOR_HOOK_PROGRAM_ID = PROGRAM_IDS.floorHook;
19
+
20
+ export const FLOOR_HOOK_PLACEHOLDER_ID = "824TastyTFiKGJ8HteV5NHDkqcAcGVAn4L9JT8ZSdAE4";
21
+ export function isFloorHookPlaceholder(id ) {
22
+ return id === FLOOR_HOOK_PLACEHOLDER_ID;
23
+ }
24
+
25
+ const pid = (c ) => c?.programId ?? FLOOR_AMM_PROGRAM_ID;
26
+ const hid = (c ) => c?.hookProgramId ?? FLOOR_HOOK_PROGRAM_ID;
27
+ const k = addressBytes;
28
+
29
+ export const holderBookPda = (launch , hook = FLOOR_HOOK_PROGRAM_ID) => findProgramAddress(["holder_book", k(launch)], hook);
30
+ export const holderPositionPda = (launch , owner , hook = FLOOR_HOOK_PROGRAM_ID) => findProgramAddress(["holder", k(launch), k(owner)], hook);
31
+ export const deadMarkPda = (launch , tokenAccount , p = FLOOR_AMM_PROGRAM_ID) => findProgramAddress(["dead", k(launch), k(tokenAccount)], p);
32
+
33
+ export const extraAccountMetasPda = (baseMint , hook = FLOOR_HOOK_PROGRAM_ID) => findProgramAddress(["extra-account-metas", k(baseMint)], hook);
34
+
35
+ const V2_ERROR_NAMES = [
36
+ "NothingToSettle", "HolderPositionExists", "HolderPositionNotEmpty", "HolderBookMismatch", "DeadAccountNotDead",
37
+ "HookAccountsMissing", "RowTooManyRanges", "RowNotFeasible",
38
+ ] ;
39
+
40
+ export const V2_ERROR_BASE = FLOOR_AMM_ERRORS.BurnTooLarge + 1;
41
+ export const FLOOR_AMM_ERRORS_V2 = Object.freeze(
42
+ Object.fromEntries(V2_ERROR_NAMES.map((n, i) => [n, V2_ERROR_BASE + i])) ,
43
+ );
44
+
45
+ export const FLOOR_HOOK_ERRORS = Object.freeze({
46
+ NotTransferring: 0, WrongMint: 1, WrongProgram: 2, WrongAccount: 3, PositionNotWritable: 4, AlreadyOpen: 5, NotEmpty: 6, MetasExist: 7,
47
+ NotLaunchSigner: 8, HookAuthoritySet: 9, Math: 10, OwnerNotImmutable: 11,
48
+
49
+ NotTheOwnersAta: 12,
50
+
51
+ OffCurveOwner: 13,
52
+ } );
53
+
54
+ export const canHoldPosition = (owner ) => isOnCurve(k(owner));
55
+
56
+ export const TRANSFER_HOOK_EXECUTE_DISCRIMINATOR = Uint8Array.from([105, 37, 101, 197, 75, 251, 102, 26]);
57
+
58
+ export function hookAccountsFor(p ) {
59
+ const hook = hid(p);
60
+ const launch = launchPda(p.baseMint, pid(p))[0];
61
+ return [
62
+ R(extraAccountMetasPda(p.baseMint, hook)[0]),
63
+ R(launch),
64
+ W(holderBookPda(launch, hook)[0]),
65
+ W(holderPositionPda(launch, p.srcOwner, hook)[0]),
66
+ W(holderPositionPda(launch, p.dstOwner, hook)[0]),
67
+ R(hook),
68
+ ];
69
+ }
70
+
71
+ export function transferCheckedWithHookInstruction(p ) {
72
+ return {
73
+ programAddress: TOKEN_2022_PROGRAM_ID,
74
+ accounts: [W(p.source), R(p.baseMint), W(p.destination), RS(p.authority), ...hookAccountsFor({ baseMint: p.baseMint, srcOwner: p.srcOwner ?? p.authority, dstOwner: p.dstOwner, programId: p.programId, hookProgramId: p.hookProgramId })],
75
+ data: new Writer().u8(12).u64(p.amount).u8(p.decimals).done(),
76
+ };
77
+ }
78
+
79
+ export const HOOK_IX_RESYNC = new TextEncoder().encode("resync__");
80
+
81
+ export function resyncInstruction(p ) {
82
+ const hook = hid(p);
83
+ const launch = launchPda(p.baseMint, pid(p))[0];
84
+ return {
85
+ programAddress: hook,
86
+ accounts: [W(holderBookPda(launch, hook)[0]), W(holderPositionPda(launch, p.owner, hook)[0]), R(p.ownerBase ?? associatedTokenAddress(p.owner, p.baseMint, TOKEN_2022_PROGRAM_ID))],
87
+ data: HOOK_IX_RESYNC.slice(),
88
+ };
89
+ }
90
+
91
+ export function burnCheckedInstruction(p ) {
92
+ if (p.amount <= 0n) throw new RangeError("burn: the amount must be > 0");
93
+ return { programAddress: TOKEN_2022_PROGRAM_ID, accounts: [W(p.account), W(p.baseMint), RS(p.owner)], data: new Writer().u8(15).u64(p.amount).u8(p.decimals).done() };
94
+ }
95
+
96
+ const evPair = (c ) => [R(floorEventAuthorityPda(pid(c))[0]), R(pid(c))];
97
+
98
+ const hookProg = (c ) => R(hid(c));
99
+ const opt = (a , c , role ) => (a ? role(a) : R(pid(c)));
100
+ function ix(name , accounts , args , c , events = true) {
101
+ return { programAddress: pid(c), accounts: [...accounts, ...(events ? evPair(c) : [])], data: args(new Writer().bytes(instructionDiscriminator(name))).done() };
102
+ }
103
+
104
+ export function creditBackingInstructionV2(p ) {
105
+ if (p.amount <= 0n) throw new RangeError("credit_backing: the amount must be > 0");
106
+ const base = ix("credit_backing", [RS(p.donor), W(p.launch), R(p.baseMint), R(p.quoteMint), W(p.donorQuote), W(p.quoteVault), R(p.quoteTokenProgram)], (w) => w.u64(p.amount), p);
107
+ if (!p.quoteIsLaunch) return base;
108
+ return { ...base, accounts: [...base.accounts, ...hookAccountsFor({ baseMint: p.quoteMint, srcOwner: p.donor, dstOwner: p.launch, programId: p.programId, hookProgramId: p.hookProgramId })] };
109
+ }
110
+
111
+ export const SYNC_DEAD_MAX_PAIRS = 8;
112
+
113
+ export function syncDeadInstruction(p ) {
114
+ const n = p.deadTokenAccounts.length;
115
+ if (n < 1 || n > SYNC_DEAD_MAX_PAIRS) throw new RangeError(`sync_dead takes 1..=${SYNC_DEAD_MAX_PAIRS} dead token accounts`);
116
+ const base = ix("sync_dead", [WS(p.payer), W(p.launch), R(p.baseMint), R(SYSTEM_PROGRAM_ID)], (w) => w, p);
117
+ const pairs = p.deadTokenAccounts.flatMap((a) => [R(a), W(deadMarkPda(p.launch, a, pid(p))[0])]);
118
+ return { ...base, accounts: [...base.accounts, ...pairs] };
119
+ }
120
+
121
+ export function openHolderPositionInstruction(p ) {
122
+ if (!canHoldPosition(p.owner)) throw new RangeError("open_holder_position: the owner is off the ed25519 curve (OffCurveOwner 13): a PDA or vault holds no position");
123
+ const hook = hid(p);
124
+ return ix("open_holder_position", [WS(p.owner), R(p.launch), W(holderBookPda(p.launch, hook)[0]), W(holderPositionPda(p.launch, p.owner, hook)[0]), R(p.ownerBase), R(SYSTEM_PROGRAM_ID), hookProg(p)], (w) => w, p);
125
+ }
126
+
127
+ export function holderAccountWarning(p ) {
128
+ const ata = associatedTokenAddress(p.owner, p.baseMint, TOKEN_2022_PROGRAM_ID);
129
+ const idle = p.accounts.filter((a) => a.address !== ata && a.amount > 0n);
130
+ const idleAmount = idle.reduce((s, a) => s + a.amount, 0n);
131
+ return {
132
+ idle,
133
+ idleAmount,
134
+ message: idle.length
135
+ ? "Only your main token account earns Holder Fees and Holder Milestones. Tokens in another account of this wallet earn nothing until they move back."
136
+ : null,
137
+ };
138
+ }
139
+
140
+ export function closeHolderPositionInstruction(p ) {
141
+ const hook = hid(p);
142
+
143
+ const base = ix(
144
+ "close_holder_position",
145
+ [WS(p.owner), W(p.launch), W(holderBookPda(p.launch, hook)[0]), W(holderPositionPda(p.launch, p.owner, hook)[0]), R(p.ownerBase), W(p.quoteVault), W(p.ownerQuote), R(p.quoteMint), R(p.quoteTokenProgram), hookProg(p)],
146
+ (w) => w,
147
+ p,
148
+ );
149
+ if (!p.quoteIsLaunch) return base;
150
+ return { ...base, accounts: [...base.accounts, ...hookAccountsFor({ baseMint: p.quoteMint, srcOwner: p.launch, dstOwner: p.owner, programId: p.programId, hookProgramId: p.hookProgramId })] };
151
+ }
152
+
153
+ export const PRESET_CHUNK_MAX = 900;
154
+
155
+ export function writePresetChunkInstruction(p ) {
156
+ if (p.offset < 0 || p.data.length === 0 || p.offset + p.data.length > ROW_LEN) throw new RangeError(`a chunk lies inside the ${ROW_LEN}-byte row`);
157
+ return ix("write_preset_chunk", [WS(p.authority), R(p.config), W(p.preset), R(SYSTEM_PROGRAM_ID)], (w) => w.u8(p.id).u16(p.offset).u32(p.data.length).bytes(p.data), p, false);
158
+ }
159
+
160
+ export function presetV2Instruction(p ) {
161
+
162
+ const accts = p.name === "update_preset" ? [RS(p.authority), R(p.config), W(p.preset)] : [WS(p.authority), W(p.config), W(p.preset), R(SYSTEM_PROGRAM_ID)];
163
+ return ix(p.name, accts, (w) => w.u8(p.id), p);
164
+ }
165
+
166
+ export function presetV2Instructions(p ) {
167
+ if (p.row.length !== ROW_LEN) throw new RangeError(`a v2 row is ${ROW_LEN} bytes`);
168
+ const n = p.chunk ?? PRESET_CHUNK_MAX;
169
+ if (n < 1) throw new RangeError("chunk must be > 0");
170
+ const out = [];
171
+ for (let o = 0; o < ROW_LEN; o += n) out.push(writePresetChunkInstruction({ ...p, offset: o, data: p.row.slice(o, Math.min(o + n, ROW_LEN)) }));
172
+ out.push(presetV2Instruction(p));
173
+ return out;
174
+ }
175
+
176
+ export function teamShareBuyInstructionV2(p ) {
177
+ const base = ix(
178
+ "team_share_buy",
179
+ [WS(p.member), W(p.launch), R(p.baseMint), R(p.quoteMint), W(p.tokenVault), R(p.quoteVault), R(p.teamSlot ?? teamSlotPda(p.launch, pid(p))[0]), W(p.memberBase), R(TOKEN_2022_PROGRAM_ID)],
180
+ (w) => w.u64(p.amountIn).u64(minOut(p.minOut, "minOut")),
181
+ p,
182
+ );
183
+ return { ...base, accounts: [...base.accounts, ...hookAccountsFor({ baseMint: p.baseMint, srcOwner: p.launch, dstOwner: p.member, programId: p.programId, hookProgramId: p.hookProgramId })] };
184
+ }
185
+
186
+ const REM_HOOK_AT = 5;
187
+
188
+ export const REM_QUOTE_HOOK_AT = 11;
189
+
190
+ export const REM_REF_POSITION_AT = 17;
191
+
192
+ function tradeMetas(a , c ) {
193
+ return [
194
+ WS(a.trader), W(a.launch), R(a.baseMint), R(a.quoteMint), W(a.tokenVault), W(a.quoteVault), W(a.traderBase), W(a.traderQuote), R(TOKEN_2022_PROGRAM_ID), R(a.quoteTokenProgram),
195
+ opt(a.traderRef, c, W), opt(a.escrow, c, W),
196
+
197
+ ];
198
+ }
199
+
200
+ function remMetas(a , isBuy , c ) {
201
+ const rem = [];
202
+ if (a.user) {
203
+ const wallet = a.linkWallet ?? a.user;
204
+ rem.push(WS(a.user), R(traderLinkPda(wallet, c?.programId)[0]), R(SYSTEM_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID));
205
+ if (a.recipient && a.recipient !== wallet) rem.push(R(traderLinkPda(a.recipient, c?.programId)[0]));
206
+ }
207
+ while (rem.length < REM_HOOK_AT) rem.push(R(pid(c)));
208
+ const holder = a.baseOwner ?? a.trader;
209
+
210
+ const [srcOwner, dstOwner] = isBuy ? [a.launch, holder] : [holder, a.launch];
211
+ const out = [...rem, ...hookAccountsFor({ baseMint: a.baseMint, srcOwner, dstOwner, programId: c?.programId, hookProgramId: c?.hookProgramId })];
212
+ if (!a.quoteIsLaunch) return out;
213
+
214
+ const payer = a.quoteOwner ?? a.trader;
215
+ const [qSrc, qDst] = isBuy ? [payer, a.launch] : [a.launch, payer];
216
+ const q = [...out, ...hookAccountsFor({ baseMint: a.quoteMint, srcOwner: qSrc, dstOwner: qDst, programId: c?.programId, hookProgramId: c?.hookProgramId })];
217
+
218
+ if (!a.traderRef || !a.escrow) return q;
219
+ return [...q, W(holderPositionPda(launchPda(a.quoteMint, pid(c))[0], a.traderRef, hid(c))[0])];
220
+ }
221
+
222
+ const minOut = (v , what ) => {
223
+ if (v <= 0n) throw new RangeError(`${what} must be > 0 (the NO_MINIMUM rule)`);
224
+ return v;
225
+ };
226
+ function trade(name , a , isBuy , x , m , what ) {
227
+ const base = ix(name, tradeMetas(a, a), (w) => w.u64(x).u64(minOut(m, what)), a);
228
+ return { ...base, accounts: [...base.accounts, ...remMetas(a, isBuy, a)] };
229
+ }
230
+
231
+ export const buyInstructionV2 = (p ) => trade("buy", p, true, p.amountIn, p.minOut, "minOut");
232
+ export const buyFromBalanceInstructionV2 = (p ) => trade("buy_from_balance", p, true, p.keep, p.minOut, "minOut");
233
+
234
+ export const sellInstructionV2 = (p ) => trade("sell", p, false, p.tokensIn, p.minOut, "minOut");
235
+ export const sellFromBalanceInstructionV2 = (p ) => trade("sell_from_balance", p, false, p.keep, p.minOut, "minOut");
236
+
237
+ export function buyWithPositionInstructions(p ) {
238
+ const buy = buyInstructionV2(p);
239
+ if (p.hasPosition) return [buy];
240
+ const owner = p.baseOwner ?? p.trader;
241
+ if (owner !== p.trader) return [buy];
242
+ if (!canHoldPosition(owner)) return [buy];
243
+ return [buy, openHolderPositionInstruction({ owner, launch: p.launch, ownerBase: p.traderBase, programId: p.programId, hookProgramId: p.hookProgramId })];
244
+ }
245
+
246
+ export function createLaunchInstructionV2(p
247
+
248
+  
249
+
250
+  
251
+
252
+ ) {
253
+ const enc = new TextEncoder();
254
+ if (p.registry && p.args.quoteUsdCentsX64 === 0n) freshRegistryPrice(p.registry.entry, p.registry.now);
255
+ if (enc.encode(p.args.name).length > MAX_NAME_LEN || enc.encode(p.args.symbol).length > MAX_SYMBOL_LEN) throw new RangeError("name ≤ 32, symbol ≤ 10 bytes");
256
+ const buy = p.args.creatorBuy ?? 0n;
257
+ const minTok = p.args.creatorBuyMinTokens ?? 0n;
258
+ if (buy < 0n || minTok < 0n) throw new RangeError("the Team Buy and its minimum are ≥ 0");
259
+ if (buy === 0n && minTok > 0n) throw new RangeError("a minimum without a Team Buy");
260
+ if (p.teamBuyMax !== undefined && buy > p.teamBuyMax) throw new RangeError(`the Team Buy ${buy} is over the row's share ${p.teamBuyMax} (CreatorBuyTooLarge)`);
261
+ const rate = p.teamSlotMaxBps;
262
+ if (!Number.isInteger(rate) || rate < 0 || rate > 0xffff) throw new RangeError(`the row's Team Slot max is a u16: ${rate}`);
263
+ if (p.args.teamSlotRateBps !== undefined && p.args.teamSlotRateBps !== rate) {
264
+ throw new RangeError(`the Team Slot opens at the row's max ${rate}, never ${p.args.teamSlotRateBps} (TeamSlotRateOverMax)`);
265
+ }
266
+ const prog = pid(p);
267
+ const m = createMintAccounts({ ...p, programId: prog });
268
+ const base = ix(
269
+ "create_launch",
270
+ [
271
+ WS(p.creator), W(p.config), R(p.preset), W(p.quote), R(p.quoteMint), W(m.baseMint), W(m.launch), W(m.tokenVault), W(m.quoteVault), W(p.creatorQuote),
272
+ W(m.metadata), R(p.metadataProgram ?? METADATA_PROGRAM_ID), R(p.creatorRef ?? traderRefPda(p.creator, p.quoteMint, prog)[0]),
273
+ R(p.creatorLink ?? traderLinkPda(p.creator, prog)[0]), R(TOKEN_2022_PROGRAM_ID), R(p.quoteTokenProgram), R(ASSOCIATED_TOKEN_PROGRAM_ID),
274
+ R(SYSTEM_PROGRAM_ID), R(INSTRUCTIONS_SYSVAR_ID), W(teamSlotPda(m.launch, prog)[0]),
275
+ W(holderBookPda(m.launch, hid(p))[0]), W(extraAccountMetasPda(m.baseMint, hid(p))[0]), hookProg(p),
276
+ ],
277
+ (w) => w.string(p.args.name).string(p.args.symbol).u64(buy).u64(minTok).u128(p.args.quoteUsdCentsX64).u16(rate).u64(m.mintNonce),
278
+ p,
279
+ );
280
+ if (!p.quoteIsLaunch) return base;
281
+ const quoteHook = hookAccountsFor({ baseMint: p.quoteMint, srcOwner: p.creator, dstOwner: m.launch, programId: p.programId, hookProgramId: p.hookProgramId });
282
+ return { ...base, accounts: [...base.accounts, ...quoteHook] };
283
+ }
284
+
285
+ const vaultPayoutHook = (baseMint , launch , wallet , c ) =>
286
+ hookAccountsFor({ baseMint, srcOwner: launch, dstOwner: wallet, programId: c?.programId, hookProgramId: c?.hookProgramId });
287
+
288
+ export function unlockCreatorBuyInstructionV2(p ) {
289
+ const dest = p.destination ?? associatedTokenAddress(p.creator, p.baseMint, TOKEN_2022_PROGRAM_ID);
290
+ const base = ix(
291
+ "unlock_creator_buy",
292
+ [WS(p.sender), W(p.launch), R(p.creator), R(p.baseMint), W(p.tokenVault), W(dest), R(TOKEN_2022_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)],
293
+ (w) => w,
294
+ p,
295
+ );
296
+ return { ...base, accounts: [...base.accounts, ...vaultPayoutHook(p.baseMint, p.launch, p.creator, p)] };
297
+ }
298
+
299
+ export function burnTokensInstructionV2(p ) {
300
+ if (!Number.isInteger(p.place) || p.place < 0 || p.place >= MEMBERS_MAX) throw new RangeError(`burn_tokens: place is 0..${MEMBERS_MAX}`);
301
+ if (p.teamAmount < 0n || p.walletAmount < 0n || (p.teamAmount === 0n && p.walletAmount === 0n)) throw new RangeError("burn_tokens: nothing to burn");
302
+ const hook = hid(p);
303
+ return ix(
304
+ "burn_tokens",
305
+ [
306
+ RS(p.holder), W(p.launch), R(p.teamSlot ?? p.launch), W(p.baseMint), W(p.tokenVault), W(associatedTokenAddress(p.holder, p.baseMint, TOKEN_2022_PROGRAM_ID)), R(TOKEN_2022_PROGRAM_ID),
307
+ W(holderBookPda(p.launch, hook)[0]), W(holderPositionPda(p.launch, p.holder, hook)[0]), hookProg(p),
308
+ ],
309
+ (w) => w.u8(p.place).u64(p.teamAmount).u64(p.walletAmount),
310
+ p,
311
+ );
312
+ }
313
+
314
+ export function quoteHookBlock(quoteMint , receiver , payers , p = {}) {
315
+ const hook = hid(p);
316
+ const ql = launchPda(quoteMint, pid(p))[0];
317
+ return [
318
+ R(extraAccountMetasPda(quoteMint, hook)[0]),
319
+ R(ql),
320
+ W(holderBookPda(ql, hook)[0]),
321
+ W(holderPositionPda(ql, receiver, hook)[0]),
322
+ R(hook),
323
+ ...payers.map((x) => W(holderPositionPda(ql, x, hook)[0])),
324
+ ];
325
+ }
326
+
327
+ export function withdrawPlatformManyInstructionV2(p ) {
328
+ if (!p.quoteIsLaunch || p.launches.length === 0) return withdrawPlatformManyInstruction(p);
329
+ if (!p.treasury) throw new RangeError("withdraw_platform_many: a launch-token quote needs the treasury (its position receives)");
330
+ return withdrawPlatformManyInstruction({ ...p, quoteHook: quoteHookBlock(p.quoteMint, p.treasury, p.launches.map((l) => l.launch), p) });
331
+ }
332
+
333
+ export const CLAIM_HOLDER = CLAIM_BITS.solana.holder;
334
+
335
+ const CLAIM_QUOTE_BITS = CLAIM_CREATOR | CLAIM_CREATOR_REF | CLAIM_TEAM_SLOT | CLAIM_TRADER_REF;
336
+
337
+ export const CLAIM_KNOWN_WIDE_V2 = CLAIM_KNOWN_MASKS.solana.v2;
338
+
339
+ export function claimInstructionV2(p
340
+
341
+ ) {
342
+ if (p.unwrap && p.quoteIsLaunch) throw new RangeError("unwrap: a launch-token quote is never wSOL");
343
+ if (p.unwrap && (p.quoteMint !== WSOL_MINT || p.quoteTokenProgram !== TOKEN_PROGRAM_ID)) throw new RangeError("unwrap: only a wSOL claim");
344
+ if (!Number.isInteger(p.types) || p.types <= 0 || (p.types & ~CLAIM_KNOWN_WIDE_V2) !== 0) throw new RangeError("UnknownClaimType: bits 1, 2, 4, 8, 128, 256, 512 (16, 32 and 64 are reserved)");
345
+ if (p.launches.length > 255) throw new RangeError("at most 255 launches a claim");
346
+ const tok = (p.types & (CLAIM_TEAM_BUY | CLAIM_SHARE_BOUGHT)) !== 0;
347
+ if (tok && p.launches.some((l) => !l.teamBuy)) throw new RangeError("bits 128 / 256: every launch needs its teamBuy accounts");
348
+ const holder = (p.types & CLAIM_HOLDER) !== 0;
349
+ const wide = p.types > 0xff;
350
+ const prog = pid(p);
351
+ const base = ix(
352
+ wide ? "claim_wide" : "claim",
353
+ [WS(p.sender), p.unwrap ? W(p.wallet) : R(p.wallet), R(p.quoteMint), R(p.quoteTokenProgram), W(p.destination), R(p.config), opt(p.refOwed, p, W), opt(p.refOwedVault, p, W), R(SYSTEM_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID)],
354
+ (w) => (wide ? w.pubkey(p.wallet).u16(p.types) : w.pubkey(p.wallet).u8(p.types)).u8(p.launches.length),
355
+ p,
356
+ );
357
+ const rest = [];
358
+ for (const l of p.launches) {
359
+ rest.push(W(l.launch));
360
+ if (holder) {
361
+ const owner = l.holderOwner ?? p.wallet;
362
+ const mint = l.baseMint ?? l.teamBuy?.baseMint;
363
+ if (!l.holderBase && !mint) throw new RangeError("bit 512: every launch needs its holderBase or baseMint (Take's owner_base)");
364
+ rest.push(W(holderBookPda(l.launch, hid(p))[0]), W(holderPositionPda(l.launch, owner, hid(p))[0]), R(l.holderBase ?? associatedTokenAddress(owner, mint , TOKEN_2022_PROGRAM_ID)));
365
+ }
366
+ rest.push(W(l.quoteVault), W(l.teamSlot ?? l.launch));
367
+ if (tok) rest.push(R(l.teamBuy .baseMint), W(l.teamBuy .tokenVault), W(l.teamBuy .walletBase), ...vaultPayoutHook(l.teamBuy .baseMint, l.launch, p.wallet, p));
368
+ }
369
+ if (tok) rest.push(R(TOKEN_2022_PROGRAM_ID));
370
+ if (holder) rest.push(hookProg(p));
371
+ if (p.unwrap) rest.push(W(unwrapPda(p.wallet, prog)[0]));
372
+ if (p.quoteIsLaunch && (p.types & (CLAIM_QUOTE_BITS | CLAIM_HOLDER)) !== 0) {
373
+ const payers = p.launches.map((l) => l.launch);
374
+ if ((p.types & CLAIM_TRADER_REF) !== 0 && p.refOwed) payers.push(p.refOwed);
375
+ rest.push(...quoteHookBlock(p.quoteMint, p.wallet, payers, p));
376
+ }
377
+ return { ...base, accounts: [...base.accounts, ...rest] };
378
+ }
379
+
380
+ export function setGenesisConvertBandInstruction(p ) {
381
+ for (const v of [p.solPxMin, p.solPxMax, p.usdcPxMin, p.usdcPxMax]) if (v <= 0n || v >> 128n !== 0n) throw new RangeError("set_genesis_convert_band: each price is a u128 > 0");
382
+ if (p.solPxMin > p.solPxMax || p.usdcPxMin > p.usdcPxMax) throw new RangeError("set_genesis_convert_band: min ≤ max");
383
+ const g = pid(p);
384
+ return ix(
385
+ "set_genesis_convert_band",
386
+ [WS(p.authority), R(p.config ?? configPda(g)[0]), R(p.genesisRaise ?? genesisRaisePda(g)[0]), W(p.convertBand ?? genesisConvertBandPda(g)[0]), R(SYSTEM_PROGRAM_ID)],
387
+ (w) => w.u128(p.solPxMin).u128(p.solPxMax).u128(p.usdcPxMin).u128(p.usdcPxMax),
388
+ p,
389
+ false,
390
+ );
391
+ }
@@ -0,0 +1,97 @@
1
+
2
+
3
+ import { RANGES_MAX, SOL_BUY_WALK_STEPS, cloneBook } from "../math/v2/book.js";
4
+
5
+ import { buy } from "../math/v2/trade.js";
6
+
7
+ import { MAX_BANDS, V2_VERSION } from "./layouts.js";
8
+
9
+ import { quoteBuyNetInV2, quoteReceivedV2 } from "./quoteMint.js";
10
+
11
+ export const MATH_TOKEN_DECIMALS = 6;
12
+
13
+ export const SOLANA_TOKEN_DECIMALS = 6;
14
+
15
+ const DECIMALS_MAX = 24;
16
+
17
+ function scaleOf(decimals ) {
18
+ if (!Number.isInteger(decimals) || decimals < MATH_TOKEN_DECIMALS || decimals > DECIMALS_MAX) {
19
+ throw new RangeError(`token decimals ${decimals}: ${MATH_TOKEN_DECIMALS} ..= ${DECIMALS_MAX}`);
20
+ }
21
+ return 10n ** BigInt(decimals - MATH_TOKEN_DECIMALS);
22
+ }
23
+
24
+ export function tokensToMathV2(chain , decimals ) {
25
+ if (chain < 0n) throw new RangeError(`tokens ${chain} < 0`);
26
+ return chain / scaleOf(decimals);
27
+ }
28
+
29
+ export function tokensToChainV2(math , decimals ) {
30
+ if (math < 0n) throw new RangeError(`tokens ${math} < 0`);
31
+ return math * scaleOf(decimals);
32
+ }
33
+
34
+ const n = (x ) => Number(x);
35
+
36
+ function liveSupplyV2(stored , mintSupply ) {
37
+ if (mintSupply === undefined) return stored;
38
+ if (mintSupply < 0n) throw new RangeError(`mint supply ${mintSupply} < 0`);
39
+ return mintSupply < stored ? mintSupply : stored;
40
+ }
41
+
42
+ export function solanaSettleInputsV2(l , opts = {}) {
43
+ if (l.version !== V2_VERSION) throw new RangeError(`LaunchV2 version ${l.version}, want ${V2_VERSION}`);
44
+ if (l.decimals !== SOLANA_TOKEN_DECIMALS) throw new RangeError(`LaunchV2 decimals ${l.decimals}: a Solana launch token has ${SOLANA_TOKEN_DECIMALS}`);
45
+ const ranges = Array.from({ length: RANGES_MAX }, (_, i) => ({
46
+ sqrtA: l.rngSqrtA[i], sqrtB: l.rngSqrtB[i], tokens: l.rngTokens[i], askLeft: l.rngAskLeft[i], back: l.rngBack[i],
47
+ place: l.rngPlace[i], thetaBps: n(l.rngThetaBps[i]), psiBps: n(l.rngPsiBps[i]),
48
+ kind: n(l.rngKind[i]), class: n(l.rngClass[i]), msCapPpm: n(l.rngMsCapPpm[i]),
49
+ }));
50
+ const book = {
51
+ sqrtP: l.sqrtP, sqrtF: l.sqrtF, sqrtLMax: l.sqrtLMax, floor: l.floor, floorTokens: l.floorTokens,
52
+ nBands: l.nBands,
53
+ bandEdge: [...l.bandEdge], bandMoney: [...l.bandMoney], bandTokens: [...l.bandTokens],
54
+ nC: l.nC, bandF: [...l.bandF], bandFClaim: [...l.bandFClaim], ckAtF: l.ckAtF, ckFloorTokens: l.ckFloorTokens,
55
+ feePpm: l.feePpm.map(n), feeStartPpm: l.feeStartPpm.map(n), sqrtFeeA: l.sqrtFeeA, sqrtFeeB: l.sqrtFeeB,
56
+ graduated: l.graduated !== 0, nRanges: l.nRanges, ranges,
57
+
58
+ ...(l.ownFees !== 0 && { ownFees: true }),
59
+ };
60
+
61
+ const row = {
62
+ phiBps: l.phiBps, ckBps: l.ckBps, wcBps: l.wcBps, cShareBps: 5_000,
63
+ msClass: l.msClass, platformMsBps: (l.flags & 1) !== 0 ? l.platformMsBps : 0, floorMsBps: (l.flags & 2) !== 0 ? l.floorMsBps : 0,
64
+ };
65
+ return {
66
+ book, row, sup: { live: liveSupplyV2(l.supply, opts.mintSupply), dead: l.deadTokens },
67
+ fee: { feePpm: [...book.feePpm], feeStartPpm: [...book.feeStartPpm], sqrtFeeA: book.sqrtFeeA, sqrtFeeB: book.sqrtFeeB },
68
+ msCapPpm: ranges.map((r) => r.msCapPpm),
69
+ };
70
+ }
71
+
72
+ export const bookFromLaunchV2 = (l ) => solanaSettleInputsV2(l).book;
73
+
74
+ export function withFeeCurveV2 (inputs , curve ) {
75
+ if (curve.feePpm.length !== 5 || curve.feeStartPpm.length !== 5) throw new RangeError("a fee curve has 5 legs");
76
+ const book = cloneBook(inputs.book);
77
+ book.feePpm = [...curve.feePpm];
78
+ book.feeStartPpm = [...curve.feeStartPpm];
79
+ book.sqrtFeeA = curve.sqrtFeeA;
80
+ book.sqrtFeeB = curve.sqrtFeeB;
81
+ return { ...inputs, book };
82
+ }
83
+
84
+ export function quoteBuySolanaV2(
85
+ l ,
86
+ gross ,
87
+ opts = {},
88
+ ) {
89
+ const s = solanaSettleInputsV2(l, { mintSupply: opts.mintSupply });
90
+ const after = cloneBook(s.book);
91
+
92
+ const tf = opts.quoteTransferFee;
93
+ const net = tf ? quoteBuyNetInV2(tf.fee, tf.epoch, gross) : gross;
94
+ const r = buy(after, s.row, s.sup, net, opts.minTokens ?? 0n, { walkSteps: SOL_BUY_WALK_STEPS });
95
+ const refundReceived = tf ? quoteReceivedV2(tf.fee, tf.epoch, r.refund) : r.refund;
96
+ return { buy: r, after, withheld: gross - net, refundReceived };
97
+ }
@@ -0,0 +1,126 @@
1
+
2
+
3
+ import { bytesToHex, checksumAddress, decodeAbi, encodeCall, hexToBytes } from "../../evm/abi.js";
4
+
5
+ import { teamSlotHandleId } from "../../handle.js";
6
+ import { freshRegistryPrice } from "../../registryPrice.js";
7
+
8
+ import { evmSignature } from "../../generated/index.js";
9
+
10
+ export { claimV2Call, CLAIM_HOLDER_EVM, CLAIM_KNOWN_V2_EVM, creditBackingCall, optInCall, viewCall, decodeTeamShareOf } from "../evm.js";
11
+
12
+ export { traderRefDigest, traderRefAuthDigest, bindHandleDigest, bindHandleAuthDigest } from "../../evm/floor/calls.js";
13
+ export { CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF, CLAIM_PAYOUT, CLAIM_TEAM_BUY, CLAIM_SHARE_BOUGHT, MEMBER_WALLET, MEMBER_HANDLE, NATIVE_QUOTE } from "../../evm/floor/iface.js";
14
+
15
+ export const EVM_V2_TX = {
16
+
17
+ createLaunch: evmSignature("createLaunch"),
18
+
19
+ teamSlotAdd: evmSignature("teamSlotAdd"),
20
+ unlockCreatorBuy: evmSignature("unlockCreatorBuy"),
21
+ claim: evmSignature("claim"),
22
+
23
+ teamShareBuy: evmSignature("teamShareBuy"),
24
+
25
+ setTraderRef: evmSignature("setTraderRef"),
26
+ bindHandle: evmSignature("bindHandle"),
27
+ claimPayoutTo: evmSignature("claimPayoutTo"),
28
+ } ;
29
+
30
+ export const EVM_V2_TX_VIEWS = {
31
+ traderRef: { sig: evmSignature("traderRef"), ret: ["address[3]"] },
32
+ launchCount: { sig: evmSignature("launchCount"), ret: ["uint256"] },
33
+ predictToken: { sig: evmSignature("predictToken"), ret: ["address"] },
34
+ } ;
35
+
36
+ const NATIVE = "0x0000000000000000000000000000000000000000";
37
+ const MAX_NAME = 32;
38
+ const MAX_SYMBOL = 10;
39
+ const U128 = (1n << 128n) - 1n;
40
+ const call = (to , sig , args , value = 0n) => ({ to: checksumAddress(to), data: bytesToHex(encodeCall(sig, args)), value });
41
+ const deadlineOf = (d ) => d ?? (1n << 256n) - 1n;
42
+
43
+ export function createLaunchCallV2(p
44
+
45
+  
46
+
47
+ ) {
48
+ const q = p.params;
49
+ if ("uri" in (q )) throw new RangeError("no uri: the token's uri is derived (metadataBase/<token>.json)");
50
+ const enc = new TextEncoder();
51
+ const nl = enc.encode(q.name).length;
52
+ const sl = enc.encode(q.symbol).length;
53
+ if (nl === 0 || sl === 0 || nl > MAX_NAME || sl > MAX_SYMBOL) throw new RangeError("name 1..32, symbol 1..10 bytes (BadArgument)");
54
+ if (!Number.isInteger(q.presetId) || q.presetId <= 0 || q.presetId > 255) throw new RangeError("presetId is a u8 > 0 (id 0 reserved)");
55
+ if (q.presetId === 2) throw new RangeError("preset id 2 is retired");
56
+ const rate = p.teamSlotMaxBps;
57
+ if (!Number.isInteger(rate) || rate < 0 || rate > 0xffff) throw new RangeError("teamSlotMaxBps is a u16");
58
+ const buy = q.creatorBuy ?? 0n;
59
+ const minTok = q.creatorBuyMinTokens ?? 0n;
60
+ if (buy < 0n || minTok < 0n) throw new RangeError("the Team Buy and its minimum are ≥ 0");
61
+ if (buy === 0n && minTok > 0n) throw new RangeError("a minimum without a Team Buy (SlippageExceeded)");
62
+ if (p.teamBuyMax !== undefined && buy > p.teamBuyMax) throw new RangeError(`the Team Buy ${buy} is over the row's share ${p.teamBuyMax} (CreatorBuyTooLarge)`);
63
+ const usd = q.quoteUsdCentsX64 ?? 0n;
64
+ if (p.listed && usd !== 0n) throw new RangeError("a listed quote declares no price (BadArgument)");
65
+ if (!p.listed && (usd === 0n || usd > U128)) throw new RangeError("an unlisted quote declares 0 < price ≤ 2^128 − 1 (BadArgument)");
66
+ if (p.listed && p.registry) freshRegistryPrice(p.registry.entry, p.registry.now);
67
+ const native = q.quote.toLowerCase() === NATIVE;
68
+ const value = !p.listed ? p.createFee : native ? p.createFee + buy : 0n;
69
+ const salt = hexToBytes(q.creatorSalt ?? "0x" + "00".repeat(32));
70
+ if (salt.length !== 32) throw new RangeError("creatorSalt is bytes32");
71
+ return call(
72
+ p.floorAmm,
73
+ EVM_V2_TX.createLaunch,
74
+ [[BigInt(q.presetId), q.quote, q.name, q.symbol, buy, minTok, q.sameAddressEverywhere ?? false, salt, usd, BigInt(rate)]],
75
+ value,
76
+ );
77
+ }
78
+
79
+ export function createApprovalV2(p ) {
80
+ if (p.quote.toLowerCase() === NATIVE) return 0n;
81
+ return (p.listed ? p.createFee : 0n) + (p.creatorBuy ?? 0n);
82
+ }
83
+
84
+ export function slotMemberKeyEvm(m ) {
85
+ if ("wallet" in m) return { key: "0x" + "00".repeat(12) + checksumAddress(m.wallet).slice(2).toLowerCase(), kind: 0 };
86
+ return { key: teamSlotHandleId(m.platform, m.handle), kind: 1 };
87
+ }
88
+
89
+ export function teamSlotAddCallV2(p ) {
90
+ if (p.shares <= 0n) throw new RangeError("teamSlotAdd: shares > 0 (NothingToDo)");
91
+ const { key, kind } = slotMemberKeyEvm(p.member);
92
+ return call(p.floorAmm, EVM_V2_TX.teamSlotAdd, [p.launchId, hexToBytes(key), BigInt(kind), p.shares, false]);
93
+ }
94
+
95
+ export const unlockCreatorBuyCallV2 = (p ) => call(p.floorAmm, EVM_V2_TX.unlockCreatorBuy, [p.launchId]);
96
+
97
+ export const EVM_V2_TX_ONE_PRESS = {
98
+ teamShareBuyAndClaim: evmSignature("teamShareBuyAndClaim"),
99
+ } ;
100
+
101
+ export function teamShareBuyAndClaimCallV2(p ) {
102
+ const amount = p.amountIn;
103
+ if (amount < 0n) throw new RangeError("teamShareBuyAndClaim: amount ≥ 0 (0 = everything owed)");
104
+ return call(p.floorAmm, EVM_V2_TX_ONE_PRESS.teamShareBuyAndClaim, [p.launchId, amount, p.minOut, deadlineOf(p.deadline)]);
105
+ }
106
+
107
+ export function teamShareBuyCallV2(p ) {
108
+ const amount = p.amountIn;
109
+ if (amount <= 0n) throw new RangeError("teamShareBuy: amount > 0");
110
+ return call(p.floorAmm, EVM_V2_TX.teamShareBuy, [p.launchId, amount, p.minOut, deadlineOf(p.deadline)]);
111
+ }
112
+
113
+ export function setTraderRefCallV2(p ) {
114
+ return call(p.floorAmm, EVM_V2_TX.setTraderRef, [p.trader, [...p.tiers], p.deadline, hexToBytes(p.authSig), hexToBytes(p.traderSig ?? "0x")]);
115
+ }
116
+
117
+ export function bindHandleCallV2(p ) {
118
+ return call(p.floorAmm, EVM_V2_TX.bindHandle, [hexToBytes(p.handleId), p.wallet, [...p.quotes], p.deadline, hexToBytes(p.authSig), hexToBytes(p.walletSig ?? "0x")]);
119
+ }
120
+
121
+ export const claimPayoutToCallV2 = (p ) => call(p.floorAmm, EVM_V2_TX.claimPayoutTo, [p.quote, p.to]);
122
+
123
+ export const traderRefDataV2 = (trader ) => bytesToHex(encodeCall(EVM_V2_TX_VIEWS.traderRef.sig, [trader]));
124
+ export const decodeTraderRefV2 = (data ) => (decodeAbi(EVM_V2_TX_VIEWS.traderRef.ret, data)[0] ).map((a) => checksumAddress(a));
125
+
126
+ export const predictTokenDataV2 = (creator , salt ) => bytesToHex(encodeCall(EVM_V2_TX_VIEWS.predictToken.sig, [creator, hexToBytes(salt)]));
@@ -0,0 +1,47 @@
1
+
2
+
3
+ import { accountDiscriminator } from "../../solana/instruction.js";
4
+ import { Reader } from "../../solana/floor/borsh.js";
5
+ import { decodeGenesisAllocation, decodeGenesisRaise } from "../../solana/floor/accounts.js";
6
+ import { FLOOR_AMM_PROGRAM_ID, configPda, genesisAllocPda, genesisConvertBandPda, genesisRaisePda } from "../../solana/floor/program.js";
7
+ import { genesisWalletClaim } from "../../solana/floor/genesis.js";
8
+
9
+ export { openGenesisLaunchInstructionV2, openGenesisWagmiInstructionV2, claimGenesisInstructionV2, claimGenesisWagmiInstructionV2, sweepGenesisWagmiDustInstructionV2 } from "../genesis.js";
10
+ export { setGenesisConvertBandInstruction } from "../solana.js";
11
+ export { contributeGenesisInstruction, contributeGenesisListedInstruction, refundGenesisMigratedInstruction, claimGenesisRefundInstruction, convertGenesisSolInstruction } from "../../solana/floor/instructions.js";
12
+ export { decodeGenesisRaise, decodeGenesisAllocation } from "../../solana/floor/accounts.js";
13
+ export { genesisWalletClaim } from "../../solana/floor/genesis.js";
14
+
15
+ export const GENESIS_CONVERT_BAND_SIZE = 8 + 1 + 16 * 4 + 8;
16
+
17
+ export function decodeGenesisConvertBand(data ) {
18
+ if (data.length !== GENESIS_CONVERT_BAND_SIZE) throw new RangeError(`GenesisConvertBand is ${GENESIS_CONVERT_BAND_SIZE} bytes, got ${data.length}`);
19
+ const d = accountDiscriminator("GenesisConvertBand");
20
+ for (let i = 0; i < 8; i++) if (data[i] !== d[i]) throw new RangeError("not a GenesisConvertBand account");
21
+ const r = new Reader(data.subarray(8));
22
+ return { bump: r.u8(), solPxMin: r.u128(), solPxMax: r.u128(), usdcPxMin: r.u128(), usdcPxMax: r.u128(), updatedAt: r.i64() };
23
+ }
24
+
25
+ export const genesisBandHolds = (b , solPx , usdcPx ) =>
26
+ b.solPxMin > 0n && b.usdcPxMin > 0n && solPx >= b.solPxMin && solPx <= b.solPxMax && usdcPx >= b.usdcPxMin && usdcPx <= b.usdcPxMax;
27
+
28
+ export function genesisAccountsV2(p )
29
+
30
+ {
31
+ const g = p.programId ?? FLOOR_AMM_PROGRAM_ID;
32
+ return {
33
+ config: configPda(g)[0],
34
+ genesisRaise: genesisRaisePda(g)[0],
35
+ convertBand: genesisConvertBandPda(g)[0],
36
+ allocation: p.wallet ? genesisAllocPda(p.wallet, g)[0] : null,
37
+ };
38
+ }
39
+
40
+ export function readGenesisV2(p ) {
41
+ const raise = decodeGenesisRaise(p.raise);
42
+ const allocation = p.allocation ? decodeGenesisAllocation(p.allocation) : null;
43
+ let claim = null;
44
+ if (allocation && BigInt(raise.raised_final ?? 0n) > 0n) claim = genesisWalletClaim(raise, allocation);
45
+ const band = p.band ? decodeGenesisConvertBand(p.band) : null;
46
+ return { raise, allocation, claim, band };
47
+ }