@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,41 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { checksumAddress } from "../abi.js";
|
|
4
|
+
import { decodeEventLog, itemTopic } from "../items.js";
|
|
5
|
+
|
|
6
|
+
import { FLOOR_AMM_ABI, FLOOR_AMM_UNFROZEN_EVENTS, FLOOR_ZAP_ABI } from "./iface.js";
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
const ALL = [...FLOOR_AMM_ABI, ...FLOOR_ZAP_ABI, ...FLOOR_AMM_UNFROZEN_EVENTS];
|
|
11
|
+
|
|
12
|
+
let unfrozen ;
|
|
13
|
+
const UNFROZEN = () => (unfrozen ??= new Set(FLOOR_AMM_UNFROZEN_EVENTS.map((e) => itemTopic(e))));
|
|
14
|
+
|
|
15
|
+
export const FLOOR_EVENT_TOPICS = {};
|
|
16
|
+
for (const i of ALL.filter((i) => i.type === "event").reverse()) {
|
|
17
|
+
const name = i.name;
|
|
18
|
+
const first = ALL.find((j) => j.type === "event" && j.name === name) ;
|
|
19
|
+
if (!(name in FLOOR_EVENT_TOPICS))
|
|
20
|
+
Object.defineProperty(FLOOR_EVENT_TOPICS, name, {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
get: () => {
|
|
24
|
+
const value = itemTopic(first);
|
|
25
|
+
Object.defineProperty(FLOOR_EVENT_TOPICS, name, { value, enumerable: true, configurable: true, writable: true });
|
|
26
|
+
return value;
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function decodeFloorLog(log , from ) {
|
|
32
|
+
if (from && (!log.address || !from.some((a) => a.toLowerCase() === log.address .toLowerCase()))) return null;
|
|
33
|
+
const d = decodeEventLog(ALL, log);
|
|
34
|
+
if (!d) return null;
|
|
35
|
+
if (UNFROZEN().has((log.topics[0] ?? "").toLowerCase())) d.unfrozen = true;
|
|
36
|
+
return log.address ? { ...d, address: checksumAddress(log.address) } : d;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function decodeFloorLogs(logs , from ) {
|
|
40
|
+
return logs.map((l) => decodeFloorLog(l, from)).filter((e) => e !== null);
|
|
41
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { CLAIM_BITS, CLAIM_RESERVED, CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF, CLAIM_RESERVED_HOLDER, CLAIM_EXTRA, CLAIM_TEAM_BUY, CLAIM_SHARE_BOUGHT } from "../../generated/index.js";
|
|
4
|
+
export { FLOOR_AMM_ABI, FLOOR_AMM_UNFROZEN_EVENTS, FLOOR_TOKEN_ABI, FLOOR_ZAP_ABI } from "./abi.gen.js";
|
|
5
|
+
|
|
6
|
+
export { CLAIM_CREATOR, CLAIM_CREATOR_REF, CLAIM_TEAM_SLOT, CLAIM_TRADER_REF, CLAIM_RESERVED_HOLDER, CLAIM_EXTRA, CLAIM_TEAM_BUY, CLAIM_SHARE_BOUGHT };
|
|
7
|
+
|
|
8
|
+
export const CLAIM_PAYOUT = CLAIM_BITS.evm.payout;
|
|
9
|
+
|
|
10
|
+
export const CLAIM_KNOWN = CLAIM_CREATOR | CLAIM_CREATOR_REF | CLAIM_TEAM_SLOT | CLAIM_TRADER_REF | CLAIM_EXTRA | CLAIM_PAYOUT | CLAIM_TEAM_BUY | CLAIM_SHARE_BOUGHT;
|
|
11
|
+
|
|
12
|
+
export const OWED_BITS = [
|
|
13
|
+
["creator", CLAIM_CREATOR],
|
|
14
|
+
["creatorRef", CLAIM_CREATOR_REF],
|
|
15
|
+
["teamSlot", CLAIM_TEAM_SLOT],
|
|
16
|
+
["traderRef", CLAIM_TRADER_REF],
|
|
17
|
+
["extra", CLAIM_EXTRA],
|
|
18
|
+
] ;
|
|
19
|
+
|
|
20
|
+
export const TRADE_CREDIT_PAYEES = [
|
|
21
|
+
"creator",
|
|
22
|
+
"creatorRef1", "creatorRef2", "creatorRef3",
|
|
23
|
+
"member0", "member1", "member2", "member3", "member4", "member5", "member6", "member7", "member8", "member9",
|
|
24
|
+
"traderRef1", "traderRef2", "traderRef3",
|
|
25
|
+
"holder",
|
|
26
|
+
"extra0", "extra1", "extra2", "extra3",
|
|
27
|
+
] ;
|
|
28
|
+
|
|
29
|
+
export const NATIVE_QUOTE = "0x0000000000000000000000000000000000000000";
|
|
30
|
+
|
|
31
|
+
export const MEMBER_WALLET = 0;
|
|
32
|
+
export const MEMBER_HANDLE = 1;
|
|
33
|
+
export const MEMBER_EMPTY = 0;
|
|
34
|
+
export const MEMBER_REQUESTED = 1;
|
|
35
|
+
export const MEMBER_PAID = 2;
|
|
36
|
+
export const MEMBER_CANCELLED = 3;
|
|
37
|
+
|
|
38
|
+
export const EVM_TOKEN_DECIMALS = 18;
|
|
39
|
+
|
|
40
|
+
export { SWEPT_BURN, SWEPT_DUST, SWEPT_EXIT } from "../../solana/floor/events.js";
|
|
41
|
+
|
|
42
|
+
export const MATH_ERROR_CODES = ["VaultShareOutOfRange", "Zero", "Overflow", "NotDue", "NothingToDo",
|
|
43
|
+
"BookEmpty", "StepBudget", "SqrtRange", "RowTooManyRanges", "RowBands", "RowShares", "RowNotFeasible", "TeamBuyOverShare", "LotsFull", "IndexGuard", "NothingToSettle", "SlippageExceeded",
|
|
44
|
+
"QuoteRowCannotOpen", "RangeCover", "RowLength", "RowVersion", "RowEdges", "RowTail", "RowBounds", "RowFees", "RowTeam", "RowFlags", "RowMsClass", "RowNonCanonical"];
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { AbiError, bytesToHex, decodeAbi, encodeAbi, parseType } from "./abi.js";
|
|
4
|
+
|
|
5
|
+
import { keccak256 } from "./keccak.js";
|
|
6
|
+
|
|
7
|
+
export function paramType(p ) {
|
|
8
|
+
if (!p.type.startsWith("tuple")) return p.type;
|
|
9
|
+
if (!p.components) throw new AbiError(`tuple ${p.name} has no components`);
|
|
10
|
+
return `(${p.components.map(paramType).join(",")})${p.type.slice("tuple".length)}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function itemSignature(item ) {
|
|
14
|
+
if (!item.name) throw new AbiError(`a ${item.type} has no signature`);
|
|
15
|
+
return `${item.name}(${(item.inputs ?? []).map(paramType).join(",")})`;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const hash = (s ) => keccak256(new TextEncoder().encode(s));
|
|
19
|
+
|
|
20
|
+
const TOPICS = new WeakMap ();
|
|
21
|
+
export const itemTopic = (item ) => {
|
|
22
|
+
let t = TOPICS.get(item);
|
|
23
|
+
if (t === undefined) TOPICS.set(item, (t = bytesToHex(hash(itemSignature(item)))));
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
export const itemSelector = (item ) => itemTopic(item).slice(0, 10);
|
|
27
|
+
|
|
28
|
+
function arrayLevels(t ) {
|
|
29
|
+
const dims = [];
|
|
30
|
+
let s = t;
|
|
31
|
+
while (s.endsWith("]")) {
|
|
32
|
+
const i = s.lastIndexOf("[");
|
|
33
|
+
const n = s.slice(i + 1, -1);
|
|
34
|
+
dims.unshift(n === "" ? null : Number(n));
|
|
35
|
+
s = s.slice(0, i);
|
|
36
|
+
}
|
|
37
|
+
return { base: s, dims };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function toValue(p , v , depth = 0) {
|
|
41
|
+
const { base, dims } = arrayLevels(p.type);
|
|
42
|
+
if (depth < dims.length) {
|
|
43
|
+
if (!Array.isArray(v)) throw new AbiError(`${p.name}: expected an array`);
|
|
44
|
+
return v.map((x) => toValue(p, x, depth + 1));
|
|
45
|
+
}
|
|
46
|
+
if (base === "tuple") {
|
|
47
|
+
const c = p.components ?? [];
|
|
48
|
+
if (Array.isArray(v)) return c.map((q, i) => toValue(q, v[i]));
|
|
49
|
+
if (v === null || typeof v !== "object") throw new AbiError(`${p.name}: expected a struct`);
|
|
50
|
+
const o = v ;
|
|
51
|
+
return c.map((q, i) => {
|
|
52
|
+
const key = q.name !== "" ? q.name : String(i);
|
|
53
|
+
if (!(key in o)) throw new AbiError(`${p.name}: missing ${key}`);
|
|
54
|
+
return toValue(q, o[key]);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (/^u?int/.test(base)) {
|
|
58
|
+
if (typeof v === "number" && Number.isSafeInteger(v)) return BigInt(v);
|
|
59
|
+
if (typeof v !== "bigint") throw new AbiError(`${p.name}: expected a bigint`);
|
|
60
|
+
return v;
|
|
61
|
+
}
|
|
62
|
+
return v ;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function fromValue(p , v , depth = 0) {
|
|
66
|
+
const { base, dims } = arrayLevels(p.type);
|
|
67
|
+
if (depth < dims.length) return (v ).map((x) => fromValue(p, x, depth + 1));
|
|
68
|
+
if (base === "tuple") return named(p.components ?? [], v );
|
|
69
|
+
return v;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function named(params , values ) {
|
|
73
|
+
const out = {};
|
|
74
|
+
params.forEach((p, i) => {
|
|
75
|
+
out[p.name !== "" ? p.name : String(i)] = fromValue(p, values[i]);
|
|
76
|
+
});
|
|
77
|
+
return out;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function encodeParams(params , args ) {
|
|
81
|
+
const vals = params.map((p, i) => {
|
|
82
|
+
if (Array.isArray(args)) return toValue(p, args[i]);
|
|
83
|
+
const o = args ;
|
|
84
|
+
const key = p.name !== "" ? p.name : String(i);
|
|
85
|
+
if (!(key in o)) throw new AbiError(`missing argument ${key}`);
|
|
86
|
+
return toValue(p, o[key]);
|
|
87
|
+
});
|
|
88
|
+
return encodeAbi(params.map(paramType), vals);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function decodeParams(params , data ) {
|
|
92
|
+
return named(params, decodeAbi(params.map(paramType), data));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function findItem(abi , kind , key ) {
|
|
96
|
+
const all = abi.filter((i) => i.type === kind);
|
|
97
|
+
const bySig = all.filter((i) => key.includes("(") && itemSignature(i) === key.replace(/\s+/g, ""));
|
|
98
|
+
if (bySig.length === 1) return bySig[0];
|
|
99
|
+
const byName = all.filter((i) => i.name === key);
|
|
100
|
+
if (byName.length === 1) return byName[0];
|
|
101
|
+
throw new AbiError(byName.length > 1 ? `${kind} ${key} is overloaded: name it by signature` : `no ${kind} ${key}`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
const INDEX = new WeakMap ();
|
|
105
|
+
function lookup(abi , kind , key ) {
|
|
106
|
+
let ix = INDEX.get(abi);
|
|
107
|
+
if (!ix || ix.len !== abi.length) INDEX.set(abi, (ix = { len: abi.length, by: {} }));
|
|
108
|
+
let m = ix.by[kind];
|
|
109
|
+
if (!m) {
|
|
110
|
+
m = new Map();
|
|
111
|
+
for (const i of abi) {
|
|
112
|
+
if (i.type !== kind || (kind === "event" && i.anonymous)) continue;
|
|
113
|
+
const k = kind === "event" ? itemTopic(i) : itemSelector(i);
|
|
114
|
+
if (!m.has(k)) m.set(k, i);
|
|
115
|
+
}
|
|
116
|
+
ix.by[kind] = m;
|
|
117
|
+
}
|
|
118
|
+
return m.get(key);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function encodeFunctionData(abi , name , args = {}) {
|
|
122
|
+
const f = findItem(abi, "function", name);
|
|
123
|
+
const body = encodeParams(f.inputs ?? [], args);
|
|
124
|
+
return itemSelector(f) + bytesToHex(body).slice(2);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function decodeFunctionData(abi , data ) {
|
|
128
|
+
const hex = typeof data === "string" ? data.toLowerCase() : bytesToHex(data);
|
|
129
|
+
const f = lookup(abi, "function", hex.slice(0, 10));
|
|
130
|
+
if (!f) throw new AbiError(`no function with selector ${hex.slice(0, 10)}`);
|
|
131
|
+
return { name: f.name , args: decodeParams(f.inputs ?? [], "0x" + hex.slice(10)) };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function decodeFunctionResult(abi , name , data ) {
|
|
135
|
+
return decodeParams(findItem(abi, "function", name).outputs ?? [], data);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function encodeFunctionResult(abi , name , result ) {
|
|
139
|
+
return bytesToHex(encodeParams(findItem(abi, "function", name).outputs ?? [], result));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const isDynamicType = (t ) => {
|
|
143
|
+
const at = parseType(t);
|
|
144
|
+
return at.kind === "bytes" || at.kind === "string" || at.kind === "array" || at.kind === "tuple";
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
export function decodeEventLog(abi , log ) {
|
|
148
|
+
const t0 = (log.topics[0] ?? "").toLowerCase();
|
|
149
|
+
const ev = lookup(abi, "event", t0);
|
|
150
|
+
if (!ev) return null;
|
|
151
|
+
const inputs = ev.inputs ?? [];
|
|
152
|
+
const indexed = inputs.filter((p) => p.indexed);
|
|
153
|
+
if (log.topics.length !== indexed.length + 1) throw new AbiError(`${ev.name}: expected ${indexed.length + 1} topics, got ${log.topics.length}`);
|
|
154
|
+
const plain = inputs.filter((p) => !p.indexed);
|
|
155
|
+
const body = decodeAbi(plain.map(paramType), log.data);
|
|
156
|
+
const out = {};
|
|
157
|
+
let ti = 1;
|
|
158
|
+
let di = 0;
|
|
159
|
+
inputs.forEach((p, i) => {
|
|
160
|
+
const key = p.name !== "" ? p.name : String(i);
|
|
161
|
+
if (p.indexed) {
|
|
162
|
+
const topic = log.topics[ti++];
|
|
163
|
+
out[key] = isDynamicType(paramType(p)) ? topic.toLowerCase() : fromValue(p, decodeAbi([paramType(p)], topic)[0]);
|
|
164
|
+
} else out[key] = fromValue(p, body[di++]);
|
|
165
|
+
});
|
|
166
|
+
return { name: ev.name , args: out };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export function encodeEventLog(abi , name , args ) {
|
|
170
|
+
const ev = findItem(abi, "event", name);
|
|
171
|
+
const inputs = ev.inputs ?? [];
|
|
172
|
+
const topics = [itemTopic(ev)];
|
|
173
|
+
for (const p of inputs.filter((q) => q.indexed)) {
|
|
174
|
+
const t = paramType(p);
|
|
175
|
+
if (isDynamicType(t)) throw new AbiError(`${name}.${p.name}: an indexed dynamic value is hashed; pass the hash`);
|
|
176
|
+
topics.push(bytesToHex(encodeAbi([t], [toValue(p, args[p.name])])));
|
|
177
|
+
}
|
|
178
|
+
const plain = inputs.filter((p) => !p.indexed);
|
|
179
|
+
return { topics, data: bytesToHex(encodeParams(plain, args)) };
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const ERROR_STRING = { type: "error", name: "Error", inputs: [{ name: "message", type: "string" }] };
|
|
183
|
+
const PANIC = { type: "error", name: "Panic", inputs: [{ name: "code", type: "uint256" }] };
|
|
184
|
+
|
|
185
|
+
export function decodeErrorResult(abi , data ) {
|
|
186
|
+
const hex = typeof data === "string" ? data.toLowerCase() : bytesToHex(data);
|
|
187
|
+
if (hex.length < 10) return null;
|
|
188
|
+
const sel = hex.slice(0, 10);
|
|
189
|
+
const e = abi.find((i) => i.type === "error" && itemSelector(i) === sel) ?? [ERROR_STRING, PANIC].find((i) => itemSelector(i) === sel);
|
|
190
|
+
if (!e) return null;
|
|
191
|
+
return { name: e.name , args: decodeParams(e.inputs ?? [], "0x" + hex.slice(10)) };
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export function encodeErrorResult(abi , name , args = {}) {
|
|
195
|
+
const e = findItem(abi, "error", name);
|
|
196
|
+
return itemSelector(e) + bytesToHex(encodeParams(e.inputs ?? [], args)).slice(2);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export function abiOfArtifact(json ) {
|
|
200
|
+
const a = Array.isArray(json) ? json : (json )?.abi;
|
|
201
|
+
if (!Array.isArray(a)) throw new AbiError("not an ABI or a forge artifact");
|
|
202
|
+
return a ;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
export function signatures(abi ) {
|
|
206
|
+
const of = (k ) => abi.filter((i) => i.type === k).map(itemSignature).sort();
|
|
207
|
+
return { functions: of("function"), events: of("event"), errors: of("error") };
|
|
208
|
+
}
|
|
209
|
+
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
const RC_HI = new Uint32Array(24);
|
|
4
|
+
const RC_LO = new Uint32Array(24);
|
|
5
|
+
{
|
|
6
|
+
|
|
7
|
+
let r = 1;
|
|
8
|
+
for (let i = 0; i < 24; i++) {
|
|
9
|
+
let hi = 0;
|
|
10
|
+
let lo = 0;
|
|
11
|
+
for (let j = 0; j < 7; j++) {
|
|
12
|
+
r = ((r << 1) ^ ((r >> 7) * 0x71)) & 0xff;
|
|
13
|
+
if (r & 2) {
|
|
14
|
+
const bit = (1 << j) - 1;
|
|
15
|
+
if (bit < 32) lo ^= 1 << bit;
|
|
16
|
+
else hi ^= 1 << (bit - 32);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
RC_HI[i] = hi >>> 0;
|
|
20
|
+
RC_LO[i] = lo >>> 0;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const ROT = [0, 1, 62, 28, 27, 36, 44, 6, 55, 20, 3, 10, 43, 25, 39, 41, 45, 15, 21, 8, 18, 2, 61, 56, 14];
|
|
24
|
+
|
|
25
|
+
const bh = new Uint32Array(25);
|
|
26
|
+
const bl = new Uint32Array(25);
|
|
27
|
+
const ch = new Uint32Array(5);
|
|
28
|
+
const cl = new Uint32Array(5);
|
|
29
|
+
|
|
30
|
+
function keccakF(s ) {
|
|
31
|
+
for (let round = 0; round < 24; round++) {
|
|
32
|
+
for (let x = 0; x < 5; x++) {
|
|
33
|
+
ch[x] = s[2 * x] ^ s[2 * (x + 5)] ^ s[2 * (x + 10)] ^ s[2 * (x + 15)] ^ s[2 * (x + 20)];
|
|
34
|
+
cl[x] = s[2 * x + 1] ^ s[2 * (x + 5) + 1] ^ s[2 * (x + 10) + 1] ^ s[2 * (x + 15) + 1] ^ s[2 * (x + 20) + 1];
|
|
35
|
+
}
|
|
36
|
+
for (let x = 0; x < 5; x++) {
|
|
37
|
+
const h1 = ch[(x + 1) % 5];
|
|
38
|
+
const l1 = cl[(x + 1) % 5];
|
|
39
|
+
const dh = ch[(x + 4) % 5] ^ ((h1 << 1) | (l1 >>> 31));
|
|
40
|
+
const dl = cl[(x + 4) % 5] ^ ((l1 << 1) | (h1 >>> 31));
|
|
41
|
+
for (let y = 0; y < 25; y += 5) {
|
|
42
|
+
s[2 * (x + y)] ^= dh;
|
|
43
|
+
s[2 * (x + y) + 1] ^= dl;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
for (let x = 0; x < 5; x++) {
|
|
48
|
+
for (let y = 0; y < 5; y++) {
|
|
49
|
+
const i = x + 5 * y;
|
|
50
|
+
const h = s[2 * i];
|
|
51
|
+
const l = s[2 * i + 1];
|
|
52
|
+
const r = ROT[i];
|
|
53
|
+
let rh ;
|
|
54
|
+
let rl ;
|
|
55
|
+
|
|
56
|
+
if (r === 0) {
|
|
57
|
+
rh = h;
|
|
58
|
+
rl = l;
|
|
59
|
+
} else if (r < 32) {
|
|
60
|
+
rh = (h << r) | (l >>> (32 - r));
|
|
61
|
+
rl = (l << r) | (h >>> (32 - r));
|
|
62
|
+
} else if (r === 32) {
|
|
63
|
+
rh = l;
|
|
64
|
+
rl = h;
|
|
65
|
+
} else {
|
|
66
|
+
rh = (l << (r - 32)) | (h >>> (64 - r));
|
|
67
|
+
rl = (h << (r - 32)) | (l >>> (64 - r));
|
|
68
|
+
}
|
|
69
|
+
const j = y + 5 * ((2 * x + 3 * y) % 5);
|
|
70
|
+
bh[j] = rh;
|
|
71
|
+
bl[j] = rl;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
for (let y = 0; y < 25; y += 5) {
|
|
76
|
+
for (let x = 0; x < 5; x++) {
|
|
77
|
+
s[2 * (x + y)] = bh[x + y] ^ (~bh[((x + 1) % 5) + y] & bh[((x + 2) % 5) + y]);
|
|
78
|
+
s[2 * (x + y) + 1] = bl[x + y] ^ (~bl[((x + 1) % 5) + y] & bl[((x + 2) % 5) + y]);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
s[0] ^= RC_HI[round];
|
|
83
|
+
s[1] ^= RC_LO[round];
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function keccak256(data ) {
|
|
88
|
+
return sponge256(data, 0x01);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function sponge256(data , pad ) {
|
|
92
|
+
const rate = 136;
|
|
93
|
+
const s = new Uint32Array(50);
|
|
94
|
+
const padded = new Uint8Array(Math.floor(data.length / rate) * rate + rate);
|
|
95
|
+
padded.set(data);
|
|
96
|
+
padded[data.length] ^= pad;
|
|
97
|
+
padded[padded.length - 1] ^= 0x80;
|
|
98
|
+
for (let off = 0; off < padded.length; off += rate) {
|
|
99
|
+
for (let i = 0; i < rate / 8; i++) {
|
|
100
|
+
const o = off + 8 * i;
|
|
101
|
+
const lo = padded[o] | (padded[o + 1] << 8) | (padded[o + 2] << 16) | (padded[o + 3] << 24);
|
|
102
|
+
const hi = padded[o + 4] | (padded[o + 5] << 8) | (padded[o + 6] << 16) | (padded[o + 7] << 24);
|
|
103
|
+
s[2 * i] ^= hi;
|
|
104
|
+
s[2 * i + 1] ^= lo;
|
|
105
|
+
}
|
|
106
|
+
keccakF(s);
|
|
107
|
+
}
|
|
108
|
+
const out = new Uint8Array(32);
|
|
109
|
+
for (let i = 0; i < 4; i++) {
|
|
110
|
+
const hi = s[2 * i];
|
|
111
|
+
const lo = s[2 * i + 1];
|
|
112
|
+
for (let b = 0; b < 4; b++) {
|
|
113
|
+
out[8 * i + b] = (lo >>> (8 * b)) & 0xff;
|
|
114
|
+
out[8 * i + 4 + b] = (hi >>> (8 * b)) & 0xff;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { checksumAddress, decodeAbi, encodeAbi, encodeCall, bytesToHex, functionSelector, hexToBytes } from "./abi.js";
|
|
4
|
+
|
|
5
|
+
export const MULTICALL3_ADDRESS = "0xcA11bde05977b3631167028862bE2a173976CA11";
|
|
6
|
+
|
|
7
|
+
const AGGREGATE3 = "aggregate3((address,bool,bytes)[])";
|
|
8
|
+
const RESULTS = ["(bool,bytes)[]"] ;
|
|
9
|
+
|
|
10
|
+
export const AGGREGATE3_SELECTOR = bytesToHex(functionSelector(AGGREGATE3));
|
|
11
|
+
|
|
12
|
+
export function aggregate3Call(calls , multicall = MULTICALL3_ADDRESS) {
|
|
13
|
+
const data = encodeCall(AGGREGATE3, [calls.map((c) => [checksumAddress(c.target), c.allowFailure, c.callData])]);
|
|
14
|
+
return { to: checksumAddress(multicall), data: bytesToHex(data), value: 0n };
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function decodeAggregate3Result(data ) {
|
|
18
|
+
const [rows] = decodeAbi(RESULTS, data) ;
|
|
19
|
+
return rows.map(([success, returnData]) => ({ success, returnData }));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function decodeAggregate3Call(data ) {
|
|
23
|
+
const b = hexToBytes(data);
|
|
24
|
+
if (bytesToHex(b.slice(0, 4)) !== AGGREGATE3_SELECTOR) throw new RangeError("not aggregate3 calldata");
|
|
25
|
+
const [rows] = decodeAbi(["(address,bool,bytes)[]"], b.slice(4)) ;
|
|
26
|
+
return rows.map(([target, allowFailure, callData]) => ({ target, allowFailure, callData }));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const encodeAggregate3Result = (results ) =>
|
|
30
|
+
bytesToHex(encodeAbi(RESULTS, [results.map((r) => [r.success, r.returnData])]));
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
import { encodeFunctionData } from "./items.js";
|
|
4
|
+
import { deadlineArg } from "./floor/calls.js";
|
|
5
|
+
import { EVM_NATIVE, NoRouteError, coinKey } from "../route/graph.js";
|
|
6
|
+
import { chainById } from "./chains.js";
|
|
7
|
+
|
|
8
|
+
const BUY = [
|
|
9
|
+
{ name: "path", type: "uint256[]" },
|
|
10
|
+
{ name: "tokenIn", type: "address" },
|
|
11
|
+
{ name: "amountIn", type: "uint256" },
|
|
12
|
+
{ name: "swapData", type: "bytes" },
|
|
13
|
+
{ name: "minQuoteIn", type: "uint256" },
|
|
14
|
+
{ name: "minTokensOut", type: "uint256[]" },
|
|
15
|
+
{ name: "to", type: "address" },
|
|
16
|
+
{ name: "deadline", type: "uint256" },
|
|
17
|
+
] ;
|
|
18
|
+
|
|
19
|
+
const SELL = [
|
|
20
|
+
{ name: "path", type: "uint256[]" },
|
|
21
|
+
{ name: "tokensIn", type: "uint256" },
|
|
22
|
+
{ name: "minQuoteOut", type: "uint256[]" },
|
|
23
|
+
{ name: "tokenOut", type: "address" },
|
|
24
|
+
{ name: "swapIn", type: "uint256" },
|
|
25
|
+
{ name: "swapData", type: "bytes" },
|
|
26
|
+
{ name: "minOut", type: "uint256" },
|
|
27
|
+
{ name: "to", type: "address" },
|
|
28
|
+
{ name: "deadline", type: "uint256" },
|
|
29
|
+
] ;
|
|
30
|
+
const buyArg = (name ) => ({ name, type: "tuple", internalType: "struct IFloorRouter.Buy", components: BUY }) ;
|
|
31
|
+
const sellArg = (name ) => ({ name, type: "tuple", internalType: "struct IFloorRouter.Sell", components: SELL }) ;
|
|
32
|
+
const err = (name , inputs = []) => ({ type: "error", name, inputs }) ;
|
|
33
|
+
const SELL_OUT = [{ name: "amountOut", type: "uint256" }, { name: "quoteOut", type: "uint256" }] ;
|
|
34
|
+
|
|
35
|
+
export const FLOOR_ROUTER_ABI = [
|
|
36
|
+
{ type: "function", name: "floorAmm", inputs: [], outputs: [{ name: "", type: "address", internalType: "contract IFloorAmm" }], stateMutability: "view" },
|
|
37
|
+
{ type: "function", name: "router", inputs: [], outputs: [{ name: "", type: "address" }], stateMutability: "view" },
|
|
38
|
+
{ type: "function", name: "sell", inputs: [sellArg("s")], outputs: SELL_OUT, stateMutability: "nonpayable" },
|
|
39
|
+
{
|
|
40
|
+
type: "function",
|
|
41
|
+
name: "sellWithPermit",
|
|
42
|
+
inputs: [sellArg("s"), { name: "permitDeadline", type: "uint256" }, { name: "v", type: "uint8" }, { name: "r", type: "bytes32" }, { name: "ss", type: "bytes32" }],
|
|
43
|
+
outputs: SELL_OUT,
|
|
44
|
+
stateMutability: "nonpayable",
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
type: "event",
|
|
48
|
+
name: "Sold",
|
|
49
|
+
anonymous: false,
|
|
50
|
+
inputs: [
|
|
51
|
+
{ name: "sender", type: "address", indexed: true },
|
|
52
|
+
{ name: "to", type: "address", indexed: true },
|
|
53
|
+
{ name: "launchId", type: "uint256", indexed: true },
|
|
54
|
+
{ name: "tokensIn", type: "uint256", indexed: false },
|
|
55
|
+
{ name: "hops", type: "uint256", indexed: false },
|
|
56
|
+
{ name: "tokenOut", type: "address", indexed: false },
|
|
57
|
+
{ name: "quoteOut", type: "uint256", indexed: false },
|
|
58
|
+
{ name: "amountOut", type: "uint256", indexed: false },
|
|
59
|
+
{ name: "refundQuote", type: "uint256", indexed: false },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
type: "event",
|
|
64
|
+
name: "Bought",
|
|
65
|
+
anonymous: false,
|
|
66
|
+
inputs: [
|
|
67
|
+
{ name: "sender", type: "address", indexed: true },
|
|
68
|
+
{ name: "to", type: "address", indexed: true },
|
|
69
|
+
{ name: "launchId", type: "uint256", indexed: true },
|
|
70
|
+
{ name: "tokenIn", type: "address", indexed: false },
|
|
71
|
+
{ name: "amountIn", type: "uint256", indexed: false },
|
|
72
|
+
{ name: "hops", type: "uint256", indexed: false },
|
|
73
|
+
{ name: "quoteIn", type: "uint256", indexed: false },
|
|
74
|
+
{ name: "tokensOut", type: "uint256", indexed: false },
|
|
75
|
+
{ name: "refundIn", type: "uint256", indexed: false },
|
|
76
|
+
],
|
|
77
|
+
},
|
|
78
|
+
err("NotRouter"),
|
|
79
|
+
err("SwapFailed", [{ name: "reason", type: "bytes" }]),
|
|
80
|
+
err("SwapDataNotEmpty"),
|
|
81
|
+
err("BadPath"),
|
|
82
|
+
err("QuoteShort"),
|
|
83
|
+
err("SlippageExceeded"),
|
|
84
|
+
err("Expired"),
|
|
85
|
+
err("BadValue"),
|
|
86
|
+
err("Reentrancy"),
|
|
87
|
+
|
|
88
|
+
{ type: "function", name: "buy", inputs: [buyArg("b")], outputs: [{ name: "tokensOut", type: "uint256" }, { name: "quoteIn", type: "uint256" }], stateMutability: "payable" },
|
|
89
|
+
{ type: "function", name: "swap", inputs: [sellArg("s"), buyArg("b")], outputs: [{ name: "tokensOut", type: "uint256" }], stateMutability: "nonpayable" },
|
|
90
|
+
] ;
|
|
91
|
+
|
|
92
|
+
export const SWAP_IN_ALL = (1n << 256n) - 1n;
|
|
93
|
+
|
|
94
|
+
export function checkFloorHops(chainId , floorHops ) {
|
|
95
|
+
if (typeof chainId !== "number" || !Number.isInteger(chainId)) throw new RangeError("an EVM route names its chainId");
|
|
96
|
+
const row = chainById(chainId);
|
|
97
|
+
if (row?.maxFloorHops !== undefined && floorHops > row.maxFloorHops) {
|
|
98
|
+
throw new NoRouteError("TooManyHops", `${floorHops} FLOOR trades: ${row.name} (chain ${row.chainId}) carries at most ${row.maxFloorHops} per route`);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const checkPath = (path , mins ) => {
|
|
103
|
+
if (path.length === 0) throw new RangeError("BadPath: an empty path");
|
|
104
|
+
if (mins.length !== path.length) throw new RangeError("BadPath: one min-out a hop");
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
export function encodeRouterBuy(b , chainId ) {
|
|
108
|
+
checkPath(b.path, b.minTokensOut);
|
|
109
|
+
checkFloorHops(chainId, b.path.length);
|
|
110
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "buy", { b });
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export function encodeRouterSell(s , chainId ) {
|
|
114
|
+
checkPath(s.path, s.minQuoteOut);
|
|
115
|
+
checkFloorHops(chainId, s.path.length);
|
|
116
|
+
if (s.tokensIn === 0n || coinKey(s.to) === EVM_NATIVE) throw new RangeError("BadValue: tokensIn > 0 and a recipient");
|
|
117
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "sell", { s });
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export function encodeRouterSellWithPermit(s , permit , chainId ) {
|
|
121
|
+
checkPath(s.path, s.minQuoteOut);
|
|
122
|
+
checkFloorHops(chainId, s.path.length);
|
|
123
|
+
if (s.tokensIn === 0n || coinKey(s.to) === EVM_NATIVE) throw new RangeError("BadValue: tokensIn > 0 and a recipient");
|
|
124
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "sellWithPermit", { s, permitDeadline: permit.deadline, v: BigInt(permit.v), r: permit.r, ss: permit.s });
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export function encodeRouterSwap(s , b , chainId ) {
|
|
128
|
+
checkPath(s.path, s.minQuoteOut);
|
|
129
|
+
checkPath(b.path, b.minTokensOut);
|
|
130
|
+
checkFloorHops(chainId, s.path.length + b.path.length);
|
|
131
|
+
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)");
|
|
132
|
+
return encodeFunctionData(FLOOR_ROUTER_ABI, "swap", { s, b });
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const idOf = (pool ) => {
|
|
136
|
+
if (pool.launchId === undefined) throw new RangeError(`pool ${pool.symbol}: an EVM pool needs its launchId`);
|
|
137
|
+
return pool.launchId;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
export function evmRouteCall(q , p ) {
|
|
141
|
+
if (q.path.chain !== "evm") throw new RangeError("evmRouteCall builds EVM routes; Solana routes are `routeInstructions`");
|
|
142
|
+
checkFloorHops(p.chainId, q.hops.filter((h) => h.hop.kind !== "edge").length);
|
|
143
|
+
deadlineArg(p.deadline);
|
|
144
|
+
const hs = q.hops;
|
|
145
|
+
const sells = hs.filter((h) => h.hop.kind === "sell");
|
|
146
|
+
const buys = hs.filter((h) => h.hop.kind === "buy");
|
|
147
|
+
const edges = hs.filter((h) => h.hop.kind === "edge");
|
|
148
|
+
if (edges.length > 1) throw new RangeError("BadPath: one edge leg a route");
|
|
149
|
+
const sd = p.swapData ?? [];
|
|
150
|
+
if (sd.length !== edges.length) throw new RangeError(`the route has ${edges.length} edge leg(s): pass ${edges.length} swapData`);
|
|
151
|
+
const edge = edges[0] ;
|
|
152
|
+
const pool = (h ) => (h.hop ).pool;
|
|
153
|
+
const ids = (xs ) => xs.map((h) => idOf(pool(h)));
|
|
154
|
+
const mins = (xs ) => xs.map((h) => h.minOut);
|
|
155
|
+
const gas = hs.every((h) => h.gas !== undefined) ? hs.reduce((n, h) => n + (h.gas ), 0n) : undefined;
|
|
156
|
+
const call = (data , value ) => ({ to: p.router, data, value, ...(gas !== undefined ? { gas } : {}) });
|
|
157
|
+
|
|
158
|
+
if (sells.length === 0) {
|
|
159
|
+
|
|
160
|
+
const base = pool(buys[0]).quote;
|
|
161
|
+
const tokenIn = edge ? edge.hop.from : base;
|
|
162
|
+
const native = coinKey(tokenIn) === EVM_NATIVE;
|
|
163
|
+
const r = { 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 };
|
|
164
|
+
return call(encodeRouterBuy(r, p.chainId), native ? q.amountIn : 0n);
|
|
165
|
+
}
|
|
166
|
+
const last = sells[sells.length - 1];
|
|
167
|
+
|
|
168
|
+
const s = {
|
|
169
|
+
path: ids(sells),
|
|
170
|
+
tokensIn: q.amountIn,
|
|
171
|
+
minQuoteOut: mins(sells),
|
|
172
|
+
tokenOut: edge ? edge.hop.to : pool(last).quote,
|
|
173
|
+
|
|
174
|
+
swapIn: edge ? edge.amountIn : 0n,
|
|
175
|
+
swapData: edge ? sd[0] : "0x",
|
|
176
|
+
minOut: edge ? edge.minOut : last.minOut,
|
|
177
|
+
|
|
178
|
+
to: buys.length ? EVM_NATIVE : p.to,
|
|
179
|
+
deadline: p.deadline,
|
|
180
|
+
};
|
|
181
|
+
if (buys.length === 0) return call(encodeRouterSell(s, p.chainId), 0n);
|
|
182
|
+
const b = { path: ids(buys), tokenIn: s.tokenOut, amountIn: 0n, swapData: "0x", minQuoteIn: 0n, minTokensOut: mins(buys), to: p.to, deadline: p.deadline };
|
|
183
|
+
return call(encodeRouterSwap(s, b, p.chainId), 0n);
|
|
184
|
+
}
|