swapnet-sdk-test-4
Version:
TypeScript SDK for SwapNet API.
303 lines (302 loc) • 8.12 kB
JavaScript
// Copied from `https://github.com/Uniswap/universal-router-sdk/blob/main/src/swapRouter.ts`
// with minor update (ethers v5 -> v6)
import { AbiCoder } from 'ethers';
const defaultAbiCoder = AbiCoder.defaultAbiCoder();
export var CommandType;
(function(CommandType) {
CommandType[CommandType["V3_SWAP_EXACT_IN"] = 0] = "V3_SWAP_EXACT_IN";
CommandType[CommandType["V3_SWAP_EXACT_OUT"] = 1] = "V3_SWAP_EXACT_OUT";
CommandType[CommandType["PERMIT2_TRANSFER_FROM"] = 2] = "PERMIT2_TRANSFER_FROM";
CommandType[CommandType["PERMIT2_PERMIT_BATCH"] = 3] = "PERMIT2_PERMIT_BATCH";
CommandType[CommandType["SWEEP"] = 4] = "SWEEP";
CommandType[CommandType["TRANSFER"] = 5] = "TRANSFER";
CommandType[CommandType["PAY_PORTION"] = 6] = "PAY_PORTION";
CommandType[CommandType["V2_SWAP_EXACT_IN"] = 8] = "V2_SWAP_EXACT_IN";
CommandType[CommandType["V2_SWAP_EXACT_OUT"] = 9] = "V2_SWAP_EXACT_OUT";
CommandType[CommandType["PERMIT2_PERMIT"] = 10] = "PERMIT2_PERMIT";
CommandType[CommandType["WRAP_ETH"] = 11] = "WRAP_ETH";
CommandType[CommandType["UNWRAP_WETH"] = 12] = "UNWRAP_WETH";
CommandType[CommandType["PERMIT2_TRANSFER_FROM_BATCH"] = 13] = "PERMIT2_TRANSFER_FROM_BATCH";
CommandType[CommandType["BALANCE_CHECK_ERC20"] = 14] = "BALANCE_CHECK_ERC20";
// NFT-related command types
CommandType[CommandType["SEAPORT_V1_5"] = 16] = "SEAPORT_V1_5";
CommandType[CommandType["LOOKS_RARE_V2"] = 17] = "LOOKS_RARE_V2";
CommandType[CommandType["NFTX"] = 18] = "NFTX";
CommandType[CommandType["CRYPTOPUNKS"] = 19] = "CRYPTOPUNKS";
// 0x14
CommandType[CommandType["OWNER_CHECK_721"] = 21] = "OWNER_CHECK_721";
CommandType[CommandType["OWNER_CHECK_1155"] = 22] = "OWNER_CHECK_1155";
CommandType[CommandType["SWEEP_ERC721"] = 23] = "SWEEP_ERC721";
CommandType[CommandType["X2Y2_721"] = 24] = "X2Y2_721";
CommandType[CommandType["SUDOSWAP"] = 25] = "SUDOSWAP";
CommandType[CommandType["NFT20"] = 26] = "NFT20";
CommandType[CommandType["X2Y2_1155"] = 27] = "X2Y2_1155";
CommandType[CommandType["FOUNDATION"] = 28] = "FOUNDATION";
CommandType[CommandType["SWEEP_ERC1155"] = 29] = "SWEEP_ERC1155";
CommandType[CommandType["ELEMENT_MARKET"] = 30] = "ELEMENT_MARKET";
CommandType[CommandType["SEAPORT_V1_4"] = 32] = "SEAPORT_V1_4";
CommandType[CommandType["EXECUTE_SUB_PLAN"] = 33] = "EXECUTE_SUB_PLAN";
CommandType[CommandType["APPROVE_ERC20"] = 34] = "APPROVE_ERC20";
// Added by SwapNet protocol
CommandType[CommandType["CURVE_V1"] = 35] = "CURVE_V1";
CommandType[CommandType["MAKER_ORDER"] = 36] = "MAKER_ORDER";
CommandType[CommandType["WRAP_UNWRAP_FEW_TOKEN"] = 39] = "WRAP_UNWRAP_FEW_TOKEN";
})(CommandType || (CommandType = {}));
const ALLOW_REVERT_FLAG = 0x80;
const REVERTIBLE_COMMANDS = new Set([
16,
32,
18,
17,
24,
27,
28,
25,
26,
33,
19,
30
]);
const PERMIT_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 nonce) details,address spender,uint256 sigDeadline)';
const PERMIT_BATCH_STRUCT = '((address token,uint160 amount,uint48 expiration,uint48 nonce)[] details,address spender,uint256 sigDeadline)';
const PERMIT2_TRANSFER_FROM_STRUCT = '(address from,address to,uint160 amount,address token)';
const PERMIT2_TRANSFER_FROM_BATCH_STRUCT = PERMIT2_TRANSFER_FROM_STRUCT + '[]';
const ABI_DEFINITION = {
// Batch Reverts
[]: [
'bytes',
'bytes[]'
],
// Permit2 Actions
[]: [
PERMIT_STRUCT,
'bytes'
],
[]: [
PERMIT_BATCH_STRUCT,
'bytes'
],
[]: [
'address',
'address',
'uint160'
],
[]: [
PERMIT2_TRANSFER_FROM_BATCH_STRUCT
],
// Uniswap Actions
[]: [
'address',
'uint256',
'uint256',
'bytes',
'bool',
'uint256'
],
[]: [
'address',
'uint256',
'uint256',
'bytes',
'bool',
'uint256'
],
[]: [
'address',
'uint256',
'uint256',
'address[]',
'bool',
'uint256'
],
[]: [
'address',
'uint256',
'uint256',
'address[]',
'bool',
'uint256'
],
// Token Actions and Checks
[]: [
'address',
'uint256'
],
[]: [
'address',
'uint256'
],
[]: [
'address',
'address',
'uint256'
],
[]: [
'address',
'address',
'uint256'
],
[]: [
'address',
'address',
'uint256',
'uint256'
],
[]: [
'address',
'address',
'uint256'
],
[]: [
'address',
'address',
'uint256'
],
[]: [
'address',
'address',
'uint256'
],
[]: [
'address',
'address',
'uint256'
],
[]: [
'address',
'address',
'uint256',
'uint256'
],
[]: [
'address',
'uint256'
],
// NFT Markets
[]: [
'uint256',
'bytes'
],
[]: [
'uint256',
'bytes'
],
[]: [
'uint256',
'bytes'
],
[]: [
'uint256',
'bytes'
],
[]: [
'uint256',
'bytes',
'address',
'address',
'uint256'
],
[]: [
'uint256',
'bytes',
'address',
'address',
'uint256',
'uint256'
],
[]: [
'uint256',
'bytes',
'address',
'address',
'uint256'
],
[]: [
'uint256',
'bytes'
],
[]: [
'uint256',
'bytes'
],
[]: [
'uint256',
'address',
'uint256'
],
[]: [
'uint256',
'bytes'
],
// Added by SwapNet protocol
[]: [
'address',
'address',
'address',
'uint256',
'uint256'
],
[]: [
'address',
'uint256',
'bool',
'address',
'address',
'address',
'uint256',
'uint256',
'uint256',
'uint256'
],
[]: [
'address',
'address',
'uint256',
'bool'
]
};
export class RoutePlanner {
commands;
inputs;
constructor(){
this.commands = '0x';
this.inputs = [];
}
addSubPlan(subplan) {
this.addCommand(33, [
subplan.commands,
subplan.inputs
], true);
}
addCommand(type, parameters, allowRevert = false) {
let command = createCommand(type, parameters);
this.inputs.push(command.encodedInput);
if (allowRevert) {
if (!REVERTIBLE_COMMANDS.has(command.type)) {
throw new Error(`command type: ${command.type} cannot be allowed to revert`);
}
command.type = command.type | ALLOW_REVERT_FLAG;
}
this.commands = this.commands.concat(command.type.toString(16).padStart(2, '0'));
}
}
export function createCommand(type, parameters) {
const encodedInput = defaultAbiCoder.encode(ABI_DEFINITION[type], parameters);
return {
type,
encodedInput
};
}
export var UniswapV2ForkNames;
(function(UniswapV2ForkNames) {
UniswapV2ForkNames[UniswapV2ForkNames["Uniswap"] = 0] = "Uniswap";
UniswapV2ForkNames[UniswapV2ForkNames["Thruster3k"] = 1] = "Thruster3k";
UniswapV2ForkNames[UniswapV2ForkNames["Thruster10k"] = 2] = "Thruster10k";
UniswapV2ForkNames[UniswapV2ForkNames["Ringswap"] = 3] = "Ringswap";
})(UniswapV2ForkNames || (UniswapV2ForkNames = {}));
export var UniswapV3ForkNames;
(function(UniswapV3ForkNames) {
UniswapV3ForkNames[UniswapV3ForkNames["Uniswap"] = 0] = "Uniswap";
UniswapV3ForkNames[UniswapV3ForkNames["Thruster"] = 1] = "Thruster";
UniswapV3ForkNames[UniswapV3ForkNames["Ringswap"] = 2] = "Ringswap";
})(UniswapV3ForkNames || (UniswapV3ForkNames = {}));