UNPKG

@rarimo/swap

Version:

Internal tools that other Rarimo packages use to swap tokens.

54 lines (53 loc) 2.92 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "getExecuteData", { enumerable: true, get: function() { return getExecuteData; } }); const _shared = require("@rarimo/shared"); const _errors = require("../../../errors"); const _bridge = require("./bridge"); const _helpers = require("./helpers"); const _samechain = require("./same-chain"); const _swap = require("./swap"); const _transfer = require("./transfer"); const getExecuteData = (args)=>{ return (0, _shared.isOneElementArray)(args.swapOpts) ? getExecuteDataSingle(args) : getExecuteDataMultiple(args); }; const getExecuteDataMultiple = ({ swapOpts , intermediateOpts , chainTo , chainFrom , receiver , bundle , isWrapped , multiplePaymentsOpts })=>{ if (!multiplePaymentsOpts) throw new _errors.SwapperMultiplePaymentOptsEmptyError(); const { amountOut , to } = multiplePaymentsOpts || {}; const isBridgingRequired = (0, _helpers.getIsBridgingRequired)(chainFrom, chainTo); const data = [ ...swapOpts.map((i)=>(0, _swap.getSwapData)(isBridgingRequired, i, receiver, bundle)).flat(), ...(0, _bridge.getBridgeData)(isBridgingRequired, intermediateOpts, chainTo, to, amountOut, receiver, bundle, isWrapped), ...(0, _samechain.getSameChainBundleData)(isBridgingRequired, bundle), ...swapOpts.map((i)=>(0, _transfer.getTransferData)(i.from, receiver)).flat(), ...(0, _transfer.getTransferData)(to, receiver) ]; return (0, _helpers.encodeCommandPayload)(data); }; const getExecuteDataSingle = ({ swapOpts , chainFrom , chainTo , bundle , intermediateOpts , isWrapped , receiver })=>{ // for the case of single execute, there will be only one item in the array const swapArgs = swapOpts[0]; const { from , to , amountIn , amountOut } = swapArgs; const { isUnwrapRequired , isWrapRequired } = (0, _helpers.getIsWrappedOrUnwrappedRequired)(from, to); const isWrappedOrUnwrapped = isWrapRequired || isUnwrapRequired; const isBridgingRequired = (0, _helpers.getIsBridgingRequired)(chainFrom, chainTo); // If token was wrapped\unwrapped thus amount for bridging equals to amountIn // and amountOut could be undefined otherwise if token was swapped, we need // to take amountOut as amount for bridging\transferring const amount = isWrappedOrUnwrapped ? amountIn : amountOut; return (0, _helpers.encodeCommandPayload)([ ...(0, _swap.getSwapData)(isBridgingRequired, swapOpts[0], receiver, bundle), ...(0, _bridge.getBridgeData)(isBridgingRequired, intermediateOpts, chainTo, to, amount, receiver, bundle, isWrapped), ...(0, _samechain.getSameChainBundleData)(isBridgingRequired, bundle), ...(0, _transfer.getTransferData)(from, receiver), ...(0, _transfer.getTransferData)(to, receiver) ]); }; //# sourceMappingURL=index.js.map