@floorlaunchpad/sdk 0.6.0 → 0.8.1

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 (125) hide show
  1. package/CHANGELOG.md +279 -0
  2. package/README.md +12 -1
  3. package/dist/abi/evm-event-names.js +7 -0
  4. package/dist/abi/evm.gen.js +81 -2
  5. package/dist/abi/functions.gen.js +135 -1
  6. package/dist/abi/solana.gen.js +64 -0
  7. package/dist/client/solana/index.js +25 -6
  8. package/dist/config/generated.js +803 -26374
  9. package/dist/errors.js +21 -2
  10. package/dist/events/types.gen.js +1 -1
  11. package/dist/evm/chains.js +80 -0
  12. package/dist/evm/floor/abi.gen.js +115 -0
  13. package/dist/evm/floor/calls.js +5 -0
  14. package/dist/evm/squid.js +21 -5
  15. package/dist/evm/venues.js +302 -0
  16. package/dist/generated/interface.gen.js +93 -44
  17. package/dist/index.js +7 -0
  18. package/dist/indexer/bodies.js +288 -5
  19. package/dist/indexer/client.js +17 -1
  20. package/dist/indexer/contract.js +141 -15
  21. package/dist/indexer/paths.js +80 -7
  22. package/dist/indexer/read.js +25 -3
  23. package/dist/indexer/stream.js +2 -2
  24. package/dist/math/v2/book.js +24 -7
  25. package/dist/math/v2/create.js +1 -1
  26. package/dist/math/v2/escrow.js +205 -0
  27. package/dist/math/v2/fees.js +30 -4
  28. package/dist/math/v2/milestones.js +1 -1
  29. package/dist/math/v2/office.js +110 -0
  30. package/dist/math/v2/receipt.js +3 -3
  31. package/dist/math/v2/relay.js +263 -96
  32. package/dist/math/v2/rows.js +17 -0
  33. package/dist/math/v2/team.js +42 -1
  34. package/dist/math/v2/trade.js +3 -1
  35. package/dist/reprice.js +88 -0
  36. package/dist/route/quote.js +16 -0
  37. package/dist/scaledUi.js +113 -0
  38. package/dist/solana/floor/accounts.js +38 -5
  39. package/dist/solana/floor/events.js +62 -1
  40. package/dist/solana/floor/instructions.js +39 -1
  41. package/dist/solana/floor/program.js +3 -0
  42. package/dist/stockRefresh.js +99 -0
  43. package/dist/ton/consts.generated.js +2 -2
  44. package/dist/ton/gas.js +3 -3
  45. package/dist/v2/claims.js +28 -5
  46. package/dist/v2/devbuy.js +28 -3
  47. package/dist/v2/events.js +2 -0
  48. package/dist/v2/evm.js +183 -5
  49. package/dist/v2/layouts.js +102 -2
  50. package/dist/v2/office-events.js +127 -0
  51. package/dist/v2/office-evm.js +68 -0
  52. package/dist/v2/office-read.js +182 -0
  53. package/dist/v2/office-solana.js +134 -0
  54. package/dist/v2/office-trade.js +73 -0
  55. package/dist/v2/route-alt.js +1 -0
  56. package/dist/v2/route.js +1 -1
  57. package/dist/v2/solana.js +224 -5
  58. package/dist/v2/solanaSettle.js +2 -0
  59. package/dist/v2/tx/evm.js +66 -2
  60. package/dist/v2/tx/solana.js +3 -3
  61. package/dist/v2/types.js +4 -0
  62. package/docs/aggregator.md +19 -0
  63. package/docs/cursor-pages.md +9 -3
  64. package/docs/ton.md +1 -1
  65. package/examples/floor-line.html +1 -1
  66. package/package.json +26 -1
  67. package/src/abi/evm-event-names.ts +7 -0
  68. package/src/abi/evm.gen.ts +81 -2
  69. package/src/abi/functions.gen.ts +135 -1
  70. package/src/abi/solana.gen.ts +64 -0
  71. package/src/client/solana/index.ts +25 -6
  72. package/src/config/generated.ts +803 -26374
  73. package/src/errors.ts +21 -2
  74. package/src/events/types.gen.ts +458 -1
  75. package/src/evm/chains.ts +89 -0
  76. package/src/evm/floor/abi.gen.ts +115 -0
  77. package/src/evm/floor/calls.ts +5 -0
  78. package/src/evm/squid.ts +22 -6
  79. package/src/evm/venues.ts +340 -0
  80. package/src/generated/interface.gen.ts +93 -44
  81. package/src/index.ts +7 -0
  82. package/src/indexer/bodies.ts +308 -5
  83. package/src/indexer/client.ts +31 -4
  84. package/src/indexer/contract.ts +148 -17
  85. package/src/indexer/paths.ts +139 -7
  86. package/src/indexer/read.ts +27 -5
  87. package/src/indexer/stream.ts +4 -4
  88. package/src/math/v2/book.ts +23 -7
  89. package/src/math/v2/create.ts +1 -1
  90. package/src/math/v2/escrow.ts +213 -0
  91. package/src/math/v2/fees.ts +30 -4
  92. package/src/math/v2/milestones.ts +1 -1
  93. package/src/math/v2/office.ts +114 -0
  94. package/src/math/v2/receipt.ts +7 -3
  95. package/src/math/v2/relay.ts +269 -96
  96. package/src/math/v2/rows.ts +17 -0
  97. package/src/math/v2/team.ts +42 -1
  98. package/src/math/v2/trade.ts +3 -1
  99. package/src/reprice.ts +121 -0
  100. package/src/route/quote.ts +16 -0
  101. package/src/scaledUi.ts +123 -0
  102. package/src/solana/floor/accounts.ts +38 -5
  103. package/src/solana/floor/events.ts +63 -2
  104. package/src/solana/floor/instructions.ts +39 -1
  105. package/src/solana/floor/program.ts +3 -0
  106. package/src/stockRefresh.ts +131 -0
  107. package/src/ton/consts.generated.ts +2 -2
  108. package/src/ton/gas.ts +3 -3
  109. package/src/v2/claims.ts +34 -5
  110. package/src/v2/devbuy.ts +38 -3
  111. package/src/v2/events.ts +3 -1
  112. package/src/v2/evm.ts +209 -5
  113. package/src/v2/layouts.ts +102 -2
  114. package/src/v2/office-events.ts +133 -0
  115. package/src/v2/office-evm.ts +76 -0
  116. package/src/v2/office-read.ts +264 -0
  117. package/src/v2/office-solana.ts +159 -0
  118. package/src/v2/office-trade.ts +96 -0
  119. package/src/v2/route-alt.ts +1 -0
  120. package/src/v2/route.ts +1 -1
  121. package/src/v2/solana.ts +235 -5
  122. package/src/v2/solanaSettle.ts +2 -0
  123. package/src/v2/tx/evm.ts +66 -2
  124. package/src/v2/tx/solana.ts +5 -3
  125. package/src/v2/types.ts +76 -1
