@floorlaunchpad/sdk 0.3.2 → 0.4.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 (75) hide show
  1. package/CHANGELOG.md +94 -0
  2. package/README.md +16 -0
  3. package/dist/abi/evm.gen.js +6 -1
  4. package/dist/abi/solana.gen.js +1 -1
  5. package/dist/client/evm/index.js +16 -2
  6. package/dist/client/solana/index.js +31 -14
  7. package/dist/config/generated.js +26864 -402
  8. package/dist/config/registry.gen.js +33 -2
  9. package/dist/evm/chains.js +85 -16
  10. package/dist/evm/floor/abi.gen.js +1 -1
  11. package/dist/evm/floor/events.js +9 -1
  12. package/dist/evm/floor/legacy-events.js +25 -0
  13. package/dist/evm/router.js +25 -1
  14. package/dist/evm/squid.js +9 -9
  15. package/dist/evm/zap.js +148 -35
  16. package/dist/indexer/bodies.js +96 -17
  17. package/dist/indexer/client.js +36 -14
  18. package/dist/indexer/contract.js +178 -28
  19. package/dist/indexer/partners.js +3 -2
  20. package/dist/indexer/paths.js +87 -29
  21. package/dist/indexer/read.js +30 -15
  22. package/dist/indexer/search.js +130 -0
  23. package/dist/indexer/stack-edge.js +35 -0
  24. package/dist/indexer/stream.js +70 -23
  25. package/dist/math/v2/split.js +11 -3
  26. package/dist/solana/floor/events.js +1 -1
  27. package/dist/solana/floor/genesis.js +9 -3
  28. package/dist/solana/floor/instructions.js +14 -5
  29. package/dist/solana/floor/registry.js +4 -0
  30. package/dist/v2/devbuy.js +6 -4
  31. package/dist/v2/presetRowEvm.js +21 -2
  32. package/dist/v2/solana.js +17 -2
  33. package/dist/v2/tx/solana.js +1 -1
  34. package/docs/aggregator.md +22 -0
  35. package/docs/bot.md +44 -1
  36. package/docs/cursor-pages.md +58 -0
  37. package/docs/floor-line.md +9 -4
  38. package/docs/frontend.md +2 -0
  39. package/docs/wallet.md +2 -0
  40. package/examples/floor-line.html +6 -6
  41. package/package.json +1 -1
  42. package/src/abi/evm.gen.ts +6 -1
  43. package/src/abi/solana.gen.ts +1 -1
  44. package/src/client/evm/index.ts +16 -2
  45. package/src/client/solana/index.ts +31 -14
  46. package/src/client/types.ts +1 -1
  47. package/src/config/generated.ts +26864 -402
  48. package/src/config/index.ts +9 -2
  49. package/src/config/registry.gen.ts +33 -2
  50. package/src/events/types.gen.ts +2 -1
  51. package/src/evm/chains.ts +94 -15
  52. package/src/evm/floor/abi.gen.ts +1 -1
  53. package/src/evm/floor/events.ts +11 -1
  54. package/src/evm/floor/legacy-events.ts +27 -0
  55. package/src/evm/router.ts +26 -1
  56. package/src/evm/squid.ts +9 -9
  57. package/src/evm/zap.ts +185 -36
  58. package/src/indexer/bodies.ts +106 -17
  59. package/src/indexer/client.ts +56 -30
  60. package/src/indexer/contract.ts +197 -29
  61. package/src/indexer/partners.ts +3 -2
  62. package/src/indexer/paths.ts +131 -29
  63. package/src/indexer/read.ts +36 -17
  64. package/src/indexer/search.ts +138 -0
  65. package/src/indexer/stack-edge.ts +46 -0
  66. package/src/indexer/stream.ts +78 -24
  67. package/src/math/v2/split.ts +11 -3
  68. package/src/solana/floor/events.ts +1 -1
  69. package/src/solana/floor/genesis.ts +8 -2
  70. package/src/solana/floor/instructions.ts +13 -5
  71. package/src/solana/floor/registry.ts +4 -0
  72. package/src/v2/devbuy.ts +6 -4
  73. package/src/v2/presetRowEvm.ts +21 -2
  74. package/src/v2/solana.ts +20 -3
  75. package/src/v2/tx/solana.ts +1 -1
package/src/evm/zap.ts CHANGED
@@ -5,14 +5,44 @@ import { encodeFunctionData } from "./items.ts";
5
5
 
6
6
  export type ZapFamily = "uniswap-ur" | "v3-router" | "v3-router02" | "algebra" | "v2-router" | "aerodrome-v2";
7
7
 
8
+ export type ZapUrFork = "uniswap" | "aerodrome" | "pancake" | "ramses";
9
+
10
+ export const AERODROME_SLIPSTREAM_KEY = 0x100000;
11
+
8
12
  export const UR_V3_SWAP_EXACT_IN = 0x00;
9
13
  export const UR_V2_SWAP_EXACT_IN = 0x08;
10
14
  export const UR_WRAP_ETH = 0x0b;
11
15
  export const UR_UNWRAP_WETH = 0x0c;
12
16
  export const UR_SWEEP = 0x04;
17
+ export const UR_V4_SWAP = 0x10;
13
18
  export const UR_MSG_SENDER = "0x0000000000000000000000000000000000000001";
14
19
  export const UR_ADDRESS_THIS = "0x0000000000000000000000000000000000000002";
15
20
 
