@floorlaunchpad/sdk 0.1.1 → 0.3.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.
- package/CHANGELOG.md +114 -0
- package/LICENSE +201 -60
- package/README.md +54 -24
- package/dist/abi/evm-event-names.js +12 -0
- package/dist/abi/evm.gen.js +130 -0
- package/dist/abi/functions.gen.js +144 -0
- package/dist/abi/index.js +17 -0
- package/dist/abi/solana.gen.js +130 -0
- package/dist/burn.js +21 -0
- package/dist/client/evm/index.js +191 -0
- package/dist/client/index.js +19 -0
- package/dist/client/solana/index.js +259 -0
- package/dist/client/types.js +5 -0
- package/dist/codec.js +77 -0
- package/dist/config/codeHash.js +9 -0
- package/dist/config/generated.js +803 -0
- package/dist/config/index.js +49 -0
- package/dist/config/registry.gen.js +118 -0
- package/dist/config/solana-genesis.js +7 -0
- package/dist/ed25519.js +31 -0
- package/dist/errors.js +249 -0
- package/dist/events/index.js +40 -0
- package/dist/events/types.gen.js +3 -0
- package/dist/evm/abi.js +283 -0
- package/dist/evm/balances.js +26 -0
- package/dist/evm/chains.js +148 -0
- package/dist/evm/floor/abi.gen.js +195 -0
- package/dist/evm/floor/calls.js +230 -0
- package/dist/evm/floor/create.js +49 -0
- package/dist/evm/floor/events.js +41 -0
- package/dist/evm/floor/iface.js +44 -0
- package/dist/evm/items.js +209 -0
- package/dist/evm/keccak.js +118 -0
- package/dist/evm/multicall.js +30 -0
- package/dist/evm/router.js +184 -0
- package/dist/evm/squid.js +107 -0
- package/dist/evm/zap.js +128 -0
- package/dist/fixtures.js +53 -0
- package/dist/generated/index.js +28 -0
- package/dist/generated/interface.gen.js +146 -0
- package/dist/handle.js +22 -0
- package/dist/index.js +93 -7
- package/dist/indexer/bodies.js +473 -0
- package/dist/indexer/client.js +105 -0
- package/dist/indexer/contract.js +358 -0
- package/dist/indexer/decode.js +97 -0
- package/dist/indexer/partners.js +105 -0
- package/dist/indexer/paths.js +127 -0
- package/dist/indexer/read.js +109 -0
- package/dist/indexer/stream.js +71 -0
- package/dist/lock.js +16 -0
- package/dist/math/fixed.js +154 -0
- package/dist/math/genesis.js +39 -0
- package/dist/math/index.js +20 -0
- package/dist/math/u256.js +49 -0
- package/dist/math/v2/ask.js +162 -0
- package/dist/math/v2/book.js +607 -0
- package/dist/math/v2/circulating.js +62 -0
- package/dist/math/v2/create.js +122 -0
- package/dist/math/v2/display.js +208 -0
- package/dist/math/v2/error.js +17 -0
- package/dist/math/v2/feasibility.js +174 -0
- package/dist/math/v2/fees.js +121 -0
- package/dist/math/v2/holders.js +105 -0
- package/dist/math/v2/milestones.js +40 -0
- package/dist/math/v2/pow.js +69 -0
- package/dist/math/v2/preset.js +151 -0
- package/dist/math/v2/receipt.js +108 -0
- package/dist/math/v2/relay.js +217 -0
- package/dist/math/v2/rows.js +365 -0
- package/dist/math/v2/split.js +111 -0
- package/dist/math/v2/sqrt.js +84 -0
- package/dist/math/v2/supply.js +21 -0
- package/dist/math/v2/team.js +75 -0
- package/dist/math/v2/trade.js +126 -0
- package/dist/math/v2/wide.js +51 -0
- package/dist/quote.js +6 -0
- package/dist/registryPrice.js +23 -0
- package/dist/route/edge-jupiter.js +42 -0
- package/dist/route/edge.js +11 -0
- package/dist/route/graph.js +51 -0
- package/dist/route/path.js +75 -0
- package/dist/route/quote.js +15 -0
- package/dist/route-jupiter.js +35 -0
- package/dist/route.js +8 -0
- package/dist/sha256.js +61 -0
- package/dist/solana/alt.js +18 -0
- package/dist/solana/floor/accounts.js +392 -0
- package/dist/solana/floor/borsh.js +66 -0
- package/dist/solana/floor/create.js +65 -0
- package/dist/solana/floor/events.js +144 -0
- package/dist/solana/floor/genesis.js +91 -0
- package/dist/solana/floor/genesisList.js +68 -0
- package/dist/solana/floor/instructions.js +448 -0
- package/dist/solana/floor/metadata.js +198 -0
- package/dist/solana/floor/mint.js +206 -0
- package/dist/solana/floor/program.js +64 -0
- package/dist/solana/floor/registry.js +83 -0
- package/dist/solana/instruction.js +49 -0
- package/dist/solana/pda.js +88 -0
- package/dist/solana/tx.js +138 -0
- package/dist/v2/burnPreview.js +35 -0
- package/dist/v2/circulating.js +11 -0
- package/dist/v2/claimAllEvm.js +70 -0
- package/dist/v2/claims-evm.js +120 -0
- package/dist/v2/claims.js +218 -0
- package/dist/v2/dead.js +21 -0
- package/dist/v2/devbuy.js +37 -0
- package/dist/v2/events.js +115 -0
- package/dist/v2/evm.js +299 -0
- package/dist/v2/evmReads.js +76 -0
- package/dist/v2/genesis.js +76 -0
- package/dist/v2/layouts.js +217 -0
- package/dist/v2/market-evm.js +74 -0
- package/dist/v2/presetRowEvm.js +45 -0
- package/dist/v2/press.js +106 -0
- package/dist/v2/quoteMint.js +52 -0
- package/dist/v2/receipt-tx.js +149 -0
- package/dist/v2/route-alt.js +114 -0
- package/dist/v2/route.js +389 -0
- package/dist/v2/router.js +22 -0
- package/dist/v2/solana.js +391 -0
- package/dist/v2/solanaSettle.js +97 -0
- package/dist/v2/tx/evm.js +126 -0
- package/dist/v2/tx/genesis.js +47 -0
- package/dist/v2/tx/index.js +5 -0
- package/dist/v2/tx/solana.js +118 -0
- package/dist/v2/types.js +13 -0
- package/docs/aggregator.md +61 -0
- package/docs/bot.md +50 -0
- package/docs/floor-line.md +66 -0
- package/docs/frontend.md +29 -0
- package/docs/wallet.md +54 -0
- package/examples/floor-line.html +104 -0
- package/package.json +429 -32
- package/src/abi/evm-event-names.ts +12 -0
- package/src/abi/evm.gen.ts +139 -0
- package/src/abi/functions.gen.ts +154 -0
- package/src/abi/index.ts +30 -0
- package/src/abi/solana.gen.ts +140 -0
- package/src/burn.ts +37 -0
- package/src/client/evm/index.ts +199 -0
- package/src/client/index.ts +23 -0
- package/src/client/solana/index.ts +260 -0
- package/src/client/types.ts +162 -0
- package/src/codec.ts +77 -0
- package/src/config/codeHash.ts +9 -0
- package/src/config/generated.ts +803 -0
- package/src/config/index.ts +93 -0
- package/src/config/registry.gen.ts +118 -0
- package/src/config/solana-genesis.ts +7 -0
- package/src/ed25519.ts +31 -0
- package/src/errors.ts +270 -0
- package/src/events/index.ts +44 -0
- package/src/events/types.gen.ts +842 -0
- package/src/evm/abi.ts +292 -0
- package/src/evm/balances.ts +26 -0
- package/src/evm/chains.ts +171 -0
- package/src/evm/floor/abi.gen.ts +195 -0
- package/src/evm/floor/calls.ts +247 -0
- package/src/evm/floor/create.ts +68 -0
- package/src/evm/floor/events.ts +51 -0
- package/src/evm/floor/iface.ts +45 -0
- package/src/evm/items.ts +252 -0
- package/src/evm/keccak.ts +118 -0
- package/src/evm/multicall.ts +43 -0
- package/src/evm/router.ts +209 -0
- package/src/evm/squid.ts +138 -0
- package/src/evm/zap.ts +152 -0
- package/src/fixtures.ts +53 -0
- package/src/generated/index.ts +29 -0
- package/src/generated/interface.gen.ts +153 -0
- package/src/handle.ts +22 -0
- package/src/index.ts +93 -6
- package/src/indexer/bodies.ts +528 -0
- package/src/indexer/client.ts +180 -0
- package/src/indexer/contract.ts +419 -0
- package/src/indexer/decode.ts +109 -0
- package/src/indexer/partners.ts +115 -0
- package/src/indexer/paths.ts +190 -0
- package/src/indexer/read.ts +119 -0
- package/src/indexer/stream.ts +83 -0
- package/src/lock.ts +16 -0
- package/src/math/fixed.ts +154 -0
- package/src/math/genesis.ts +39 -0
- package/src/math/index.ts +24 -0
- package/src/math/u256.ts +49 -0
- package/src/math/v2/ask.ts +218 -0
- package/src/math/v2/book.ts +660 -0
- package/src/math/v2/circulating.ts +69 -0
- package/src/math/v2/create.ts +139 -0
- package/src/math/v2/display.ts +250 -0
- package/src/math/v2/error.ts +19 -0
- package/src/math/v2/feasibility.ts +200 -0
- package/src/math/v2/fees.ts +121 -0
- package/src/math/v2/holders.ts +124 -0
- package/src/math/v2/milestones.ts +51 -0
- package/src/math/v2/pow.ts +71 -0
- package/src/math/v2/preset.ts +189 -0
- package/src/math/v2/receipt.ts +162 -0
- package/src/math/v2/relay.ts +235 -0
- package/src/math/v2/rows.ts +412 -0
- package/src/math/v2/split.ts +160 -0
- package/src/math/v2/sqrt.ts +86 -0
- package/src/math/v2/supply.ts +27 -0
- package/src/math/v2/team.ts +75 -0
- package/src/math/v2/trade.ts +145 -0
- package/src/math/v2/wide.ts +52 -0
- package/src/quote.ts +6 -0
- package/src/registryPrice.ts +28 -0
- package/src/route/edge-jupiter.ts +71 -0
- package/src/route/edge.ts +22 -0
- package/src/route/graph.ts +77 -0
- package/src/route/path.ts +84 -0
- package/src/route/quote.ts +46 -0
- package/src/route-jupiter.ts +36 -0
- package/src/route.ts +8 -0
- package/src/sha256.ts +61 -0
- package/src/solana/alt.ts +25 -0
- package/src/solana/floor/accounts.ts +395 -0
- package/src/solana/floor/borsh.ts +66 -0
- package/src/solana/floor/create.ts +89 -0
- package/src/solana/floor/events.ts +151 -0
- package/src/solana/floor/genesis.ts +111 -0
- package/src/solana/floor/genesisList.ts +75 -0
- package/src/solana/floor/instructions.ts +504 -0
- package/src/solana/floor/metadata.ts +246 -0
- package/src/solana/floor/mint.ts +242 -0
- package/src/solana/floor/program.ts +64 -0
- package/src/solana/floor/registry.ts +96 -0
- package/src/solana/instruction.ts +61 -0
- package/src/solana/pda.ts +91 -0
- package/src/solana/tx.ts +143 -0
- package/src/v2/burnPreview.ts +52 -0
- package/src/v2/circulating.ts +22 -0
- package/src/v2/claimAllEvm.ts +97 -0
- package/src/v2/claims-evm.ts +179 -0
- package/src/v2/claims.ts +288 -0
- package/src/v2/dead.ts +21 -0
- package/src/v2/devbuy.ts +55 -0
- package/src/v2/events.ts +131 -0
- package/src/v2/evm.ts +349 -0
- package/src/v2/evmReads.ts +121 -0
- package/src/v2/genesis.ts +80 -0
- package/src/v2/layouts.ts +219 -0
- package/src/v2/market-evm.ts +95 -0
- package/src/v2/presetRowEvm.ts +54 -0
- package/src/v2/press.ts +124 -0
- package/src/v2/quoteMint.ts +63 -0
- package/src/v2/receipt-tx.ts +251 -0
- package/src/v2/route-alt.ts +114 -0
- package/src/v2/route.ts +494 -0
- package/src/v2/router.ts +22 -0
- package/src/v2/solana.ts +439 -0
- package/src/v2/solanaSettle.ts +117 -0
- package/src/v2/tx/evm.ts +152 -0
- package/src/v2/tx/genesis.ts +66 -0
- package/src/v2/tx/index.ts +5 -0
- package/src/v2/tx/solana.ts +140 -0
- package/src/v2/types.ts +284 -0
- package/dist/adapter.d.ts +0 -28
- package/dist/adapter.d.ts.map +0 -1
- package/dist/adapter.js +0 -2
- package/dist/adapter.js.map +0 -1
- package/dist/chain.d.ts +0 -23
- package/dist/chain.d.ts.map +0 -1
- package/dist/chain.js +0 -25
- package/dist/chain.js.map +0 -1
- package/dist/client.d.ts +0 -26
- package/dist/client.d.ts.map +0 -1
- package/dist/client.js +0 -43
- package/dist/client.js.map +0 -1
- package/dist/dex-adapter.d.ts +0 -228
- package/dist/dex-adapter.d.ts.map +0 -1
- package/dist/dex-adapter.js +0 -407
- package/dist/dex-adapter.js.map +0 -1
- package/dist/index.d.ts +0 -7
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/quotes.d.ts +0 -12
- package/dist/quotes.d.ts.map +0 -1
- package/dist/quotes.gen.d.ts +0 -8
- package/dist/quotes.gen.d.ts.map +0 -1
- package/dist/quotes.gen.js +0 -2170
- package/dist/quotes.gen.js.map +0 -1
- package/dist/quotes.js +0 -24
- package/dist/quotes.js.map +0 -1
- package/dist/types.d.ts +0 -111
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/src/adapter.ts +0 -20
- package/src/chain.ts +0 -39
- package/src/client.ts +0 -41
- package/src/dex-adapter.ts +0 -477
- package/src/quotes.gen.ts +0 -2170
- package/src/quotes.ts +0 -29
- package/src/types.ts +0 -115
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { encodeFunctionData } from "./items.ts";
|
|
4
|
+
import { deadlineArg } from "./floor/calls.ts";
|
|
5
|
+
import { EVM_NATIVE, NoRouteError, coinKey } from "../route/graph.ts";
|
|
6
|
+
import { chainById } from "./chains.ts";
|
|
7
|
+
import type { Pool } from "../route/graph.ts";
|
|
8
|
+
import type { HopQuote, RouteQuote } from "../route/quote.ts";
|
|
9
|
+
|
|
10
|
+
const BUY = [
|
|
11
|
+
{ name: "path", type: "uint256[]" },
|
|
12
|
+
{ name: "tokenIn", type: "address" },
|
|
13
|
+
{ name: "amountIn", type: "uint256" },
|
|
14
|
+
{ name: "swapData", type: "bytes" },
|
|
15
|
+
{ name: "minQuoteIn", type: "uint256" },
|
|
16
|
+
{ name: "minTokensOut", type: "uint256[]" },
|
|
17
|
+
{ name: "to", type: "address" },
|
|
18
|
+
{ name: "deadline", type: "uint256" },
|
|
19
|
+
] as const;
|
|
20
|
+
|
|
21
|
+
const SELL = [
|
|
22
|
+
{ name: "path", type: "uint256[]" },
|
|
23
|
+
{ name: "tokensIn", type: "uint256" },
|
|
24
|
+
{ name: "minQuoteOut", type: "uint256[]" },
|
|
25
|
+
{ name: "tokenOut", type: "address" },
|
|
26
|
+
{ name: "swapIn", type: "uint256" },
|
|
27
|
+
{ name: "swapData", type: "bytes" },
|
|
28
|
+
{ name: "minOut", type: "uint256" },
|
|
29
|
+
{ name: "to", type: "address" },
|
|
30
|
+
{ name: "deadline", type: "uint256" },
|
|
31
|
+
] as const;
|
|
32
|
+
const buyArg = (name: string) => ({ name, type: "tuple", internalType: "struct IFloorRouter.Buy", components: BUY }) as const;
|
|
33
|
+
const sellArg = (name: string) => ({ name, type: "tuple", internalType: "struct IFloorRouter.Sell", components: SELL }) as const;
|
|
34
|
+
const err = (name: string, inputs: readonly { name: string; type: string }[] = []) => ({ type: "error", name, inputs }) as const;
|
|
35
|
+
const SELL_OUT = [{ name: "amountOut", type: "uint256" }, { name: "quoteOut", type: "uint256" }] as const;
|
|
36
|
+
|
|
37
|
+
export const FLOOR_ROUTER_ABI = [
|
|
38
|
+
{ type: "function", name: "floorAmm", inputs: [], outputs: [{ name: "", type: "address", internalType: "contract IFloorAmm" }], stateMutability: "view" },
|
|
39
|
+
{ type: "function", name: "router", inputs: [], outputs: [{ name: "", type: "address" }], stateMutability: "view" },
|
|
40
|
+
{ type: "function", name: "sell", inputs: [sellArg("s")], outputs: SELL_OUT, stateMutability: "nonpayable" },
|
|
41
|
+
{
|
|
42
|
+
type: "function",
|
|
43
|
+
name: "sellWithPermit",
|
|
44
|
+
inputs: [sellArg("s"), { name: "permitDeadline", type: "uint256" }, { name: "v", type: "uint8" }, { name: "r", type: "bytes32" }, { name: "ss", type: "bytes32" }],
|
|
45
|
+
outputs: SELL_OUT,
|
|
46
|
+
stateMutability: "nonpayable",
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
type: "event",
|
|
50
|
+
name: "Sold",
|
|
51
|
+
anonymous: false,
|
|
52
|
+
inputs: [
|
|
53
|
+
{ name: "sender", type: "address", indexed: true },
|
|
54
|
+
{ name: "to", type: "address", indexed: true },
|
|
55
|
+
{ name: "launchId", type: "uint256", indexed: true },
|
|
56
|
+
{ name: "tokensIn", type: "uint256", indexed: false },
|
|
57
|
+
{ name: "hops", type: "uint256", indexed: false },
|
|
58
|
+
{ name: "tokenOut", type: "address", indexed: false },
|
|
59
|
+
{ name: "quoteOut", type: "uint256", indexed: false },
|
|
60
|
+
{ name: "amountOut", type: "uint256", indexed: false },
|
|
61
|
+
{ name: "refundQuote", type: "uint256", indexed: false },
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
type: "event",
|
|
66
|
+
name: "Bought",
|
|
67
|
+
anonymous: false,
|
|
68
|
+
inputs: [
|
|
69
|
+
{ name: "sender", type: "address", indexed: true },
|
|
70
|
+
{ name: "to", type: "address", indexed: true },
|
|
71
|
+
{ name: "launchId", type: "uint256", indexed: true },
|
|
72
|
+
{ name: "tokenIn", type: "address", indexed: false },
|
|
73
|
+
{ name: "amountIn", type: "uint256", indexed: false },
|
|
74
|
+
{ name: "hops", type: "uint256", indexed: false },
|
|
75
|
+
{ name: "quoteIn", type: "uint256", indexed: false },
|
|
76
|
+
{ name: "tokensOut", type: "uint256", indexed: false },
|
|
77
|
+
{ name: "refundIn", type: "uint256", indexed: false },
|
|
78
|
+
],
|
|
79
|
+
},
|
|
80
|
+
err("NotRouter"),
|
|
81
|
+
err("SwapFailed", [{ name: "reason", type: "bytes" }]),
|
|
82
|
+
err("SwapDataNotEmpty"),
|
|
83
|
+
err("BadPath"),
|
|
84
|
+
err("QuoteShort"),
|
|
85
|
+
err("SlippageExceeded"),
|
|
86
|
+
err("Expired"),
|
|
87
|
+
err("BadValue"),
|
|
88
|
+
err("Reentrancy"),
|
|
89
|
+
|
|
90
|
+
{ type: "function", name: "buy", inputs: [buyArg("b")], outputs: [{ name: "tokensOut", type: "uint256" }, { name: "quoteIn", type: "uint256" }], stateMutability: "payable" },
|
|
91
|
+
{ type: "function", name: "swap", inputs: [sellArg("s"), buyArg("b")], outputs: [{ name: "tokensOut", type: "uint256" }], stateMutability: "nonpayable" },
|
|
92
|
+
] as const;
|
|
93
|
+
|
|
94
|
+
export const SWAP_IN_ALL = (1n << 256n) - 1n;
|
|
95
|
+
|
|
96
|
+
export interface RouterBuy {
|
|
97
|
+
path: bigint[];
|
|
98
|
+
tokenIn: string;
|
|
99
|
+
amountIn: bigint;
|
|
100
|
+
swapData: string;
|
|
101
|
+
minQuoteIn: bigint;
|
|
102
|
+
minTokensOut: bigint[];
|
|
103
|
+
to: string;
|
|
104
|
+
deadline: bigint;
|
|
105
|
+
}
|
|
106
|
+
export interface RouterSell {
|
|
107
|
+
path: bigint[];
|
|
108
|
+
tokensIn: bigint;
|
|
109
|
+
minQuoteOut: bigint[];
|
|
110
|
+
tokenOut: string;
|
|
111
|
+
|
|
112
|
+
swapIn: bigint;
|
|
113
|
+
swapData: string;
|
|
114
|
+
minOut: bigint;
|
|
115
|
+
to: string;
|
|
116
|
+
deadline: bigint;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export function checkFloorHops(chainId: unknown, floorHops: number): void {
|
|
120
|
+
if (typeof chainId !== "number" || !Number.isInteger(chainId)) throw new RangeError("an EVM route names its chainId");
|
|
121
|
+
const row = chainById(chainId);
|
|
122
|
+
if (row?.maxFloorHops !== undefined && floorHops > row.maxFloorHops) {
|
|
123
|
+
throw new NoRouteError("TooManyHops", `${floorHops} FLOOR trades: ${row.name} (chain ${row.chainId}) carries at most ${row.maxFloorHops} per route`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const checkPath = (path: bigint[], mins: bigint[]): void => {
|
|
128
|
+
if (path.length === 0) throw new RangeError("BadPath: an empty path");
|
|
129
|
+
if (mins.length !== path.length) throw new RangeError("BadPath: one min-out a hop");
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export function encodeRouterBuy(b: RouterBuy, chainId: number): string {
|
|
133
|
+
checkPath(b.path, b.minTokensOut);
|
|
134
|
+
checkFloorHops(chainId, b.path.length);
|
|
135
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "buy", { b });
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function encodeRouterSell(s: RouterSell, chainId: number): string {
|
|
139
|
+
checkPath(s.path, s.minQuoteOut);
|
|
140
|
+
checkFloorHops(chainId, s.path.length);
|
|
141
|
+
if (s.tokensIn === 0n || coinKey(s.to) === EVM_NATIVE) throw new RangeError("BadValue: tokensIn > 0 and a recipient");
|
|
142
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "sell", { s });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export function encodeRouterSellWithPermit(s: RouterSell, permit: { deadline: bigint; v: number; r: string; s: string }, chainId: number): string {
|
|
146
|
+
checkPath(s.path, s.minQuoteOut);
|
|
147
|
+
checkFloorHops(chainId, s.path.length);
|
|
148
|
+
if (s.tokensIn === 0n || coinKey(s.to) === EVM_NATIVE) throw new RangeError("BadValue: tokensIn > 0 and a recipient");
|
|
149
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "sellWithPermit", { s, permitDeadline: permit.deadline, v: BigInt(permit.v), r: permit.r, ss: permit.s });
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
export function encodeRouterSwap(s: RouterSell, b: RouterBuy, chainId: number): string {
|
|
153
|
+
checkPath(s.path, s.minQuoteOut);
|
|
154
|
+
checkPath(b.path, b.minTokensOut);
|
|
155
|
+
checkFloorHops(chainId, s.path.length + b.path.length);
|
|
156
|
+
if (coinKey(b.tokenIn) !== coinKey(s.tokenOut) || b.amountIn !== 0n) throw new RangeError("BadPath: swap's buy takes the sell's output (b.tokenIn = s.tokenOut, b.amountIn = 0)");
|
|
157
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "swap", { s, b });
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
const idOf = (pool: Pool): bigint => {
|
|
161
|
+
if (pool.launchId === undefined) throw new RangeError(`pool ${pool.symbol}: an EVM pool needs its launchId`);
|
|
162
|
+
return pool.launchId;
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
export function evmRouteCall(q: RouteQuote, p: { router: string; to: string; deadline: bigint; chainId: number; swapData?: string[] }): { to: string; data: string; value: bigint; gas?: bigint } {
|
|
166
|
+
if (q.path.chain !== "evm") throw new RangeError("evmRouteCall builds EVM routes; Solana routes are `routeInstructions`");
|
|
167
|
+
checkFloorHops(p.chainId, q.hops.filter((h) => h.hop.kind !== "edge").length);
|
|
168
|
+
deadlineArg(p.deadline);
|
|
169
|
+
const hs = q.hops;
|
|
170
|
+
const sells = hs.filter((h) => h.hop.kind === "sell");
|
|
171
|
+
const buys = hs.filter((h) => h.hop.kind === "buy");
|
|
172
|
+
const edges = hs.filter((h) => h.hop.kind === "edge");
|
|
173
|
+
if (edges.length > 1) throw new RangeError("BadPath: one edge leg a route");
|
|
174
|
+
const sd = p.swapData ?? [];
|
|
175
|
+
if (sd.length !== edges.length) throw new RangeError(`the route has ${edges.length} edge leg(s): pass ${edges.length} swapData`);
|
|
176
|
+
const edge = edges[0] as (HopQuote & { hop: { kind: "edge"; from: string; to: string } }) | undefined;
|
|
177
|
+
const pool = (h: HopQuote): Pool => (h.hop as { pool: Pool }).pool;
|
|
178
|
+
const ids = (xs: HopQuote[]): bigint[] => xs.map((h) => idOf(pool(h)));
|
|
179
|
+
const mins = (xs: HopQuote[]): bigint[] => xs.map((h) => h.minOut);
|
|
180
|
+
const gas = hs.every((h) => h.gas !== undefined) ? hs.reduce((n, h) => n + (h.gas as bigint), 0n) : undefined;
|
|
181
|
+
const call = (data: string, value: bigint) => ({ to: p.router, data, value, ...(gas !== undefined ? { gas } : {}) });
|
|
182
|
+
|
|
183
|
+
if (sells.length === 0) {
|
|
184
|
+
|
|
185
|
+
const base = pool(buys[0]).quote;
|
|
186
|
+
const tokenIn = edge ? edge.hop.from : base;
|
|
187
|
+
const native = coinKey(tokenIn) === EVM_NATIVE;
|
|
188
|
+
const r: RouterBuy = { path: ids(buys), tokenIn, amountIn: native ? 0n : q.amountIn, swapData: edge ? sd[0] : "0x", minQuoteIn: edge ? edge.minOut : 0n, minTokensOut: mins(buys), to: p.to, deadline: p.deadline };
|
|
189
|
+
return call(encodeRouterBuy(r, p.chainId), native ? q.amountIn : 0n);
|
|
190
|
+
}
|
|
191
|
+
const last = sells[sells.length - 1];
|
|
192
|
+
|
|
193
|
+
const s: RouterSell = {
|
|
194
|
+
path: ids(sells),
|
|
195
|
+
tokensIn: q.amountIn,
|
|
196
|
+
minQuoteOut: mins(sells),
|
|
197
|
+
tokenOut: edge ? edge.hop.to : pool(last).quote,
|
|
198
|
+
|
|
199
|
+
swapIn: edge ? edge.amountIn : 0n,
|
|
200
|
+
swapData: edge ? sd[0] : "0x",
|
|
201
|
+
minOut: edge ? edge.minOut : last.minOut,
|
|
202
|
+
|
|
203
|
+
to: buys.length ? EVM_NATIVE : p.to,
|
|
204
|
+
deadline: p.deadline,
|
|
205
|
+
};
|
|
206
|
+
if (buys.length === 0) return call(encodeRouterSell(s, p.chainId), 0n);
|
|
207
|
+
const b: RouterBuy = { path: ids(buys), tokenIn: s.tokenOut, amountIn: 0n, swapData: "0x", minQuoteIn: 0n, minTokensOut: mins(buys), to: p.to, deadline: p.deadline };
|
|
208
|
+
return call(encodeRouterSwap(s, b, p.chainId), 0n);
|
|
209
|
+
}
|
package/src/evm/squid.ts
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { bytesToHex, encodeAbi, encodeCall } from "./abi.ts";
|
|
4
|
+
|
|
5
|
+
export const SQUID_CALL = { Default: 0, FullTokenBalance: 1, FullNativeBalance: 2, CollectTokenBalance: 3 } as const;
|
|
6
|
+
|
|
7
|
+
export const SQUID_FULL = 0n;
|
|
8
|
+
|
|
9
|
+
const SOLANA_CHAINS: readonly (number | string)[] = [101, 1151111081099710, "solana", "solana-mainnet-beta"];
|
|
10
|
+
|
|
11
|
+
export type SquidHookCode = "SOLANA" | "RECIPIENT_IS_MULTICALL" | "BAD_RECIPIENT" | "NO_MINIMUM" | "NATIVE_QUOTE" | "NOT_WHOLE_UNITS" | "BAD_ADDRESS";
|
|
12
|
+
|
|
13
|
+
export class SquidHookError extends Error {
|
|
14
|
+
readonly code: SquidHookCode;
|
|
15
|
+
constructor(code: SquidHookCode, detail: string) {
|
|
16
|
+
super(`squid: ${code}: ${detail}`);
|
|
17
|
+
this.code = code;
|
|
18
|
+
this.name = "SquidHookError";
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type SquidHookCall = {
|
|
23
|
+
chainType: "evm";
|
|
24
|
+
callType: number;
|
|
25
|
+
target: string;
|
|
26
|
+
value: string;
|
|
27
|
+
callData: string;
|
|
28
|
+
payload: string;
|
|
29
|
+
estimatedGas: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type SquidHook = {
|
|
33
|
+
provider: "FLOOR";
|
|
34
|
+
description: string;
|
|
35
|
+
calls: SquidHookCall[];
|
|
36
|
+
fundToken?: string;
|
|
37
|
+
fundAmount?: string;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const SQUID_TOKEN_SCALE = 10n ** 12n;
|
|
41
|
+
|
|
42
|
+
export const SQUID_GAS = { transfer: 65_000, buy: 700_000, sell: 750_000, zapBuy: 900_000, passOn: 30_000 } as const;
|
|
43
|
+
|
|
44
|
+
type Common = {
|
|
45
|
+
chainId: number | string;
|
|
46
|
+
|
|
47
|
+
multicall: string;
|
|
48
|
+
floorAmm: string;
|
|
49
|
+
launchId: bigint;
|
|
50
|
+
|
|
51
|
+
user: string;
|
|
52
|
+
deadline: bigint;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const ZERO = "0x" + "0".repeat(40);
|
|
56
|
+
const lc = (a: string, what: string): string => {
|
|
57
|
+
if (!/^0x[0-9a-fA-F]{40}$/.test(a)) throw new SquidHookError("BAD_ADDRESS", `${what} ${a}`);
|
|
58
|
+
return a.toLowerCase();
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
function checkCommon(p: Common): void {
|
|
62
|
+
if (SOLANA_CHAINS.includes(p.chainId)) throw new SquidHookError("SOLANA", `chain ${p.chainId}: hooks run on EVM chains only`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function checkUser(p: Common): string {
|
|
66
|
+
const user = lc(p.user, "user");
|
|
67
|
+
if (user === lc(p.multicall, "multicall")) throw new SquidHookError("RECIPIENT_IS_MULTICALL", "the tokens would stay on SquidMulticall");
|
|
68
|
+
if (user === ZERO || user === lc(p.floorAmm, "floorAmm")) throw new SquidHookError("BAD_RECIPIENT", user);
|
|
69
|
+
return user;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const call = (callType: number, target: string, value: bigint, callData: string, payload: string, gas: number): SquidHookCall => ({
|
|
73
|
+
chainType: "evm",
|
|
74
|
+
callType,
|
|
75
|
+
target,
|
|
76
|
+
value: value.toString(),
|
|
77
|
+
callData,
|
|
78
|
+
payload,
|
|
79
|
+
estimatedGas: String(gas),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
export function buyHookErc20(p: Common & { quote: string; minTokensOut: bigint }): SquidHook {
|
|
83
|
+
checkCommon(p);
|
|
84
|
+
const user = checkUser(p);
|
|
85
|
+
const quote = lc(p.quote, "quote");
|
|
86
|
+
if (quote === ZERO) throw new SquidHookError("NATIVE_QUOTE", "buyFromBalance refuses a native quote: use buyHookNative");
|
|
87
|
+
if (p.minTokensOut <= 0n) throw new SquidHookError("NO_MINIMUM", "minTokensOut must be > 0");
|
|
88
|
+
const amm = lc(p.floorAmm, "floorAmm");
|
|
89
|
+
return {
|
|
90
|
+
provider: "FLOOR",
|
|
91
|
+
description: "Buy a FLOOR launch token",
|
|
92
|
+
calls: [
|
|
93
|
+
call(SQUID_CALL.FullTokenBalance, quote, 0n, bytesToHex(encodeCall("transfer(address,uint256)", [amm, SQUID_FULL])), bytesToHex(encodeAbi(["address", "uint256"], [quote, 1n])), SQUID_GAS.transfer),
|
|
94
|
+
call(SQUID_CALL.Default, amm, 0n, bytesToHex(encodeCall("buyFromBalance(uint256,uint256,address,address,uint256)", [p.launchId, p.minTokensOut, user, user, p.deadline])), "0x", SQUID_GAS.buy),
|
|
95
|
+
],
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export function buyHookNative(p: Common & { zap: string; swapData: string; minQuoteIn: bigint; minTokensOut: bigint }): SquidHook {
|
|
100
|
+
checkCommon(p);
|
|
101
|
+
const user = checkUser(p);
|
|
102
|
+
if (p.minQuoteIn <= 0n || p.minTokensOut <= 0n) throw new SquidHookError("NO_MINIMUM", "minQuoteIn and minTokensOut must be > 0");
|
|
103
|
+
const zap = lc(p.zap, "zap");
|
|
104
|
+
return {
|
|
105
|
+
provider: "FLOOR",
|
|
106
|
+
description: "Buy a FLOOR launch token with the native coin",
|
|
107
|
+
calls: [
|
|
108
|
+
call(SQUID_CALL.FullNativeBalance, zap, 0n, bytesToHex(encodeCall("buyWithEth(uint256,bytes,uint256,uint256,address,uint256)", [p.launchId, p.swapData, p.minQuoteIn, p.minTokensOut, user, p.deadline])), "0x", SQUID_GAS.zapBuy),
|
|
109
|
+
call(SQUID_CALL.FullNativeBalance, user, 0n, "0x", "0x", SQUID_GAS.passOn),
|
|
110
|
+
],
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function sellHook(p: Common & { token: string; tokensIn: bigint; minQuoteOut: bigint; proceedsTo?: string }): SquidHook {
|
|
115
|
+
checkCommon(p);
|
|
116
|
+
lc(p.user, "user");
|
|
117
|
+
if (p.tokensIn <= 0n || p.tokensIn % SQUID_TOKEN_SCALE !== 0n) throw new SquidHookError("NOT_WHOLE_UNITS", `${p.tokensIn} is not a positive multiple of ${SQUID_TOKEN_SCALE}`);
|
|
118
|
+
if (p.minQuoteOut <= 0n) throw new SquidHookError("NO_MINIMUM", "minQuoteOut must be > 0");
|
|
119
|
+
const to = lc(p.proceedsTo ?? p.multicall, "proceedsTo");
|
|
120
|
+
if (to === ZERO) throw new SquidHookError("BAD_RECIPIENT", "proceedsTo");
|
|
121
|
+
const token = lc(p.token, "token");
|
|
122
|
+
const amm = lc(p.floorAmm, "floorAmm");
|
|
123
|
+
return {
|
|
124
|
+
provider: "FLOOR",
|
|
125
|
+
description: "Sell a FLOOR launch token",
|
|
126
|
+
calls: [
|
|
127
|
+
call(SQUID_CALL.Default, token, 0n, bytesToHex(encodeCall("transfer(address,uint256)", [amm, p.tokensIn])), "0x", SQUID_GAS.transfer),
|
|
128
|
+
call(SQUID_CALL.Default, amm, 0n, bytesToHex(encodeCall("sellFromBalance(uint256,uint256,address,uint256)", [p.launchId, p.minQuoteOut, to, p.deadline])), "0x", SQUID_GAS.sell),
|
|
129
|
+
],
|
|
130
|
+
fundToken: token,
|
|
131
|
+
fundAmount: p.tokensIn.toString(),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function encodeMulticallRun(calls: readonly SquidHookCall[]): string {
|
|
136
|
+
const tuples = calls.map((c) => [BigInt(c.callType), c.target, BigInt(c.value), c.callData, c.payload]);
|
|
137
|
+
return bytesToHex(encodeCall("run((uint8,address,uint256,bytes,bytes)[])", [tuples]));
|
|
138
|
+
}
|
package/src/evm/zap.ts
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { bytesToHex, checksumAddress, encodeAbi, hexToBytes } from "./abi.ts";
|
|
4
|
+
import { encodeFunctionData } from "./items.ts";
|
|
5
|
+
|
|
6
|
+
export type ZapFamily = "uniswap-ur" | "v3-router" | "v3-router02" | "algebra" | "v2-router" | "aerodrome-v2";
|
|
7
|
+
|
|
8
|
+
export const UR_V3_SWAP_EXACT_IN = 0x00;
|
|
9
|
+
export const UR_V2_SWAP_EXACT_IN = 0x08;
|
|
10
|
+
export const UR_WRAP_ETH = 0x0b;
|
|
11
|
+
export const UR_UNWRAP_WETH = 0x0c;
|
|
12
|
+
export const UR_SWEEP = 0x04;
|
|
13
|
+
export const UR_MSG_SENDER = "0x0000000000000000000000000000000000000001";
|
|
14
|
+
export const UR_ADDRESS_THIS = "0x0000000000000000000000000000000000000002";
|
|
15
|
+
|
|
16
|
+
export interface ZapRoute {
|
|
17
|
+
|
|
18
|
+
tokens: readonly string[];
|
|
19
|
+
|
|
20
|
+
fees?: readonly number[];
|
|
21
|
+
|
|
22
|
+
stable?: readonly boolean[];
|
|
23
|
+
factory?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ZapSwapParams {
|
|
27
|
+
family: ZapFamily;
|
|
28
|
+
route: ZapRoute;
|
|
29
|
+
|
|
30
|
+
amountIn: bigint;
|
|
31
|
+
|
|
32
|
+
minQuoteOut: bigint;
|
|
33
|
+
|
|
34
|
+
zap: string;
|
|
35
|
+
deadline: bigint;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const fn = (name: string, inputs: readonly { name: string; type: string; components?: any }[]) => ({ type: "function", name, inputs, outputs: [], stateMutability: "payable" }) as const;
|
|
39
|
+
const tup = (fields: [string, string][]) => fields.map(([name, type]) => ({ name, type }));
|
|
40
|
+
|
|
41
|
+
export function v3Path(tokens: readonly string[], fees: readonly number[]): string {
|
|
42
|
+
if (tokens.length < 2 || fees.length !== tokens.length - 1) throw new RangeError("a V3 path is n tokens and n − 1 fees");
|
|
43
|
+
let out = "0x";
|
|
44
|
+
tokens.forEach((t, i) => {
|
|
45
|
+
out += bytesToHex(hexToBytes(t)).slice(2);
|
|
46
|
+
if (i < fees.length) {
|
|
47
|
+
const f = fees[i];
|
|
48
|
+
if (!Number.isInteger(f) || f < 0 || f >= 1 << 24) throw new RangeError(`fee ${f} is not a uint24`);
|
|
49
|
+
out += f.toString(16).padStart(6, "0");
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
return out;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function algebraPath(tokens: readonly string[]): string {
|
|
56
|
+
if (tokens.length < 2) throw new RangeError("a path is at least two tokens");
|
|
57
|
+
return "0x" + tokens.map((t) => bytesToHex(hexToBytes(t)).slice(2)).join("");
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const checkRoute = (p: ZapSwapParams): void => {
|
|
61
|
+
if (p.amountIn <= 0n) throw new RangeError("amountIn must be > 0");
|
|
62
|
+
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");
|
|
64
|
+
};
|
|
65
|
+
|
|
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;
|
|
70
|
+
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, []]));
|
|
73
|
+
} 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, []]));
|
|
76
|
+
}
|
|
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 });
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const REFUND_ETH = [fn("refundETH", [])];
|
|
83
|
+
const REFUND_NATIVE = [fn("refundNativeToken", [])];
|
|
84
|
+
|
|
85
|
+
function v3Router(p: ZapSwapParams, v02: boolean): string {
|
|
86
|
+
const fees = p.route.fees;
|
|
87
|
+
if (!fees) throw new RangeError("a V3 router route needs its fees");
|
|
88
|
+
const [tokenIn, tokenOut] = [p.route.tokens[0], p.route.tokens[p.route.tokens.length - 1]];
|
|
89
|
+
let swap: string;
|
|
90
|
+
if (p.route.tokens.length === 2) {
|
|
91
|
+
const fields: [string, string][] = v02
|
|
92
|
+
? [["tokenIn", "address"], ["tokenOut", "address"], ["fee", "uint24"], ["recipient", "address"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"], ["sqrtPriceLimitX96", "uint160"]]
|
|
93
|
+
: [["tokenIn", "address"], ["tokenOut", "address"], ["fee", "uint24"], ["recipient", "address"], ["deadline", "uint256"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"], ["sqrtPriceLimitX96", "uint160"]];
|
|
94
|
+
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 } });
|
|
96
|
+
} else {
|
|
97
|
+
const fields: [string, string][] = v02
|
|
98
|
+
? [["path", "bytes"], ["recipient", "address"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"]]
|
|
99
|
+
: [["path", "bytes"], ["recipient", "address"], ["deadline", "uint256"], ["amountIn", "uint256"], ["amountOutMinimum", "uint256"]];
|
|
100
|
+
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 } });
|
|
102
|
+
}
|
|
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] });
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function algebra(p: ZapSwapParams): string {
|
|
109
|
+
const [tokenIn, tokenOut] = [p.route.tokens[0], p.route.tokens[p.route.tokens.length - 1]];
|
|
110
|
+
let swap: string;
|
|
111
|
+
if (p.route.tokens.length === 2) {
|
|
112
|
+
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 } });
|
|
114
|
+
} else {
|
|
115
|
+
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 } });
|
|
117
|
+
}
|
|
118
|
+
return encodeFunctionData([fn("multicall", [{ name: "data", type: "bytes[]" }])], "multicall", { data: [swap, encodeFunctionData(REFUND_NATIVE, "refundNativeToken")] });
|
|
119
|
+
}
|
|
120
|
+
|
|
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 });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function aerodromeV2(p: ZapSwapParams): string {
|
|
127
|
+
const { tokens, stable, factory } = p.route;
|
|
128
|
+
if (!stable || stable.length !== tokens.length - 1 || !factory) throw new RangeError("an Aerodrome route needs a stable flag per hop and the factory");
|
|
129
|
+
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" }])];
|
|
131
|
+
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 });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function zapSwapData(p: ZapSwapParams): string {
|
|
136
|
+
checkRoute(p);
|
|
137
|
+
checksumAddress(p.zap);
|
|
138
|
+
switch (p.family) {
|
|
139
|
+
case "uniswap-ur":
|
|
140
|
+
return universalRouter(p);
|
|
141
|
+
case "v3-router":
|
|
142
|
+
return v3Router(p, false);
|
|
143
|
+
case "v3-router02":
|
|
144
|
+
return v3Router(p, true);
|
|
145
|
+
case "algebra":
|
|
146
|
+
return algebra(p);
|
|
147
|
+
case "v2-router":
|
|
148
|
+
return v2Router(p);
|
|
149
|
+
case "aerodrome-v2":
|
|
150
|
+
return aerodromeV2(p);
|
|
151
|
+
}
|
|
152
|
+
}
|
package/src/fixtures.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { addressBytes, base58Encode } from "./codec.ts";
|
|
4
|
+
import { encodeAccount } from "./solana/floor/accounts.ts";
|
|
5
|
+
import { TOKEN_PROGRAM_ID } from "./solana/pda.ts";
|
|
6
|
+
import { encodeAccountV2 } from "./v2/layouts.ts";
|
|
7
|
+
|
|
8
|
+
export function fixtureAddress(i: number): string {
|
|
9
|
+
if (!Number.isInteger(i) || i < 0 || i > 0xffff) throw new RangeError(`fixture slot ${i}`);
|
|
10
|
+
const b = new Uint8Array(32);
|
|
11
|
+
b[0] = 0xf0;
|
|
12
|
+
b[1] = i & 0xff;
|
|
13
|
+
b[2] = i >> 8;
|
|
14
|
+
return base58Encode(b);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function fixtureEvmAddress(i: number): string {
|
|
18
|
+
if (!Number.isInteger(i) || i < 0 || i > 0xffff) throw new RangeError(`fixture slot ${i}`);
|
|
19
|
+
return "0x" + "f0".padEnd(36, "0") + i.toString(16).padStart(4, "0");
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const fixtureKeys = () => ({
|
|
23
|
+
launch: fixtureAddress(1), creator: fixtureAddress(2), wallet: fixtureAddress(3), quoteMint: fixtureAddress(4), baseMint: fixtureAddress(5),
|
|
24
|
+
tokenVault: fixtureAddress(6), quoteVault: fixtureAddress(7), teamSlot: fixtureAddress(8),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export function launchV2Fixture(v: Record<string, unknown> = {}): { address: string; data: Uint8Array } {
|
|
28
|
+
const k = fixtureKeys();
|
|
29
|
+
return {
|
|
30
|
+
address: k.launch,
|
|
31
|
+
data: encodeAccountV2("LaunchV2", {
|
|
32
|
+
createdAt: 1_000n, creator: k.creator, quoteMint: k.quoteMint, quoteTokenProgram: TOKEN_PROGRAM_ID, baseMint: k.baseMint, tokenVault: k.tokenVault,
|
|
33
|
+
quoteVault: k.quoteVault, creatorRef: [k.creator, k.creator, k.creator], ...v,
|
|
34
|
+
}),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function teamSlotFixture(members: readonly { key: string; kind?: number; state?: number }[] = []): { address: string; data: Uint8Array } {
|
|
39
|
+
const k = fixtureKeys();
|
|
40
|
+
if (members.length > 10) throw new RangeError("a Team Slot holds at most 10 members");
|
|
41
|
+
const ms = Array.from({ length: 10 }, (_, i) => ({ key: members[i] ? addressBytes(members[i].key) : new Uint8Array(32), kind: members[i]?.kind ?? 0, state: members[i] ? members[i].state ?? 2 : 0 }));
|
|
42
|
+
return { address: k.teamSlot, data: encodeAccount("TeamSlot", { memberCount: members.length, flags: 1, launch: k.launch, creator: k.creator, members: ms }) };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function holderPositionFixture(v: { amount?: bigint; owed?: bigint; indexSnap?: bigint } = {}): Uint8Array {
|
|
46
|
+
const k = fixtureKeys();
|
|
47
|
+
return encodeAccountV2("HolderPosition", { launch: k.launch, owner: k.wallet, amount: v.amount ?? 0n, indexSnap: v.indexSnap ?? 0n, owed: v.owed ?? 0n });
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function holderBookFixture(v: { index?: bigint; w?: bigint; carry?: bigint } = {}): Uint8Array {
|
|
51
|
+
const k = fixtureKeys();
|
|
52
|
+
return encodeAccountV2("HolderBook", { launch: k.launch, mint: k.baseMint, w: v.w ?? 0n, index: v.index ?? 0n, carry: v.carry ?? 0n, bookedTotal: 0n, paidTotal: 0n });
|
|
53
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
|
|
2
|
+
import { EVM_FUNCTION_SELECTORS } from "./interface.gen.ts";
|
|
3
|
+
import type { EvmFunctionSelectorRow } from "./interface.gen.ts";
|
|
4
|
+
|
|
5
|
+
export { CLAIM_BITS, CLAIM_KNOWN_MASKS, CLAIM_RESERVED, EVM_FUNCTION_SELECTORS, PROGRAM_IDS } from "./interface.gen.ts";
|
|
6
|
+
export type { EvmFunctionSelectorRow } from "./interface.gen.ts";
|
|
7
|
+
import { CLAIM_BITS, CLAIM_RESERVED } from "./interface.gen.ts";
|
|
8
|
+
|
|
9
|
+
export const CLAIM_CREATOR = CLAIM_BITS.solana.creator;
|
|
10
|
+
export const CLAIM_CREATOR_REF = CLAIM_BITS.solana.creatorRef;
|
|
11
|
+
export const CLAIM_TEAM_SLOT = CLAIM_BITS.solana.teamSlot;
|
|
12
|
+
export const CLAIM_TRADER_REF = CLAIM_BITS.solana.traderRef;
|
|
13
|
+
|
|
14
|
+
export const CLAIM_RESERVED_HOLDER = CLAIM_RESERVED.solana.holderV1;
|
|
15
|
+
|
|
16
|
+
export const CLAIM_EXTRA = CLAIM_RESERVED.solana.extra;
|
|
17
|
+
|
|
18
|
+
export const CLAIM_TEAM_BUY = CLAIM_BITS.solana.teamBuy;
|
|
19
|
+
|
|
20
|
+
export const CLAIM_SHARE_BOUGHT = CLAIM_BITS.solana.shareBought;
|
|
21
|
+
|
|
22
|
+
export function evmFunction(name: string, source?: string): EvmFunctionSelectorRow {
|
|
23
|
+
const rows = EVM_FUNCTION_SELECTORS.filter((r) => r.name === name && (source === undefined || r.sources.includes(source)));
|
|
24
|
+
if (rows.length === 0) throw new Error(`no printed EVM function ${name}${source ? ` in ${source}` : ""}`);
|
|
25
|
+
if (rows.length > 1) throw new Error(`${name} is overloaded (${rows.map((r) => r.signature).join(", ")}): pass the source module`);
|
|
26
|
+
return rows[0];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const evmSignature = (name: string, source?: string): string => evmFunction(name, source).signature;
|