@rarimo/swap
Version:
Internal tools that other Rarimo packages use to swap tokens.
104 lines (103 loc) • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
function _export(target, all) {
for(var name in all)Object.defineProperty(target, name, {
enumerable: true,
get: all[name]
});
}
_export(exports, {
getSwapData: function() {
return getSwapData;
},
buildSwapData: function() {
return buildSwapData;
}
});
const _const = require("../../../const");
const _enums = require("../../../enums");
const _helpers = require("./helpers");
const _payload = require("./payload");
const getSwapData = (isBridgingRequired, args, receiver = _const.CALLER_ADDRESS, bundle)=>{
const data = [];
const isSameChainBundleExecution = Boolean(bundle === null || bundle === void 0 ? void 0 : bundle.bundle);
const { from , to , amountIn , amountOut } = args;
const { isUnwrapRequired , isWrapRequired } = (0, _helpers.getIsWrappedOrUnwrappedRequired)(from, to);
const isWrappedOrUnwrapped = isWrapRequired || isUnwrapRequired;
// If bridging is required or if there is same chain bundle execution,
// tokens must be on the swap-contract balance.
// During swapping tokens swap contract will be receiver by default.
const rcvr = isBridgingRequired || isSameChainBundleExecution ? _const.THIS_ADDRESS : receiver;
// If wrap of the native token is required
if (isWrapRequired) {
data.push((0, _payload.buildPayload)(_enums.SwapCommands.WrapNative, [
rcvr,
amountIn.value
]));
}
// If input not native token transfer erc20 to the contract balance is required
if (!from.isNative) {
data.push((0, _payload.buildPayload)(_enums.SwapCommands.TransferFromErc20, [
from.address,
amountIn.value
]));
}
// If unwrap required and native is the target token
if (isUnwrapRequired) {
data.push((0, _payload.buildPayload)(_enums.SwapCommands.UnwrapNative, [
rcvr,
amountIn.value
]));
}
// If input token wasn't wrapped\unwrapped thus swap is required, and we need to
// add swap data such as native -> erc20 | erc20 -> native | erc20 -> erc20
if (!isWrappedOrUnwrapped) {
if (!args.path || !amountOut) {
throw new TypeError('path, amountOut args are required for swap');
}
data.push(...buildSwapData(from, to, amountIn, amountOut, args.path));
// If to.isNative swap output token, we need to unwrap it for UniswapV3
if (to.isNative && to.isUniswapV3) {
data.push((0, _payload.buildPayload)(_enums.SwapCommands.UnwrapNative, [
_const.THIS_ADDRESS,
_const.CONTRACT_BALANCE
]));
}
}
return data;
};
const buildSwapData = (from, to, amountIn, amountOut, path)=>{
const data = [];
const swapValues = [
_const.THIS_ADDRESS,
amountOut.value,
amountIn.value,
// path type is different for UniswapV2 and UniswapV3 routers,
// for the V3 router it is `address[]`, for the V2 router it is `bytes`
// (concatenated addresses)
from.isUniswapV2 ? path : path[0]
];
if (from.isUniswapV2) {
let command = from.isTraderJoe ? _enums.SwapCommands.SwapTokensForExactTokensTj : _enums.SwapCommands.SwapTokensForExactTokensV2;
if (from.isNative) {
command = from.isTraderJoe ? _enums.SwapCommands.SwapAvaxForExactTokens : _enums.SwapCommands.SwapEthForExactTokens;
}
if (to.isNative) {
command = from.isTraderJoe ? _enums.SwapCommands.SwapTokensForExactAvax : _enums.SwapCommands.SwapTokensForExactEth;
}
data.push((0, _payload.buildPayload)(command, swapValues));
}
if (from.isUniswapV3) {
const command = _enums.SwapCommands.ExactOutput;
// from.isNative determines for the UniswapV3 router, does input token be
// native or not
data.push((0, _payload.buildPayload)(command, [
from.isNative,
...swapValues
]));
}
return data;
};
//# sourceMappingURL=swap.js.map