@floorlaunchpad/sdk 0.7.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 (105) hide show
  1. package/CHANGELOG.md +143 -0
  2. package/README.md +12 -1
  3. package/dist/abi/evm.gen.js +75 -2
  4. package/dist/abi/functions.gen.js +87 -1
  5. package/dist/abi/solana.gen.js +59 -0
  6. package/dist/client/solana/index.js +25 -6
  7. package/dist/config/generated.js +803 -26374
  8. package/dist/errors.js +20 -1
  9. package/dist/events/types.gen.js +1 -1
  10. package/dist/evm/chains.js +49 -0
  11. package/dist/evm/floor/abi.gen.js +115 -0
  12. package/dist/evm/squid.js +21 -5
  13. package/dist/evm/venues.js +302 -0
  14. package/dist/generated/interface.gen.js +45 -1
  15. package/dist/index.js +7 -0
  16. package/dist/indexer/bodies.js +154 -2
  17. package/dist/indexer/client.js +8 -1
  18. package/dist/indexer/contract.js +78 -11
  19. package/dist/indexer/paths.js +46 -0
  20. package/dist/indexer/read.js +14 -1
  21. package/dist/math/v2/book.js +24 -7
  22. package/dist/math/v2/create.js +1 -1
  23. package/dist/math/v2/escrow.js +205 -0
  24. package/dist/math/v2/office.js +110 -0
  25. package/dist/math/v2/relay.js +263 -96
  26. package/dist/math/v2/team.js +28 -0
  27. package/dist/math/v2/trade.js +3 -1
  28. package/dist/reprice.js +88 -0
  29. package/dist/route/quote.js +16 -0
  30. package/dist/scaledUi.js +113 -0
  31. package/dist/solana/floor/accounts.js +8 -2
  32. package/dist/solana/floor/events.js +56 -1
  33. package/dist/solana/floor/instructions.js +16 -0
  34. package/dist/stockRefresh.js +99 -0
  35. package/dist/ton/consts.generated.js +2 -2
  36. package/dist/ton/gas.js +3 -3
  37. package/dist/v2/claims.js +28 -5
  38. package/dist/v2/devbuy.js +28 -3
  39. package/dist/v2/evm.js +178 -5
  40. package/dist/v2/layouts.js +100 -2
  41. package/dist/v2/office-events.js +127 -0
  42. package/dist/v2/office-evm.js +68 -0
  43. package/dist/v2/office-read.js +182 -0
  44. package/dist/v2/office-solana.js +134 -0
  45. package/dist/v2/office-trade.js +73 -0
  46. package/dist/v2/route-alt.js +1 -0
  47. package/dist/v2/route.js +1 -1
  48. package/dist/v2/solana.js +224 -5
  49. package/dist/v2/solanaSettle.js +2 -0
  50. package/dist/v2/tx/evm.js +51 -1
  51. package/dist/v2/tx/solana.js +3 -3
  52. package/dist/v2/types.js +4 -0
  53. package/docs/aggregator.md +19 -0
  54. package/examples/floor-line.html +1 -1
  55. package/package.json +26 -1
  56. package/src/abi/evm.gen.ts +75 -2
  57. package/src/abi/functions.gen.ts +87 -1
  58. package/src/abi/solana.gen.ts +59 -0
  59. package/src/client/solana/index.ts +25 -6
  60. package/src/config/generated.ts +803 -26374
  61. package/src/errors.ts +20 -1
  62. package/src/events/types.gen.ts +402 -1
  63. package/src/evm/chains.ts +58 -0
  64. package/src/evm/floor/abi.gen.ts +115 -0
  65. package/src/evm/squid.ts +22 -6
  66. package/src/evm/venues.ts +340 -0
  67. package/src/generated/interface.gen.ts +45 -1
  68. package/src/index.ts +7 -0
  69. package/src/indexer/bodies.ts +167 -2
  70. package/src/indexer/client.ts +16 -2
  71. package/src/indexer/contract.ts +84 -12
  72. package/src/indexer/paths.ts +77 -0
  73. package/src/indexer/read.ts +16 -3
  74. package/src/math/v2/book.ts +23 -7
  75. package/src/math/v2/create.ts +1 -1
  76. package/src/math/v2/escrow.ts +213 -0
  77. package/src/math/v2/office.ts +114 -0
  78. package/src/math/v2/relay.ts +269 -96
  79. package/src/math/v2/team.ts +28 -0
  80. package/src/math/v2/trade.ts +3 -1
  81. package/src/reprice.ts +121 -0
  82. package/src/route/quote.ts +16 -0
  83. package/src/scaledUi.ts +123 -0
  84. package/src/solana/floor/accounts.ts +8 -2
  85. package/src/solana/floor/events.ts +57 -2
  86. package/src/solana/floor/instructions.ts +16 -0
  87. package/src/stockRefresh.ts +131 -0
  88. package/src/ton/consts.generated.ts +2 -2
  89. package/src/ton/gas.ts +3 -3
  90. package/src/v2/claims.ts +34 -5
  91. package/src/v2/devbuy.ts +38 -3
  92. package/src/v2/evm.ts +204 -5
  93. package/src/v2/layouts.ts +100 -2
  94. package/src/v2/office-events.ts +133 -0
  95. package/src/v2/office-evm.ts +76 -0
  96. package/src/v2/office-read.ts +264 -0
  97. package/src/v2/office-solana.ts +159 -0
  98. package/src/v2/office-trade.ts +96 -0
  99. package/src/v2/route-alt.ts +1 -0
  100. package/src/v2/route.ts +1 -1
  101. package/src/v2/solana.ts +235 -5
  102. package/src/v2/solanaSettle.ts +2 -0
  103. package/src/v2/tx/evm.ts +51 -1
  104. package/src/v2/tx/solana.ts +5 -3
  105. package/src/v2/types.ts +66 -1
