mirror of
https://github.com/Instadapp/dsa-connectors-old.git
synced 2024-07-29 22:47:46 +00:00
Done with 1inch connector
This commit is contained in:
parent
6ef89a38fc
commit
e6574acdad
|
@ -23,15 +23,27 @@ interface OneInchInterace {
|
||||||
returns (uint256 returnAmount);
|
returns (uint256 returnAmount);
|
||||||
}
|
}
|
||||||
|
|
||||||
interface OneSplitInterface {
|
interface OneProtoInterface {
|
||||||
function swap(
|
function swapWithReferral(
|
||||||
TokenInterface fromToken,
|
TokenInterface fromToken,
|
||||||
TokenInterface toToken,
|
TokenInterface destToken,
|
||||||
uint256 amount,
|
uint256 amount,
|
||||||
uint256 minReturn,
|
uint256 minReturn,
|
||||||
uint256[] calldata distribution,
|
uint256[] calldata distribution,
|
||||||
uint256 disableFlags
|
uint256 flags, // See contants in IOneSplit.sol
|
||||||
) external payable;
|
address referral,
|
||||||
|
uint256 feePercent
|
||||||
|
) external payable returns(uint256);
|
||||||
|
|
||||||
|
function swapWithReferralMulti(
|
||||||
|
TokenInterface[] calldata tokens,
|
||||||
|
uint256 amount,
|
||||||
|
uint256 minReturn,
|
||||||
|
uint256[] calldata distribution,
|
||||||
|
uint256[] calldata flags,
|
||||||
|
address referral,
|
||||||
|
uint256 feePercent
|
||||||
|
) external payable returns(uint256 returnAmount);
|
||||||
|
|
||||||
function getExpectedReturn(
|
function getExpectedReturn(
|
||||||
TokenInterface fromToken,
|
TokenInterface fromToken,
|
||||||
|
@ -78,6 +90,10 @@ contract OneHelpers is Stores, DSMath {
|
||||||
return 0xf88309d7;
|
return 0xf88309d7;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getReferralAddr() internal pure returns (address) {
|
||||||
|
return 0xa7615CD307F323172331865181DC8b80a2834324;
|
||||||
|
}
|
||||||
|
|
||||||
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
function convert18ToDec(uint _dec, uint256 _amt) internal pure returns (uint256 amt) {
|
||||||
amt = (_amt / 10 ** (18 - _dec));
|
amt = (_amt / 10 ** (18 - _dec));
|
||||||
}
|
}
|
||||||
|
@ -108,8 +124,8 @@ contract Resolver is OneHelpers {
|
||||||
isOk = sig == getOneSplitSig();
|
isOk = sig == getOneSplitSig();
|
||||||
}
|
}
|
||||||
|
|
||||||
function oneSplitSwap(
|
function oneProtoSwap(
|
||||||
OneSplitInterface oneSplitContract,
|
OneProtoInterface oneSplitContract,
|
||||||
TokenInterface _sellAddr,
|
TokenInterface _sellAddr,
|
||||||
TokenInterface _buyAddr,
|
TokenInterface _buyAddr,
|
||||||
uint _sellAmt,
|
uint _sellAmt,
|
||||||
|
@ -130,13 +146,15 @@ contract Resolver is OneHelpers {
|
||||||
|
|
||||||
uint initalBal = getTokenBal(_buyAddr);
|
uint initalBal = getTokenBal(_buyAddr);
|
||||||
|
|
||||||
oneSplitContract.swap.value(ethAmt)(
|
oneSplitContract.swapWithReferral.value(ethAmt)(
|
||||||
_sellAddr,
|
_sellAddr,
|
||||||
_buyAddr,
|
_buyAddr,
|
||||||
_sellAmt,
|
_sellAmt,
|
||||||
_slippageAmt,
|
_slippageAmt,
|
||||||
distribution,
|
distribution,
|
||||||
disableDexes
|
disableDexes,
|
||||||
|
getReferralAddr(),
|
||||||
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
uint finalBal = getTokenBal(_buyAddr);
|
uint finalBal = getTokenBal(_buyAddr);
|
||||||
|
@ -145,6 +163,44 @@ contract Resolver is OneHelpers {
|
||||||
require(_slippageAmt <= buyAmt, "Too much slippage");
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function oneProtoSwapMulti(
|
||||||
|
OneProtoInterface oneSplitContract,
|
||||||
|
TokenInterface[] memory _tokens,
|
||||||
|
TokenInterface _sellAddr,
|
||||||
|
TokenInterface _buyAddr,
|
||||||
|
uint _sellAmt,
|
||||||
|
uint unitAmt,
|
||||||
|
uint[] memory distribution,
|
||||||
|
uint[] memory disableDexes
|
||||||
|
) internal returns (uint buyAmt){
|
||||||
|
(uint _buyDec, uint _sellDec) = getTokensDec(_buyAddr, _sellAddr);
|
||||||
|
uint _sellAmt18 = convertTo18(_sellDec, _sellAmt);
|
||||||
|
uint _slippageAmt = convert18ToDec(_buyDec, wmul(unitAmt, _sellAmt18));
|
||||||
|
|
||||||
|
uint ethAmt;
|
||||||
|
if (address(_sellAddr) == getEthAddr()) {
|
||||||
|
ethAmt = _sellAmt;
|
||||||
|
} else {
|
||||||
|
_sellAddr.approve(address(oneSplitContract), _sellAmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint initalBal = getTokenBal(_buyAddr);
|
||||||
|
oneSplitContract.swapWithReferralMulti.value(ethAmt)(
|
||||||
|
_tokens,
|
||||||
|
_sellAmt,
|
||||||
|
_slippageAmt,
|
||||||
|
distribution,
|
||||||
|
disableDexes,
|
||||||
|
getReferralAddr(),
|
||||||
|
0
|
||||||
|
);
|
||||||
|
uint finalBal = getTokenBal(_buyAddr);
|
||||||
|
|
||||||
|
buyAmt = sub(finalBal, initalBal);
|
||||||
|
|
||||||
|
require(_slippageAmt <= buyAmt, "Too much slippage");
|
||||||
|
}
|
||||||
|
|
||||||
function oneInchSwap(
|
function oneInchSwap(
|
||||||
TokenInterface _buyAddr,
|
TokenInterface _buyAddr,
|
||||||
TokenInterface _sellAddr,
|
TokenInterface _sellAddr,
|
||||||
|
@ -169,7 +225,7 @@ contract Resolver is OneHelpers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
contract BasicResolver is Resolver {
|
contract OneProtoResolver is Resolver {
|
||||||
event LogSell(
|
event LogSell(
|
||||||
address indexed buyToken,
|
address indexed buyToken,
|
||||||
address indexed sellToken,
|
address indexed sellToken,
|
||||||
|
@ -188,7 +244,8 @@ contract BasicResolver is Resolver {
|
||||||
uint256 setId
|
uint256 setId
|
||||||
);
|
);
|
||||||
|
|
||||||
event LogSellThree(
|
event LogSellMulti(
|
||||||
|
address[] tokens,
|
||||||
address indexed buyToken,
|
address indexed buyToken,
|
||||||
address indexed sellToken,
|
address indexed sellToken,
|
||||||
uint256 buyAmt,
|
uint256 buyAmt,
|
||||||
|
@ -221,7 +278,7 @@ contract BasicResolver is Resolver {
|
||||||
|
|
||||||
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
||||||
|
|
||||||
OneSplitInterface oneSplitContract = OneSplitInterface(getOneSplitAddress());
|
OneProtoInterface oneSplitContract = OneProtoInterface(getOneSplitAddress());
|
||||||
|
|
||||||
(, uint[] memory distribution) = oneSplitContract.getExpectedReturn(
|
(, uint[] memory distribution) = oneSplitContract.getExpectedReturn(
|
||||||
_sellAddr,
|
_sellAddr,
|
||||||
|
@ -231,7 +288,7 @@ contract BasicResolver is Resolver {
|
||||||
0
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
uint _buyAmt = oneSplitSwap(
|
uint _buyAmt = oneProtoSwap(
|
||||||
oneSplitContract,
|
oneSplitContract,
|
||||||
_sellAddr,
|
_sellAddr,
|
||||||
_buyAddr,
|
_buyAddr,
|
||||||
|
@ -277,8 +334,8 @@ contract BasicResolver is Resolver {
|
||||||
|
|
||||||
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
||||||
|
|
||||||
uint _buyAmt = oneSplitSwap(
|
uint _buyAmt = oneProtoSwap(
|
||||||
OneSplitInterface(getOneSplitAddress()),
|
OneProtoInterface(getOneSplitAddress()),
|
||||||
_sellAddr,
|
_sellAddr,
|
||||||
_buyAddr,
|
_buyAddr,
|
||||||
_sellAmt,
|
_sellAmt,
|
||||||
|
@ -295,6 +352,68 @@ contract BasicResolver is Resolver {
|
||||||
emitEvent(_eventCode, _eventParam);
|
emitEvent(_eventCode, _eventParam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dev Sell ETH/ERC20_Token using 1split using muliple token.
|
||||||
|
* @param tokens buying token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
|
* @param sellAmt selling token amount.
|
||||||
|
* @param unitAmt unit amount of buyAmt/sellAmt with slippage.
|
||||||
|
* @param distribution distribution of swap across different dex.
|
||||||
|
* @param disableDexes disable a dex. (To disable none: 0)
|
||||||
|
* @param getId Get token amount at this ID from `InstaMemory` Contract.
|
||||||
|
* @param setId Set token amount at this ID in `InstaMemory` Contract.
|
||||||
|
*/
|
||||||
|
function sellMulti(
|
||||||
|
address[] calldata tokens,
|
||||||
|
uint sellAmt,
|
||||||
|
uint unitAmt,
|
||||||
|
uint[] calldata distribution,
|
||||||
|
uint[] calldata disableDexes,
|
||||||
|
uint getId,
|
||||||
|
uint setId
|
||||||
|
) external payable {
|
||||||
|
uint _sellAmt = getUint(getId, sellAmt);
|
||||||
|
uint len = tokens.length;
|
||||||
|
require(len >= 2, "token length is less than 2");
|
||||||
|
TokenInterface _sellAddr = TokenInterface(address(tokens[0]));
|
||||||
|
TokenInterface _buyAddr = TokenInterface(address(tokens[len-1]));
|
||||||
|
|
||||||
|
_sellAmt = _sellAmt == uint(-1) ? getTokenBal(_sellAddr) : _sellAmt;
|
||||||
|
|
||||||
|
TokenInterface[] memory _tokens = new TokenInterface[](len);
|
||||||
|
for (uint i = 0; i < len; i++) {
|
||||||
|
_tokens[i] = TokenInterface(tokens[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint _buyAmt = oneProtoSwapMulti(
|
||||||
|
OneProtoInterface(getOneSplitAddress()),
|
||||||
|
_tokens,
|
||||||
|
_sellAddr,
|
||||||
|
_buyAddr,
|
||||||
|
_sellAmt,
|
||||||
|
unitAmt,
|
||||||
|
distribution,
|
||||||
|
disableDexes
|
||||||
|
);
|
||||||
|
|
||||||
|
setUint(setId, _buyAmt);
|
||||||
|
|
||||||
|
emit LogSellMulti(tokens, address(_buyAddr), address(_sellAddr), _buyAmt, _sellAmt, getId, setId);
|
||||||
|
bytes32 _eventCode = keccak256("LogSellMulti(address[],address,address,uint256,uint256,uint256,uint256)");
|
||||||
|
bytes memory _eventParam = abi.encode(tokens, address(_buyAddr), address(_sellAddr), _buyAmt, _sellAmt, getId, setId);
|
||||||
|
emitEvent(_eventCode, _eventParam);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
contract OneInchResolver is OneProtoResolver {
|
||||||
|
event LogSellThree(
|
||||||
|
address indexed buyToken,
|
||||||
|
address indexed sellToken,
|
||||||
|
uint256 buyAmt,
|
||||||
|
uint256 sellAmt,
|
||||||
|
uint256 getId,
|
||||||
|
uint256 setId
|
||||||
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @dev Sell ETH/ERC20_Token using 1split.
|
* @dev Sell ETH/ERC20_Token using 1split.
|
||||||
* @param buyAddr buying token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
* @param buyAddr buying token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
|
||||||
|
@ -334,7 +453,6 @@ contract BasicResolver is Resolver {
|
||||||
emitEvent(_eventCode, _eventParam);
|
emitEvent(_eventCode, _eventParam);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
contract ConnectOne is OneInchResolver {
|
||||||
contract ConnectOne is BasicResolver {
|
string public name = "1Inch-1proto-v1";
|
||||||
string public name = "1Inch-1Split-v1";
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user