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;
|
address sellAddr;
|
||||||
uint256 sellAmt;
|
uint256 sellAmt;
|
||||||
uint256[] unitAmts;
|
uint256[] unitAmts;
|
||||||
|
bytes4[] swapDatas;
|
||||||
bytes[] callDatas;
|
bytes[] callDatas;
|
||||||
uint256 setId;
|
uint256 setId;
|
||||||
}
|
}
|
||||||
|
@ -27,11 +28,7 @@ contract SwapHelpers {
|
||||||
*/
|
*/
|
||||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
function _swap(string[] memory _connectors, InputData memory _inputData)
|
||||||
internal
|
internal
|
||||||
returns (
|
returns (bool success, bytes memory returnData)
|
||||||
bool success,
|
|
||||||
bytes memory returnData,
|
|
||||||
string memory _connector
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
uint256 _length = _connectors.length;
|
uint256 _length = _connectors.length;
|
||||||
require(_length > 0, "zero-length-not-allowed");
|
require(_length > 0, "zero-length-not-allowed");
|
||||||
|
@ -43,25 +40,16 @@ contract SwapHelpers {
|
||||||
_inputData.callDatas.length == _length,
|
_inputData.callDatas.length == _length,
|
||||||
"callDatas-length-invalid"
|
"callDatas-length-invalid"
|
||||||
);
|
);
|
||||||
|
require(
|
||||||
|
_inputData.swapDatas.length == _length,
|
||||||
|
"swapDatas-length-invalid"
|
||||||
|
);
|
||||||
|
|
||||||
// require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors
|
// 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++) {
|
||||||
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(
|
bytes memory _data = abi.encodeWithSelector(
|
||||||
swapData,
|
_inputData.swapDatas[i],
|
||||||
_inputData.buyAddr,
|
_inputData.buyAddr,
|
||||||
_inputData.sellAddr,
|
_inputData.sellAddr,
|
||||||
_inputData.sellAmt,
|
_inputData.sellAmt,
|
||||||
|
@ -74,28 +62,8 @@ contract SwapHelpers {
|
||||||
.connectors(_connectors[i])
|
.connectors(_connectors[i])
|
||||||
.delegatecall(_data);
|
.delegatecall(_data);
|
||||||
if (success) {
|
if (success) {
|
||||||
_connector = _connectors[i];
|
|
||||||
break;
|
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 sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param sellAmt The amount of the token to sell.
|
* @param sellAmt The amount of the token to sell.
|
||||||
* @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs.
|
* @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 callDatas Data from APIs for respective DEXs.
|
||||||
* @param setId ID stores the amount of token brought.
|
* @param setId ID stores the amount of token brought.
|
||||||
*/
|
*/
|
||||||
function swap(
|
function swap(
|
||||||
string[] memory _connectors,
|
|
||||||
address buyAddr,
|
address buyAddr,
|
||||||
address sellAddr,
|
address sellAddr,
|
||||||
uint256 sellAmt,
|
uint256 sellAmt,
|
||||||
uint256[] memory unitAmts,
|
uint256[] memory unitAmts,
|
||||||
|
bytes4[] memory swapDatas,
|
||||||
bytes[] calldata callDatas,
|
bytes[] calldata callDatas,
|
||||||
|
string[] memory _connectors,
|
||||||
uint256 setId
|
uint256 setId
|
||||||
)
|
)
|
||||||
external
|
external
|
||||||
|
@ -41,31 +43,15 @@ abstract contract Swap is SwapHelpers, Events {
|
||||||
sellAddr: sellAddr,
|
sellAddr: sellAddr,
|
||||||
sellAmt: sellAmt,
|
sellAmt: sellAmt,
|
||||||
unitAmts: unitAmts,
|
unitAmts: unitAmts,
|
||||||
|
swapDatas: swapDatas,
|
||||||
callDatas: callDatas,
|
callDatas: callDatas,
|
||||||
setId: setId
|
setId: setId
|
||||||
});
|
});
|
||||||
|
|
||||||
(
|
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
||||||
bool success,
|
|
||||||
bytes memory returnData,
|
|
||||||
string memory _connector
|
|
||||||
) = _swap(_connectors, inputData);
|
|
||||||
|
|
||||||
uint256 _buyAmt;
|
|
||||||
uint256 _sellAmt;
|
|
||||||
|
|
||||||
require(success, "swap-Aggregator-failed");
|
require(success, "swap-Aggregator-failed");
|
||||||
(_buyAmt, _sellAmt) = decodeEvents(_connector, returnData);
|
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||||
|
|
||||||
_eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)";
|
|
||||||
_eventParam = abi.encode(
|
|
||||||
_connector,
|
|
||||||
buyAddr,
|
|
||||||
sellAddr,
|
|
||||||
_buyAmt,
|
|
||||||
_sellAmt,
|
|
||||||
setId
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ contract SwapHelpers {
|
||||||
address sellAddr;
|
address sellAddr;
|
||||||
uint256 sellAmt;
|
uint256 sellAmt;
|
||||||
uint256[] unitAmts;
|
uint256[] unitAmts;
|
||||||
|
bytes4[] swapDatas;
|
||||||
bytes[] callDatas;
|
bytes[] callDatas;
|
||||||
uint256 setId;
|
uint256 setId;
|
||||||
}
|
}
|
||||||
|
@ -27,11 +28,7 @@ contract SwapHelpers {
|
||||||
*/
|
*/
|
||||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
function _swap(string[] memory _connectors, InputData memory _inputData)
|
||||||
internal
|
internal
|
||||||
returns (
|
returns (bool success, bytes memory returnData)
|
||||||
bool success,
|
|
||||||
bytes memory returnData,
|
|
||||||
string memory _connector
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
uint256 _length = _connectors.length;
|
uint256 _length = _connectors.length;
|
||||||
require(_length > 0, "zero-length-not-allowed");
|
require(_length > 0, "zero-length-not-allowed");
|
||||||
|
@ -43,25 +40,16 @@ contract SwapHelpers {
|
||||||
_inputData.callDatas.length == _length,
|
_inputData.callDatas.length == _length,
|
||||||
"callDatas-length-invalid"
|
"callDatas-length-invalid"
|
||||||
);
|
);
|
||||||
|
require(
|
||||||
|
_inputData.swapDatas.length == _length,
|
||||||
|
"swapDatas-length-invalid"
|
||||||
|
);
|
||||||
|
|
||||||
// require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors
|
// 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++) {
|
||||||
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(
|
bytes memory _data = abi.encodeWithSelector(
|
||||||
swapData,
|
_inputData.swapDatas[i],
|
||||||
_inputData.buyAddr,
|
_inputData.buyAddr,
|
||||||
_inputData.sellAddr,
|
_inputData.sellAddr,
|
||||||
_inputData.sellAmt,
|
_inputData.sellAmt,
|
||||||
|
@ -74,28 +62,8 @@ contract SwapHelpers {
|
||||||
.connectors(_connectors[i])
|
.connectors(_connectors[i])
|
||||||
.delegatecall(_data);
|
.delegatecall(_data);
|
||||||
if (success) {
|
if (success) {
|
||||||
_connector = _connectors[i];
|
|
||||||
break;
|
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 sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param sellAmt The amount of the token to sell.
|
* @param sellAmt The amount of the token to sell.
|
||||||
* @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs.
|
* @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 callDatas Data from APIs for respective DEXs.
|
||||||
* @param setId ID stores the amount of token brought.
|
* @param setId ID stores the amount of token brought.
|
||||||
*/
|
*/
|
||||||
function swap(
|
function swap(
|
||||||
string[] memory _connectors,
|
|
||||||
address buyAddr,
|
address buyAddr,
|
||||||
address sellAddr,
|
address sellAddr,
|
||||||
uint256 sellAmt,
|
uint256 sellAmt,
|
||||||
uint256[] memory unitAmts,
|
uint256[] memory unitAmts,
|
||||||
|
bytes4[] memory swapDatas,
|
||||||
bytes[] calldata callDatas,
|
bytes[] calldata callDatas,
|
||||||
|
string[] memory _connectors,
|
||||||
uint256 setId
|
uint256 setId
|
||||||
)
|
)
|
||||||
external
|
external
|
||||||
|
@ -41,31 +43,15 @@ abstract contract Swap is SwapHelpers, Events {
|
||||||
sellAddr: sellAddr,
|
sellAddr: sellAddr,
|
||||||
sellAmt: sellAmt,
|
sellAmt: sellAmt,
|
||||||
unitAmts: unitAmts,
|
unitAmts: unitAmts,
|
||||||
|
swapDatas: swapDatas,
|
||||||
callDatas: callDatas,
|
callDatas: callDatas,
|
||||||
setId: setId
|
setId: setId
|
||||||
});
|
});
|
||||||
|
|
||||||
(
|
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
||||||
bool success,
|
|
||||||
bytes memory returnData,
|
|
||||||
string memory _connector
|
|
||||||
) = _swap(_connectors, inputData);
|
|
||||||
|
|
||||||
uint256 _buyAmt;
|
|
||||||
uint256 _sellAmt;
|
|
||||||
|
|
||||||
require(success, "swap-Aggregator-failed");
|
require(success, "swap-Aggregator-failed");
|
||||||
(_buyAmt, _sellAmt) = decodeEvents(_connector, returnData);
|
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||||
|
|
||||||
_eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)";
|
|
||||||
_eventParam = abi.encode(
|
|
||||||
_connector,
|
|
||||||
buyAddr,
|
|
||||||
sellAddr,
|
|
||||||
_buyAmt,
|
|
||||||
_sellAmt,
|
|
||||||
setId
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@ contract SwapHelpers {
|
||||||
address sellAddr;
|
address sellAddr;
|
||||||
uint256 sellAmt;
|
uint256 sellAmt;
|
||||||
uint256[] unitAmts;
|
uint256[] unitAmts;
|
||||||
|
bytes4[] swapDatas;
|
||||||
bytes[] callDatas;
|
bytes[] callDatas;
|
||||||
uint256 setId;
|
uint256 setId;
|
||||||
}
|
}
|
||||||
|
@ -27,11 +28,7 @@ contract SwapHelpers {
|
||||||
*/
|
*/
|
||||||
function _swap(string[] memory _connectors, InputData memory _inputData)
|
function _swap(string[] memory _connectors, InputData memory _inputData)
|
||||||
internal
|
internal
|
||||||
returns (
|
returns (bool success, bytes memory returnData)
|
||||||
bool success,
|
|
||||||
bytes memory returnData,
|
|
||||||
string memory _connector
|
|
||||||
)
|
|
||||||
{
|
{
|
||||||
uint256 _length = _connectors.length;
|
uint256 _length = _connectors.length;
|
||||||
require(_length > 0, "zero-length-not-allowed");
|
require(_length > 0, "zero-length-not-allowed");
|
||||||
|
@ -43,25 +40,16 @@ contract SwapHelpers {
|
||||||
_inputData.callDatas.length == _length,
|
_inputData.callDatas.length == _length,
|
||||||
"callDatas-length-invalid"
|
"callDatas-length-invalid"
|
||||||
);
|
);
|
||||||
|
require(
|
||||||
|
_inputData.swapDatas.length == _length,
|
||||||
|
"swapDatas-length-invalid"
|
||||||
|
);
|
||||||
|
|
||||||
// require _connectors[i] == "1INCH-A" || "ZEROX-A" || "PARASWAP-A" || similar connectors
|
// 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++) {
|
||||||
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(
|
bytes memory _data = abi.encodeWithSelector(
|
||||||
swapData,
|
_inputData.swapDatas[i],
|
||||||
_inputData.buyAddr,
|
_inputData.buyAddr,
|
||||||
_inputData.sellAddr,
|
_inputData.sellAddr,
|
||||||
_inputData.sellAmt,
|
_inputData.sellAmt,
|
||||||
|
@ -73,30 +61,9 @@ contract SwapHelpers {
|
||||||
(success, returnData) = instaConnectors
|
(success, returnData) = instaConnectors
|
||||||
.connectors(_connectors[i])
|
.connectors(_connectors[i])
|
||||||
.delegatecall(_data);
|
.delegatecall(_data);
|
||||||
|
|
||||||
if (success) {
|
if (success) {
|
||||||
_connector = _connectors[i];
|
|
||||||
break;
|
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 sellAddr The address of the token to sell.(For MATIC: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
* @param sellAmt The amount of the token to sell.
|
* @param sellAmt The amount of the token to sell.
|
||||||
* @param unitAmts The amount of buyAmt/sellAmt with slippage for respective DEXs.
|
* @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 callDatas Data from APIs for respective DEXs.
|
||||||
* @param setId ID stores the amount of token brought.
|
* @param setId ID stores the amount of token brought.
|
||||||
*/
|
*/
|
||||||
function swap(
|
function swap(
|
||||||
string[] memory _connectors,
|
|
||||||
address buyAddr,
|
address buyAddr,
|
||||||
address sellAddr,
|
address sellAddr,
|
||||||
uint256 sellAmt,
|
uint256 sellAmt,
|
||||||
uint256[] memory unitAmts,
|
uint256[] memory unitAmts,
|
||||||
|
bytes4[] memory swapDatas,
|
||||||
bytes[] calldata callDatas,
|
bytes[] calldata callDatas,
|
||||||
|
string[] memory _connectors,
|
||||||
uint256 setId
|
uint256 setId
|
||||||
)
|
)
|
||||||
external
|
external
|
||||||
|
@ -41,31 +43,15 @@ abstract contract Swap is SwapHelpers, Events {
|
||||||
sellAddr: sellAddr,
|
sellAddr: sellAddr,
|
||||||
sellAmt: sellAmt,
|
sellAmt: sellAmt,
|
||||||
unitAmts: unitAmts,
|
unitAmts: unitAmts,
|
||||||
|
swapDatas: swapDatas,
|
||||||
callDatas: callDatas,
|
callDatas: callDatas,
|
||||||
setId: setId
|
setId: setId
|
||||||
});
|
});
|
||||||
|
|
||||||
(
|
(bool success, bytes memory returnData) = _swap(_connectors, inputData);
|
||||||
bool success,
|
|
||||||
bytes memory returnData,
|
|
||||||
string memory _connector
|
|
||||||
) = _swap(_connectors, inputData);
|
|
||||||
|
|
||||||
uint256 _buyAmt;
|
|
||||||
uint256 _sellAmt;
|
|
||||||
|
|
||||||
require(success, "swap-Aggregator-failed");
|
require(success, "swap-Aggregator-failed");
|
||||||
(_buyAmt, _sellAmt) = decodeEvents(_connector, returnData);
|
(_eventName, _eventParam) = abi.decode(returnData, (string, bytes));
|
||||||
|
|
||||||
_eventName = "LogSwapAggregator(string,address,address,uint256,uint256,uint256)";
|
|
||||||
_eventParam = abi.encode(
|
|
||||||
_connector,
|
|
||||||
buyAddr,
|
|
||||||
sellAddr,
|
|
||||||
_buyAmt,
|
|
||||||
_sellAmt,
|
|
||||||
setId
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,22 @@ describe("Swap | Avalanche", function () {
|
||||||
let buyTokenAmountZeroX: any;
|
let buyTokenAmountZeroX: 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 */
|
||||||
|
@ -90,7 +106,6 @@ describe("Swap | Avalanche", function () {
|
||||||
let paraswapUrl1 = `https://apiv5.paraswap.io/prices/`;
|
let paraswapUrl1 = `https://apiv5.paraswap.io/prices/`;
|
||||||
let paraswapUrl2 = `https://apiv5.paraswap.io/transactions/43114?ignoreChecks=true`;
|
let paraswapUrl2 = `https://apiv5.paraswap.io/transactions/43114?ignoreChecks=true`;
|
||||||
|
|
||||||
|
|
||||||
//paraswap
|
//paraswap
|
||||||
let paramsPara = {
|
let paramsPara = {
|
||||||
srcToken: sellTokenAddress,
|
srcToken: sellTokenAddress,
|
||||||
|
@ -142,13 +157,15 @@ describe("Swap | Avalanche", 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"));
|
||||||
let unitAmts = [ unitAmtParaswap, unitAmt0x];
|
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
||||||
let calldatas = [ calldataPara, calldataZeroX];
|
let unitAmts = [unitAmtParaswap, unitAmt0x];
|
||||||
|
let calldatas = [calldataPara, calldataZeroX];
|
||||||
|
let swapDatas = [swapDataPara, swapDataZeroX];
|
||||||
|
|
||||||
let connectors = ["PARASWAP-A", "ZEROX-A"];
|
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();
|
let arg = await getArg();
|
||||||
|
|
|
@ -75,6 +75,23 @@ describe("Swap | Mainnet", function () {
|
||||||
let buyTokenAmountZeroX: any;
|
let buyTokenAmountZeroX: 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;
|
||||||
/* eth -> dai */
|
/* eth -> dai */
|
||||||
|
@ -135,7 +152,7 @@ describe("Swap | Mainnet", function () {
|
||||||
sellToken: sellTokenAddress,
|
sellToken: sellTokenAddress,
|
||||||
sellAmount: "1000000000000000000",
|
sellAmount: "1000000000000000000",
|
||||||
dsaAddress: dsaWallet0.address
|
dsaAddress: dsaWallet0.address
|
||||||
}
|
};
|
||||||
const response1 = await axios.get("https://api.instadapp.io/defi/mainnet/1inch/swap", {
|
const response1 = await axios.get("https://api.instadapp.io/defi/mainnet/1inch/swap", {
|
||||||
params: paramDaiUsdc
|
params: paramDaiUsdc
|
||||||
});
|
});
|
||||||
|
@ -161,12 +178,17 @@ 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"));
|
||||||
|
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
||||||
|
let swapData1Inch = ethers.utils.hexlify(await getSelector("1INCH-A"));
|
||||||
|
|
||||||
let unitAmts = [unitAmt1Inch, unitAmt0x, unitAmtParaswap];
|
let unitAmts = [unitAmt1Inch, unitAmt0x, unitAmtParaswap];
|
||||||
let calldatas = [calldata1Inch, calldataZeroX, calldataPara];
|
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();
|
let arg = await getArg();
|
||||||
|
|
|
@ -63,7 +63,7 @@ describe("Swap", function () {
|
||||||
it("Deposit matic into DSA wallet", async function () {
|
it("Deposit matic into DSA wallet", async function () {
|
||||||
await wallet0.sendTransaction({
|
await wallet0.sendTransaction({
|
||||||
to: dsaWallet0.address,
|
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"));
|
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 buyTokenAmountZeroX: 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;
|
||||||
/* matic -> usdt */
|
/* matic -> usdt */
|
||||||
|
@ -82,7 +99,7 @@ describe("Swap", function () {
|
||||||
const sellTokenDecimals = 18;
|
const sellTokenDecimals = 18;
|
||||||
const buyTokenAddress = "0xc2132d05d31c914a87c6611c10748aeb04b58e8f"; // USDT, decimals 6
|
const buyTokenAddress = "0xc2132d05d31c914a87c6611c10748aeb04b58e8f"; // USDT, decimals 6
|
||||||
const buyTokenDecimals = 6;
|
const buyTokenDecimals = 6;
|
||||||
const amount = 2;
|
const amount = 1;
|
||||||
|
|
||||||
const srcAmount = new BigNumber(amount).times(new BigNumber(10).pow(sellTokenDecimals)).toFixed(0);
|
const srcAmount = new BigNumber(amount).times(new BigNumber(10).pow(sellTokenDecimals)).toFixed(0);
|
||||||
|
|
||||||
|
@ -94,7 +111,7 @@ describe("Swap", function () {
|
||||||
const paramsZeroX = {
|
const paramsZeroX = {
|
||||||
buyToken: "USDT",
|
buyToken: "USDT",
|
||||||
sellToken: "MATIC",
|
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);
|
const responseZeroX = await axios.get(zeroXUrl, { params: paramsZeroX }).then((data: any) => data);
|
||||||
|
@ -142,12 +159,16 @@ 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"));
|
||||||
|
let swapDataZeroX = ethers.utils.hexlify(await getSelector("ZEROX-A"));
|
||||||
|
|
||||||
let unitAmts = [unitAmt0x, unitAmtParaswap];
|
let unitAmts = [unitAmt0x, unitAmtParaswap];
|
||||||
let calldatas = [calldataZeroX, calldataPara];
|
let calldatas = [calldataZeroX, calldataPara];
|
||||||
|
let swapDatas = [swapDataZeroX, swapDataPara];
|
||||||
|
|
||||||
let connectors = ["ZEROX-A", "PARASWAP-A"];
|
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();
|
let arg = await getArg();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user