@@ -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;
@@ -0,0 +1,159 @@
1
+
2
+
3
+ import { R, RS, W, WS, instructionDiscriminator } from "../solana/instruction.ts";
4
+ import type { AccountMeta, Instruction } from "../solana/instruction.ts";
5
+ import { Writer } from "../solana/floor/borsh.ts";
6
+ import { ASSOCIATED_TOKEN_PROGRAM_ID, SYSTEM_PROGRAM_ID, TOKEN_2022_PROGRAM_ID, TOKEN_PROGRAM_ID, associatedTokenAddress, findProgramAddress } from "../solana/pda.ts";
7
+ import type { Pda } from "../solana/pda.ts";
8
+ import { FLOOR_AMM_PROGRAM_ID, floorEventAuthorityPda, quoteEntryPda } from "../solana/floor/program.ts";
9
+ import { hexToBytes } from "../evm/abi.ts";
10
+ import type { Ctx } from "./solana.ts";
11
+
12
+ const pid = (c?: Ctx): string => c?.programId ?? FLOOR_AMM_PROGRAM_ID;
13
+
14
+ export const officePda = (officeId: Uint8Array | string, p = FLOOR_AMM_PROGRAM_ID): Pda => findProgramAddress(["office", idBytes(officeId)], p);
15
+
16
+ export const officeQuoteVault = (office: string, quoteMint: string, quoteTokenProgram = TOKEN_PROGRAM_ID): string => associatedTokenAddress(office, quoteMint, quoteTokenProgram);
17
+
18
+ export const officeBaseVault = (office: string, baseMint: string): string => associatedTokenAddress(office, baseMint, TOKEN_2022_PROGRAM_ID);
19
+
20
+ function idBytes(id: Uint8Array | string): Uint8Array {
21
+ const b = typeof id === "string" ? hexToBytes(id) : id;
22
+ if (b.length !== 32) throw new RangeError("office_id is 32 bytes");
23
+ if (b.every((x) => x === 0)) throw new RangeError("office_id is non-zero (OfficeExists)");
24
+ return b;
25
+ }
26
+
27
+ export interface OfficeValuePosition { launch: string; baseMint: string }
28
+
29
+ export interface OfficeLeavePosition extends OfficeValuePosition {
30
+
31
+ memberBaseAta?: string;
32
+
33
+ poolBaseAta?: string;
34
+
35
+ hookExtra: AccountMeta[];
36
+ }
37
+
38
+ export interface OfficeLaunchAccounts extends OfficeValuePosition {
39
+ tokenVault: string;
40
+ quoteVault: string;
41
+ poolBaseAta?: string;
42
+
43
+ hookExtra: AccountMeta[];
44
+ }
45
+ const HOOK_EXTRAS = 6;
46
+ const six = (x: AccountMeta[], what: string): AccountMeta[] => {
47
+ if (x.length !== HOOK_EXTRAS) throw new RangeError(`${what}: the hook takes exactly ${HOOK_EXTRAS} extra accounts`);
48
+ return x;
49
+ };
50
+ const valueBlock = (ps: readonly OfficeValuePosition[] = []): AccountMeta[] => ps.flatMap((l) => [R(l.launch), R(l.baseMint)]);
51
+
52
+ const quoteTail = (q?: AccountMeta[]): AccountMeta[] => (q === undefined ? [] : six(q, "quoteExtra"));
53
+
54
+ function ix(name: string, accounts: AccountMeta[], args: (w: Writer) => Writer, rest: AccountMeta[], c?: Ctx): Instruction {
55
+ const w = args(new Writer().bytes(instructionDiscriminator(name)));
56
+ return { programAddress: pid(c), accounts: [...accounts, R(floorEventAuthorityPda(pid(c))[0]), R(pid(c)), ...rest], data: w.done() };
57
+ }
58
+ const none = (w: Writer): Writer => w;
59
+ const idArg = (id: Uint8Array | string) => (w: Writer): Writer => w.bytes(idBytes(id));
60
+
61
+ export interface OfficeBase extends Ctx {
62
+ officeId: Uint8Array | string;
63
+
64
+ office?: string;
65
+ }
66
+ const officeOf = (p: OfficeBase): string => p.office ?? officePda(p.officeId, pid(p))[0];
67
+ interface QuoteCtx { quoteMint: string; quoteTokenProgram?: string; quoteExtra?: AccountMeta[] }
68
+
69
+ export function officeOpenInstruction(p: OfficeBase & { signer: string; quoteMint: string; quoteTokenProgram?: string }): Instruction {
70
+ const office = officeOf(p);
71
+ const tp = p.quoteTokenProgram ?? TOKEN_PROGRAM_ID;
72
+ return ix("office_open", [WS(p.signer), W(office), R(p.quoteMint), R(quoteEntryPda(p.quoteMint, pid(p))[0]), W(officeQuoteVault(office, p.quoteMint, tp)), R(tp), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)],
73
+ (w) => idArg(p.officeId)(w), [], p);
74
+ }
75
+ export function officeAddMemberInstruction(p: OfficeBase & { controller: string; member: string }): Instruction {
76
+ return ix("office_add_member", [RS(p.controller), W(officeOf(p))], (w) => w.pubkey(p.member), [], p);
77
+ }
78
+ export function officeSetShareInstruction(p: OfficeBase & { controller: string; member: string; shareBps: number }): Instruction {
79
+ if (!Number.isInteger(p.shareBps) || p.shareBps < 0 || p.shareBps > 10_000) throw new RangeError("shareBps is 0..=10000 (ShareSum)");
80
+ return ix("office_set_share", [RS(p.controller), W(officeOf(p))], (w) => w.pubkey(p.member).u16(p.shareBps), [], p);
81
+ }
82
+ function leaveAccounts(signer: string, member: string, office: string, memberQuoteAta: string, q: QuoteCtx): AccountMeta[] {
83
+ const tp = q.quoteTokenProgram ?? TOKEN_PROGRAM_ID;
84
+ return [WS(signer), W(office), R(member), R(q.quoteMint), W(officeQuoteVault(office, q.quoteMint, tp)), W(memberQuoteAta), R(tp), R(TOKEN_2022_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)];
85
+ }
86
+
87
+ function leaveRest(office: string, member: string, positions: readonly OfficeLeavePosition[] = [], q: QuoteCtx): AccountMeta[] {
88
+ return [
89
+ ...positions.flatMap((l) => [R(l.launch), R(l.baseMint), W(l.poolBaseAta ?? officeBaseVault(office, l.baseMint)), W(l.memberBaseAta ?? associatedTokenAddress(member, l.baseMint, TOKEN_2022_PROGRAM_ID)), ...six(l.hookExtra, "hookExtra")]),
90
+ ...quoteTail(q.quoteExtra),
91
+ ];
92
+ }
93
+
94
+ export function officeRemoveMemberInstruction(p: OfficeBase & QuoteCtx & { controller: string; member: string; memberQuoteAta: string; positions?: OfficeLeavePosition[] }): Instruction {
95
+ const office = officeOf(p);
96
+ return ix("office_remove_member", leaveAccounts(p.controller, p.member, office, p.memberQuoteAta, p), (w) => w.pubkey(p.member), leaveRest(office, p.member, p.positions, p), p);
97
+ }
98
+ export function officeLeaveInstruction(p: OfficeBase & QuoteCtx & { member: string; memberQuoteAta: string; positions?: OfficeLeavePosition[] }): Instruction {
99
+ const office = officeOf(p);
100
+ return ix("office_leave", leaveAccounts(p.member, p.member, office, p.memberQuoteAta, p), none, leaveRest(office, p.member, p.positions, p), p);
101
+ }
102
+
103
+ export function officeDepositInstruction(p: OfficeBase & QuoteCtx & { member: string; memberQuoteAta: string; amount: bigint; positions?: OfficeValuePosition[] }): Instruction {
104
+ const office = officeOf(p);
105
+ const tp = p.quoteTokenProgram ?? TOKEN_PROGRAM_ID;
106
+ return ix("office_deposit", [RS(p.member), W(office), R(p.quoteMint), W(p.memberQuoteAta), W(officeQuoteVault(office, p.quoteMint, tp)), R(tp)], (w) => w.u64(p.amount),
107
+ [...valueBlock(p.positions), ...quoteTail(p.quoteExtra)], p);
108
+ }
109
+ function tradeAccounts(signer: string, office: string, l: OfficeLaunchAccounts, q: QuoteCtx): AccountMeta[] {
110
+ const tp = q.quoteTokenProgram ?? TOKEN_PROGRAM_ID;
111
+ return [WS(signer), W(office), W(l.launch), R(l.baseMint), R(q.quoteMint), W(l.tokenVault), W(l.quoteVault), W(l.poolBaseAta ?? officeBaseVault(office, l.baseMint)), W(officeQuoteVault(office, q.quoteMint, tp)),
112
+ R(TOKEN_2022_PROGRAM_ID), R(tp), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)];
113
+ }
114
+
115
+ export function officeBuyInstruction(p: OfficeBase & QuoteCtx & { member: string; launch: OfficeLaunchAccounts; gross: bigint; minTokens: bigint; positions?: OfficeValuePosition[] }): Instruction {
116
+ const office = officeOf(p);
117
+ return ix("office_buy", tradeAccounts(p.member, office, p.launch, p), (w) => w.u64(p.gross).u64(p.minTokens),
118
+ [...valueBlock(p.positions), ...six(p.launch.hookExtra, "hookExtra"), ...quoteTail(p.quoteExtra)], p);
119
+ }
120
+
121
+ export function officeSellInstruction(p: OfficeBase & QuoteCtx & { signer: string; launch: OfficeLaunchAccounts; buyer: string; tokens: bigint; minOut: bigint }): Instruction {
122
+ const office = officeOf(p);
123
+ return ix("office_sell", tradeAccounts(p.signer, office, p.launch, p), (w) => w.pubkey(p.buyer).u64(p.tokens).u64(p.minOut),
124
+ [...six(p.launch.hookExtra, "hookExtra"), ...quoteTail(p.quoteExtra)], p);
125
+ }
126
+
127
+ export function officeProposeControllerInstruction(p: OfficeBase & { controller: string; member?: string }): Instruction {
128
+ return ix("office_propose_controller", [RS(p.controller), W(officeOf(p))], (w) => w.pubkey(p.member ?? SYSTEM_PROGRAM_ID), [], p);
129
+ }
130
+ export function officeAcceptControllerInstruction(p: OfficeBase & { member: string }): Instruction {
131
+ return ix("office_accept_controller", [RS(p.member), W(officeOf(p))], none, [], p);
132
+ }
133
+ export function officeProposeMergeInstruction(p: OfficeBase & { controller: string; otherOfficeId: Uint8Array | string; newController: string }): Instruction {
134
+ return ix("office_propose_merge", [RS(p.controller), W(officeOf(p))], (w) => idArg(p.otherOfficeId)(w).pubkey(p.newController), [], p);
135
+ }
136
+
137
+ export interface OfficeMergePosition { baseMint: string; bAta?: string; aAta?: string; hookExtra: AccountMeta[] }
138
+
139
+ export function officeAcceptMergeInstruction(p: OfficeBase & QuoteCtx & { controller: string; otherOfficeId: Uint8Array | string; newController: string; otherOffice?: string; aPositions?: OfficeValuePosition[]; bPositions?: OfficeMergePosition[]; bLaunches?: OfficeValuePosition[] }): Instruction {
140
+ const b = officeOf(p);
141
+ const a = p.otherOffice ?? officePda(p.otherOfficeId, pid(p))[0];
142
+ const tp = p.quoteTokenProgram ?? TOKEN_PROGRAM_ID;
143
+ const bPos = p.bPositions ?? [];
144
+ const bVal = p.bLaunches ?? [];
145
+ if (bVal.length !== bPos.length) throw new RangeError("bLaunches (B's value block) and bPositions (B's transfer groups) are one per B position");
146
+ return ix("office_accept_merge", [WS(p.controller), W(a), W(b), R(p.quoteMint), W(officeQuoteVault(a, p.quoteMint, tp)), W(officeQuoteVault(b, p.quoteMint, tp)), R(tp), R(TOKEN_2022_PROGRAM_ID), R(ASSOCIATED_TOKEN_PROGRAM_ID), R(SYSTEM_PROGRAM_ID)],
147
+ (w) => idArg(p.otherOfficeId)(w).pubkey(p.newController),
148
+ [...valueBlock(p.aPositions), ...valueBlock(bVal), ...quoteTail(p.quoteExtra),
149
+ ...bPos.flatMap((g) => [R(g.baseMint), W(g.bAta ?? officeBaseVault(b, g.baseMint)), W(g.aAta ?? officeBaseVault(a, g.baseMint)), ...six(g.hookExtra, "hookExtra")])], p);
150
+ }
151
+
152
+ export function officeMergeStepInstruction(p: OfficeBase & { signer: string; otherOfficeId: Uint8Array | string; otherOffice?: string }): Instruction {
153
+ const a = officeOf(p);
154
+ const b = p.otherOffice ?? officePda(p.otherOfficeId, pid(p))[0];
155
+ return ix("office_merge_step", [WS(p.signer), W(a), W(b), R(SYSTEM_PROGRAM_ID)], none, [], p);
156
+ }
157
+ export function officeCancelMergeInstruction(p: OfficeBase & { controller: string }): Instruction {
158
+ return ix("office_cancel_merge", [RS(p.controller), W(officeOf(p))], none, [], p);
159
+ }
@@ -0,0 +1,96 @@
1
+
2
+
3
+ import { addressBytes } from "../codec.ts";
4
+ import { launchPda } from "../solana/floor/program.ts";
5
+ import { TOKEN_2022_PROGRAM_ID } from "../solana/pda.ts";
6
+ import type { AccountMeta, Instruction } from "../solana/instruction.ts";
7
+ import { decodeLaunchV2 } from "./layouts.ts";
8
+ import { mintTlvV2 } from "./quoteMint.ts";
9
+ import { FLOOR_HOOK_PROGRAM_ID, hookAccountsFor } from "./solana.ts";
10
+ import type { Ctx } from "./solana.ts";
11
+ import { officeBuyInstruction, officePda, officeSellInstruction } from "./office-solana.ts";
12
+ import type { OfficeLaunchAccounts, OfficeValuePosition } from "./office-solana.ts";
13
+ import { decodeOffice, officeAccountToRead } from "./office-read.ts";
14
+ import type { OfficeRead } from "./office-read.ts";
15
+
16
+ const EXT_TRANSFER_HOOK = 14;
17
+
18
+ export function isHookedQuoteMint(mintData: Uint8Array, hookProgramId: string = FLOOR_HOOK_PROGRAM_ID): boolean {
19
+ const hook = addressBytes(hookProgramId);
20
+ return mintTlvV2(mintData).some((e) => e.type === EXT_TRANSFER_HOOK && e.value.length >= 64 && e.value.subarray(32, 64).every((b, i) => b === hook[i]));
21
+ }
22
+
23
+ export interface OfficeQuoteSide { quoteMint: string; quoteTokenProgram: string; quoteExtra?: AccountMeta[] }
24
+
25
+ export interface OfficeTradeSetup {
26
+
27
+ launch: string;
28
+ baseMint: string;
29
+ quoteMint: string;
30
+ quoteTokenProgram: string;
31
+
32
+ quoteHooked: boolean;
33
+
34
+ buy: { launch: OfficeLaunchAccounts } & OfficeQuoteSide;
35
+
36
+ sell: { launch: OfficeLaunchAccounts } & OfficeQuoteSide;
37
+ }
38
+
39
+ export function officeLaunchAccounts(p: { launchData: Uint8Array; officeId: Uint8Array | string; office?: string; launch?: string; quoteMintData?: Uint8Array } & Ctx): OfficeTradeSetup {
40
+ const l = decodeLaunchV2(p.launchData);
41
+ const baseMint: string = l.baseMint;
42
+ const quoteMint: string = l.quoteMint;
43
+ const quoteTokenProgram: string = l.quoteTokenProgram;
44
+ const launch = launchPda(baseMint, p.programId)[0];
45
+ if (p.launch !== undefined && p.launch !== launch) throw new RangeError(`launch ${p.launch} is not the launch PDA of base mint ${baseMint} (${launch})`);
46
+ const office = p.office ?? officePda(p.officeId, p.programId)[0];
47
+ const hooked = quoteTokenProgram === TOKEN_2022_PROGRAM_ID && p.quoteMintData !== undefined && isHookedQuoteMint(p.quoteMintData, p.hookProgramId);
48
+ const ctx = { programId: p.programId, hookProgramId: p.hookProgramId };
49
+ const accounts = (isBuy: boolean): OfficeLaunchAccounts => ({
50
+ launch, baseMint, tokenVault: l.tokenVault, quoteVault: l.quoteVault,
51
+ hookExtra: hookAccountsFor({ baseMint, srcOwner: isBuy ? launch : office, dstOwner: isBuy ? office : launch, ...ctx }),
52
+ });
53
+ const side = (isBuy: boolean): OfficeQuoteSide => ({
54
+ quoteMint, quoteTokenProgram,
55
+ ...(hooked ? { quoteExtra: hookAccountsFor({ baseMint: quoteMint, srcOwner: isBuy ? office : launch, dstOwner: isBuy ? launch : office, ...ctx }) } : {}),
56
+ });
57
+ return { launch, baseMint, quoteMint, quoteTokenProgram, quoteHooked: hooked, buy: { launch: accounts(true), ...side(true) }, sell: { launch: accounts(false), ...side(false) } };
58
+ }
59
+
60
+ export interface OfficeTradeRead {
61
+ officeId: string;
62
+ office: OfficeRead;
63
+
64
+ positions: OfficeValuePosition[];
65
+ trade: OfficeTradeSetup;
66
+
67
+ ctx: Ctx;
68
+ }
69
+
70
+ export async function readOfficeTrade(p: { officeId: Uint8Array | string; baseMint: string; fetch: (address: string) => Promise<Uint8Array | null> } & Ctx): Promise<OfficeTradeRead> {
71
+ const ctx = { programId: p.programId, hookProgramId: p.hookProgramId };
72
+ const officeAddr = officePda(p.officeId, p.programId)[0];
73
+ const launchAddr = launchPda(p.baseMint, p.programId)[0];
74
+ const [officeData, launchData] = await Promise.all([p.fetch(officeAddr), p.fetch(launchAddr)]);
75
+ if (!officeData) throw new Error(`Office ${officeAddr} does not exist`);
76
+ const acc = decodeOffice(officeData);
77
+ const office = officeAccountToRead(acc);
78
+ if (!office.opened) throw new Error(`Office ${officeAddr} is not opened (merged away)`);
79
+ if (!launchData) throw new Error(`launch ${launchAddr} does not exist`);
80
+ const l = decodeLaunchV2(launchData);
81
+ if (l.baseMint !== p.baseMint) throw new Error(`launch ${launchAddr} holds base mint ${l.baseMint}, not ${p.baseMint}`);
82
+ if (l.quoteMint !== acc.quoteMint) throw new Error(`launch quote ${l.quoteMint} is not the Office's quote ${acc.quoteMint} (OfficeWrongQuote)`);
83
+ const quoteMintData = l.quoteTokenProgram === TOKEN_2022_PROGRAM_ID ? (await p.fetch(l.quoteMint)) ?? undefined : undefined;
84
+ if (l.quoteTokenProgram === TOKEN_2022_PROGRAM_ID && !quoteMintData) throw new Error(`quote mint ${l.quoteMint} does not exist`);
85
+ const trade = officeLaunchAccounts({ launchData, officeId: p.officeId, office: officeAddr, quoteMintData, ...ctx });
86
+ const positions = office.positions.map((x) => ({ launch: launchPda(x.launch, p.programId)[0], baseMint: x.launch }));
87
+ return { officeId: typeof p.officeId === "string" ? p.officeId : "0x" + [...p.officeId].map((b) => b.toString(16).padStart(2, "0")).join(""), office, positions, trade, ctx };
88
+ }
89
+
90
+ export function officeBuyFromRead(r: OfficeTradeRead, a: { member: string; gross: bigint; minTokens: bigint }): Instruction {
91
+ return officeBuyInstruction({ ...r.ctx, officeId: r.officeId, member: a.member, gross: a.gross, minTokens: a.minTokens, positions: r.positions, ...r.trade.buy });
92
+ }
93
+
94
+ export function officeSellFromRead(r: OfficeTradeRead, a: { signer: string; buyer: string; tokens: bigint; minOut: bigint }): Instruction {
95
+ return officeSellInstruction({ ...r.ctx, officeId: r.officeId, signer: a.signer, buyer: a.buyer, tokens: a.tokens, minOut: a.minOut, ...r.trade.sell });
96
+ }
@@ -28,6 +28,7 @@ function hopForms(trader: string, pool: PoolV2, all: readonly PoolV2[], programI
28
28
  const a = { ...routeTradeAccountsV2(trader, pool, qp, programId), quoteIsLaunch: pool.quoteIsLaunch ?? all.some((x) => coinKey(x.token) === coinKey(pool.quote)), programId };
29
29
  return [
30
30
  buyInstructionV2({ ...a, amountIn: 1n, minOut: 1n }),
31
+ buyInstructionV2({ ...a, amountIn: 1n, minOut: 1n, refVolumeBook: true }),
31
32
  buyFromBalanceInstructionV2({ ...a, keep: 0n, minOut: 1n }),
32
33
  sellInstructionV2({ ...a, tokensIn: 1n, minOut: 1n }),
33
34
  sellFromBalanceInstructionV2({ ...a, keep: 0n, minOut: 1n }),
package/src/v2/route.ts CHANGED
@@ -275,7 +275,7 @@ export function routeTradeAccountsV2(trader: string, pool: PoolV2, quoteTokenPro
275
275
  tokenVault: tokenVault(launch, pool.token), quoteVault: quoteVault(launch, pool.quote, quoteTokenProgram),
276
276
  traderBase: associatedTokenAddress(trader, pool.token, TOKEN_2022_PROGRAM_ID), traderQuote: associatedTokenAddress(trader, pool.quote, quoteTokenProgram),
277
277
  quoteTokenProgram, traderRef, escrow: traderRef ? traderRefEscrow(traderRef, pool.quote, quoteTokenProgram) : null,
278
- user: pool.linked ? trader : null,
278
+ user: pool.linked ? trader : null, refVolumeBook: traderRef ? true : undefined,
279
279
  };
280
280
  }
281
281