swapnet-sdk-test-4
Version:
TypeScript SDK for SwapNet API.
51 lines (50 loc) • 2.08 kB
JavaScript
import { toAmountOutMinimum } from "../utils.js";
import { defaultRouters } from "./routerInfo.js";
export class RouterBase {
name;
chainId;
routerAddress;
tokenProxyAddress;
constructor(_name, _chainId, _routerAddress = undefined, _tokenProxyAddress = undefined){
this.name = _name;
this.chainId = _chainId;
const defaultRouterInfo = defaultRouters.find((r)=>r.name === this.name && r.chainId === this.chainId);
if (defaultRouterInfo === undefined) {
if (_routerAddress === undefined || _tokenProxyAddress === undefined) {
throw new Error(`Unable to find ${this.name} on chain ${this.chainId}!`);
}
this.routerAddress = _routerAddress;
this.tokenProxyAddress = _tokenProxyAddress;
return;
}
if (defaultRouterInfo.tokenProxyAddress === undefined) {
throw new Error(`Invalid router info for ${this.name}: missing tokenProxyAddress!`);
}
this.routerAddress = _routerAddress === undefined ? defaultRouterInfo.routerAddress : _routerAddress;
this.tokenProxyAddress = _tokenProxyAddress === undefined ? defaultRouterInfo.tokenProxyAddress : _tokenProxyAddress;
}
}
export const resolveEncodeOptions = (routingPlan, options)=>{
let { slippageTolerance, amountOutMinimum, deadline, wrapInput, unwrapOutput } = options;
if (slippageTolerance !== undefined && amountOutMinimum !== undefined) {
throw new Error(`Conflict encoding options: both 'slippageTolerance' and 'amountOutMinimum' are specified.`);
}
if (amountOutMinimum === undefined) {
if (slippageTolerance === undefined) {
slippageTolerance = 0.01;
}
amountOutMinimum = toAmountOutMinimum(routingPlan.amountOut, slippageTolerance);
}
if (wrapInput === undefined) {
wrapInput = false;
}
if (unwrapOutput === undefined) {
unwrapOutput = false;
}
return {
amountOutMinimum,
wrapInput,
unwrapOutput,
deadline
};
};