package/src/v2/layouts.ts CHANGED
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- import { hex } from "../codec.ts";
3
+ import { base58Encode, hex } from "../codec.ts";
4
4
  import { accountDiscriminator } from "../solana/instruction.ts";
5
5
  import { AccountDecodeError, decodeStruct, encodeStruct, layoutSize } from "../solana/floor/accounts.ts";
6
6
  import type { Layout } from "../solana/floor/accounts.ts";
@@ -135,7 +135,9 @@ export const LAUNCH_V2: Layout = [
135
135
  ["k_at_f", "u64"],
136
136
  ["k_floor_tokens", "u64"],
137
137
  ["kpot", "u8"],
138
- ["pad_end", "bytes:15"],
138
+
139
+ ["k_kept", "bytes:3"],
140
+ ["creator_ref_og_until", "u32[3]"],
139
141
  ];
140
142
 
141
143
  export const HOLDER_POSITION: Layout = [
@@ -178,6 +180,85 @@ export const DEAD_MARK: Layout = [
178
180
  ["counted", "u64"],
179
181
  ];
180
182
 
183
+ export const DEAL_ESCROW: Layout = [
184
+ ["version", "u8"],
185
+ ["bump", "u8"],
186
+ ["n", "u8"],
187
+ ["kind", "u8"],
188
+ ["released", "u8"],
189
+ ["refunded", "u8"],
190
+ ["decimals", "u8"],
191
+ ["pad_fee_bps", "u8"],
192
+ ["fee_bps", "u16"],
193
+ ["cancel", "u8"],
194
+ ["pad_expiry", "bytes:5"],
195
+ ["expiry", "i64"],
196
+ ["opened_at", "i64"],
197
+ ["launch", "pubkey"],
198
+ ["launcher", "pubkey"],
199
+ ["kol", "pubkey"],
200
+ ["asset_mint", "pubkey"],
201
+ ["asset_token_program", "pubkey"],
202
+ ["vault", "pubkey"],
203
+ ["ref32", "bytes:32"],
204
+ ["targets", "u128l[8]"],
205
+ ["amounts", "u64[8]"],
206
+ ["armed_at", "i64[8]"],
207
+ ["armed_slot", "u64[8]"],
208
+
209
+ ["agency", "pubkey"],
210
+ ["vol_at_lock", "u128l"],
211
+ ["proof_ends", "i64[8]"],
212
+ ["proof_hash", "bytes:32[8]"],
213
+ ["kinds", "u8[8]"],
214
+ ["proof", "u8[8]"],
215
+ ["agency_bps", "u16"],
216
+
217
+ ["vol_src", "u8"],
218
+
219
+ ["pad_size", "bytes:13"],
220
+ ];
221
+
222
+ export const DEAL_ESCROW_SIZE = 976;
223
+
224
+ export const DEAL_ESCROW_V3_VERSION = 3;
225
+
226
+ export const REF_VOLUME: Layout = [
227
+ ["launch", "pubkey"],
228
+ ["referrer", "pubkey"],
229
+ ["volume", "u128l"],
230
+ ["bump", "u8"],
231
+ ["pad_size", "bytes:15"],
232
+ ];
233
+ export const REF_VOLUME_SIZE = 96;
234
+
235
+ export const SEAT_AGENCY_LAYOUT: Layout = [
236
+ ["launch", "pubkey"],
237
+ ["agency", "pubkey"],
238
+ ["agency_bps", "u16"],
239
+ ["index", "u8"],
240
+ ["bump", "u8"],
241
+ ["pad_size", "bytes:4"],
242
+ ];
243
+ export const SEAT_AGENCY_SIZE = 72;
244
+
245
+ export const REF_VOLUME_ENTRY: Layout = [
246
+ ["referrer", "pubkey"],
247
+ ["volume", "u128l"],
248
+ ];
249
+ export const REF_VOLUME_BOOK: Layout = [
250
+ ["launch", "pubkey"],
251
+ ["len", "u8"],
252
+ ["bump", "u8"],
253
+ ["pad_entries", "bytes:14"],
254
+ ["entries", "bytes:1536"],
255
+ ];
256
+ export const REF_VOLUME_BOOK_SIZE = 1584;
257
+ export const REF_VOLUME_BOOK_CAP = 32;
258
+
259
+ export const VOL_SRC_REF_VOLUME = 0;
260
+ export const VOL_SRC_BOOK = 1;
261
+
181
262
  export const LOTS_MAX = 16;
182
263
  export const MEMBERS_MAX = 10;
183
264
 
@@ -189,6 +270,10 @@ export const LAYOUTS_V2 = {
189
270
  HolderPosition: HOLDER_POSITION,
190
271
  HolderBook: HOLDER_BOOK,
191
272
  DeadMark: DEAD_MARK,
273
+ DealEscrow: DEAL_ESCROW,
274
+ RefVolume: REF_VOLUME,
275
+ RefVolumeBook: REF_VOLUME_BOOK,
276
+ SeatAgency: SEAT_AGENCY_LAYOUT,
192
277
  } as const;
193
278
  export type AccountNameV2 = keyof typeof LAYOUTS_V2;
194
279
 
@@ -221,3 +306,18 @@ export const decodePresetRowV2 = (d: Uint8Array) => decodeAccountV2("PresetRowV2
221
306
  export const decodeHolderPosition = (d: Uint8Array) => decodeAccountV2("HolderPosition", d);
222
307
  export const decodeHolderBook = (d: Uint8Array) => decodeAccountV2("HolderBook", d);
223
308
  export const decodeDeadMark = (d: Uint8Array) => decodeAccountV2("DeadMark", d);
309
+
310
+ export const decodeDealEscrow = (d: Uint8Array): Record<string, any> => decodeAccountV2("DealEscrow", d);
311
+
312
+ export const decodeRefVolume = (d: Uint8Array): Record<string, any> => decodeAccountV2("RefVolume", d);
313
+
314
+ export function refVolumeBookEntries(book: { len: number; entries: Uint8Array }): { referrer: string; volume: bigint }[] {
315
+ const out: { referrer: string; volume: bigint }[] = [];
316
+ for (let i = 0; i < Math.min(book.len, REF_VOLUME_BOOK_CAP); i++) {
317
+ const e = book.entries.subarray(48 * i, 48 * i + 48);
318
+ let v = 0n;
319
+ for (let j = 47; j >= 32; j--) v = (v << 8n) | BigInt(e[j]);
320
+ out.push({ referrer: base58Encode(e.subarray(0, 32)), volume: v });
321
+ }
322
+ return out;
323
+ }
@@ -0,0 +1,133 @@
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
+ import { base58Encode } from "../codec.ts";
7
+ import { decodeAbi, eventTopic, hexToBytes } from "../evm/abi.ts";
8
+
9
+ type K = "id" | "addr" | "launch" | "bool" | "u16" | "amt" | "amt64" | "tok" | "toks" | "launches" | "toksV";
10
+ const S = (...f: [string, K][]): [string, K][] => f;
11
+
12
+ export const OFFICE_EVENTS: Record<string, [string, K][]> = {
13
+ OfficeOpened: S(["officeId", "id"], ["controller", "addr"], ["quote", "addr"]),
14
+ OfficeMemberAdded: S(["officeId", "id"], ["member", "addr"]),
15
+ OfficeMemberLeft: S(["officeId", "id"], ["member", "addr"], ["byController", "bool"], ["units", "amt"], ["cashOut", "amt64"], ["launches", "launches"], ["tokensOut", "toksV"]),
16
+ OfficeDeposit: S(["officeId", "id"], ["member", "addr"], ["amount", "amt64"], ["units", "amt"], ["valueBefore", "amt"], ["unitsTotal", "amt"]),
17
+ OfficeShareSet: S(["officeId", "id"], ["member", "addr"], ["shareBps", "u16"], ["shareSum", "u16"]),
18
+ OfficeBuy: S(["officeId", "id"], ["buyer", "addr"], ["launch", "launch"], ["gross", "amt64"], ["tokens", "tok"], ["refund", "amt64"]),
19
+ OfficeSell: S(["officeId", "id"], ["seller", "addr"], ["buyer", "addr"], ["launch", "launch"], ["tokens", "tok"], ["out", "amt64"], ["unfilled", "tok"]),
20
+ OfficeControllerProposed: S(["officeId", "id"], ["member", "addr"]),
21
+ OfficeControllerSet: S(["officeId", "id"], ["controller", "addr"]),
22
+ OfficeMergeProposed: S(["officeId", "id"], ["other", "id"], ["newController", "addr"]),
23
+ OfficeMergeCancelled: S(["officeId", "id"]),
24
+ OfficeMerged: S(["officeId", "id"], ["other", "id"], ["newController", "addr"], ["unitsFrom", "amt"], ["unitsTo", "amt"], ["valueFrom", "amt"], ["valueTo", "amt"]),
25
+
26
+ OfficeMergeStarted: S(["officeId", "id"], ["other", "id"], ["newController", "addr"], ["unitsFrom", "amt"], ["valueFrom", "amt"], ["valueTo", "amt"], ["members", "u16"]),
27
+
28
+ OfficeMergeStep: S(["officeId", "id"], ["other", "id"], ["moved", "u16"], ["left", "u16"], ["unitsTo", "amt"]),
29
+
30
+ OfficePayoutOwed: S(["officeId", "id"], ["member", "addr"], ["amount", "amt64"]),
31
+ };
32
+ export const OFFICE_EVENT_NAMES = Object.keys(OFFICE_EVENTS);
33
+
34
+ export const OFFICE_UNFILLED_LEN = 6;
35
+
36
+ export interface OfficeEvent {
37
+ name: string;
38
+ fields: Record<string, any>;
39
+ }
40
+
41
+ function solRead(r: Reader, k: K): unknown {
42
+ switch (k) {
43
+ case "id": return hex(r.bytes(32));
44
+ case "addr":
45
+ case "launch": return base58Encode(r.bytes(32));
46
+ case "bool": return r.bool();
47
+ case "u16": return r.u16();
48
+ case "amt": return r.u128();
49
+ case "amt64": return r.u64();
50
+ case "tok": return r.u64();
51
+ case "toks": return Array.from({ length: OFFICE_UNFILLED_LEN }, () => r.u64());
52
+ case "launches": return Array.from({ length: r.u32() }, () => base58Encode(r.bytes(32)));
53
+ case "toksV": return Array.from({ length: r.u32() }, () => r.u64());
54
+ }
55
+ }
56
+ function solWrite(w: Writer, k: K, v: any): void {
57
+ switch (k) {
58
+ case "id": w.bytes(hexToBytes(v)); break;
59
+ case "addr":
60
+ case "launch": w.pubkey(v); break;
61
+ case "bool": w.bool(v); break;
62
+ case "u16": w.u16(v); break;
63
+ case "amt": w.u128(v); break;
64
+ case "amt64": w.u64(v); break;
65
+ case "tok": w.u64(v); break;
66
+ case "toks":
67
+ if (v.length !== OFFICE_UNFILLED_LEN) throw new RangeError(`tokensUnfilled: ${OFFICE_UNFILLED_LEN} values`);
68
+ for (const x of v) w.u64(x);
69
+ break;
70
+ case "launches": w.u32(v.length); for (const x of v) w.pubkey(x); break;
71
+ case "toksV": w.u32(v.length); for (const x of v) w.u64(x); break;
72
+ }
73
+ }
74
+
75
+ let disc: Map<string, string> | undefined;
76
+ const DISC = (): Map<string, string> => (disc ??= new Map(OFFICE_EVENT_NAMES.map((n) => [hex(eventDiscriminator(n)), n])));
77
+
78
+ export function decodeOfficeEventData(data: Uint8Array): OfficeEvent | null {
79
+ const name = DISC().get(hex(data.slice(0, 8)));
80
+ if (!name) return null;
81
+ const r = new Reader(data.slice(8));
82
+ const fields: Record<string, any> = {};
83
+ for (const [f, k] of OFFICE_EVENTS[name]) fields[f] = solRead(r, k);
84
+ if (r.o !== r.b.length) throw new RangeError(`${name}: ${r.b.length} body bytes, the schema reads ${r.o}`);
85
+ return { name, fields };
86
+ }
87
+
88
+ export function encodeOfficeEventData(e: OfficeEvent): Uint8Array {
89
+ const w = new Writer().bytes(eventDiscriminator(e.name));
90
+ for (const [f, k] of OFFICE_EVENTS[e.name] ?? []) solWrite(w, k, e.fields[f]);
91
+ return w.done();
92
+ }
93
+
94
+ export function decodeOfficeEventCpi(ixData: Uint8Array): OfficeEvent | null {
95
+ if (ixData.length < 16 || hex(ixData.slice(0, 8)) !== hex(EVENT_IX_TAG_LE)) return null;
96
+ return decodeOfficeEventData(ixData.slice(8));
97
+ }
98
+
99
+ const EVM_TYPE: Record<K, string> = { id: "bytes32", addr: "address", launch: "uint256", bool: "bool", u16: "uint16", amt: "uint256", amt64: "uint256", tok: "uint256", toks: "uint256[]", launches: "uint256[]", toksV: "uint256[]" };
100
+
101
+ export const officeEventSignature = (name: string): string => `${name}(${OFFICE_EVENTS[name].map(([, k]) => EVM_TYPE[k]).join(",")})`;
102
+
103
+ export const OFFICE_LOG_TOPICS: ReadonlyMap<string, string> = new Map(OFFICE_EVENT_NAMES.map((n) => [eventTopic(officeEventSignature(n)), n]));
104
+
105
+ const EVM_INDEXED: Record<string, readonly string[]> = {
106
+ OfficeOpened: ["officeId", "controller"], OfficeMemberAdded: ["officeId", "member"], OfficeMemberLeft: ["officeId", "member"],
107
+ OfficeDeposit: ["officeId", "member"], OfficeShareSet: ["officeId", "member"], OfficeBuy: ["officeId", "buyer", "launch"],
108
+ OfficeSell: ["officeId", "seller", "launch"], OfficeControllerProposed: ["officeId", "member"], OfficeControllerSet: ["officeId", "controller"],
109
+ OfficeMergeProposed: ["officeId", "other"], OfficeMergeCancelled: ["officeId"], OfficeMerged: ["officeId", "other"], OfficeMergeStarted: ["officeId", "other"], OfficeMergeStep: ["officeId", "other"], OfficePayoutOwed: ["officeId", "member"],
110
+ };
111
+ const topicWord = (t: string, k: K): any => {
112
+ const h = t.toLowerCase();
113
+ return k === "addr" ? "0x" + h.slice(-40) : k === "launch" ? String(BigInt(h)) : h;
114
+ };
115
+
116
+ export function decodeOfficeLog(log: { topics: readonly string[]; data: string }): OfficeEvent | null {
117
+ const name = OFFICE_LOG_TOPICS.get(log.topics[0]?.toLowerCase() ?? "");
118
+ if (!name) return null;
119
+ const spec = OFFICE_EVENTS[name];
120
+ const n = log.topics.length - 1;
121
+ const table = EVM_INDEXED[name] ?? [];
122
+ const idx = n === table.length ? table : spec.slice(0, n).map(([f]) => f);
123
+ const dataSpec = spec.filter(([f]) => !idx.includes(f));
124
+ const vals = dataSpec.length === 0 ? [] : decodeAbi(dataSpec.map(([, k]) => EVM_TYPE[k]), log.data);
125
+ const fields: Record<string, any> = {};
126
+ let ti = 1;
127
+ for (const [f, k] of spec.filter(([f]) => idx.includes(f))) fields[f] = topicWord(log.topics[ti++], k);
128
+ dataSpec.forEach(([f, k], i) => {
129
+ const v: any = vals[i];
130
+ fields[f] = k === "launch" ? String(v) : k === "launches" ? (v as bigint[]).map(String) : k === "id" || k === "addr" ? String(v).toLowerCase() : v;
131
+ });
132
+ return { name, fields };
133
+ }
@@ -0,0 +1,76 @@
1
+
2
+
3
+ import { decodeAbi, encodeCall } from "../evm/abi.ts";
4
+ import type { AbiValue } from "../evm/abi.ts";
5
+
6
+ export interface OfficeCall {
7
+
8
+ data: Uint8Array;
9
+
10
+ value: bigint;
11
+ }
12
+
13
+ export const OFFICE_SIGNATURES = {
14
+ officeOpen: "officeOpen(bytes32,address)",
15
+ officeAddMember: "officeAddMember(bytes32,address)",
16
+ officeRemoveMember: "officeRemoveMember(bytes32,address)",
17
+ officeLeave: "officeLeave(bytes32)",
18
+ officeDeposit: "officeDeposit(bytes32,uint256)",
19
+ officeSetShare: "officeSetShare(bytes32,address,uint16)",
20
+ officeBuy: "officeBuy(bytes32,uint256,uint256,uint256)",
21
+ officeSell: "officeSell(bytes32,uint256,address,uint256,uint256)",
22
+ officeProposeController: "officeProposeController(bytes32,address)",
23
+ officeAcceptController: "officeAcceptController(bytes32)",
24
+ officeProposeMerge: "officeProposeMerge(bytes32,bytes32,address)",
25
+ officeAcceptMerge: "officeAcceptMerge(bytes32,bytes32,address)",
26
+ officeCancelMerge: "officeCancelMerge(bytes32)",
27
+ officeMergeStep: "officeMergeStep(bytes32)",
28
+ officeOf: "officeOf(bytes32)",
29
+ officeMemberOf: "officeMemberOf(bytes32,address)",
30
+ officeValue: "officeValue(bytes32)",
31
+ } as const;
32
+
33
+ const ZERO_ADDRESS = "0x" + "00".repeat(20);
34
+ const call = (sig: keyof typeof OFFICE_SIGNATURES, v: AbiValue[], value = 0n): OfficeCall => ({ data: encodeCall(OFFICE_SIGNATURES[sig], v), value });
35
+ const u = (name: string, x: bigint, bits: number): bigint => {
36
+ if (x < 0n || x >= 1n << BigInt(bits)) throw new RangeError(`${name} is a u${bits}`);
37
+ return x;
38
+ };
39
+ const id = (officeId: string): string => {
40
+ if (!/^0x[0-9a-fA-F]{64}$/.test(officeId) || /^0x0+$/.test(officeId)) throw new RangeError("officeId is 32 non-zero bytes (0x hex)");
41
+ return officeId;
42
+ };
43
+
44
+ export const officeOpenCall = (p: { officeId: string; quote: string }): OfficeCall => call("officeOpen", [id(p.officeId), p.quote]);
45
+ export const officeAddMemberCall = (p: { officeId: string; member: string }): OfficeCall => call("officeAddMember", [id(p.officeId), p.member]);
46
+ export const officeRemoveMemberCall = (p: { officeId: string; member: string }): OfficeCall => call("officeRemoveMember", [id(p.officeId), p.member]);
47
+ export const officeLeaveCall = (p: { officeId: string }): OfficeCall => call("officeLeave", [id(p.officeId)]);
48
+
49
+ export const officeDepositCall = (p: { officeId: string; amount: bigint; nativeQuote?: boolean }): OfficeCall =>
50
+ call("officeDeposit", [id(p.officeId), u("amount", p.amount, 256)], p.nativeQuote ? p.amount : 0n);
51
+ export const officeSetShareCall = (p: { officeId: string; member: string; shareBps: number }): OfficeCall => {
52
+ if (!Number.isInteger(p.shareBps) || p.shareBps < 0 || p.shareBps > 10_000) throw new RangeError("shareBps is 0..=10000 (ShareSum)");
53
+ return call("officeSetShare", [id(p.officeId), p.member, BigInt(p.shareBps)]);
54
+ };
55
+ export const officeBuyCall = (p: { officeId: string; launchId: bigint; gross: bigint; minTokens: bigint }): OfficeCall =>
56
+ call("officeBuy", [id(p.officeId), u("launchId", p.launchId, 256), u("gross", p.gross, 256), u("minTokens", p.minTokens, 256)]);
57
+ export const officeSellCall = (p: { officeId: string; launchId: bigint; buyer: string; tokens: bigint; minOut: bigint }): OfficeCall =>
58
+ call("officeSell", [id(p.officeId), u("launchId", p.launchId, 256), p.buyer, u("tokens", p.tokens, 256), u("minOut", p.minOut, 256)]);
59
+
60
+ export const officeProposeControllerCall = (p: { officeId: string; member?: string }): OfficeCall => call("officeProposeController", [id(p.officeId), p.member ?? ZERO_ADDRESS]);
61
+ export const officeAcceptControllerCall = (p: { officeId: string }): OfficeCall => call("officeAcceptController", [id(p.officeId)]);
62
+ export const officeProposeMergeCall = (p: { officeId: string; other: string; newController: string }): OfficeCall => call("officeProposeMerge", [id(p.officeId), id(p.other), p.newController]);
63
+ export const officeAcceptMergeCall = (p: { officeId: string; other: string; newController: string }): OfficeCall => call("officeAcceptMerge", [id(p.officeId), id(p.other), p.newController]);
64
+ export const officeCancelMergeCall = (p: { officeId: string }): OfficeCall => call("officeCancelMerge", [id(p.officeId)]);
65
+
66
+ export const officeMergeStepCall = (p: { officeId: string }): OfficeCall => call("officeMergeStep", [id(p.officeId)]);
67
+ export const officeOfCall = (p: { officeId: string }): OfficeCall => call("officeOf", [id(p.officeId)]);
68
+ export const officeMemberOfCall = (p: { officeId: string; member: string }): OfficeCall => call("officeMemberOf", [id(p.officeId), p.member]);
69
+ export const officeValueCall = (p: { officeId: string }): OfficeCall => call("officeValue", [id(p.officeId)]);
70
+
71
+ export function decodeOfficeMemberOf(ret: Uint8Array | string): { units: bigint; shareBps: number } {
72
+ const [units, shareBps] = decodeAbi(["uint256", "uint16"], ret) as [bigint, bigint];
73
+ return { units, shareBps: Number(shareBps) };
74
+ }
75
+
76
+ export const decodeOfficeValue = (ret: Uint8Array | string): bigint => decodeAbi(["uint256"], ret)[0] as bigint;
@@ -0,0 +1,264 @@
1
+
2
+
3
+ import { decodeAbi, encodeAbi, bytesToHex, hexToBytes } from "../evm/abi.ts";
4
+ import type { AbiValue } from "../evm/abi.ts";
5
+ import { FLOOR_AMM_ABI } from "../evm/floor/abi.gen.ts";
6
+ import { decodeOfficeMemberOf, decodeOfficeValue } from "./office-evm.ts";
7
+ import { base58Encode } from "../codec.ts";
8
+ import { FLOOR_AMM_PROGRAM_ID } from "../solana/floor/program.ts";
9
+ import { officePda } from "./office-solana.ts";
10
+
11
+ export { officePda };
12
+
13
+ export const officeAddress = (officeId: Uint8Array | string, programId = FLOOR_AMM_PROGRAM_ID): string => officePda(officeId, programId)[0];
14
+
15
+ interface AbiItem { type: string; name?: string; components?: AbiItem[]; outputs?: AbiItem[] }
16
+ const typeOf = (i: AbiItem): string => (i.type.startsWith("tuple") ? `(${(i.components ?? []).map(typeOf).join(",")})${i.type.slice(5)}` : i.type);
17
+ const officeOfAbi = (FLOOR_AMM_ABI as readonly AbiItem[]).find((x) => x.type === "function" && x.name === "officeOf");
18
+ if (!officeOfAbi?.outputs?.[0]) throw new Error("officeOf is not in the generated ABI");
19
+ const OUT = officeOfAbi.outputs[0];
20
+ const VIEW_TYPE = typeOf(OUT);
21
+ const VIEW_FIELDS = (OUT.components ?? []) as Required<AbiItem>[];
22
+ const POSITION_FIELDS = (VIEW_FIELDS.find((c) => c.name === "positions")?.components ?? []) as Required<AbiItem>[];
23
+
24
+ export interface OfficePositionView { launchId: bigint; buyer: string; tokens: bigint; cost: bigint }
25
+
26
+ export interface OfficeView {
27
+ opened: boolean;
28
+ controller: string;
29
+ pendingController: string;
30
+ quote: string;
31
+ cash: bigint;
32
+ unitsTotal: bigint;
33
+ shareSum: number;
34
+ cap: number;
35
+ mergeable: boolean;
36
+ merged: boolean;
37
+ mergeWith: string;
38
+ mergeController: string;
39
+ mergedFrom: string;
40
+ members: string[];
41
+ positions: OfficePositionView[];
42
+ merging: string;
43
+ }
44
+
45
+ const bool = (v: AbiValue): boolean => v as boolean;
46
+ const big = (v: AbiValue): bigint => v as bigint;
47
+ const str = (v: AbiValue): string => v as string;
48
+ const hex32 = (v: AbiValue): string => (typeof v === "string" ? v : bytesToHex(v as Uint8Array));
49
+
50
+ export function decodeOfficeView(ret: Uint8Array | string): OfficeView {
51
+ const b = typeof ret === "string" ? hexToBytes(ret) : ret;
52
+ const [t] = decodeAbi([VIEW_TYPE], b) as [AbiValue[]];
53
+ if (!Array.isArray(t) || t.length !== VIEW_FIELDS.length) throw new RangeError("officeOf: not an OfficeView");
54
+ if (encodeAbi([VIEW_TYPE], [t]).length !== b.length) throw new RangeError("officeOf: trailing bytes after the OfficeView");
55
+ const f = (name: string): AbiValue => t[VIEW_FIELDS.findIndex((c) => c.name === name)];
56
+ const ps = f("positions") as AbiValue[][];
57
+ const pf = (p: AbiValue[], name: string): AbiValue => p[POSITION_FIELDS.findIndex((c) => c.name === name)];
58
+ return {
59
+ opened: bool(f("opened")),
60
+ controller: str(f("controller")),
61
+ pendingController: str(f("pendingController")),
62
+ quote: str(f("quote")),
63
+ cash: big(f("cash")),
64
+ unitsTotal: big(f("unitsTotal")),
65
+ shareSum: Number(big(f("shareSum"))),
66
+ cap: Number(big(f("cap"))),
67
+ mergeable: bool(f("mergeable")),
68
+ merged: bool(f("merged")),
69
+ mergeWith: hex32(f("mergeWith")),
70
+ mergeController: str(f("mergeController")),
71
+ mergedFrom: hex32(f("mergedFrom")),
72
+ members: (f("members") as AbiValue[]).map(str),
73
+ positions: ps.map((p) => ({ launchId: big(pf(p, "launchId")), buyer: str(pf(p, "buyer")), tokens: big(pf(p, "tokens")), cost: big(pf(p, "cost")) })),
74
+ merging: hex32(f("merging")),
75
+ };
76
+ }
77
+
78
+ export { decodeOfficeMemberOf, decodeOfficeValue };
79
+
80
+ export const OFFICE_HEAD = 960;
81
+ export const OFFICE_MEMBER_SIZE = 64;
82
+ export const OFFICE_POSITION_SIZE = 96;
83
+ export const OFFICE_POSITION_SLOTS = 6;
84
+
85
+ export const MERGE_NONE = 0;
86
+ export const MERGE_KEPT = 1;
87
+ export const MERGE_AWAY = 2;
88
+
89
+ export interface OfficeAccountMember { wallet: string; units: bigint; shareBps: number }
90
+ export interface OfficeAccountPosition { baseMint: string; buyer: string; tokens: bigint; cost: bigint }
91
+
92
+ export interface OfficeAccount {
93
+ officeId: string;
94
+ controller: string;
95
+ pendingController: string;
96
+ quoteMint: string;
97
+ cash: bigint;
98
+ unitsTotal: bigint;
99
+ shareSum: number;
100
+ nMembers: number;
101
+ cap: number;
102
+
103
+ slots: number;
104
+ mergeable: boolean;
105
+ merged: boolean;
106
+ bump: number;
107
+
108
+ mergeRole: number;
109
+ mergeWith: string;
110
+ mergeController: string;
111
+ mergedFrom: string;
112
+
113
+ merging: string;
114
+
115
+ mergeUnitsFrom: bigint;
116
+ mergeValueFrom: bigint;
117
+ mergeUnitsTo: bigint;
118
+ mergeValueTo: bigint;
119
+ mergeUnitsMoved: bigint;
120
+ members: OfficeAccountMember[];
121
+ positions: OfficeAccountPosition[];
122
+ }
123
+
124
+ import { sha256 } from "../sha256.ts";
125
+ const DISC = sha256(new TextEncoder().encode("account:Office")).slice(0, 8);
126
+
127
+ const u16 = (d: DataView, o: number): number => d.getUint16(o, true);
128
+ const u128 = (d: DataView, o: number): bigint => d.getBigUint64(o, true) | (d.getBigUint64(o + 8, true) << 64n);
129
+ const key = (b: Uint8Array, o: number): string => base58Encode(b.subarray(o, o + 32));
130
+ const id32 = (b: Uint8Array, o: number): string => bytesToHex(b.subarray(o, o + 32));
131
+
132
+ export function decodeOffice(data: Uint8Array): OfficeAccount {
133
+ if (data.length < 8 + OFFICE_HEAD || (data.length - 8 - OFFICE_HEAD) % OFFICE_MEMBER_SIZE !== 0) throw new RangeError(`Office: ${data.length} bytes is not 8 + ${OFFICE_HEAD} + ${OFFICE_MEMBER_SIZE} × slots`);
134
+ for (let i = 0; i < 8; i++) if (data[i] !== DISC[i]) throw new RangeError("Office: wrong account discriminator");
135
+ const b = data.subarray(8);
136
+ const d = new DataView(b.buffer, b.byteOffset, b.byteLength);
137
+ const slots = (b.length - OFFICE_HEAD) / OFFICE_MEMBER_SIZE;
138
+ const nMembers = u16(d, 162);
139
+ const nPositions = b[166];
140
+ if (nMembers > slots) throw new RangeError(`Office: ${nMembers} members in ${slots} slots`);
141
+ if (nPositions > OFFICE_POSITION_SLOTS) throw new RangeError(`Office: ${nPositions} positions (at most ${OFFICE_POSITION_SLOTS})`);
142
+ const members: OfficeAccountMember[] = [];
143
+ for (let i = 0; i < nMembers; i++) {
144
+ const o = OFFICE_HEAD + OFFICE_MEMBER_SIZE * i;
145
+ members.push({ wallet: key(b, o), units: u128(d, o + 32), shareBps: u16(d, o + 48) });
146
+ }
147
+ const positions: OfficeAccountPosition[] = [];
148
+ for (let i = 0; i < nPositions; i++) {
149
+ const o = 384 + OFFICE_POSITION_SIZE * i;
150
+ positions.push({ baseMint: key(b, o), buyer: key(b, o + 32), tokens: d.getBigUint64(o + 64, true), cost: u128(d, o + 80) });
151
+ }
152
+ return {
153
+ officeId: id32(b, 0),
154
+ controller: key(b, 32),
155
+ pendingController: key(b, 64),
156
+ quoteMint: key(b, 96),
157
+ cash: u128(d, 128),
158
+ unitsTotal: u128(d, 144),
159
+ shareSum: u16(d, 160),
160
+ nMembers,
161
+ cap: u16(d, 164),
162
+ slots,
163
+ mergeable: b[167] !== 0,
164
+ merged: b[168] !== 0,
165
+ bump: b[169],
166
+ mergeRole: b[170],
167
+ mergeWith: id32(b, 176),
168
+ mergeController: key(b, 208),
169
+ mergedFrom: id32(b, 240),
170
+ merging: id32(b, 272),
171
+ mergeUnitsFrom: u128(d, 304),
172
+ mergeValueFrom: u128(d, 320),
173
+ mergeUnitsTo: u128(d, 336),
174
+ mergeValueTo: u128(d, 352),
175
+ mergeUnitsMoved: u128(d, 368),
176
+ members,
177
+ positions,
178
+ };
179
+ }
180
+
181
+ export interface OfficeMemberRead { wallet: string; units: bigint | null; shareBps: number | null }
182
+
183
+ export interface OfficePositionRead { launch: string; buyer: string; tokens: bigint; cost: bigint }
184
+
185
+ export interface OfficeRead {
186
+ chain: "evm" | "solana";
187
+ opened: boolean;
188
+ controller: string;
189
+ pendingController: string;
190
+ quote: string;
191
+ cash: bigint;
192
+ unitsTotal: bigint;
193
+ shareSum: number;
194
+ cap: number;
195
+ mergeable: boolean;
196
+ merged: boolean;
197
+ mergeWith: string;
198
+ mergeController: string;
199
+ mergedFrom: string;
200
+ merging: string;
201
+ mergeInFlight: boolean;
202
+ members: OfficeMemberRead[];
203
+ positions: OfficePositionRead[];
204
+ }
205
+
206
+ const ZERO32 = "0x" + "00".repeat(32);
207
+ const isZero = (h: string): boolean => /^0x0*$/.test(h);
208
+
209
+ export function officeViewToRead(view: OfficeView, memberRets?: readonly (Uint8Array | string)[]): OfficeRead {
210
+ if (memberRets !== undefined && memberRets.length !== view.members.length) throw new RangeError("memberRets is one return per member");
211
+ return {
212
+ chain: "evm",
213
+ opened: view.opened,
214
+ controller: view.controller,
215
+ pendingController: view.pendingController,
216
+ quote: view.quote,
217
+ cash: view.cash,
218
+ unitsTotal: view.unitsTotal,
219
+ shareSum: view.shareSum,
220
+ cap: view.cap,
221
+ mergeable: view.mergeable,
222
+ merged: view.merged,
223
+ mergeWith: view.mergeWith,
224
+ mergeController: view.mergeController,
225
+ mergedFrom: view.mergedFrom,
226
+ merging: view.merging,
227
+ mergeInFlight: !isZero(view.merging),
228
+ members: view.members.map((wallet, i) => {
229
+ if (memberRets === undefined) return { wallet, units: null, shareBps: null };
230
+ const m = decodeOfficeMemberOf(memberRets[i]);
231
+ return { wallet, units: m.units, shareBps: m.shareBps };
232
+ }),
233
+ positions: view.positions.map((p) => ({ launch: p.launchId.toString(), buyer: p.buyer, tokens: p.tokens, cost: p.cost })),
234
+ };
235
+ }
236
+
237
+ export const readOfficeView = (ret: Uint8Array | string, memberRets?: readonly (Uint8Array | string)[]): OfficeRead => officeViewToRead(decodeOfficeView(ret), memberRets);
238
+
239
+ export function officeAccountToRead(a: OfficeAccount): OfficeRead {
240
+ return {
241
+ chain: "solana",
242
+ opened: a.cap > 0,
243
+ controller: a.controller,
244
+ pendingController: a.pendingController,
245
+ quote: a.quoteMint,
246
+ cash: a.cash,
247
+ unitsTotal: a.unitsTotal,
248
+ shareSum: a.shareSum,
249
+ cap: a.cap,
250
+ mergeable: a.mergeable,
251
+ merged: a.merged,
252
+ mergeWith: a.mergeWith,
253
+ mergeController: a.mergeController,
254
+ mergedFrom: a.mergedFrom,
255
+ merging: a.merging,
256
+ mergeInFlight: !isZero(a.merging),
257
+ members: a.members.map((m) => ({ wallet: m.wallet, units: m.units, shareBps: m.shareBps })),
258
+ positions: a.positions.map((p) => ({ launch: p.baseMint, buyer: p.buyer, tokens: p.tokens, cost: p.cost })),
259
+ };
260
+ }
261
+
262
+ export const readOffice = (data: Uint8Array): OfficeRead => officeAccountToRead(decodeOffice(data));
263
+
264
+ export const OFFICE_ZERO_ID = ZERO32;