@rarimo/swap
Version:
Internal tools that other Rarimo packages use to swap tokens.
69 lines (68 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "buildIntermediateBundleData", {
enumerable: true,
get: function() {
return buildIntermediateBundleData;
}
});
const _tools = require("@distributedlab/tools");
const _shared = require("@rarimo/shared");
const _ethers = require("ethers");
const _const = require("../../../const");
const _enums = require("../../../enums");
const _helpers = require("./helpers");
const _payload = require("./payload");
const _samechain = require("./same-chain");
const _swap = require("./swap");
const _transfer = require("./transfer");
const buildIntermediateBundleData = (opts, chainTo, receiver, bundle)=>{
const { from , to , amountIn , amountOut , path } = opts;
const swapDiamondContractAddress = chainTo.contractAddress;
const intermediateTokenAddress = from.address;
const swapDiamondFunctionData = (0, _helpers.encodeCommandPayload)([
(0, _payload.buildPayload)(_enums.SwapCommands.TransferFromErc20, [
intermediateTokenAddress,
amountIn.value
]),
...(0, _swap.buildSwapData)(from, to, amountIn, amountOut, path),
...to.isNative && to.isUniswapV3 ? [
(0, _payload.buildPayload)(_enums.SwapCommands.UnwrapNative, [
_const.THIS_ADDRESS,
amountOut.value
])
] : [],
...(0, _samechain.buildSameChainBundleData)(bundle),
...(0, _transfer.getTransferData)(from, receiver),
...(0, _transfer.getTransferData)(to, receiver)
]);
// to be able to swap diamond transfer intermediate token from the proxy contract,
// we need to approve the swap diamond contract to spend the intermediate token
const approveFunctionData = new _ethers.utils.Interface(_shared.ERC20_ABI).encodeFunctionData('approve', [
swapDiamondContractAddress,
_tools.BN.MAX_UINT256.value
]);
return _ethers.utils.defaultAbiCoder.encode([
'address[]',
'uint256[]',
'bytes[]'
], [
[
intermediateTokenAddress,
swapDiamondContractAddress
],
[
'0',
// for approve we don't need to send any amount
'0'
],
// the amount will always be 0, because the intermediate token is erc20
[
approveFunctionData,
swapDiamondFunctionData
]
]);
};
//# sourceMappingURL=intermediate-bundle.js.map