@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,246 @@
1
+
2
+
3
+ import { addressBytes, base58Encode, hex } from "../../codec.ts";
4
+ import { sha256 } from "../../sha256.ts";
5
+ import { launchPda } from "./program.ts";
6
+ import { metadataUri } from "./instructions.ts";
7
+
8
+ export const FLOOR_ORIGIN = "https://floor.supply";
9
+
10
+ export const WALLET_AUTH_PREFIX = "FLOOR metadata";
11
+ export const WALLET_AUTH_MESSAGE_HEADER = "x-floor-auth-message";
12
+ export const WALLET_AUTH_SIGNATURE_HEADER = "x-floor-auth-signature";
13
+
14
+ export const WALLET_AUTH_MAX_AHEAD_S = 300;
15
+
16
+ export const WALLET_AUTH_TTL_S = 120;
17
+
18
+ export const MAX_DESCRIPTION = 2000;
19
+ export const MAX_URL = 300;
20
+ export const MAX_HANDLE = 64;
21
+ export const MAX_IMAGE_BYTES = { avatar: 1024 * 1024, banner: 2 * 1024 * 1024 } as const;
22
+ export const SOCIAL_KEYS = ["x", "telegram", "discord", "github"] as const;
23
+
24
+ export type SolanaCluster = "mainnet" | "devnet";
25
+ export type ImageKind = keyof typeof MAX_IMAGE_BYTES;
26
+ export type ImageType = "image/png" | "image/jpeg" | "image/webp";
27
+ export type WalletAuthAction = "upload" | "meta";
28
+ export type Social = (typeof SOCIAL_KEYS)[number];
29
+
30
+ export interface CreatorSigner {
31
+ address: string;
32
+ signMessage(message: Uint8Array): Promise<Uint8Array>;
33
+ }
34
+
35
+ export type Fetch = (url: string, init: { method: string; headers: Record<string, string>; body: Uint8Array }) => Promise<{
36
+ status: number;
37
+ json(): Promise<unknown>;
38
+ }>;
39
+
40
+ export class LaunchMetadataError extends Error {
41
+ readonly code: string;
42
+ readonly status: number | undefined;
43
+ constructor(code: string, message: string, status?: number) {
44
+ super(message);
45
+ this.name = "LaunchMetadataError";
46
+ this.code = code;
47
+ this.status = status;
48
+ }
49
+ }
50
+
51
+ export { launchPda, metadataUri };
52
+
53
+ export const metadataBase = (cluster: SolanaCluster, origin = FLOOR_ORIGIN): string =>
54
+ `${origin.replace(/\/$/, "")}/api/meta/solana/${cluster}`;
55
+
56
+ export const chainParam = (cluster: SolanaCluster): string => `solana-${cluster}`;
57
+
58
+ const SITE = /^(?=.{1,253}(?::|$))[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*(?::[1-9][0-9]{0,4})?$/;
59
+
60
+ function assertAddress(field: string, a: string): void {
61
+ let ok = false;
62
+ try {
63
+ ok = addressBytes(a).length === 32 && base58Encode(addressBytes(a)) === a;
64
+ } catch {
65
+ ok = false;
66
+ }
67
+ if (!ok) throw new LaunchMetadataError("BadAddress", `${field} is not a base58 address in its canonical spelling`);
68
+ }
69
+
70
+ export function siteOf(origin: string): string {
71
+ const site = new URL(origin).host.toLowerCase();
72
+ const port = site.split(":")[1];
73
+ if (!SITE.test(site) || (port !== undefined && Number(port) > 65535)) {
74
+ throw new LaunchMetadataError("BadSite", `${origin} has no host the server can name`);
75
+ }
76
+ return site;
77
+ }
78
+
79
+ export function walletAuthMessage(f: {
80
+ site: string; action: WalletAuthAction; cluster: SolanaCluster; launch: string; signer: string; bodySha256: string; expires: number;
81
+ }): string {
82
+ if (!SITE.test(f.site)) throw new LaunchMetadataError("BadSite", `site ${f.site} is not a lowercase host[:port]`);
83
+ assertAddress("launch", f.launch);
84
+ assertAddress("signer", f.signer);
85
+ if (!/^[0-9a-f]{64}$/.test(f.bodySha256)) throw new LaunchMetadataError("BadHash", "bodySha256 is not 64 lowercase hex");
86
+ if (!Number.isSafeInteger(f.expires) || !/^[1-9][0-9]{0,10}$/.test(String(f.expires))) {
87
+ throw new LaunchMetadataError("BadExpiry", "expires is not unix seconds");
88
+ }
89
+ return [
90
+ WALLET_AUTH_PREFIX,
91
+ `site: ${f.site}`,
92
+ `action: ${f.action}`,
93
+ `chain: ${chainParam(f.cluster)}`,
94
+ `launch: ${f.launch}`,
95
+ `signer: ${f.signer}`,
96
+ `body-sha256: ${f.bodySha256}`,
97
+ `expires: ${f.expires}`,
98
+ ].join("\n");
99
+ }
100
+
101
+ export function walletAuthHeaders(message: string, signature: Uint8Array): Record<string, string> {
102
+ if (signature.length !== 64) throw new LaunchMetadataError("BadSignature", "an ed25519 signature is 64 bytes");
103
+ return { [WALLET_AUTH_MESSAGE_HEADER]: btoa(message), [WALLET_AUTH_SIGNATURE_HEADER]: base58Encode(signature) };
104
+ }
105
+
106
+ const ascii = (s: string): Uint8Array => new TextEncoder().encode(s);
107
+
108
+ export function sniffImageType(b: Uint8Array): ImageType | null {
109
+ const at = (sig: number[], off = 0) => sig.every((x, i) => b[off + i] === x);
110
+ if (at([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) return "image/png";
111
+ if (at([0xff, 0xd8, 0xff])) return "image/jpeg";
112
+ if (at([0x52, 0x49, 0x46, 0x46]) && at([0x57, 0x45, 0x42, 0x50], 8)) return "image/webp";
113
+ return null;
114
+ }
115
+
116
+ export function encodeUploadBody(f: { kind: ImageKind; launch: string; cluster: SolanaCluster; image: Uint8Array }, boundary: string): {
117
+ body: Uint8Array; contentType: string;
118
+ } {
119
+ const type = sniffImageType(f.image);
120
+ if (type === null) throw new LaunchMetadataError("BadImage", `the ${f.kind} is not a PNG, JPEG or WebP`);
121
+ if (f.image.length === 0 || f.image.length > MAX_IMAGE_BYTES[f.kind]) {
122
+ throw new LaunchMetadataError("ImageTooLarge", `the ${f.kind} is at most ${MAX_IMAGE_BYTES[f.kind]} bytes`);
123
+ }
124
+ if (!/^[0-9A-Za-z-]{16,70}$/.test(boundary)) throw new LaunchMetadataError("BadBoundary", "boundary must be 16-70 of [0-9A-Za-z-]");
125
+ const field = (name: string, value: string) =>
126
+ `--${boundary}\r\nContent-Disposition: form-data; name="${name}"\r\n\r\n${value}\r\n`;
127
+ const ext = type === "image/jpeg" ? "jpg" : type.slice(6);
128
+ const head = ascii(
129
+ field("kind", f.kind) + field("launchAddress", f.launch) + field("chainId", chainParam(f.cluster)) +
130
+ `--${boundary}\r\nContent-Disposition: form-data; name="file"; filename="${f.kind}.${ext}"\r\nContent-Type: ${type}\r\n\r\n`,
131
+ );
132
+ const tail = ascii(`\r\n--${boundary}--\r\n`);
133
+ const body = new Uint8Array(head.length + f.image.length + tail.length);
134
+ body.set(head, 0);
135
+ body.set(f.image, head.length);
136
+ body.set(tail, head.length + f.image.length);
137
+ return { body, contentType: `multipart/form-data; boundary=${boundary}` };
138
+ }
139
+
140
+ export interface LaunchMeta {
141
+ description?: string | null;
142
+
143
+ website?: string | null;
144
+
145
+ socials?: Partial<Record<Social, string>>;
146
+
147
+ avatarKey?: string | null;
148
+ bannerKey?: string | null;
149
+ }
150
+
151
+ export function encodeLaunchMetaBody(m: LaunchMeta): Uint8Array {
152
+ const description = m.description ?? null;
153
+ if (description !== null && description.length > MAX_DESCRIPTION) {
154
+ throw new LaunchMetadataError("BadDescription", `description is at most ${MAX_DESCRIPTION} characters`);
155
+ }
156
+ const website = m.website ?? null;
157
+ if (website !== null) {
158
+ if (website.length > MAX_URL) throw new LaunchMetadataError("BadWebsite", "website is too long");
159
+ let ok = /^https?:\/\//i.test(website);
160
+ try { new URL(website); } catch { ok = false; }
161
+ if (!ok) throw new LaunchMetadataError("BadWebsite", "website must be an http(s) URL");
162
+ }
163
+ const socials: Partial<Record<Social, string>> = {};
164
+ for (const k of SOCIAL_KEYS) {
165
+ const v = m.socials?.[k];
166
+ if (v == null || v === "") continue;
167
+ const handle = v.trim().replace(/^@/, "").toLowerCase();
168
+ if (handle.length > MAX_HANDLE) throw new LaunchMetadataError("BadSocial", `socials.${k} is at most ${MAX_HANDLE} characters`);
169
+ socials[k] = handle;
170
+ }
171
+ return ascii(JSON.stringify({
172
+ description, website, avatarKey: m.avatarKey ?? null, bannerKey: m.bannerKey ?? null, socials,
173
+ }));
174
+ }
175
+
176
+ interface WriteContext {
177
+ cluster: SolanaCluster;
178
+
179
+ launch: string;
180
+ signer: CreatorSigner;
181
+
182
+ origin?: string;
183
+ fetch?: Fetch;
184
+
185
+ now?: () => number;
186
+ }
187
+
188
+ async function signedWrite(ctx: WriteContext, action: WalletAuthAction, method: string, path: string, body: Uint8Array, contentType: string): Promise<unknown> {
189
+ const origin = (ctx.origin ?? FLOOR_ORIGIN).replace(/\/$/, "");
190
+ const now = ctx.now ?? (() => Math.floor(Date.now() / 1000));
191
+ const message = walletAuthMessage({
192
+ site: siteOf(origin), action, cluster: ctx.cluster, launch: ctx.launch, signer: ctx.signer.address,
193
+ bodySha256: hex(sha256(body)), expires: now() + WALLET_AUTH_TTL_S,
194
+ });
195
+ const signature = await ctx.signer.signMessage(ascii(message));
196
+ const f = ctx.fetch ?? (globalThis.fetch as unknown as Fetch);
197
+ const res = await f(`${origin}${path}`, {
198
+ method, headers: { ...walletAuthHeaders(message, signature), "content-type": contentType }, body,
199
+ });
200
+ let json: unknown = null;
201
+ try { json = await res.json(); } catch { json = null; }
202
+ if (res.status !== 200) {
203
+ const error = (json as { error?: unknown } | null)?.error;
204
+ throw new LaunchMetadataError("Refused", typeof error === "string" ? error : `HTTP ${res.status}`, res.status);
205
+ }
206
+ return (json as { result?: unknown } | null)?.result;
207
+ }
208
+
209
+ export async function uploadLaunchImage(ctx: WriteContext & { kind: ImageKind; image: Uint8Array }): Promise<string> {
210
+ const boundary = `floor-${hex(sha256(ctx.image)).slice(0, 24)}`;
211
+ const { body, contentType } = encodeUploadBody({ kind: ctx.kind, launch: ctx.launch, cluster: ctx.cluster, image: ctx.image }, boundary);
212
+ const result = await signedWrite(ctx, "upload", "POST", "/api/uploads", body, contentType);
213
+ const key = (result as { key?: unknown } | undefined)?.key;
214
+ if (typeof key !== "string") throw new LaunchMetadataError("BadResponse", "the upload answered without a key");
215
+ return key;
216
+ }
217
+
218
+ export function putLaunchMeta(ctx: WriteContext & { meta: LaunchMeta }): Promise<unknown> {
219
+ return signedWrite(ctx, "meta", "PUT", `/api/meta/launches/${chainParam(ctx.cluster)}/${ctx.launch}`,
220
+ encodeLaunchMetaBody(ctx.meta), "application/json");
221
+ }
222
+
223
+ export async function saveLaunchMetadata(input: {
224
+ cluster: SolanaCluster;
225
+ mint: string;
226
+ signer: CreatorSigner;
227
+ description?: string | null;
228
+ website?: string | null;
229
+ socials?: Partial<Record<Social, string>>;
230
+ avatar?: Uint8Array;
231
+ banner?: Uint8Array;
232
+ programId?: string;
233
+ origin?: string;
234
+ fetch?: Fetch;
235
+ now?: () => number;
236
+ }): Promise<{ launch: string; avatarKey: string | null; bannerKey: string | null }> {
237
+ assertAddress("mint", input.mint);
238
+ const launch = launchPda(input.mint, input.programId)[0];
239
+ const ctx: WriteContext = { cluster: input.cluster, launch, signer: input.signer, origin: input.origin, fetch: input.fetch, now: input.now };
240
+
241
+ encodeLaunchMetaBody({ description: input.description, website: input.website, socials: input.socials });
242
+ const avatarKey = input.avatar ? await uploadLaunchImage({ ...ctx, kind: "avatar", image: input.avatar }) : null;
243
+ const bannerKey = input.banner ? await uploadLaunchImage({ ...ctx, kind: "banner", image: input.banner }) : null;
244
+ await putLaunchMeta({ ...ctx, meta: { description: input.description, website: input.website, socials: input.socials, avatarKey, bannerKey } });
245
+ return { launch, avatarKey, bannerKey };
246
+ }
@@ -0,0 +1,242 @@
1
+
2
+
3
+ import { addressBytes, base58Encode } from "../../codec.ts";
4
+ import { isOnCurve } from "../../ed25519.ts";
5
+ import { findProgramAddress } from "../pda.ts";
6
+ import type { Pda } from "../pda.ts";
7
+ import { FLOOR_AMM_PROGRAM_ID } from "./program.ts";
8
+
9
+ export const MINT_SEED = "mint";
10
+
11
+ const U64_MAX = (1n << 64n) - 1n;
12
+
13
+ function nonceBytes(nonce: bigint): Uint8Array {
14
+ if (typeof nonce !== "bigint" || nonce < 0n || nonce > U64_MAX) throw new RangeError(`a mint nonce is a u64: ${String(nonce)}`);
15
+ const b = new Uint8Array(8);
16
+ let v = nonce;
17
+ for (let i = 0; i < 8; i++) {
18
+ b[i] = Number(v & 0xffn);
19
+ v >>= 8n;
20
+ }
21
+ return b;
22
+ }
23
+
24
+ export const launchMintPda = (creator: string, mintNonce: bigint, p = FLOOR_AMM_PROGRAM_ID): Pda =>
25
+ findProgramAddress([MINT_SEED, addressBytes(creator), nonceBytes(mintNonce)], p);
26
+
27
+ export const launchMintAddress = (creator: string, mintNonce: bigint, p = FLOOR_AMM_PROGRAM_ID): string => launchMintPda(creator, mintNonce, p)[0];
28
+
29
+ export interface MintNonceCheck {
30
+
31
+ mint: string;
32
+
33
+ bump: number;
34
+
35
+ firstTry: boolean;
36
+
37
+ fits: boolean;
38
+ }
39
+
40
+ export function checkMintNonce(p: { creator: string; nonce: bigint; programId?: string; fits?: (address: string) => boolean }): MintNonceCheck {
41
+ const [mint, bump] = launchMintPda(p.creator, p.nonce, p.programId ?? FLOOR_AMM_PROGRAM_ID);
42
+ return { mint, bump, firstTry: bump === 255, fits: p.fits ? p.fits(mint) : true };
43
+ }
44
+
45
+ const K = new Int32Array([
46
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
47
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
48
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
49
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
50
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
51
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
52
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
53
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
54
+ ]);
55
+ const IV = [0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19];
56
+ const MSG_LEN = 98;
57
+
58
+ const NONCE_WORD = 9;
59
+ const ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
60
+ const M4 = 58 ** 4;
61
+
62
+ const bswap = (x: number): number => ((x << 24) | ((x & 0xff00) << 8) | ((x >>> 8) & 0xff00) | (x >>> 24)) | 0;
63
+
64
+ function expand(w: Int32Array): void {
65
+ for (let i = 16; i < 64; i++) {
66
+ const a = w[i - 15], b = w[i - 2];
67
+ const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3);
68
+ const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10);
69
+ w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
70
+ }
71
+ }
72
+
73
+ function rounds(s: Int32Array, kw: Int32Array, from: number): void {
74
+ let a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7];
75
+ for (let i = from; i < 64; i++) {
76
+ const t1 = (h + (((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7))) + ((e & f) ^ (~e & g)) + kw[i]) | 0;
77
+ const t2 = ((((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10))) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
78
+ h = g; g = f; f = e; e = (d + t1) | 0; d = c; c = b; b = a; a = (t1 + t2) | 0;
79
+ }
80
+ s[0] = a; s[1] = b; s[2] = c; s[3] = d; s[4] = e; s[5] = f; s[6] = g; s[7] = h;
81
+ }
82
+
83
+ class Kernel {
84
+ readonly w1 = new Int32Array(64);
85
+ readonly kw1 = new Int32Array(64);
86
+ readonly kw2 = new Int32Array(64);
87
+ readonly pre = new Int32Array(8);
88
+ readonly s = new Int32Array(8);
89
+ readonly h1 = new Int32Array(8);
90
+ readonly out = new Int32Array(8);
91
+
92
+ constructor(creator: string, programId: string) {
93
+ const msg = new Uint8Array(128);
94
+ msg.set(new TextEncoder().encode(MINT_SEED), 0);
95
+ msg.set(addressBytes(creator), 4);
96
+ msg[44] = 255;
97
+ msg.set(addressBytes(programId), 45);
98
+ msg.set(new TextEncoder().encode("ProgramDerivedAddress"), 77);
99
+ msg[MSG_LEN] = 0x80;
100
+ const bits = MSG_LEN * 8;
101
+ msg[126] = bits >>> 8;
102
+ msg[127] = bits & 0xff;
103
+ const word = (o: number) => (msg[o] << 24) | (msg[o + 1] << 16) | (msg[o + 2] << 8) | msg[o + 3];
104
+ for (let i = 0; i < 16; i++) this.w1[i] = word(4 * i);
105
+ const w2 = new Int32Array(64);
106
+ for (let i = 0; i < 16; i++) w2[i] = word(64 + 4 * i);
107
+ expand(w2);
108
+ for (let i = 0; i < 64; i++) this.kw2[i] = (K[i] + w2[i]) | 0;
109
+
110
+ for (let i = 0; i < NONCE_WORD; i++) this.kw1[i] = (K[i] + this.w1[i]) | 0;
111
+ const s = this.pre;
112
+ for (let i = 0; i < 8; i++) s[i] = IV[i];
113
+ let a = s[0], b = s[1], c = s[2], d = s[3], e = s[4], f = s[5], g = s[6], h = s[7];
114
+ for (let i = 0; i < NONCE_WORD; i++) {
115
+ const t1 = (h + (((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7))) + ((e & f) ^ (~e & g)) + this.kw1[i]) | 0;
116
+ const t2 = ((((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10))) + ((a & b) ^ (a & c) ^ (b & c))) | 0;
117
+ h = g; g = f; f = e; e = (d + t1) | 0; d = c; c = b; b = a; a = (t1 + t2) | 0;
118
+ }
119
+ s[0] = a; s[1] = b; s[2] = c; s[3] = d; s[4] = e; s[5] = f; s[6] = g; s[7] = h;
120
+ }
121
+
122
+ hash(lo: number, hi: number): Int32Array {
123
+ const w = this.w1, kw = this.kw1, s = this.s, h1 = this.h1, out = this.out;
124
+ w[NONCE_WORD] = bswap(lo);
125
+ w[NONCE_WORD + 1] = bswap(hi);
126
+ expand(w);
127
+ for (let i = NONCE_WORD; i < 64; i++) kw[i] = (K[i] + w[i]) | 0;
128
+ s.set(this.pre);
129
+ rounds(s, kw, NONCE_WORD);
130
+ for (let i = 0; i < 8; i++) h1[i] = s[i] = (IV[i] + s[i]) | 0;
131
+ rounds(s, this.kw2, 0);
132
+ for (let i = 0; i < 8; i++) out[i] = (h1[i] + s[i]) | 0;
133
+ return out;
134
+ }
135
+ }
136
+
137
+ function digestBytes(h: Int32Array): Uint8Array {
138
+ const b = new Uint8Array(32);
139
+ for (let i = 0; i < 8; i++) {
140
+ b[4 * i] = h[i] >>> 24;
141
+ b[4 * i + 1] = (h[i] >>> 16) & 0xff;
142
+ b[4 * i + 2] = (h[i] >>> 8) & 0xff;
143
+ b[4 * i + 3] = h[i] & 0xff;
144
+ }
145
+ return b;
146
+ }
147
+
148
+ function tail4(h: Int32Array): number {
149
+ let r = 0;
150
+ for (let i = 0; i < 8; i++) {
151
+ const x = h[i];
152
+ r = (r * 65536 + (x >>> 16)) % M4;
153
+ r = (r * 65536 + (x & 0xffff)) % M4;
154
+ }
155
+ return r;
156
+ }
157
+
158
+ function tailString(r: number, n: number): string {
159
+ let t = "";
160
+ for (let i = 0; i < n; i++) {
161
+ t = ALPHABET[r % 58] + t;
162
+ r = Math.floor(r / 58);
163
+ }
164
+ return t;
165
+ }
166
+
167
+ function randomNonce(): bigint {
168
+ const b = new Uint8Array(8);
169
+ globalThis.crypto.getRandomValues(b);
170
+ let v = 0n;
171
+ for (let i = 7; i >= 0; i--) v = (v << 8n) | BigInt(b[i]);
172
+ return v;
173
+ }
174
+
175
+ export interface GrindMintNonceOptions {
176
+
177
+ creator: string;
178
+ programId?: string;
179
+
180
+ fits: (address: string) => boolean;
181
+
182
+ tail?: number;
183
+
184
+ start?: bigint;
185
+
186
+ sliceMs?: number;
187
+
188
+ yieldTo?: () => Promise<void>;
189
+
190
+ cancelled?: () => boolean;
191
+
192
+ budgetMs?: number;
193
+
194
+ onTried?: (tried: number) => void;
195
+ }
196
+
197
+ export interface MintNonceHit {
198
+ nonce: bigint;
199
+ mint: string;
200
+ }
201
+
202
+ const now = (): number => globalThis.performance?.now?.() ?? Date.now();
203
+ const macrotask = (): Promise<void> => new Promise((r) => setTimeout(r, 0));
204
+
205
+ export async function grindMintNonce(o: GrindMintNonceOptions): Promise<MintNonceHit | null> {
206
+ const programId = o.programId ?? FLOOR_AMM_PROGRAM_ID;
207
+ const tail = o.tail;
208
+ if (tail !== undefined && (!Number.isInteger(tail) || tail < 1 || tail > 4)) throw new RangeError(`tail is 1..4 characters: ${tail}`);
209
+ const k = new Kernel(o.creator, programId);
210
+ const sliceMs = o.sliceMs ?? 40;
211
+ const yieldTo = o.yieldTo ?? macrotask;
212
+ const t0 = now();
213
+ const deadline = o.budgetMs === undefined ? Infinity : t0 + o.budgetMs;
214
+ const start = o.start ?? randomNonce();
215
+ nonceBytes(start);
216
+ let lo = Number(start & 0xffffffffn) | 0;
217
+ let hi = Number(start >> 32n) | 0;
218
+ let tried = 0;
219
+ const CHUNK = 2048;
220
+ for (;;) {
221
+ const sliceEnd = Math.min(now() + sliceMs, deadline);
222
+ do {
223
+ for (let n = 0; n < CHUNK; n++) {
224
+ const h = k.hash(lo, hi);
225
+ const nlo = lo, nhi = hi;
226
+ lo = (lo + 1) | 0;
227
+ if (lo === 0) hi = (hi + 1) | 0;
228
+ if (tail !== undefined && !o.fits(tailString(tail4(h), tail))) continue;
229
+ const bytes = digestBytes(h);
230
+ const mint = base58Encode(bytes);
231
+ if (!o.fits(mint) || isOnCurve(bytes)) continue;
232
+ o.onTried?.(tried + n + 1);
233
+ return { nonce: (BigInt(nhi >>> 0) << 32n) | BigInt(nlo >>> 0), mint };
234
+ }
235
+ tried += CHUNK;
236
+ } while (now() < sliceEnd);
237
+ o.onTried?.(tried);
238
+ if (now() >= deadline || o.cancelled?.()) return null;
239
+ await yieldTo();
240
+ if (o.cancelled?.()) return null;
241
+ }
242
+ }
@@ -0,0 +1,64 @@
1
+
2
+
3
+ import { addressBytes } from "../../codec.ts";
4
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, associatedTokenAddress, eventAuthorityPda, findProgramAddress } from "../pda.ts";
5
+ import type { Pda } from "../pda.ts";
6
+ import { PROGRAM_IDS } from "../../generated/index.ts";
7
+
8
+ export const FLOOR_AMM_PROGRAM_ID: string = PROGRAM_IDS.floorAmm;
9
+
10
+ export const FLOOR_AMM_TIMELOCK: string = PROGRAM_IDS.upgradeTimelock;
11
+
12
+ export const OLD_FLOOR_LAUNCH_PROGRAM_ID = "8vfdPWfaA3b9GtJoFUwc36JKDaMfVCJ4oT9HrCrQ6Tdi";
13
+ export const METADATA_PROGRAM_ID = "metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s";
14
+ export const INSTRUCTIONS_SYSVAR_ID = "Sysvar1nstructions1111111111111111111111111";
15
+ export { ASSOCIATED_TOKEN_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID };
16
+
17
+ export const FLOOR_AMM_ERRORS = { NotAuthority: 6000, QuoteNotListed: 6006, BadArgument: 6025, TreasuryNotSet: 6043, NotPriceUpdater: 6044, BadMember: 6045, PresetDeleted: 6004, UnknownClaimType: 6017, BadDecimals: 6026, GenesisNotListed: 6046, GenesisListedAllowanceUsed: 6047, QuoteRowCannotOpen: 6048, QuoteTransferShort: 6049, PresetUnchecked: 6050, GenesisRefundUnderRent: 6051, GenesisDustNotYet: 6052, GenesisConvertCapped: 6053, GenesisPriceStale: 6054, GenesisConvertSlippage: 6055, GenesisSolNotConverted: 6056, GenesisWagmiWrongState: 6057, GenesisWagmiNoPreset: 6058, GenesisWagmiQuoteNotListed: 6059, HolderDestNotSet: 6060, NotTeamMember: 6061, BurnTooLarge: 6062 } as const;
18
+
19
+ const k = addressBytes;
20
+
21
+ export const configPda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["config"], p);
22
+ export const presetPda = (id: number, p = FLOOR_AMM_PROGRAM_ID): Pda => {
23
+
24
+ if (!Number.isInteger(id) || id < 0 || id > 255) throw new RangeError(`preset id ${id} is not a u8`);
25
+ return findProgramAddress(["preset", Uint8Array.of(id)], p);
26
+ };
27
+ export const quoteEntryPda = (quoteMint: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["quote", k(quoteMint)], p);
28
+ export const launchPda = (baseMint: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["launch", k(baseMint)], p);
29
+ export const teamSlotPda = (launch: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["team_slot", k(launch)], p);
30
+ export const traderRefPda = (trader: string, quoteMint: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["trader_ref", k(trader), k(quoteMint)], p);
31
+
32
+ export const traderLinkPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["trader_link", k(wallet)], p);
33
+
34
+ export const unwrapPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["unwrap", k(wallet)], p);
35
+ export const refOwedPda = (referrer: string, quoteMint: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["ref_owed", k(referrer), k(quoteMint)], p);
36
+
37
+ export const genesisRaisePda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_raise"], p);
38
+ export const genesisAllocPda = (wallet: string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_alloc", k(wallet)], p);
39
+
40
+ export const genesisConvertBandPda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_convert_band"], p);
41
+ export const genesisMigrationVaultPda = (p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["genesis_migration_vault"], p);
42
+
43
+ export const GENESIS_USDC_MINT = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";
44
+
45
+ export const GENESIS_CONVERT_DISCOUNT_BPS = 100;
46
+
47
+ export const GENESIS_PRICE_MAX_AGE_SECS = 900;
48
+
49
+ export const genesisVaultUsdc = (p = FLOOR_AMM_PROGRAM_ID, usdcMint = GENESIS_USDC_MINT): string => associatedTokenAddress(genesisMigrationVaultPda(p)[0], usdcMint, TOKEN_PROGRAM_ID);
50
+
51
+ export const genesisVaultFloor = (floorMint: string, p = FLOOR_AMM_PROGRAM_ID): string => associatedTokenAddress(genesisMigrationVaultPda(p)[0], floorMint, TOKEN_2022_PROGRAM_ID);
52
+
53
+ export const genesisRaiseTokenVault = (mint: string, p = FLOOR_AMM_PROGRAM_ID): string => associatedTokenAddress(genesisRaisePda(p)[0], mint, TOKEN_2022_PROGRAM_ID);
54
+ export const floorEventAuthorityPda = (p = FLOOR_AMM_PROGRAM_ID): Pda => eventAuthorityPda(p);
55
+
56
+ export const metadataPda = (mint: string): Pda => findProgramAddress(["metadata", k(METADATA_PROGRAM_ID), k(mint)], METADATA_PROGRAM_ID);
57
+
58
+ export const tokenVault = (launch: string, baseMint: string): string => associatedTokenAddress(launch, baseMint, TOKEN_2022_PROGRAM_ID);
59
+
60
+ export const quoteVault = (launch: string, quoteMint: string, quoteTokenProgram: string): string => associatedTokenAddress(launch, quoteMint, quoteTokenProgram);
61
+
62
+ export const traderRefEscrow = (traderRef: string, quoteMint: string, quoteTokenProgram: string): string => associatedTokenAddress(traderRef, quoteMint, quoteTokenProgram);
63
+
64
+ export const refOwedVault = (refOwed: string, quoteMint: string, quoteTokenProgram: string): string => associatedTokenAddress(refOwed, quoteMint, quoteTokenProgram);
@@ -0,0 +1,96 @@
1
+
2
+
3
+ import { addressBytes, base58Encode, readUintLE, hex } from "../../codec.ts";
4
+ import { R, RS, W, WS, accountDiscriminator, instructionDiscriminator } from "../instruction.ts";
5
+ import type { AccountMeta, Instruction } from "../instruction.ts";
6
+ import { SYSTEM_PROGRAM_ID, findProgramAddress } from "../pda.ts";
7
+ import type { Pda } from "../pda.ts";
8
+ import { Writer } from "./borsh.ts";
9
+ import { FLOOR_AMM_PROGRAM_ID, floorEventAuthorityPda } from "./program.ts";
10
+
11
+ export const CLAIMS_PAGE_HEADER = 80;
12
+
13
+ export const CLAIMS_PAGE: readonly (readonly [string, string])[] = [
14
+ ["wallet", "pubkey"],
15
+ ["page", "u16"],
16
+ ["count", "u16"],
17
+ ["bump", "u8"],
18
+ ["pad0", "bytes:3"],
19
+ ["lookup_table", "pubkey"],
20
+ ["reserved", "bytes:8"],
21
+ ];
22
+ export const CLAIMS_ENTRY_SIZE = 40;
23
+ export const CLAIMS_PAGE_ENTRIES = 100;
24
+
25
+ export const REGISTRY_ROLES = 1 | 2 | 4 | 32;
26
+ const DEFAULT_KEY = "11111111111111111111111111111111";
27
+
28
+ export const claimsPagePda = (wallet: string, page: number, p = FLOOR_AMM_PROGRAM_ID): Pda => {
29
+ if (!Number.isInteger(page) || page < 0 || page > 0xffff) throw new RangeError("a page is a u16");
30
+ return findProgramAddress(["claims", addressBytes(wallet), Uint8Array.of(page & 0xff, page >> 8)], p);
31
+ };
32
+
33
+ export interface ClaimsEntry {
34
+ launch: string;
35
+ roles: number;
36
+ }
37
+ export interface ClaimsPage {
38
+ wallet: string;
39
+ page: number;
40
+ count: number;
41
+
42
+ lookupTable: string | null;
43
+ entries: ClaimsEntry[];
44
+ }
45
+
46
+ export function decodeClaimsPage(data: Uint8Array): ClaimsPage {
47
+ const head = 8 + CLAIMS_PAGE_HEADER;
48
+ if (data.length < head || hex(data.slice(0, 8)) !== hex(accountDiscriminator("ClaimsPage"))) throw new RangeError("not a ClaimsPage");
49
+ const count = Number(readUintLE(data, 8 + 34, 2));
50
+ if (count > CLAIMS_PAGE_ENTRIES || data.length !== head + count * CLAIMS_ENTRY_SIZE) throw new RangeError(`ClaimsPage: ${data.length} bytes for ${count} entries`);
51
+ const lut = base58Encode(data.slice(8 + 40, 8 + 72));
52
+ const entries: ClaimsEntry[] = [];
53
+ for (let i = 0; i < count; i++) {
54
+ const o = head + i * CLAIMS_ENTRY_SIZE;
55
+ entries.push({ launch: base58Encode(data.slice(o, o + 32)), roles: data[o + 32] });
56
+ }
57
+ return { wallet: base58Encode(data.slice(8, 40)), page: Number(readUintLE(data, 8 + 32, 2)), count, lookupTable: lut === DEFAULT_KEY ? null : lut, entries };
58
+ }
59
+
60
+ export function launchesFromRegistry(pages: readonly ClaimsPage[]): { launches: ClaimsEntry[]; lookupTable: string | null } {
61
+ const launches = new Map<string, number>();
62
+ let lookupTable: string | null = null;
63
+ for (const p of pages) {
64
+ if (p.page === 0) lookupTable = p.lookupTable;
65
+ for (const e of p.entries) launches.set(e.launch, (launches.get(e.launch) ?? 0) | (e.roles & REGISTRY_ROLES));
66
+ }
67
+ return { launches: [...launches].map(([launch, roles]) => ({ launch, roles })), lookupTable };
68
+ }
69
+
70
+ export function nextClaimsPage(pages: readonly ClaimsPage[]): number {
71
+ if (pages.length === 0) return 0;
72
+ const last = pages[pages.length - 1];
73
+ return last.count < CLAIMS_PAGE_ENTRIES ? last.page : last.page + 1;
74
+ }
75
+
76
+ function ix(name: string, accounts: AccountMeta[], args: (w: Writer) => Writer, programId = FLOOR_AMM_PROGRAM_ID): Instruction {
77
+ const ev = floorEventAuthorityPda(programId)[0];
78
+ return { programAddress: programId, accounts: [...accounts, R(ev), R(programId)], data: args(new Writer().bytes(instructionDiscriminator(name))).done() };
79
+ }
80
+
81
+ export function registerRoleInstruction(p: { payer: string; wallet: string; page: number; role: number; subject: string; programId?: string }): Instruction {
82
+ if (![1, 2, 4, 32].includes(p.role)) throw new RangeError("one role bit: 1, 2, 4 or 32");
83
+ const pid = p.programId ?? FLOOR_AMM_PROGRAM_ID;
84
+ const prev = p.page === 0 ? pid : claimsPagePda(p.wallet, p.page - 1, pid)[0];
85
+ return ix("register_role", [WS(p.payer), W(claimsPagePda(p.wallet, p.page, pid)[0]), R(prev), R(p.subject), R(SYSTEM_PROGRAM_ID)], (w) => w.pubkey(p.wallet).u16(p.page).u8(p.role), pid);
86
+ }
87
+
88
+ export function setClaimsLutInstruction(p: { wallet: string; lookupTable?: string; programId?: string }): Instruction {
89
+ const pid = p.programId ?? FLOOR_AMM_PROGRAM_ID;
90
+ return ix("set_claims_lut", [RS(p.wallet), W(claimsPagePda(p.wallet, 0, pid)[0])], (w) => w.pubkey(p.lookupTable ?? "11111111111111111111111111111111"), pid);
91
+ }
92
+
93
+ export function registryHookAccounts(p: { payer: string; pages: readonly { wallet: string; page: number }[]; programId?: string }): AccountMeta[] {
94
+ const pid = p.programId ?? FLOOR_AMM_PROGRAM_ID;
95
+ return [WS(p.payer), R(SYSTEM_PROGRAM_ID), ...p.pages.map((x) => W(claimsPagePda(x.wallet, x.page, pid)[0]))];
96
+ }