21
+ export const V4_SWAP_EXACT_IN_SINGLE = 0x06;
22
+ export const V4_SETTLE = 0x0b;
23
+ export const V4_TAKE = 0x0e;
24
+ export const V4_OPEN_DELTA = 0n;
25
+ export const V4_NATIVE = "0x0000000000000000000000000000000000000000";
26
+
27
+ export interface ZapV4Pool {
28
+
29
+ fee: number;
30
+
31
+ tickSpacing: number;
32
+
33
+ hooks?: string;
34
+
35
+ hookData?: string;
36
+ }
37
+
38
+ export interface V4PoolKey {
39
+ currency0: string;
40
+ currency1: string;
41
+ fee: number;
42
+ tickSpacing: number;
43
+ hooks: string;
44
+ }
45
+
16
46
  export interface ZapRoute {
17
47
 
18
48
  tokens: readonly string[];
@@ -21,6 +51,8 @@ export interface ZapRoute {
21
51
 
22
52
  stable?: readonly boolean[];
23
53
  factory?: string;
54
+
55
+ v4?: ZapV4Pool;
24
56
  }
25
57
 
26
58
  export interface ZapSwapParams {
@@ -33,6 +65,14 @@ export interface ZapSwapParams {
33
65
 
34
66
  zap: string;
35
67
  deadline: bigint;
68
+
69
+ input?: "native" | "token";
70
+
71
+ output?: "token" | "native";
72
+
73
+ router?: string;
74
+
75
+ urFork?: ZapUrFork;
36
76
  }
37
77
 
38
78
  const fn = (name: string, inputs: readonly { name: string; type: string; components?: any }[]) => ({ type: "function", name, inputs, outputs: [], stateMutability: "payable" }) as const;
@@ -57,34 +97,136 @@ export function algebraPath(tokens: readonly string[]): string {
57
97
  return "0x" + tokens.map((t) => bytesToHex(hexToBytes(t)).slice(2)).join("");
58
98
  }
59
99
 
60
- const checkRoute = (p: ZapSwapParams): void => {
100
+ const UR_FORKS: readonly ZapUrFork[] = ["uniswap", "aerodrome", "pancake", "ramses"];
101
+
102
+ interface Ends {
103
+ nativeIn: boolean;
104
+ nativeOut: boolean;
105
+ }
106
+
107
+ const isNative = (a: string): boolean => BigInt(checksumAddress(a)) === 0n;
108
+
109
+ const checkRoute = (p: ZapSwapParams): Ends => {
61
110
  if (p.amountIn <= 0n) throw new RangeError("amountIn must be > 0");
62
111
  if (p.minQuoteOut <= 0n) throw new RangeError("minQuoteOut must be > 0 (the NO_MINIMUM rule)");
63
- if (p.route.tokens.length < 2) throw new RangeError("a route is at least two tokens");
112
+ const { tokens } = p.route;
113
+ if (tokens.length < 2) throw new RangeError("a route is at least two tokens");
114
+ if (p.input !== undefined && p.input !== "native" && p.input !== "token") throw new RangeError(`input ${String(p.input)} is "native" or "token"`);
115
+ if (p.output !== undefined && p.output !== "native" && p.output !== "token") throw new RangeError(`output ${String(p.output)} is "native" or "token"`);
116
+ if (p.urFork !== undefined && (p.family !== "uniswap-ur" || !UR_FORKS.includes(p.urFork))) {
117
+ throw new RangeError(`urFork ${String(p.urFork)} names a uniswap-ur row's router: ${UR_FORKS.join(", ")}`);
118
+ }
119
+ const [first, last] = [tokens[0], tokens[tokens.length - 1]];
120
+ if (BigInt(checksumAddress(first)) === BigInt(checksumAddress(last))) throw new RangeError("a route swaps a coin into itself");
121
+ if (p.route.v4) {
122
+ if (p.family !== "uniswap-ur") throw new RangeError("a v4 route goes through the Universal Router (uniswap-ur)");
123
+ if ((p.urFork ?? "uniswap") !== "uniswap") throw new RangeError("a v4 route goes through Uniswap's own Universal Router");
124
+ const ends = { nativeIn: isNative(first), nativeOut: isNative(last) };
125
+ if (p.input !== undefined && (p.input === "native") !== ends.nativeIn) throw new RangeError(`input "${p.input}" is not the v4 route's first currency`);
126
+ if (p.output !== undefined && (p.output === "native") !== ends.nativeOut) throw new RangeError(`output "${p.output}" is not the v4 route's last currency`);
127
+ return ends;
128
+ }
129
+ if (tokens.some(isNative)) throw new RangeError("a V2 / V3 route names the wrapped native, never address(0) (only a v4 route holds the native coin)");
130
+ const ends = { nativeIn: (p.input ?? "native") === "native", nativeOut: (p.output ?? "token") === "native" };
131
+ if (ends.nativeIn && ends.nativeOut) throw new RangeError("a route swaps the native coin into itself");
132
+ return ends;
64
133
  };
65
134
 
66
- function universalRouter(p: ZapSwapParams): string {
67
- const wrap = bytesToHex(encodeAbi(["address", "uint256"], [UR_ADDRESS_THIS, p.amountIn]));
68
- let cmd: number;
69
- let swap: string;
135
+ export function v4PoolKey(tokenIn: string, tokenOut: string, pool: ZapV4Pool): { key: V4PoolKey; zeroForOne: boolean } {
136
+ const a = checksumAddress(tokenIn);
137
+ const b = checksumAddress(tokenOut);
138
+ if (BigInt(a) === BigInt(b)) throw new RangeError("a v4 swap is between two different currencies");
139
+ if (!Number.isInteger(pool.fee) || pool.fee < 0 || pool.fee >= 1 << 24) throw new RangeError(`fee ${pool.fee} is not a uint24`);
140
+ if (!Number.isInteger(pool.tickSpacing) || pool.tickSpacing < 1 || pool.tickSpacing > 32767) throw new RangeError(`tick spacing ${pool.tickSpacing} is not 1 ..= 32767`);
141
+ const zeroForOne = BigInt(a) < BigInt(b);
142
+ const hooks = checksumAddress(pool.hooks ?? V4_NATIVE);
143
+ return { key: { currency0: zeroForOne ? a : b, currency1: zeroForOne ? b : a, fee: pool.fee, tickSpacing: pool.tickSpacing, hooks }, zeroForOne };
144
+ }
145
+
146
+ const UINT128 = 1n << 128n;
147
+ const UR_EXECUTE = [fn("execute", [{ name: "commands", type: "bytes" }, { name: "inputs", type: "bytes[]" }, { name: "deadline", type: "uint256" }])];
148
+ const cmds = (cs: readonly number[]): string => "0x" + cs.map((c) => c.toString(16).padStart(2, "0")).join("");
149
+
150
+ function universalRouterV4(p: ZapSwapParams, pool: ZapV4Pool): string {
151
+ const { tokens } = p.route;
152
+ if (tokens.length !== 2) throw new RangeError("a v4 route is one pool: [currency in, currency out]");
153
+ if (p.route.fees || p.route.stable || p.route.factory) throw new RangeError("a v4 route names its pool in `v4` only");
154
+ if (p.amountIn >= UINT128 || p.minQuoteOut >= UINT128) throw new RangeError("a v4 amount is a uint128");
155
+ const [tokenIn, tokenOut] = tokens;
156
+ const { key, zeroForOne } = v4PoolKey(tokenIn, tokenOut, pool);
157
+ const nativeOut = isNative(tokenOut);
158
+ const swap = encodeAbi(
159
+ ["((address,address,uint24,int24,address),bool,uint128,uint128,uint256,bytes)"],
160
+ [[[key.currency0, key.currency1, BigInt(key.fee), BigInt(key.tickSpacing), key.hooks], zeroForOne, p.amountIn, p.minQuoteOut, 0n, pool.hookData ?? "0x"]],
161
+ );
162
+ const settle = encodeAbi(["address", "uint256", "bool"], [checksumAddress(tokenIn), V4_OPEN_DELTA, false]);
163
+ const take = encodeAbi(["address", "address", "uint256"], [checksumAddress(tokenOut), nativeOut ? UR_ADDRESS_THIS : UR_MSG_SENDER, V4_OPEN_DELTA]);
164
+ const v4 = bytesToHex(encodeAbi(["bytes", "bytes[]"], [cmds([V4_SWAP_EXACT_IN_SINGLE, V4_SETTLE, V4_TAKE]), [swap, settle, take]]));
165
+
166
+ const nativeIn = isNative(tokenIn);
167
+ const sweep = (min: bigint): string => bytesToHex(encodeAbi(["address", "address", "uint256"], [V4_NATIVE, UR_MSG_SENDER, min]));
168
+ const inputs = nativeOut ? [v4, sweep(p.minQuoteOut)] : nativeIn ? [v4, sweep(0n)] : [v4];
169
+ return encodeFunctionData(UR_EXECUTE, "execute", { commands: cmds(nativeOut ? [UR_V4_SWAP, UR_SWEEP] : nativeIn ? [UR_V4_SWAP, UR_SWEEP] : [UR_V4_SWAP]), inputs, deadline: p.deadline });
170
+ }
171
+
172
+ const UR_SWAP_HEAD = ["address", "uint256", "uint256", "bytes", "bool"];
173
+
174
+ function aerodromeKey(k: number): number {
175
+ if (!Number.isInteger(k) || k < 1 || k >= 1 << 24) throw new RangeError(`Aerodrome key ${k} is not a tick spacing`);
176
+ if (k < AERODROME_SLIPSTREAM_KEY) return AERODROME_SLIPSTREAM_KEY | k;
177
+ if (k >>> 20 !== 1) throw new RangeError(`Aerodrome key ${k} names another factory than Slipstream's (0x100000 | tickSpacing)`);
178
+ return k;
179
+ }
180
+
181
+ function universalRouter(p: ZapSwapParams, e: Ends): string {
182
+ const fork = p.urFork ?? "uniswap";
183
+ const to = e.nativeOut ? UR_ADDRESS_THIS : UR_MSG_SENDER;
184
+ const commands: number[] = [];
185
+ const inputs: string[] = [];
186
+ if (e.nativeIn) {
187
+ commands.push(UR_WRAP_ETH);
188
+ inputs.push(bytesToHex(encodeAbi(["address", "uint256"], [UR_ADDRESS_THIS, p.amountIn])));
189
+ }
70
190
  if (p.route.fees) {
71
- cmd = UR_V3_SWAP_EXACT_IN;
72
- swap = bytesToHex(encodeAbi(["address", "uint256", "uint256", "bytes", "bool", "uint256[]"], [UR_MSG_SENDER, p.amountIn, p.minQuoteOut, v3Path(p.route.tokens, p.route.fees), false, []]));
191
+ const keys = fork === "aerodrome" ? p.route.fees.map(aerodromeKey) : p.route.fees;
192
+ const head = [to, p.amountIn, p.minQuoteOut, v3Path(p.route.tokens, keys), false];
193
+ const swap = fork === "uniswap" ? encodeAbi([...UR_SWAP_HEAD, "uint256[]"], [...head, []])
194
+ : fork === "aerodrome" ? encodeAbi([...UR_SWAP_HEAD, "bool"], [...head, false])
195
+ : encodeAbi(UR_SWAP_HEAD, head);
196
+ commands.push(UR_V3_SWAP_EXACT_IN);
197
+ inputs.push(bytesToHex(swap));
73
198
  } else {
74
- cmd = UR_V2_SWAP_EXACT_IN;
75
- swap = bytesToHex(encodeAbi(["address", "uint256", "uint256", "address[]", "bool", "uint256[]"], [UR_MSG_SENDER, p.amountIn, p.minQuoteOut, [...p.route.tokens], false, []]));
199
+ if (fork !== "uniswap") throw new RangeError(`a V2 leg through ${fork}'s Universal Router is not spelled (its V2_SWAP_EXACT_IN input is its own)`);
200
+ commands.push(UR_V2_SWAP_EXACT_IN);
201
+ inputs.push(bytesToHex(encodeAbi(["address", "uint256", "uint256", "address[]", "bool", "uint256[]"], [to, p.amountIn, p.minQuoteOut, [...p.route.tokens], false, []])));
202
+ }
203
+ if (e.nativeOut) {
204
+ commands.push(UR_UNWRAP_WETH);
205
+ inputs.push(bytesToHex(encodeAbi(["address", "uint256"], [UR_MSG_SENDER, p.minQuoteOut])));
76
206
  }
77
- const commands = "0x" + [UR_WRAP_ETH, cmd].map((c) => c.toString(16).padStart(2, "0")).join("");
78
- const abi = [fn("execute", [{ name: "commands", type: "bytes" }, { name: "inputs", type: "bytes[]" }, { name: "deadline", type: "uint256" }])];
79
- return encodeFunctionData(abi, "execute", { commands, inputs: [wrap, swap], deadline: p.deadline });
207
+ if (e.nativeIn) {
208
+
209
+ commands.push(UR_SWEEP);
210
+ inputs.push(bytesToHex(encodeAbi(["address", "address", "uint256"], [V4_NATIVE, UR_MSG_SENDER, 0n])));
211
+ }
212
+ return encodeFunctionData(UR_EXECUTE, "execute", { commands: cmds(commands), inputs, deadline: p.deadline });
80
213
  }
81
214
 
215
+ const ADDRESS_ZERO = "0x0000000000000000000000000000000000000000";
82
216
  const REFUND_ETH = [fn("refundETH", [])];
83
217
  const REFUND_NATIVE = [fn("refundNativeToken", [])];
218
+ const UNWRAP = (name: string) => [fn(name, [{ name: "amountMinimum", type: "uint256" }, { name: "recipient", type: "address" }])];
84
219
 
85
- function v3Router(p: ZapSwapParams, v02: boolean): string {
220
+ const peripheryTail = (p: ZapSwapParams, e: Ends, refund: string, unwrap: string): string[] =>
221
+ e.nativeIn ? [encodeFunctionData(refund === "refundETH" ? REFUND_ETH : REFUND_NATIVE, refund)]
222
+ : e.nativeOut ? [encodeFunctionData(UNWRAP(unwrap), unwrap, { amountMinimum: p.minQuoteOut, recipient: p.zap })]
223
+ : [];
224
+
225
+ function v3Router(p: ZapSwapParams, v02: boolean, e: Ends): string {
86
226
  const fees = p.route.fees;
87
227
  if (!fees) throw new RangeError("a V3 router route needs its fees");
228
+ if (e.nativeOut && !v02 && !p.router) throw new RangeError("a SwapRouter v1 native out is paid to the router, then unwrapped: pass the zap's `router`");
229
+ const recipient = !e.nativeOut ? p.zap : v02 ? UR_ADDRESS_THIS : checksumAddress(p.router as string);
88
230
  const [tokenIn, tokenOut] = [p.route.tokens[0], p.route.tokens[p.route.tokens.length - 1]];
89
231
  let swap: string;
90
232
  if (p.route.tokens.length === 2) {
@@ -92,61 +234,68 @@ function v3Router(p: ZapSwapParams, v02: boolean): string {
92
234
  ? [["tokenIn", "address"], ["tokenOut", "address"], ["fee", "uint24"], ["recipient", "address"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"], ["sqrtPriceLimitX96", "uint160"]]
93
235
  : [["tokenIn", "address"], ["tokenOut", "address"], ["fee", "uint24"], ["recipient", "address"], ["deadline", "uint256"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"], ["sqrtPriceLimitX96", "uint160"]];
94
236
  const abi = [fn("exactInputSingle", [{ name: "params", type: "tuple", components: tup(fields) }])];
95
- swap = encodeFunctionData(abi, "exactInputSingle", { params: { tokenIn, tokenOut, fee: BigInt(fees[0]), recipient: p.zap, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut, sqrtPriceLimitX96: 0n } });
237
+ swap = encodeFunctionData(abi, "exactInputSingle", { params: { tokenIn, tokenOut, fee: BigInt(fees[0]), recipient, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut, sqrtPriceLimitX96: 0n } });
96
238
  } else {
97
239
  const fields: [string, string][] = v02
98
240
  ? [["path", "bytes"], ["recipient", "address"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"]]
99
241
  : [["path", "bytes"], ["recipient", "address"], ["deadline", "uint256"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"]];
100
242
  const abi = [fn("exactInput", [{ name: "params", type: "tuple", components: tup(fields) }])];
101
- swap = encodeFunctionData(abi, "exactInput", { params: { path: v3Path(p.route.tokens, fees), recipient: p.zap, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut } });
243
+ swap = encodeFunctionData(abi, "exactInput", { params: { path: v3Path(p.route.tokens, fees), recipient, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut } });
102
244
  }
103
- const refund = encodeFunctionData(REFUND_ETH, "refundETH");
104
- if (v02) return encodeFunctionData([fn("multicall", [{ name: "deadline", type: "uint256" }, { name: "data", type: "bytes[]" }])], "multicall", { deadline: p.deadline, data: [swap, refund] });
105
- return encodeFunctionData([fn("multicall", [{ name: "data", type: "bytes[]" }])], "multicall", { data: [swap, refund] });
245
+ const data = [swap, ...peripheryTail(p, e, "refundETH", "unwrapWETH9")];
246
+ if (v02) return encodeFunctionData([fn("multicall", [{ name: "deadline", type: "uint256" }, { name: "data", type: "bytes[]" }])], "multicall", { deadline: p.deadline, data });
247
+ return encodeFunctionData([fn("multicall", [{ name: "data", type: "bytes[]" }])], "multicall", { data });
106
248
  }
107
249
 
108
- function algebra(p: ZapSwapParams): string {
250
+ function algebra(p: ZapSwapParams, e: Ends): string {
251
+ const recipient = e.nativeOut ? ADDRESS_ZERO : p.zap;
109
252
  const [tokenIn, tokenOut] = [p.route.tokens[0], p.route.tokens[p.route.tokens.length - 1]];
110
253
  let swap: string;
111
254
  if (p.route.tokens.length === 2) {
112
255
  const fields: [string, string][] = [["tokenIn", "address"], ["tokenOut", "address"], ["recipient", "address"], ["deadline", "uint256"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"], ["limitSqrtPrice", "uint160"]];
113
- swap = encodeFunctionData([fn("exactInputSingle", [{ name: "params", type: "tuple", components: tup(fields) }])], "exactInputSingle", { params: { tokenIn, tokenOut, recipient: p.zap, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut, limitSqrtPrice: 0n } });
256
+ swap = encodeFunctionData([fn("exactInputSingle", [{ name: "params", type: "tuple", components: tup(fields) }])], "exactInputSingle", { params: { tokenIn, tokenOut, recipient, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut, limitSqrtPrice: 0n } });
114
257
  } else {
115
258
  const fields: [string, string][] = [["path", "bytes"], ["recipient", "address"], ["deadline", "uint256"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"]];
116
- swap = encodeFunctionData([fn("exactInput", [{ name: "params", type: "tuple", components: tup(fields) }])], "exactInput", { params: { path: algebraPath(p.route.tokens), recipient: p.zap, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut } });
259
+ swap = encodeFunctionData([fn("exactInput", [{ name: "params", type: "tuple", components: tup(fields) }])], "exactInput", { params: { path: algebraPath(p.route.tokens), recipient, deadline: p.deadline, amountIn: p.amountIn, amountOutMinimum: p.minQuoteOut } });
117
260
  }
118
- return encodeFunctionData([fn("multicall", [{ name: "data", type: "bytes[]" }])], "multicall", { data: [swap, encodeFunctionData(REFUND_NATIVE, "refundNativeToken")] });
261
+ return encodeFunctionData([fn("multicall", [{ name: "data", type: "bytes[]" }])], "multicall", { data: [swap, ...peripheryTail(p, e, "refundNativeToken", "unwrapWNativeToken")] });
119
262
  }
120
263
 
121
- function v2Router(p: ZapSwapParams): string {
122
- const abi = [fn("swapExactETHForTokens", tup([["amountOutMin", "uint256"], ["path", "address[]"], ["to", "address"], ["deadline", "uint256"]]))];
123
- return encodeFunctionData(abi, "swapExactETHForTokens", { amountOutMin: p.minQuoteOut, path: [...p.route.tokens], to: p.zap, deadline: p.deadline });
264
+ const v2Name = (e: Ends): string => (e.nativeIn ? "swapExactETHForTokens" : e.nativeOut ? "swapExactTokensForETH" : "swapExactTokensForTokens");
265
+
266
+ function v2Router(p: ZapSwapParams, e: Ends): string {
267
+ const name = v2Name(e);
268
+ const head: [string, string][] = e.nativeIn ? [] : [["amountIn", "uint256"]];
269
+ const abi = [fn(name, tup([...head, ["amountOutMin", "uint256"], ["path", "address[]"], ["to", "address"], ["deadline", "uint256"]]))];
270
+ return encodeFunctionData(abi, name, { amountIn: p.amountIn, amountOutMin: p.minQuoteOut, path: [...p.route.tokens], to: p.zap, deadline: p.deadline });
124
271
  }
125
272
 
126
- function aerodromeV2(p: ZapSwapParams): string {
273
+ function aerodromeV2(p: ZapSwapParams, e: Ends): string {
127
274
  const { tokens, stable, factory } = p.route;
128
275
  if (!stable || stable.length !== tokens.length - 1 || !factory) throw new RangeError("an Aerodrome route needs a stable flag per hop and the factory");
129
276
  const route = { name: "routes", type: "tuple[]", components: tup([["from", "address"], ["to", "address"], ["stable", "bool"], ["factory", "address"]]) };
130
- const abi = [fn("swapExactETHForTokens", [{ name: "amountOutMin", type: "uint256" }, route, { name: "to", type: "address" }, { name: "deadline", type: "uint256" }])];
277
+ const name = v2Name(e);
278
+ const head = e.nativeIn ? [] : [{ name: "amountIn", type: "uint256" }];
279
+ const abi = [fn(name, [...head, { name: "amountOutMin", type: "uint256" }, route, { name: "to", type: "address" }, { name: "deadline", type: "uint256" }])];
131
280
  const routes = stable.map((s, i) => ({ from: tokens[i], to: tokens[i + 1], stable: s, factory }));
132
- return encodeFunctionData(abi, "swapExactETHForTokens", { amountOutMin: p.minQuoteOut, routes, to: p.zap, deadline: p.deadline });
281
+ return encodeFunctionData(abi, name, { amountIn: p.amountIn, amountOutMin: p.minQuoteOut, routes, to: p.zap, deadline: p.deadline });
133
282
  }
134
283
 
135
284
  export function zapSwapData(p: ZapSwapParams): string {
136
- checkRoute(p);
285
+ const e = checkRoute(p);
137
286
  checksumAddress(p.zap);
138
287
  switch (p.family) {
139
288
  case "uniswap-ur":
140
- return universalRouter(p);
289
+ return p.route.v4 ? universalRouterV4(p, p.route.v4) : universalRouter(p, e);
141
290
  case "v3-router":
142
- return v3Router(p, false);
291
+ return v3Router(p, false, e);
143
292
  case "v3-router02":
144
- return v3Router(p, true);
293
+ return v3Router(p, true, e);
145
294
  case "algebra":
146
- return algebra(p);
295
+ return algebra(p, e);
147
296
  case "v2-router":
148
- return v2Router(p);
297
+ return v2Router(p, e);
149
298
  case "aerodrome-v2":
150
- return aerodromeV2(p);
299
+ return aerodromeV2(p, e);
151
300
  }
152
301
  }
@@ -1,6 +1,6 @@
1
1
 
2
2
 
3
- import { array, big, bool, decimal, int, nullable, object, oneOf, optional, record, str } from "./decode.ts";
3
+ import { ShapeError, array, big, bool, decimal, int, nullable, object, oneOf, optional, record, str } from "./decode.ts";
4
4
  import type { Decoder, Infer } from "./decode.ts";
5
5
  import type { DevBuy, DevBuyRole } from "../v2/devbuy.ts";
6
6
 
@@ -30,6 +30,7 @@ export const DEV_BUY = object({
30
30
  block: int,
31
31
  timestamp: int,
32
32
  tx: str,
33
+ id: str,
33
34
  });
34
35
 
35
36
  export type DevBuyItem = Infer<typeof DEV_BUY>;
@@ -103,6 +104,12 @@ const LAUNCH_FIELDS = {
103
104
  unlisted: bool,
104
105
  quoteDecimals: nullable(int),
105
106
  teamSlotRateBps: nullable(int),
107
+
108
+ nextBuyPriceX64: optional(nullable(big)),
109
+ stackEndX64: optional(nullable(big)),
110
+ stackLeft: optional(nullable(big)),
111
+ stackSold: optional(nullable(big)),
112
+ stackSoldBps: optional(nullable(int)),
106
113
  bidTopX64: nullable(big),
107
114
  teamBuyUnlockAt: nullable(int),
108
115
  teamBuyUnlocked: bool,
@@ -123,19 +130,25 @@ const LAUNCH_FIELDS = {
123
130
  export const LAUNCH = object(LAUNCH_FIELDS);
124
131
  export type Launch = Infer<typeof LAUNCH>;
125
132
 
126
- export const LISTED_LAUNCH = object({
133
+ const LISTED_FIELDS = {
127
134
  ...LAUNCH_FIELDS,
128
135
  volume24hGross: nullable(big),
129
136
  holders: int,
137
+
138
+ maxLossPpm: optional(nullable(int)),
139
+
130
140
  backedPpm: nullable(int),
131
141
  volume24hUsd: nullable(decimal),
132
142
  marketCapUsd: nullable(decimal),
133
143
 
134
144
  trendingScore: nullable(big),
135
- });
145
+
146
+ change24hBps: optional(nullable(int)),
147
+ };
148
+ export const LISTED_LAUNCH = object(LISTED_FIELDS);
136
149
  export type ListedLaunch = Infer<typeof LISTED_LAUNCH>;
137
150
 
138
- export const LIVE_LAUNCH = object({ ...LAUNCH_FIELDS, volume24hGross: nullable(big), holders: int });
151
+ export const LIVE_LAUNCH = object({ ...LAUNCH_FIELDS, volume24hGross: nullable(big), holders: int, change24hBps: optional(nullable(int)) });
139
152
  export type LiveLaunch = Infer<typeof LIVE_LAUNCH>;
140
153
 
141
154
  const TRADE_FIELDS = {
@@ -226,6 +239,8 @@ export const FLOOR_ROUTED = object({
226
239
  indexed,
227
240
  launches: array(object({ launch: str, quote: str, floorFee: big, floorMilestone: big })),
228
241
  pending: array(object({ quote: str, amount: big })),
242
+
243
+ next: optional(nullable(str)),
229
244
  });
230
245
  export type FloorRouted = Infer<typeof FLOOR_ROUTED>;
231
246
 
@@ -249,11 +264,29 @@ export const EVENTS = object({
249
264
  });
250
265
  export type Events = Infer<typeof EVENTS>;
251
266
 
252
- export const LAUNCHES_ALL = object({ indexed, launches: array(LISTED_LAUNCH) });
267
+ const nextCursor: Decoder<string | null> = (v, p) => (v === undefined || v === null ? null : str(v, p));
268
+
269
+ const perRow = <T>(d: Decoder<T>): Decoder<T[]> => (v, p) => (v === undefined ? [] : array(d)(v, p));
253
270
 
254
- export const LAUNCHES = object({ indexed, total: int, offset: int, launches: array(LISTED_LAUNCH), missing: array(str) });
271
+ export const LAUNCHES = object({
272
+ indexed, total: int, offset: int, launches: array(LISTED_LAUNCH), missing: array(str),
273
+ cursors: perRow(str), keys: perRow(nullable(str)), next: optional(nullable(str)),
274
+ });
255
275
  export type Launches = Infer<typeof LAUNCHES>;
256
276
 
277
+ export const LAUNCH_LIST = object({ quote: str, total: int, launches: array(LISTED_LAUNCH), cursors: array(str), keys: array(nullable(str)), next: nullable(str) });
278
+ export const LAUNCH_LISTS = object({ indexed, lists: array(LAUNCH_LIST) });
279
+ export type LaunchLists = Infer<typeof LAUNCH_LISTS>;
280
+
281
+ const SEARCH_SERVED_TIER: Decoder<0 | 1 | 2 | 3 | 4> = (v, p) => {
282
+ const n = int(v, p);
283
+ if (n < 0 || n > 4) throw new ShapeError(p, "a tier 0..4", v);
284
+ return n as 0 | 1 | 2 | 3 | 4;
285
+ };
286
+
287
+ export const LAUNCH_SEARCH = object({ indexed, q: str, launches: array(object({ ...LISTED_FIELDS, tier: SEARCH_SERVED_TIER })), next: nextCursor });
288
+ export type LaunchSearch = Infer<typeof LAUNCH_SEARCH>;
289
+
257
290
  export const LAUNCH_ONE = object({ indexed, launch: LISTED_LAUNCH });
258
291
 
259
292
  export const LAUNCH_STATE = object({ launch: str, mint: str, block: nullable(int), ts: nullable(int), seq: int, state: LIVE_LAUNCH });
@@ -268,16 +301,17 @@ export type Holders = Infer<typeof HOLDERS>;
268
301
  export const TRADES = object({ indexed, trades: array(PAGED_TRADE), next: nullable(str) });
269
302
  export type Trades = Infer<typeof TRADES>;
270
303
 
271
- export const LAUNCH_CLAIM = object({ launch: str, claimer: str, types: int, quote: str, amount: big, holder: big, teamBuy: optional(big), block: int, ts: int, tx: str });
304
+ export const LAUNCH_CLAIM = object({ launch: str, claimer: str, types: int, quote: str, amount: big, holder: big, teamBuy: optional(big), block: int, ts: int, tx: str, id: optional(str) });
272
305
  export type LaunchClaim = Infer<typeof LAUNCH_CLAIM>;
273
- export const LAUNCH_CLAIMS = object({ indexed, claims: array(LAUNCH_CLAIM) });
306
+ export const LAUNCH_CLAIMS = object({ indexed, claims: array(LAUNCH_CLAIM), next: optional(nullable(str)) });
307
+ export type LaunchClaims = Infer<typeof LAUNCH_CLAIMS>;
274
308
 
275
- export const DEV_BUYS = object({ indexed, teamBuyUnlocked: bool, teamBuyUnlockedTs: nullable(int), devBuys: array(DEV_BUY) });
309
+ export const DEV_BUYS = object({ indexed, teamBuyUnlocked: bool, teamBuyUnlockedTs: nullable(int), devBuys: array(DEV_BUY), next: optional(nullable(str)) });
276
310
  export type DevBuys = Infer<typeof DEV_BUYS>;
277
311
 
278
312
  export const MEMBER_SOLD_ROW = object({ launch: str, member: str, place: int, amount: big, quoteOut: big, block: int, timestamp: int, tx: str, id: str });
279
313
  export type MemberSoldRow = Infer<typeof MEMBER_SOLD_ROW>;
280
- export const MEMBER_SOLD = object({ indexed, memberSold: array(MEMBER_SOLD_ROW) });
314
+ export const MEMBER_SOLD = object({ indexed, memberSold: array(MEMBER_SOLD_ROW), next: optional(nullable(str)) });
281
315
  export type MemberSoldBody = Infer<typeof MEMBER_SOLD>;
282
316
 
283
317
  export const LAUNCH_ACCOUNT = object({ indexed, launch: str, mint: str, slot: int, data: str });
@@ -313,6 +347,8 @@ export const CANDLE = object({
313
347
  volumeSellQuoteRaw: big,
314
348
  buys: int,
315
349
  sells: int,
350
+
351
+ floorX64: optional(nullable(big)),
316
352
  });
317
353
  export type Candle = Infer<typeof CANDLE>;
318
354
 
@@ -333,6 +369,9 @@ export const CANDLES = object({
333
369
  });
334
370
  export type Candles = Infer<typeof CANDLES>;
335
371
 
372
+ export const CANDLES_MANY = object({ indexed, interval: str, series: array(CANDLES), missing: array(str) });
373
+ export type CandlesMany = Infer<typeof CANDLES_MANY>;
374
+
336
375
  export const CANDLE_BUNDLE = object({
337
376
  indexed,
338
377
  launch: str,
@@ -368,7 +407,7 @@ export type QuoteChain = Infer<typeof QUOTE_CHAIN>;
368
407
  export const DEV_TRADES = object({ indexed, launch: str, creator: str, trades: array(TRADE) });
369
408
  export type DevTrades = Infer<typeof DEV_TRADES>;
370
409
 
371
- export const BURNS = object({ indexed, launch: str, burns: array(object({ amount: big, block: int, timestamp: int })), program: array(BURN), wallet: array(BURN) });
410
+ export const BURNS = object({ indexed, launch: str, burns: array(object({ id: optional(str), amount: big, block: int, timestamp: int })), program: array(object({ ...BURN_FIELDS, id: optional(str) })), wallet: array(object({ ...BURN_FIELDS, id: optional(str) })), next: optional(nullable(str)) });
372
411
  export type Burns = Infer<typeof BURNS>;
373
412
 
374
413
  const LOCK_HOLDER = object({ wallet: str, place: nullable(int) });
@@ -394,6 +433,9 @@ export const PRICE = object({
394
433
  priceX64: big,
395
434
  livePriceX64: big,
396
435
  floorX64: big,
436
+
437
+ maxLossPpm: optional(nullable(int)),
438
+
397
439
  backedPpm: int,
398
440
  bidTopX64: nullable(big),
399
441
  lastTrade: nullable(object({ priceAfterX64: big, timestamp: int, block: int })),
@@ -407,17 +449,50 @@ export type Price = Infer<typeof PRICE>;
407
449
  export const WALLET_TRADES = object({ indexed, wallet: str, trades: array(PAGED_TRADE), next: nullable(str) });
408
450
  export type WalletTrades = Infer<typeof WALLET_TRADES>;
409
451
 
410
- export const POSITIONS = object({ indexed, wallet: str, positions: array(object({ launch: str, token: str, quote: str, tokens: big })) });
452
+ export const TON_ACTIVITY_KINDS = ["buy", "sell", "claim", "team_slot_add", "team_share_buy", "burn", "withdraw", "create", "unlock_team_buy", "backing", "refused", "aborted"] as const;
453
+
454
+ export const TON_FILL = object({ tokensIn: nullable(big), tokensOut: nullable(big), quoteIn: nullable(big), quoteOut: nullable(big), refund: nullable(big) });
455
+ export type TonFill = Infer<typeof TON_FILL>;
456
+
457
+ export const TON_REASON = object({ code: oneOf("aborted", "refused"), op: nullable(int), class: nullable(int), refunded: nullable(big) });
458
+ export type TonReason = Infer<typeof TON_REASON>;
459
+
460
+ export const TON_ACTIVITY_ROW = object({
461
+ id: str,
462
+ txnId: str,
463
+ block: int,
464
+ timestamp: int,
465
+ kind: oneOf(...TON_ACTIVITY_KINDS),
466
+ status: oneOf("landed", "failed"),
467
+ final: bool,
468
+ launch: nullable(str),
469
+ quote: nullable(str),
470
+ queryId: nullable(str),
471
+ types: nullable(int),
472
+ fill: TON_FILL,
473
+ reason: nullable(TON_REASON),
474
+ });
475
+ export type TonActivityRow = Infer<typeof TON_ACTIVITY_ROW>;
476
+
477
+ export const TON_ACTIVITY = object({ indexed, wallet: str, activity: array(TON_ACTIVITY_ROW), next: nullable(str) });
478
+ export type TonActivity = Infer<typeof TON_ACTIVITY>;
479
+
480
+ export const TON_OUTCOME = object({ indexed, wallet: str, queryId: str, outcome: oneOf("pending", "landed", "failed"), seen: bool, row: nullable(TON_ACTIVITY_ROW) });
481
+ export type TonOutcome = Infer<typeof TON_OUTCOME>;
482
+
483
+ export const POSITIONS = object({ indexed, wallet: str, positions: array(object({ launch: str, token: str, quote: str, tokens: big })), next: optional(nullable(str)) });
411
484
  export type Positions = Infer<typeof POSITIONS>;
412
485
 
413
486
  export const WALLET_LOCKS = object({
414
487
  indexed,
415
488
  wallet: str,
416
489
  locks: array(object({ launch: str, token: str, kind: oneOf("teamBuy"), direction: oneOf("incoming", "outgoing"), tokens: big, place: nullable(int), createdAt: int, unlocked: bool, unlockedAt: nullable(int), unlockAt: nullable(int), locked: big, claimed: big, holders: array(LOCK_HOLDER) })),
490
+
491
+ next: optional(nullable(str)),
417
492
  });
418
493
  export type WalletLocks = Infer<typeof WALLET_LOCKS>;
419
494
 
420
- const AT = { timestamp: int, block: int };
495
+ const AT = { timestamp: int, block: int, id: optional(str) };
421
496
 
422
497
  export const INBOX_CLAIMED = object({ ...AT, kind: oneOf("claimed"), launch: str, quote: str, types: int, amount: big, holder: big, txnId: nullable(str) });
423
498
 
@@ -425,7 +500,7 @@ export const INBOX_GRADUATED = object({ ...AT, kind: oneOf("graduated"), launch:
425
500
 
426
501
  export const INBOX_SLOT = object({ ...AT, kind: oneOf("slot"), launch: str, place: int });
427
502
 
428
- export const INBOX_DEV_BURN = object({ ...BURN_FIELDS, kind: oneOf("devBurn"), launch: str });
503
+ export const INBOX_DEV_BURN = object({ ...BURN_FIELDS, id: optional(str), kind: oneOf("devBurn"), launch: str });
429
504
 
430
505
  export const INBOX_DEV_BUY = object({ ...AT, kind: oneOf("devBuy"), launch: str, wallet: str, role: DEV_BUY_ROLE, count: int, quoteIn: big, tokens: big, txnId: nullable(str) });
431
506
 
@@ -444,7 +519,7 @@ export function inboxItem(v: unknown, p: string): InboxItem | null {
444
519
  return d(v, p) as InboxItem;
445
520
  }
446
521
 
447
- export const INBOX = object({ indexed, wallet: str, items: (v: unknown, p: string) => array(inboxItem)(v, p).filter((x): x is InboxItem => x !== null) });
522
+ export const INBOX = object({ indexed, wallet: str, items: (v: unknown, p: string) => array(inboxItem)(v, p).filter((x): x is InboxItem => x !== null), next: optional(nullable(str)) });
448
523
  export type Inbox = Infer<typeof INBOX>;
449
524
 
450
525
  export const CLAIM_ROW = object({
@@ -452,7 +527,7 @@ export const CLAIM_ROW = object({
452
527
  timestamp: int,
453
528
  txnId: nullable(str),
454
529
  sender: str,
455
- launch: str,
530
+ launch: nullable(str),
456
531
  quote: str,
457
532
  types: int,
458
533
  creator: big,
@@ -466,7 +541,7 @@ export const CLAIM_ROW = object({
466
541
  });
467
542
  export type ClaimRow = Infer<typeof CLAIM_ROW>;
468
543
 
469
- export const CLAIM_HISTORY = object({ indexed, wallet: str, claims: array(CLAIM_ROW) });
544
+ export const CLAIM_HISTORY = object({ indexed, wallet: str, claims: array(CLAIM_ROW), next: optional(nullable(str)) });
470
545
  export type ClaimHistory = Infer<typeof CLAIM_HISTORY>;
471
546
 
472
547
  const QUOTE_TOTAL = object({ quote: str, total: big, types: int });
@@ -489,6 +564,8 @@ export const CLAIM_VIEW = object({
489
564
  refOwed: optional(array(object({ account: str, quoteMint: str, owed: big }))),
490
565
  pendingRollUp: optional(array(object({ traderRef: str, quoteMint: str, amount: big }))),
491
566
  owed: optional(array(object({ quote: str, creator: big, creatorRef: big, teamSlot: big, traderRef: big, holder: big, teamBuy: big }))),
567
+
568
+ next: optional(nullable(str)),
492
569
  }),
493
570
  });
494
571
  export type ClaimView = Infer<typeof CLAIM_VIEW>;
@@ -504,6 +581,8 @@ export const PEOPLE = object({
504
581
  indexed,
505
582
  wallet: str,
506
583
  people: array(object({ wallet: str, chain: str, roles: array(object({ kind: oneOf("trader", "creator"), tier: int })), earned: PER_QUOTE, teamMilestone: PER_QUOTE })),
584
+
585
+ next: optional(nullable(str)),
507
586
  });
508
587
  export type People = Infer<typeof PEOPLE>;
509
588
 
@@ -523,6 +602,8 @@ export const ROLES = object({
523
602
  holdings: array(object({ launch: str, token: str, amount: big })),
524
603
  }),
525
604
  claimed: record(big),
605
+
606
+ next: optional(nullable(str)),
526
607
  });
527
608
  export type Roles = Infer<typeof ROLES>;
528
609
 
@@ -530,6 +611,8 @@ export const HANDLE_PLACES = object({
530
611
  indexed,
531
612
  handle: str,
532
613
  places: array(object({ launch: str, place: int })),
614
+
615
+ next: optional(nullable(str)),
533
616
  });
534
617
  export type HandlePlaces = Infer<typeof HANDLE_PLACES>;
535
618
 
@@ -545,3 +628,9 @@ export const LAUNCHER_BOARD = object({
545
628
  launchers: array(object({ creator: str, launches: int, graduated: int, volumeUsd: decimal, liquidityUsd: decimal, bestLaunch: nullable(str), byQuote: record(object({ volume: big, liquidity: big, launches: int })) })),
546
629
  });
547
630
  export type LauncherBoard = Infer<typeof LAUNCHER_BOARD>;
631
+
632
+ export function maxLossPct(b: { maxLossPpm?: number | null; backedPpm?: number | null; graduated?: boolean }): number | null {
633
+ if (b.maxLossPpm != null) return b.maxLossPpm / 10_000;
634
+ if (b.maxLossPpm === undefined && b.graduated === true && b.backedPpm != null) return Math.max(0, 1_000_000 - b.backedPpm) / 10_000;
635
+ return null;
636
+ }