mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
calldata as param
This commit is contained in:
parent
c650bc0392
commit
f6fbe8614d
|
@ -11,56 +11,23 @@ contract SwapHelpers {
|
||||||
InstaConnectors internal constant instaConnectors =
|
InstaConnectors internal constant instaConnectors =
|
||||||
InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
|
InstaConnectors(0x127d8cD0E2b2E0366D522DeA53A787bfE9002C14);
|
||||||
|
|
||||||
struct InputData {
|
|
||||||
address buyAddr;
|
|
||||||
address sellAddr;
|
|
||||||
uint256 sellAmt;
|
|
||||||
uint256[] unitAmts;
|
|
||||||
bytes4[] swapDatas;
|
|
||||||
bytes[] callDatas;
|
|
||||||
uint256 setId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*@dev Swap using the dex aggregators.
|
*@dev Swap using the dex aggregators.
|
||||||
*@param _connectors name of the connectors in preference order.
|
*@param _connectors name of the connectors in preference order.
|
||||||
*@param _inputData data for the swap cast.
|
*@param _data data for the swap cast.
|
||||||
*/
|
*/
|
||||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
function _swap(string[] memory _connectors, bytes[] memory _data)
|
||||||
internal
|
internal
|
||||||
returns (bool success, bytes memory returnData)
|
returns (bool success, bytes memory returnData)
|
||||||
{
|
{
|
||||||
uint256 _length = _connectors.length;
|
uint256 _length = _connectors.length;
|
||||||
require(_length > 0, "zero-length-not-allowed");
|
require(_length > 0, "zero-length-not-allowed");
|
||||||
require(
|
require(_data.length == _length, "calldata-length-invalid");
|
||||||
_inputData.unitAmts.length == _length,
|
|
||||||
"unitAmts-length-invalid"
|
|
||||||
);
|
|
||||||
require(
|
|
||||||
_inputData.callDatas.length == _length,
|
|
||||||
"callDatas-length-invalid"
|
|
||||||
);
|
|
||||||
require(
|
|
||||||
_inputData.swapDatas.length == _length,
|
|
||||||
"swapDatas-length-invalid"
|
|
||||||
);
|
|
||||||
|
|
||||||
// require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors
|
|
||||||
|
|
||||||
for (uint256 i = 0; i < _length; i++) {
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
bytes memory _data = abi.encodeWithSelector(
|
|
||||||
_inputData.swapDatas[i],
|
|
||||||
_inputData.buyAddr,
|
|
||||||
_inputData.sellAddr,
|
|
||||||
_inputData.sellAmt,
|
|
||||||
_inputData.unitAmts[i],
|
|
||||||
_inputData.callDatas[i],
|
|
||||||
_inputData.setId
|
|
||||||
);
|
|
||||||
|
|
||||||
(success, returnData) = instaConnectors
|
(success, returnData) = instaConnectors
|
||||||
.connectors(_connectors[i])
|
.connectors(_connectors[i])
|
||||||
.delegatecall(_data);
|
.delegatecall(_data[i]);
|
||||||
if (success) {
|
if (success) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,39 +15,17 @@ abstract contract Swap is SwapHelpers {
|
||||||
* @dev Swap ETH/ERC20_Token using dex aggregators.
|
* @dev Swap ETH/ERC20_Token using dex aggregators.
|
||||||
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
|
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
|
||||||
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
|
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
|
||||||
* @param buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @param _data Encoded function call data including function selector encoded with parameters.
|
||||||
* @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
|
||||||
* @param sellAmt The amount of the token to sell.
|
|
||||||
* @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs.
|
|
||||||
* @param swapDatas The function selectors of swap methods of the DEXs.
|
|
||||||
* @param callDatas Data from APIs for respective DEXs.
|
|
||||||
* @param setId ID stores the amount of token brought.
|
|
||||||
*/
|
*/
|
||||||
function swap(
|
function swap(
|
||||||
address buyAddr,
|
|
||||||
address sellAddr,
|
|
||||||
uint256 sellAmt,
|
|
||||||
uint256[] memory unitAmts,
|
|
||||||
bytes4[] memory swapDatas,
|
|
||||||
bytes[] calldata callDatas,
|
|
||||||
string[] memory _connectors,
|
string[] memory _connectors,
|
||||||
uint256 setId
|
bytes[] memory _data
|
||||||
)
|
)
|
||||||
external
|
external
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
InputData memory inputData = InputData({
|
(bool success, bytes memory returnData) = _swap(_connectors, _data);
|
||||||
buyAddr: buyAddr,
|
|
||||||
sellAddr: sellAddr,
|
|
||||||
sellAmt: sellAmt,
|
|
||||||
unitAmts: unitAmts,
|
|
||||||
swapDatas: swapDatas,
|
|
||||||
callDatas: callDatas,
|
|
||||||
setId: setId
|
|
||||||
});
|
|
||||||
|
|
||||||
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
|
||||||
|
|
||||||
require(success, "swap-Aggregator-failed");
|
require(success, "swap-Aggregator-failed");
|
||||||
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||||
|
|
|
@ -11,56 +11,23 @@ contract SwapHelpers {
|
||||||
InstaConnectors internal constant instaConnectors =
|
InstaConnectors internal constant instaConnectors =
|
||||||
InstaConnectors(0x97b0B3A8bDeFE8cB9563a3c610019Ad10DB8aD11);
|
InstaConnectors(0x97b0B3A8bDeFE8cB9563a3c610019Ad10DB8aD11);
|
||||||
|
|
||||||
struct InputData {
|
|
||||||
address buyAddr;
|
|
||||||
address sellAddr;
|
|
||||||
uint256 sellAmt;
|
|
||||||
uint256[] unitAmts;
|
|
||||||
bytes4[] swapDatas;
|
|
||||||
bytes[] callDatas;
|
|
||||||
uint256 setId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*@dev Swap using the dex aggregators.
|
*@dev Swap using the dex aggregators.
|
||||||
*@param _connectors name of the connectors in preference order.
|
*@param _connectors name of the connectors in preference order.
|
||||||
*@param _inputData data for the swap cast.
|
*@param _data data for the swap cast.
|
||||||
*/
|
*/
|
||||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
function _swap(string[] memory _connectors, bytes[] memory _data)
|
||||||
internal
|
internal
|
||||||
returns (bool success, bytes memory returnData)
|
returns (bool success, bytes memory returnData)
|
||||||
{
|
{
|
||||||
uint256 _length = _connectors.length;
|
uint256 _length = _connectors.length;
|
||||||
require(_length > 0, "zero-length-not-allowed");
|
require(_length > 0, "zero-length-not-allowed");
|
||||||
require(
|
require(_data.length == _length, "calldata-length-invalid");
|
||||||
_inputData.unitAmts.length == _length,
|
|
||||||
"unitAmts-length-invalid"
|
|
||||||
);
|
|
||||||
require(
|
|
||||||
_inputData.callDatas.length == _length,
|
|
||||||
"callDatas-length-invalid"
|
|
||||||
);
|
|
||||||
require(
|
|
||||||
_inputData.swapDatas.length == _length,
|
|
||||||
"swapDatas-length-invalid"
|
|
||||||
);
|
|
||||||
|
|
||||||
// require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors
|
|
||||||
|
|
||||||
for (uint256 i = 0; i < _length; i++) {
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
bytes memory _data = abi.encodeWithSelector(
|
|
||||||
_inputData.swapDatas[i],
|
|
||||||
_inputData.buyAddr,
|
|
||||||
_inputData.sellAddr,
|
|
||||||
_inputData.sellAmt,
|
|
||||||
_inputData.unitAmts[i],
|
|
||||||
_inputData.callDatas[i],
|
|
||||||
_inputData.setId
|
|
||||||
);
|
|
||||||
|
|
||||||
(success, returnData) = instaConnectors
|
(success, returnData) = instaConnectors
|
||||||
.connectors(_connectors[i])
|
.connectors(_connectors[i])
|
||||||
.delegatecall(_data);
|
.delegatecall(_data[i]);
|
||||||
if (success) {
|
if (success) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,39 +15,14 @@ abstract contract Swap is SwapHelpers {
|
||||||
* @dev Swap ETH/ERC20_Token using dex aggregators.
|
* @dev Swap ETH/ERC20_Token using dex aggregators.
|
||||||
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
|
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
|
||||||
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
|
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
|
||||||
* @param buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @param _data Encoded function call data including function selector encoded with parameters.
|
||||||
* @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
|
||||||
* @param sellAmt The amount of the token to sell.
|
|
||||||
* @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs.
|
|
||||||
* @param swapDatas The function selectors of swap methods of the DEXs.
|
|
||||||
* @param callDatas Data from APIs for respective DEXs.
|
|
||||||
* @param setId ID stores the amount of token brought.
|
|
||||||
*/
|
*/
|
||||||
function swap(
|
function swap(string[] memory _connectors, bytes[] memory _data)
|
||||||
address buyAddr,
|
|
||||||
address sellAddr,
|
|
||||||
uint256 sellAmt,
|
|
||||||
uint256[] memory unitAmts,
|
|
||||||
bytes4[] memory swapDatas,
|
|
||||||
bytes[] calldata callDatas,
|
|
||||||
string[] memory _connectors,
|
|
||||||
uint256 setId
|
|
||||||
)
|
|
||||||
external
|
external
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
InputData memory inputData = InputData({
|
(bool success, bytes memory returnData) = _swap(_connectors, _data);
|
||||||
buyAddr: buyAddr,
|
|
||||||
sellAddr: sellAddr,
|
|
||||||
sellAmt: sellAmt,
|
|
||||||
unitAmts: unitAmts,
|
|
||||||
swapDatas: swapDatas,
|
|
||||||
callDatas: callDatas,
|
|
||||||
setId: setId
|
|
||||||
});
|
|
||||||
|
|
||||||
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
|
||||||
|
|
||||||
require(success, "swap-Aggregator-failed");
|
require(success, "swap-Aggregator-failed");
|
||||||
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||||
|
|
|
@ -11,56 +11,23 @@ contract SwapHelpers {
|
||||||
InstaConnectors internal constant instaConnectors =
|
InstaConnectors internal constant instaConnectors =
|
||||||
InstaConnectors(0x2A00684bFAb9717C21271E0751BCcb7d2D763c88);
|
InstaConnectors(0x2A00684bFAb9717C21271E0751BCcb7d2D763c88);
|
||||||
|
|
||||||
struct InputData {
|
|
||||||
address buyAddr;
|
|
||||||
address sellAddr;
|
|
||||||
uint256 sellAmt;
|
|
||||||
uint256[] unitAmts;
|
|
||||||
bytes4[] swapDatas;
|
|
||||||
bytes[] callDatas;
|
|
||||||
uint256 setId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*@dev Swap using the dex aggregators.
|
*@dev Swap using the dex aggregators.
|
||||||
*@param _connectors name of the connectors in preference order.
|
*@param _connectors name of the connectors in preference order.
|
||||||
*@param _inputData data for the swap cast.
|
*@param _data data for the swap cast.
|
||||||
*/
|
*/
|
||||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
function _swap(string[] memory _connectors, bytes[] memory _data)
|
||||||
internal
|
internal
|
||||||
returns (bool success, bytes memory returnData)
|
returns (bool success, bytes memory returnData)
|
||||||
{
|
{
|
||||||
uint256 _length = _connectors.length;
|
uint256 _length = _connectors.length;
|
||||||
require(_length > 0, "zero-length-not-allowed");
|
require(_length > 0, "zero-length-not-allowed");
|
||||||
require(
|
require(_data.length == _length, "calldata-length-invalid");
|
||||||
_inputData.unitAmts.length == _length,
|
|
||||||
"unitAmts-length-invalid"
|
|
||||||
);
|
|
||||||
require(
|
|
||||||
_inputData.callDatas.length == _length,
|
|
||||||
"callDatas-length-invalid"
|
|
||||||
);
|
|
||||||
require(
|
|
||||||
_inputData.swapDatas.length == _length,
|
|
||||||
"swapDatas-length-invalid"
|
|
||||||
);
|
|
||||||
|
|
||||||
// require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors
|
|
||||||
|
|
||||||
for (uint256 i = 0; i < _length; i++) {
|
for (uint256 i = 0; i < _length; i++) {
|
||||||
bytes memory _data = abi.encodeWithSelector(
|
|
||||||
_inputData.swapDatas[i],
|
|
||||||
_inputData.buyAddr,
|
|
||||||
_inputData.sellAddr,
|
|
||||||
_inputData.sellAmt,
|
|
||||||
_inputData.unitAmts[i],
|
|
||||||
_inputData.callDatas[i],
|
|
||||||
_inputData.setId
|
|
||||||
);
|
|
||||||
|
|
||||||
(success, returnData) = instaConnectors
|
(success, returnData) = instaConnectors
|
||||||
.connectors(_connectors[i])
|
.connectors(_connectors[i])
|
||||||
.delegatecall(_data);
|
.delegatecall(_data[i]);
|
||||||
if (success) {
|
if (success) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,39 +15,14 @@ abstract contract Swap is SwapHelpers {
|
||||||
* @dev Swap ETH/ERC20_Token using dex aggregators.
|
* @dev Swap ETH/ERC20_Token using dex aggregators.
|
||||||
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
|
* @notice Swap tokens from exchanges like 1INCH, 0x etc, with calculation done off-chain.
|
||||||
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
|
* @param _connectors The name of the connectors like 1INCH-A, 0x etc, in order of their priority.
|
||||||
* @param buyAddr The address of the token to buy.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @param _data Encoded function call data including function selector encoded with parameters.
|
||||||
* @param sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
|
||||||
* @param sellAmt The amount of the token to sell.
|
|
||||||
* @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs.
|
|
||||||
* @param swapDatas The function selectors of swap methods of the DEXs.
|
|
||||||
* @param callDatas Data from APIs for respective DEXs.
|
|
||||||
* @param setId ID stores the amount of token brought.
|
|
||||||
*/
|
*/
|
||||||
function swap(
|
function swap(string[] memory _connectors, bytes[] memory _data)
|
||||||
address buyAddr,
|
|
||||||
address sellAddr,
|
|
||||||
uint256 sellAmt,
|
|
||||||
uint256[] memory unitAmts,
|
|
||||||
bytes4[] memory swapDatas,
|
|
||||||
bytes[] calldata callDatas,
|
|
||||||
string[] memory _connectors,
|
|
||||||
uint256 setId
|
|
||||||
)
|
|
||||||
external
|
external
|
||||||
payable
|
payable
|
||||||
returns (string memory _eventName, bytes memory _eventParam)
|
returns (string memory _eventName, bytes memory _eventParam)
|
||||||
{
|
{
|
||||||
InputData memory inputData = InputData({
|
(bool success, bytes memory returnData) = _swap(_connectors, _data);
|
||||||
buyAddr: buyAddr,
|
|
||||||
sellAddr: sellAddr,
|
|
||||||
sellAmt: sellAmt,
|
|
||||||
unitAmts: unitAmts,
|
|
||||||
swapDatas: swapDatas,
|
|
||||||
callDatas: callDatas,
|
|
||||||
setId: setId
|
|
||||||
});
|
|
||||||
|
|
||||||
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
|
||||||
|
|
||||||
require(success, "swap-Aggregator-failed");
|
require(success, "swap-Aggregator-failed");
|
||||||
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||||
|
|
|
@ -73,24 +73,11 @@ describe("Swap | Avalanche", function () {
|
||||||
describe("Main", function () {
|
describe("Main", function () {
|
||||||
it("should swap the tokens", async function () {
|
it("should swap the tokens", async function () {
|
||||||
let buyTokenAmountZeroX: any;
|
let buyTokenAmountZeroX: any;
|
||||||
|
let unitAmount1Inch: any;
|
||||||
|
let calldata1Inch: any;
|
||||||
// let buyTokenAmount1Inch: any;
|
// let buyTokenAmount1Inch: any;
|
||||||
let buyTokenAmountParaswap: any;
|
let buyTokenAmountParaswap: any;
|
||||||
|
|
||||||
async function getSelector(connector: string) {
|
|
||||||
var abi = [
|
|
||||||
"function swap(address,address,uint256,uint256,bytes,uint256)",
|
|
||||||
"function sell(address,address,uint256,uint256,bytes,uint256)"
|
|
||||||
];
|
|
||||||
var iface = new ethers.utils.Interface(abi);
|
|
||||||
var id;
|
|
||||||
if (connector == "1INCH-A") {
|
|
||||||
id = iface.getSighash("sell");
|
|
||||||
} else {
|
|
||||||
id = iface.getSighash("swap");
|
|
||||||
}
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
async function getArg() {
|
async function getArg() {
|
||||||
// const slippage = 0.5;
|
// const slippage = 0.5;
|
||||||
/* avax -> usdt */
|
/* avax -> usdt */
|
||||||
|
@ -155,17 +142,45 @@ describe("Swap | Avalanche", function () {
|
||||||
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
|
unitAmt = unitAmt.multipliedBy(1e18).toFixed(0);
|
||||||
return unitAmt;
|
return unitAmt;
|
||||||
};
|
};
|
||||||
|
|
||||||
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
||||||
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
||||||
let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A"));
|
|
||||||
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
function getSelector(connector: string, unitAmt: any, callData: any) {
|
||||||
let unitAmts = [unitAmtParaswap, unitAmt0x];
|
var abi = [
|
||||||
let calldatas = [calldataPara, calldataZeroX];
|
"function swap(address,address,uint256,uint256,bytes,uint256)",
|
||||||
let swapDatas = [swapDataPara, swapDataZeroX];
|
"function sell(address,address,uint256,uint256,bytes,uint256)"
|
||||||
|
];
|
||||||
|
var iface = new ethers.utils.Interface(abi);
|
||||||
|
var data;
|
||||||
|
if (connector == "1INCH-A") {
|
||||||
|
data = iface.encodeFunctionData("sell", [
|
||||||
|
buyTokenAddress,
|
||||||
|
sellTokenAddress,
|
||||||
|
srcAmount,
|
||||||
|
unitAmt,
|
||||||
|
callData,
|
||||||
|
0
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
data = iface.encodeFunctionData("swap", [
|
||||||
|
buyTokenAddress,
|
||||||
|
sellTokenAddress,
|
||||||
|
srcAmount,
|
||||||
|
unitAmt,
|
||||||
|
callData,
|
||||||
|
0
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
let dataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A", unitAmtParaswap, calldataPara));
|
||||||
|
let dataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A", unitAmt0x, calldataZeroX));
|
||||||
|
let datas = [dataPara, dataZeroX];
|
||||||
|
|
||||||
let connectors = ["PARASWAP-A", "ZEROX-A"];
|
let connectors = ["PARASWAP-A", "ZEROX-A"];
|
||||||
|
|
||||||
return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0];
|
return [connectors, datas];
|
||||||
}
|
}
|
||||||
|
|
||||||
let arg = await getArg();
|
let arg = await getArg();
|
||||||
|
|
|
@ -76,22 +76,6 @@ describe("Swap | Mainnet", function () {
|
||||||
let buyTokenAmount1Inch: any;
|
let buyTokenAmount1Inch: any;
|
||||||
let buyTokenAmountParaswap: any;
|
let buyTokenAmountParaswap: any;
|
||||||
|
|
||||||
async function getSelector(connector: string) {
|
|
||||||
var abi = [
|
|
||||||
"function swap(address,address,uint256,uint256,bytes,uint256)",
|
|
||||||
"function sell(address,address,uint256,uint256,bytes,uint256)"
|
|
||||||
];
|
|
||||||
var iface = new ethers.utils.Interface(abi);
|
|
||||||
var id;
|
|
||||||
if (connector == "1INCH-A") {
|
|
||||||
id = iface.getSighash("sell");
|
|
||||||
} else {
|
|
||||||
id = iface.getSighash("swap");
|
|
||||||
}
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getArg() {
|
async function getArg() {
|
||||||
const slippage = 0.5;
|
const slippage = 0.5;
|
||||||
/* eth -> dai */
|
/* eth -> dai */
|
||||||
|
@ -178,17 +162,42 @@ describe("Swap | Mainnet", function () {
|
||||||
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
||||||
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
||||||
|
|
||||||
let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A"));
|
function getSelector(connector: string, unitAmt: any, callData: any) {
|
||||||
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
var abi = [
|
||||||
let swapData1Inch = ethers.utils.hexlify(await getSelector("1INCH-A"));
|
"function swap(address,address,uint256,uint256,bytes,uint256)",
|
||||||
|
"function sell(address,address,uint256,uint256,bytes,uint256)"
|
||||||
|
];
|
||||||
|
var iface = new ethers.utils.Interface(abi);
|
||||||
|
var data;
|
||||||
|
if (connector == "1INCH-A") {
|
||||||
|
data = iface.encodeFunctionData("sell", [
|
||||||
|
buyTokenAddress,
|
||||||
|
sellTokenAddress,
|
||||||
|
srcAmount,
|
||||||
|
unitAmt,
|
||||||
|
callData,
|
||||||
|
0
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
data = iface.encodeFunctionData("swap", [
|
||||||
|
buyTokenAddress,
|
||||||
|
sellTokenAddress,
|
||||||
|
srcAmount,
|
||||||
|
unitAmt,
|
||||||
|
callData,
|
||||||
|
0
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
let data1Inch = ethers.utils.hexlify(await getSelector("1INCH-A", unitAmt1Inch, calldata1Inch));
|
||||||
|
let dataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A", unitAmtParaswap, calldataPara));
|
||||||
|
let dataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A", unitAmt0x, calldataZeroX));
|
||||||
|
let datas = [data1Inch, dataPara, dataZeroX];
|
||||||
|
|
||||||
let unitAmts = [unitAmt1Inch, unitAmt0x, unitAmtParaswap];
|
let connectors = ["1INCH-A", "PARASWAP-A", "ZEROX-A"];
|
||||||
let calldatas = [calldata1Inch, calldataZeroX, calldataPara];
|
|
||||||
let swapDatas = [swapData1Inch, swapDataZeroX, swapDataPara];
|
|
||||||
|
|
||||||
let connectors = ["1INCH-A", "ZEROX-A", "PARASWAP-A"];
|
return [connectors, datas];
|
||||||
|
|
||||||
return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let arg = await getArg();
|
let arg = await getArg();
|
||||||
|
|
|
@ -75,6 +75,8 @@ describe("Swap", function () {
|
||||||
let buyTokenAmountZeroX: any;
|
let buyTokenAmountZeroX: any;
|
||||||
let buyTokenAmount1Inch: any;
|
let buyTokenAmount1Inch: any;
|
||||||
let buyTokenAmountParaswap: any;
|
let buyTokenAmountParaswap: any;
|
||||||
|
let unitAmount1Inch: any;
|
||||||
|
let calldata1Inch: any;
|
||||||
|
|
||||||
async function getSelector(connector: string) {
|
async function getSelector(connector: string) {
|
||||||
var abi = [
|
var abi = [
|
||||||
|
@ -159,16 +161,41 @@ describe("Swap", function () {
|
||||||
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
||||||
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
||||||
|
|
||||||
let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A"));
|
function getCallData(connector: string, unitAmt: any, callData: any) {
|
||||||
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
var abi = [
|
||||||
|
"function swap(address,address,uint256,uint256,bytes,uint256)",
|
||||||
let unitAmts = [unitAmt0x, unitAmtParaswap];
|
"function sell(address,address,uint256,uint256,bytes,uint256)"
|
||||||
let calldatas = [calldataZeroX, calldataPara];
|
];
|
||||||
let swapDatas = [swapDataZeroX, swapDataPara];
|
var iface = new ethers.utils.Interface(abi);
|
||||||
|
var data;
|
||||||
|
if (connector == "1INCH-A") {
|
||||||
|
data = iface.encodeFunctionData("sell", [
|
||||||
|
buyTokenAddress,
|
||||||
|
sellTokenAddress,
|
||||||
|
srcAmount,
|
||||||
|
unitAmt,
|
||||||
|
callData,
|
||||||
|
0
|
||||||
|
]);
|
||||||
|
} else {
|
||||||
|
data = iface.encodeFunctionData("swap", [
|
||||||
|
buyTokenAddress,
|
||||||
|
sellTokenAddress,
|
||||||
|
srcAmount,
|
||||||
|
unitAmt,
|
||||||
|
callData,
|
||||||
|
0
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
let dataPara = ethers.utils.hexlify(await getCallData("PARASWAP-A", unitAmtParaswap, calldataPara));
|
||||||
|
let dataZeroX = ethers.utils.hexlify(await getCallData("ZEROX-A", unitAmt0x, calldataZeroX));
|
||||||
|
let datas = [dataZeroX, dataPara];
|
||||||
|
|
||||||
let connectors = ["ZEROX-A", "PARASWAP-A"];
|
let connectors = ["ZEROX-A", "PARASWAP-A"];
|
||||||
|
|
||||||
return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0];
|
return [connectors, datas];
|
||||||
}
|
}
|
||||||
|
|
||||||
let arg = await getArg();
|
let arg = await getArg();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user