@rarimo/swap
Version:
Internal tools that other Rarimo packages use to swap tokens.
34 lines (33 loc) • 1.16 kB
JavaScript
import { BRIDGE_ROUTER_ABI, MASTER_ROUTER_ABI, MULTICALL_ROUTER_ABI, TRADER_JOE_ROUTER_ABI, TRANSFER_ROUTER_ABI, UNISWAP_V2_ROUTER_ABI, UNISWAP_V3_ROUTER, WRAP_ROUTER_ABI } from "@rarimo/shared";
import { utils } from "ethers";
import { SWAP_COMMANDS_NAME_MAP } from "../../../const";
const ABIS = [
MASTER_ROUTER_ABI,
BRIDGE_ROUTER_ABI,
MULTICALL_ROUTER_ABI,
TRADER_JOE_ROUTER_ABI,
TRANSFER_ROUTER_ABI,
UNISWAP_V2_ROUTER_ABI,
UNISWAP_V3_ROUTER,
WRAP_ROUTER_ABI
];
export const buildPayload = (cmd, values)=>{
const command = SWAP_COMMANDS_NAME_MAP[cmd];
if (!command) {
throw new Error(`Unknown command ${cmd}`);
}
const isFragmentExists = (j)=>{
return j.type === 'function' && j.name === command;
};
const abi = ABIS.find((i)=>i.some(isFragmentExists));
if (!abi) {
throw new Error(`ABI for command ${command} not found`);
}
const functionData = new utils.Interface(abi).encodeFunctionData(command, values);
return {
command: cmd,
skipRevert: false,
data: '0x' + functionData.slice(10)
};
};
//# sourceMappingURL=payload.js.map