mirror of
https://github.com/Instadapp/dsa-connectors-old.git
synced 2024-07-29 22:47:46 +00:00
1inch connector done
This commit is contained in:
parent
4cfc1e3cb7
commit
93cf4e81dc
|
|
@ -192,27 +192,18 @@ contract OneHelpers is Helpers {
|
||||||
|
|
||||||
contract Resolver is OneHelpers {
|
contract Resolver is OneHelpers {
|
||||||
function oneSplitSwap(
|
function oneSplitSwap(
|
||||||
|
OneSplitInterface oneSplitContract,
|
||||||
TokenInterface _sellAddr,
|
TokenInterface _sellAddr,
|
||||||
TokenInterface _buyAddr,
|
TokenInterface _buyAddr,
|
||||||
uint _sellAmt,
|
uint _sellAmt,
|
||||||
uint unitAmt
|
uint unitAmt,
|
||||||
) internal returns (uint buyAmt, uint[] memory distribution){
|
uint[] memory distribution,
|
||||||
|
uint disableDexes
|
||||||
|
) internal returns (uint buyAmt){
|
||||||
(uint _buyDec, uint _sellDec) = getTokensDec(_buyAddr, _sellAddr);
|
(uint _buyDec, uint _sellDec) = getTokensDec(_buyAddr, _sellAddr);
|
||||||
uint _sellAmt18 = convertTo18(_sellDec, _sellAmt);
|
uint _sellAmt18 = convertTo18(_sellDec, _sellAmt);
|
||||||
uint _slippageAmt = convert18ToDec(_buyDec, wmul(unitAmt, _sellAmt18));
|
uint _slippageAmt = convert18ToDec(_buyDec, wmul(unitAmt, _sellAmt18));
|
||||||
|
|
||||||
OneSplitInterface oneSplitContract = OneSplitInterface(getOneSplitAddress());
|
|
||||||
|
|
||||||
(buyAmt, distribution) = oneSplitContract.getExpectedReturn(
|
|
||||||
_sellAddr,
|
|
||||||
_buyAddr,
|
|
||||||
_sellAmt,
|
|
||||||
3, // TODO - shall we hardcode?
|
|
||||||
0 // TODO - do we need to disable anything?
|
|
||||||
);
|
|
||||||
|
|
||||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
|
||||||
|
|
||||||
uint ethAmt;
|
uint ethAmt;
|
||||||
if (address(_sellAddr) == getAddressETH()) {
|
if (address(_sellAddr) == getAddressETH()) {
|
||||||
ethAmt = _sellAmt;
|
ethAmt = _sellAmt;
|
||||||
|
|
@ -220,27 +211,23 @@ contract Resolver is OneHelpers {
|
||||||
_sellAddr.approve(address(oneSplitContract), _sellAmt);
|
_sellAddr.approve(address(oneSplitContract), _sellAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint initalBal = getTokenBal(_buyAddr);
|
||||||
|
|
||||||
oneSplitContract.swap.value(ethAmt)(
|
oneSplitContract.swap.value(ethAmt)(
|
||||||
_sellAddr,
|
_sellAddr,
|
||||||
_buyAddr,
|
_buyAddr,
|
||||||
_sellAmt,
|
_sellAmt,
|
||||||
_slippageAmt,
|
_slippageAmt,
|
||||||
distribution,
|
distribution,
|
||||||
0
|
disableDexes
|
||||||
);
|
);
|
||||||
|
|
||||||
|
uint finalBal = getTokenBal(_buyAddr);
|
||||||
|
buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
}
|
}
|
||||||
|
|
||||||
function oneInchSwap(
|
|
||||||
bytes memory _callData,
|
|
||||||
uint ethAmt
|
|
||||||
)
|
|
||||||
internal returns (uint buyAmt) {
|
|
||||||
// solium-disable-next-line security/no-call-value
|
|
||||||
(bool success, bytes memory data) = address(getOneInchAddress()).call.value(ethAmt)(_callData);
|
|
||||||
if (!success) revert("1Inch-swap-failed");
|
|
||||||
|
|
||||||
buyAmt = abi.decode(data, (uint));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
contract BasicResolver is Resolver {
|
contract BasicResolver is Resolver {
|
||||||
|
|
@ -252,7 +239,7 @@ contract BasicResolver is Resolver {
|
||||||
uint256 setId
|
uint256 setId
|
||||||
);
|
);
|
||||||
|
|
||||||
event LogSellOneSplit(
|
event LogSell(
|
||||||
address indexed buyToken,
|
address indexed buyToken,
|
||||||
address indexed sellToken,
|
address indexed sellToken,
|
||||||
uint256 buyAmt,
|
uint256 buyAmt,
|
||||||
|
|
@ -268,34 +255,84 @@ contract BasicResolver is Resolver {
|
||||||
uint unitAmt,
|
uint unitAmt,
|
||||||
uint getId,
|
uint getId,
|
||||||
uint setId
|
uint setId
|
||||||
) external payable {
|
) public payable {
|
||||||
uint _sellAmt = sellAmt;
|
uint _sellAmt = getUint(getId, sellAmt);
|
||||||
|
|
||||||
TokenInterface _buyAddr = TokenInterface(buyAddr);
|
TokenInterface _buyAddr = TokenInterface(buyAddr);
|
||||||
TokenInterface _sellAddr = TokenInterface(sellAddr);
|
TokenInterface _sellAddr = TokenInterface(sellAddr);
|
||||||
|
|
||||||
uint initalBal = getTokenBal(_buyAddr);
|
|
||||||
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
||||||
|
|
||||||
oneSplitSwap(
|
OneSplitInterface oneSplitContract = OneSplitInterface(getOneSplitAddress());
|
||||||
|
|
||||||
|
(, uint[] memory distribution) = oneSplitContract.getExpectedReturn(
|
||||||
|
_sellAddr,
|
||||||
|
_buyAddr,
|
||||||
|
_sellAmt,
|
||||||
|
3, // TODO - shall we hardcode?
|
||||||
|
0 // TODO - do we need to disable anything?
|
||||||
|
);
|
||||||
|
|
||||||
|
uint _buyAmt = oneSplitSwap(
|
||||||
|
oneSplitContract,
|
||||||
_sellAddr,
|
_sellAddr,
|
||||||
_buyAddr,
|
_buyAddr,
|
||||||
_sellAmt,
|
_sellAmt,
|
||||||
unitAmt
|
unitAmt,
|
||||||
|
distribution,
|
||||||
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
uint finialBal = getTokenBal(_buyAddr);
|
|
||||||
uint _buyAmt = sub(finialBal, initalBal);
|
|
||||||
|
|
||||||
setUint(setId, _buyAmt);
|
setUint(setId, _buyAmt);
|
||||||
emit LogSellOneSplit(address(_buyAddr), address(_sellAddr), _buyAmt, _sellAmt, getId, setId);
|
emit LogSell(address(_buyAddr), address(_sellAddr), _buyAmt, _sellAmt, getId, setId);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function sell(
|
function sell(
|
||||||
|
address buyAddr,
|
||||||
|
address sellAddr,
|
||||||
|
uint sellAmt,
|
||||||
|
uint unitAmt,
|
||||||
|
uint[] calldata distribution,
|
||||||
|
uint disableDexes,
|
||||||
|
uint getId,
|
||||||
|
uint setId
|
||||||
|
) external payable {
|
||||||
|
uint _sellAmt = getUint(getId, sellAmt);
|
||||||
|
|
||||||
|
TokenInterface _buyAddr = TokenInterface(buyAddr);
|
||||||
|
TokenInterface _sellAddr = TokenInterface(sellAddr);
|
||||||
|
|
||||||
|
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
||||||
|
|
||||||
|
uint _buyAmt = oneSplitSwap(
|
||||||
|
OneSplitInterface(getOneSplitAddress()),
|
||||||
|
_sellAddr,
|
||||||
|
_buyAddr,
|
||||||
|
_sellAmt,
|
||||||
|
unitAmt,
|
||||||
|
distribution,
|
||||||
|
disableDexes
|
||||||
|
);
|
||||||
|
|
||||||
|
setUint(setId, _buyAmt);
|
||||||
|
emit LogSell(address(_buyAddr), address(_sellAddr), _buyAmt, _sellAmt, getId, setId);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sell(
|
||||||
|
address buyAddr,
|
||||||
|
address sellAddr,
|
||||||
|
uint sellAmt,
|
||||||
|
uint unitAmt,
|
||||||
bytes calldata callData,
|
bytes calldata callData,
|
||||||
uint setId
|
uint setId
|
||||||
) external payable {
|
) external payable {
|
||||||
(address _buyAddr, address _sellAddr, uint sellAmt) = decodeData(callData);
|
TokenInterface _buyAddr = TokenInterface(buyAddr);
|
||||||
|
TokenInterface _sellAddr = TokenInterface(sellAddr);
|
||||||
|
|
||||||
|
(uint _buyDec, uint _sellDec) = getTokensDec(_buyAddr, _sellAddr);
|
||||||
|
uint _sellAmt18 = convertTo18(_sellDec, sellAmt);
|
||||||
|
uint _slippageAmt = convert18ToDec(_buyDec, wmul(unitAmt, _sellAmt18));
|
||||||
|
|
||||||
uint ethAmt;
|
uint ethAmt;
|
||||||
if (address(_sellAddr) == getAddressETH()) {
|
if (address(_sellAddr) == getAddressETH()) {
|
||||||
|
|
@ -304,10 +341,17 @@ contract BasicResolver is Resolver {
|
||||||
TokenInterface(_sellAddr).approve(getOneInchAddress(), sellAmt);
|
TokenInterface(_sellAddr).approve(getOneInchAddress(), sellAmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint _buyAmt = oneInchSwap(callData, ethAmt);
|
uint initalBal = getTokenBal(_buyAddr);
|
||||||
|
|
||||||
setUint(setId, _buyAmt);
|
address(getOneInchAddress()).call.value(ethAmt)(callData);
|
||||||
emit LogSellOneInch(address(_buyAddr), address(_sellAddr), _buyAmt, sellAmt, setId);
|
|
||||||
|
uint finalBal = getTokenBal(_buyAddr);
|
||||||
|
uint buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
|
|
||||||
|
setUint(setId, buyAmt);
|
||||||
|
emit LogSell(address(_buyAddr), address(_sellAddr), buyAmt, sellAmt, 0, setId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user