mirror of
https://github.com/Instadapp/dsa-connectors.git
synced 2024-07-29 22:37:00 +00:00
updated code
This commit is contained in:
parent
8e98da39fd
commit
77caa8df66
|
@ -16,6 +16,7 @@ contract SwapHelpers {
|
|||
address sellAddr;
|
||||
uint256 sellAmt;
|
||||
uint256[] unitAmts;
|
||||
bytes4[] swapDatas;
|
||||
bytes[] callDatas;
|
||||
uint256 setId;
|
||||
}
|
||||
|
@ -27,11 +28,7 @@ contract SwapHelpers {
|
|||
*/
|
||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
||||
internal
|
||||
returns (
|
||||
bool success,
|
||||
bytes memory returnData,
|
||||
string memory _connector
|
||||
)
|
||||
returns (bool success, bytes memory returnData)
|
||||
{
|
||||
uint256 _length = _connectors.length;
|
||||
require(_length > 0, "zero-length-not-allowed");
|
||||
|
@ -43,25 +40,16 @@ contract SwapHelpers {
|
|||
_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++) {
|
||||
bytes4 swapData = (keccak256(bytes(_connectors[i])) ==
|
||||
keccak256(bytes("1INCH-A")))
|
||||
? bytes4(
|
||||
keccak256(
|
||||
"sell(address,address,uint256,uint256,bytes,uint256)"
|
||||
)
|
||||
)
|
||||
: bytes4(
|
||||
keccak256(
|
||||
"swap(address,address,uint256,uint256,bytes,uint256)"
|
||||
)
|
||||
);
|
||||
|
||||
bytes memory _data = abi.encodeWithSelector(
|
||||
swapData,
|
||||
_inputData.swapDatas[i],
|
||||
_inputData.buyAddr,
|
||||
_inputData.sellAddr,
|
||||
_inputData.sellAmt,
|
||||
|
@ -74,28 +62,8 @@ contract SwapHelpers {
|
|||
.connectors(_connectors[i])
|
||||
.delegatecall(_data);
|
||||
if (success) {
|
||||
_connector = _connectors[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function decodeEvents(string memory _connector, bytes memory returnData)
|
||||
internal
|
||||
view
|
||||
returns (uint256 _buyAmt, uint256 _sellAmt)
|
||||
{
|
||||
(, bytes memory _eventParam) = abi.decode(returnData, (string, bytes));
|
||||
if (keccak256(bytes(_connector)) == keccak256(bytes("PARASWAP-A"))) {
|
||||
(, , _buyAmt, _sellAmt, ) = abi.decode(
|
||||
_eventParam,
|
||||
(address, address, uint256, uint256, uint256)
|
||||
);
|
||||
} else {
|
||||
(, , _buyAmt, _sellAmt, , ) = abi.decode(
|
||||
_eventParam,
|
||||
(address, address, uint256, uint256, uint256, uint256)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,16 +20,18 @@ abstract contract Swap is SwapHelpers, Events {
|
|||
* @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(
|
||||
string[] memory _connectors,
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256[] memory unitAmts,
|
||||
bytes4[] memory swapDatas,
|
||||
bytes[] calldata callDatas,
|
||||
string[] memory _connectors,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
|
@ -41,31 +43,15 @@ abstract contract Swap is SwapHelpers, Events {
|
|||
sellAddr: sellAddr,
|
||||
sellAmt: sellAmt,
|
||||
unitAmts: unitAmts,
|
||||
swapDatas: swapDatas,
|
||||
callDatas: callDatas,
|
||||
setId: setId
|
||||
});
|
||||
|
||||
(
|
||||
bool success,
|
||||
bytes memory returnData,
|
||||
string memory _connector
|
||||
) = _swap(_connectors, inputData);
|
||||
|
||||
uint256 _buyAmt;
|
||||
uint256 _sellAmt;
|
||||
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
||||
|
||||
require(success, "swap-Aggregator-failed");
|
||||
(_buyAmt, _sellAmt) = decodeEvents(_connector, returnData);
|
||||
|
||||
_eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_connector,
|
||||
buyAddr,
|
||||
sellAddr,
|
||||
_buyAmt,
|
||||
_sellAmt,
|
||||
setId
|
||||
);
|
||||
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ contract SwapHelpers {
|
|||
address sellAddr;
|
||||
uint256 sellAmt;
|
||||
uint256[] unitAmts;
|
||||
bytes4[] swapDatas;
|
||||
bytes[] callDatas;
|
||||
uint256 setId;
|
||||
}
|
||||
|
@ -27,11 +28,7 @@ contract SwapHelpers {
|
|||
*/
|
||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
||||
internal
|
||||
returns (
|
||||
bool success,
|
||||
bytes memory returnData,
|
||||
string memory _connector
|
||||
)
|
||||
returns (bool success, bytes memory returnData)
|
||||
{
|
||||
uint256 _length = _connectors.length;
|
||||
require(_length > 0, "zero-length-not-allowed");
|
||||
|
@ -43,25 +40,16 @@ contract SwapHelpers {
|
|||
_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++) {
|
||||
bytes4 swapData = (keccak256(bytes(_connectors[i])) ==
|
||||
keccak256(bytes("1INCH-A")))
|
||||
? bytes4(
|
||||
keccak256(
|
||||
"sell(address,address,uint256,uint256,bytes,uint256)"
|
||||
)
|
||||
)
|
||||
: bytes4(
|
||||
keccak256(
|
||||
"swap(address,address,uint256,uint256,bytes,uint256)"
|
||||
)
|
||||
);
|
||||
|
||||
bytes memory _data = abi.encodeWithSelector(
|
||||
swapData,
|
||||
_inputData.swapDatas[i],
|
||||
_inputData.buyAddr,
|
||||
_inputData.sellAddr,
|
||||
_inputData.sellAmt,
|
||||
|
@ -74,28 +62,8 @@ contract SwapHelpers {
|
|||
.connectors(_connectors[i])
|
||||
.delegatecall(_data);
|
||||
if (success) {
|
||||
_connector = _connectors[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function decodeEvents(string memory _connector, bytes memory returnData)
|
||||
internal
|
||||
view
|
||||
returns (uint256 _buyAmt, uint256 _sellAmt)
|
||||
{
|
||||
(, bytes memory _eventParam) = abi.decode(returnData, (string, bytes));
|
||||
if (keccak256(bytes(_connector)) == keccak256(bytes("PARASWAP-A"))) {
|
||||
(, , _buyAmt, _sellAmt, ) = abi.decode(
|
||||
_eventParam,
|
||||
(address, address, uint256, uint256, uint256)
|
||||
);
|
||||
} else {
|
||||
(, , _buyAmt, _sellAmt, , ) = abi.decode(
|
||||
_eventParam,
|
||||
(address, address, uint256, uint256, uint256, uint256)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,16 +20,18 @@ abstract contract Swap is SwapHelpers, Events {
|
|||
* @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(
|
||||
string[] memory _connectors,
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256[] memory unitAmts,
|
||||
bytes4[] memory swapDatas,
|
||||
bytes[] calldata callDatas,
|
||||
string[] memory _connectors,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
|
@ -41,31 +43,15 @@ abstract contract Swap is SwapHelpers, Events {
|
|||
sellAddr: sellAddr,
|
||||
sellAmt: sellAmt,
|
||||
unitAmts: unitAmts,
|
||||
swapDatas: swapDatas,
|
||||
callDatas: callDatas,
|
||||
setId: setId
|
||||
});
|
||||
|
||||
(
|
||||
bool success,
|
||||
bytes memory returnData,
|
||||
string memory _connector
|
||||
) = _swap(_connectors, inputData);
|
||||
|
||||
uint256 _buyAmt;
|
||||
uint256 _sellAmt;
|
||||
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
||||
|
||||
require(success, "swap-Aggregator-failed");
|
||||
(_buyAmt, _sellAmt) = decodeEvents(_connector, returnData);
|
||||
|
||||
_eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_connector,
|
||||
buyAddr,
|
||||
sellAddr,
|
||||
_buyAmt,
|
||||
_sellAmt,
|
||||
setId
|
||||
);
|
||||
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ contract SwapHelpers {
|
|||
address sellAddr;
|
||||
uint256 sellAmt;
|
||||
uint256[] unitAmts;
|
||||
bytes4[] swapDatas;
|
||||
bytes[] callDatas;
|
||||
uint256 setId;
|
||||
}
|
||||
|
@ -27,11 +28,7 @@ contract SwapHelpers {
|
|||
*/
|
||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
||||
internal
|
||||
returns (
|
||||
bool success,
|
||||
bytes memory returnData,
|
||||
string memory _connector
|
||||
)
|
||||
returns (bool success, bytes memory returnData)
|
||||
{
|
||||
uint256 _length = _connectors.length;
|
||||
require(_length > 0, "zero-length-not-allowed");
|
||||
|
@ -43,25 +40,16 @@ contract SwapHelpers {
|
|||
_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++) {
|
||||
bytes4 swapData = (keccak256(bytes(_connectors[i])) ==
|
||||
keccak256(bytes("1INCH-A")))
|
||||
? bytes4(
|
||||
keccak256(
|
||||
"sell(address,address,uint256,uint256,bytes,uint256)"
|
||||
)
|
||||
)
|
||||
: bytes4(
|
||||
keccak256(
|
||||
"swap(address,address,uint256,uint256,bytes,uint256)"
|
||||
)
|
||||
);
|
||||
|
||||
bytes memory _data = abi.encodeWithSelector(
|
||||
swapData,
|
||||
_inputData.swapDatas[i],
|
||||
_inputData.buyAddr,
|
||||
_inputData.sellAddr,
|
||||
_inputData.sellAmt,
|
||||
|
@ -73,30 +61,9 @@ contract SwapHelpers {
|
|||
(success, returnData) = instaConnectors
|
||||
.connectors(_connectors[i])
|
||||
.delegatecall(_data);
|
||||
|
||||
if (success) {
|
||||
_connector = _connectors[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function decodeEvents(string memory _connector, bytes memory returnData)
|
||||
internal
|
||||
view
|
||||
returns (uint256 _buyAmt, uint256 _sellAmt)
|
||||
{
|
||||
(, bytes memory _eventParam) = abi.decode(returnData, (string, bytes));
|
||||
if (keccak256(bytes(_connector)) == keccak256(bytes("PARASWAP-A"))) {
|
||||
(, , _buyAmt, _sellAmt, ) = abi.decode(
|
||||
_eventParam,
|
||||
(address, address, uint256, uint256, uint256)
|
||||
);
|
||||
} else {
|
||||
(, , _buyAmt, _sellAmt, , ) = abi.decode(
|
||||
_eventParam,
|
||||
(address, address, uint256, uint256, uint256, uint256)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,16 +20,18 @@ abstract contract Swap is SwapHelpers, Events {
|
|||
* @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(
|
||||
string[] memory _connectors,
|
||||
address buyAddr,
|
||||
address sellAddr,
|
||||
uint256 sellAmt,
|
||||
uint256[] memory unitAmts,
|
||||
bytes4[] memory swapDatas,
|
||||
bytes[] calldata callDatas,
|
||||
string[] memory _connectors,
|
||||
uint256 setId
|
||||
)
|
||||
external
|
||||
|
@ -41,31 +43,15 @@ abstract contract Swap is SwapHelpers, Events {
|
|||
sellAddr: sellAddr,
|
||||
sellAmt: sellAmt,
|
||||
unitAmts: unitAmts,
|
||||
swapDatas: swapDatas,
|
||||
callDatas: callDatas,
|
||||
setId: setId
|
||||
});
|
||||
|
||||
(
|
||||
bool success,
|
||||
bytes memory returnData,
|
||||
string memory _connector
|
||||
) = _swap(_connectors, inputData);
|
||||
|
||||
uint256 _buyAmt;
|
||||
uint256 _sellAmt;
|
||||
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
||||
|
||||
require(success, "swap-Aggregator-failed");
|
||||
(_buyAmt, _sellAmt) = decodeEvents(_connector, returnData);
|
||||
|
||||
_eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)";
|
||||
_eventParam = abi.encode(
|
||||
_connector,
|
||||
buyAddr,
|
||||
sellAddr,
|
||||
_buyAmt,
|
||||
_sellAmt,
|
||||
setId
|
||||
);
|
||||
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -75,6 +75,22 @@ describe("Swap | Avalanche", function () {
|
|||
let buyTokenAmountZeroX: any;
|
||||
// let buyTokenAmount1Inch: 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() {
|
||||
// const slippage = 0.5;
|
||||
/* avax -> usdt */
|
||||
|
@ -90,7 +106,6 @@ describe("Swap | Avalanche", function () {
|
|||
let paraswapUrl1 = `https://apiv5.paraswap.io/prices/`;
|
||||
let paraswapUrl2 = `https://apiv5.paraswap.io/transactions/43114?ignoreChecks=true`;
|
||||
|
||||
|
||||
//paraswap
|
||||
let paramsPara = {
|
||||
srcToken: sellTokenAddress,
|
||||
|
@ -117,7 +132,7 @@ describe("Swap | Avalanche", function () {
|
|||
userAddress: dsaWallet0.address
|
||||
};
|
||||
const calldataPara = (await axios.post(paraswapUrl2, txConfig)).data.data;
|
||||
|
||||
|
||||
// zeroX
|
||||
const paramsZeroX = {
|
||||
buyToken: buyTokenAddress,
|
||||
|
@ -142,13 +157,15 @@ describe("Swap | Avalanche", function () {
|
|||
};
|
||||
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
||||
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
||||
|
||||
let unitAmts = [ unitAmtParaswap, unitAmt0x];
|
||||
let calldatas = [ calldataPara, calldataZeroX];
|
||||
let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A"));
|
||||
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
||||
let unitAmts = [unitAmtParaswap, unitAmt0x];
|
||||
let calldatas = [calldataPara, calldataZeroX];
|
||||
let swapDatas = [swapDataPara, swapDataZeroX];
|
||||
|
||||
let connectors = ["PARASWAP-A", "ZEROX-A"];
|
||||
|
||||
return [connectors, buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, calldatas, 0];
|
||||
return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0];
|
||||
}
|
||||
|
||||
let arg = await getArg();
|
||||
|
|
|
@ -75,6 +75,23 @@ describe("Swap | Mainnet", function () {
|
|||
let buyTokenAmountZeroX: any;
|
||||
let buyTokenAmount1Inch: 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() {
|
||||
const slippage = 0.5;
|
||||
/* eth -> dai */
|
||||
|
@ -131,21 +148,21 @@ describe("Swap | Mainnet", function () {
|
|||
|
||||
//1inch
|
||||
const paramDaiUsdc = {
|
||||
buyToken: buyTokenAddress,
|
||||
sellToken: sellTokenAddress,
|
||||
sellAmount: "1000000000000000000",
|
||||
dsaAddress: dsaWallet0.address
|
||||
}
|
||||
const response1 = await axios.get("https://api.instadapp.io/defi/mainnet/1inch/swap", {
|
||||
params: paramDaiUsdc
|
||||
});
|
||||
|
||||
const data1 = response1.data;
|
||||
// console.log(data1);
|
||||
let unitAmt1Inch = data1.unitAmt;
|
||||
const calldata1Inch = data1.calldata;
|
||||
buyTokenAmount1Inch = data1.buyTokenAmount;
|
||||
console.log(buyTokenAmount1Inch);
|
||||
buyToken: buyTokenAddress,
|
||||
sellToken: sellTokenAddress,
|
||||
sellAmount: "1000000000000000000",
|
||||
dsaAddress: dsaWallet0.address
|
||||
};
|
||||
const response1 = await axios.get("https://api.instadapp.io/defi/mainnet/1inch/swap", {
|
||||
params: paramDaiUsdc
|
||||
});
|
||||
|
||||
const data1 = response1.data;
|
||||
// console.log(data1);
|
||||
let unitAmt1Inch = data1.unitAmt;
|
||||
const calldata1Inch = data1.calldata;
|
||||
buyTokenAmount1Inch = data1.buyTokenAmount;
|
||||
console.log(buyTokenAmount1Inch);
|
||||
|
||||
let calculateUnitAmt = (buyAmount: any) => {
|
||||
const buyTokenAmountRes = new BigNumber(buyAmount)
|
||||
|
@ -161,12 +178,17 @@ describe("Swap | Mainnet", function () {
|
|||
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
||||
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
||||
|
||||
let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A"));
|
||||
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
||||
let swapData1Inch = ethers.utils.hexlify(await getSelector("1INCH-A"));
|
||||
|
||||
let unitAmts = [unitAmt1Inch, unitAmt0x, unitAmtParaswap];
|
||||
let calldatas = [calldata1Inch, calldataZeroX, calldataPara];
|
||||
let swapDatas = [swapData1Inch, swapDataZeroX, swapDataPara];
|
||||
|
||||
let connectors = ["1INCH-A","ZEROX-A", "PARASWAP-A"];
|
||||
let connectors = ["1INCH-A", "ZEROX-A", "PARASWAP-A"];
|
||||
|
||||
return [connectors, buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, calldatas, 0];
|
||||
return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0];
|
||||
}
|
||||
|
||||
let arg = await getArg();
|
||||
|
|
|
@ -63,7 +63,7 @@ describe("Swap", function () {
|
|||
it("Deposit matic into DSA wallet", async function () {
|
||||
await wallet0.sendTransaction({
|
||||
to: dsaWallet0.address,
|
||||
value: ethers.utils.parseEther("10")
|
||||
value: ethers.utils.parseEther("50")
|
||||
});
|
||||
|
||||
expect(await ethers.provider.getBalance(dsaWallet0.address)).to.be.gte(ethers.utils.parseEther("10"));
|
||||
|
@ -75,6 +75,23 @@ describe("Swap", function () {
|
|||
let buyTokenAmountZeroX: any;
|
||||
let buyTokenAmount1Inch: 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() {
|
||||
// const slippage = 0.5;
|
||||
/* matic -> usdt */
|
||||
|
@ -82,7 +99,7 @@ describe("Swap", function () {
|
|||
const sellTokenDecimals = 18;
|
||||
const buyTokenAddress = "0xc2132d05d31c914a87c6611c10748aeb04b58e8f"; // USDT, decimals 6
|
||||
const buyTokenDecimals = 6;
|
||||
const amount = 2;
|
||||
const amount = 1;
|
||||
|
||||
const srcAmount = new BigNumber(amount).times(new BigNumber(10).pow(sellTokenDecimals)).toFixed(0);
|
||||
|
||||
|
@ -94,7 +111,7 @@ describe("Swap", function () {
|
|||
const paramsZeroX = {
|
||||
buyToken: "USDT",
|
||||
sellToken: "MATIC",
|
||||
sellAmount: "2000000000000000000" // Always denominated in wei
|
||||
sellAmount: "1000000000000000000" // Always denominated in wei
|
||||
};
|
||||
|
||||
const responseZeroX = await axios.get(zeroXUrl, { params: paramsZeroX }).then((data: any) => data);
|
||||
|
@ -142,12 +159,16 @@ describe("Swap", function () {
|
|||
let unitAmt0x = calculateUnitAmt(buyTokenAmountZeroX);
|
||||
let unitAmtParaswap = calculateUnitAmt(buyTokenAmountParaswap);
|
||||
|
||||
let swapDataPara = ethers.utils.hexlify(await getSelector("PARASWAP-A"));
|
||||
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
||||
|
||||
let unitAmts = [unitAmt0x, unitAmtParaswap];
|
||||
let calldatas = [calldataZeroX, calldataPara];
|
||||
let swapDatas = [swapDataZeroX, swapDataPara];
|
||||
|
||||
let connectors = ["ZEROX-A", "PARASWAP-A"];
|
||||
|
||||
return [connectors, buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, calldatas, 0];
|
||||
return [buyTokenAddress, sellTokenAddress, srcAmount, unitAmts, swapDatas, calldatas, connectors, 0];
|
||||
}
|
||||
|
||||
let arg = await getArg();
|
||||
|
|
Loading…
Reference in New Issue
Block a user