2019-03-22 22:25:46 +00:00
|
|
|
pragma solidity ^0.5.0;
|
|
|
|
|
2019-04-06 12:49:08 +00:00
|
|
|
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
|
2019-03-22 22:25:46 +00:00
|
|
|
|
2019-03-25 22:56:01 +00:00
|
|
|
|
|
|
|
contract KyberInterface {
|
2019-03-22 22:25:46 +00:00
|
|
|
function trade(
|
|
|
|
address src,
|
|
|
|
uint srcAmount,
|
|
|
|
address dest,
|
|
|
|
address destAddress,
|
|
|
|
uint maxDestAmount,
|
|
|
|
uint minConversionRate,
|
|
|
|
address walletId
|
2019-03-25 22:56:01 +00:00
|
|
|
) public payable returns (uint);
|
2019-03-22 22:25:46 +00:00
|
|
|
|
|
|
|
function getExpectedRate(
|
|
|
|
address src,
|
|
|
|
address dest,
|
|
|
|
uint srcQty
|
2019-03-25 22:56:01 +00:00
|
|
|
) public view returns (uint, uint);
|
2019-03-22 22:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract Helper {
|
|
|
|
|
2019-03-25 20:44:49 +00:00
|
|
|
/**
|
|
|
|
* @dev get ethereum address for trade
|
|
|
|
*/
|
2019-03-25 22:56:01 +00:00
|
|
|
function getAddressETH() public pure returns (address eth) {
|
2019-03-25 20:44:49 +00:00
|
|
|
eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
|
|
|
}
|
2019-03-22 22:25:46 +00:00
|
|
|
|
2019-03-25 20:44:49 +00:00
|
|
|
/**
|
|
|
|
* @dev get kyber proxy address
|
|
|
|
*/
|
2019-03-25 22:56:01 +00:00
|
|
|
function getAddressKyber() public pure returns (address kyber) {
|
2019-03-30 17:08:00 +00:00
|
|
|
kyber = 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
|
2019-03-25 20:44:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev get admin address
|
|
|
|
*/
|
2019-03-25 22:56:01 +00:00
|
|
|
function getAddressAdmin() public pure returns (address admin) {
|
2019-03-25 20:44:49 +00:00
|
|
|
admin = 0x7284a8451d9a0e7Dc62B3a71C0593eA2eC5c5638;
|
|
|
|
}
|
|
|
|
|
2019-03-25 22:56:01 +00:00
|
|
|
/**
|
|
|
|
* @dev gets ETH & token balance
|
|
|
|
* @param src is the token being sold
|
|
|
|
* @return ethBal - if not erc20, eth balance
|
|
|
|
* @return tknBal - if not eth, erc20 balance
|
|
|
|
*/
|
2019-03-27 20:38:13 +00:00
|
|
|
function getBal(address src, address _owner) public view returns (uint, uint) {
|
|
|
|
uint tknBal;
|
2019-03-25 22:56:01 +00:00
|
|
|
if (src != getAddressETH()) {
|
2019-03-27 20:38:13 +00:00
|
|
|
tknBal = IERC20(src).balanceOf(address(_owner));
|
2019-03-25 22:56:01 +00:00
|
|
|
}
|
2019-03-27 20:38:13 +00:00
|
|
|
return (address(_owner).balance, tknBal);
|
2019-03-25 22:56:01 +00:00
|
|
|
}
|
|
|
|
|
2019-03-24 00:16:50 +00:00
|
|
|
/**
|
|
|
|
* @dev getting rates from Kyber
|
|
|
|
* @param src is the token being sold
|
|
|
|
* @param dest is the token being bought
|
|
|
|
* @param srcAmt is the amount of token being sold
|
|
|
|
* @return expectedRate - the current rate
|
|
|
|
* @return slippageRate - rate with 3% slippage
|
|
|
|
*/
|
|
|
|
function getExpectedRate(
|
|
|
|
address src,
|
|
|
|
address dest,
|
|
|
|
uint srcAmt
|
|
|
|
) public view returns (
|
|
|
|
uint expectedRate,
|
|
|
|
uint slippageRate
|
|
|
|
)
|
|
|
|
{
|
2019-03-26 06:49:20 +00:00
|
|
|
(expectedRate,) = KyberInterface(getAddressKyber()).getExpectedRate(src, dest, srcAmt);
|
|
|
|
slippageRate = (expectedRate / 100) * 99; // changing slippage rate upto 99%
|
2019-03-24 00:16:50 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 22:25:46 +00:00
|
|
|
/**
|
|
|
|
* @dev fetching token from the trader if ERC20
|
|
|
|
* @param trader is the trader
|
|
|
|
* @param src is the token which is being sold
|
|
|
|
* @param srcAmt is the amount of token being sold
|
|
|
|
*/
|
|
|
|
function getToken(address trader, address src, uint srcAmt) internal returns (uint ethQty) {
|
2019-03-25 20:44:49 +00:00
|
|
|
if (src == getAddressETH()) {
|
2019-03-22 22:25:46 +00:00
|
|
|
require(msg.value == srcAmt, "not-enough-src");
|
|
|
|
ethQty = srcAmt;
|
|
|
|
} else {
|
2019-03-25 13:18:52 +00:00
|
|
|
manageApproval(src, srcAmt);
|
2019-03-25 22:56:01 +00:00
|
|
|
IERC20(src).transferFrom(trader, address(this), srcAmt);
|
2019-03-22 22:25:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev setting allowance to kyber for the "user proxy" if required
|
|
|
|
* @param token is the token address
|
|
|
|
*/
|
|
|
|
function setApproval(address token) internal returns (uint) {
|
2019-03-25 22:56:01 +00:00
|
|
|
IERC20(token).approve(getAddressKyber(), 2**255);
|
2019-03-22 22:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev configuring token approval with user proxy
|
|
|
|
* @param token is the token
|
|
|
|
*/
|
2019-03-22 22:33:45 +00:00
|
|
|
function manageApproval(address token, uint srcAmt) internal returns (uint) {
|
2019-03-25 22:56:01 +00:00
|
|
|
uint tokenAllowance = IERC20(token).allowance(address(this), getAddressKyber());
|
2019-03-22 22:25:46 +00:00
|
|
|
if (srcAmt > tokenAllowance) {
|
|
|
|
setApproval(token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract Swap is Helper {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param what 0 for BUY & 1 for SELL
|
|
|
|
*/
|
|
|
|
event LogTrade(
|
2019-03-27 20:38:13 +00:00
|
|
|
uint what, // 0 for BUY & 1 for SELL
|
2019-03-22 22:25:46 +00:00
|
|
|
address src,
|
|
|
|
uint srcAmt,
|
|
|
|
address dest,
|
|
|
|
uint destAmt,
|
|
|
|
address beneficiary,
|
|
|
|
uint minConversionRate,
|
|
|
|
address affiliate
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev buying token where destAmt is fixed
|
|
|
|
* @param src - token to sell
|
|
|
|
* @param dest - token to buy
|
|
|
|
* @param srcAmt - token amount to sell
|
|
|
|
* @param maxDestAmt is the max amount of token to be bought
|
|
|
|
*/
|
|
|
|
function buy(
|
|
|
|
address src,
|
|
|
|
address dest,
|
|
|
|
uint srcAmt,
|
2019-03-27 20:38:13 +00:00
|
|
|
uint maxDestAmt,
|
|
|
|
uint slippageRate
|
2019-03-22 22:25:46 +00:00
|
|
|
) public payable returns (uint destAmt)
|
|
|
|
{
|
|
|
|
uint ethQty = getToken(msg.sender, src, srcAmt);
|
2019-03-25 22:56:01 +00:00
|
|
|
|
2019-03-27 20:38:13 +00:00
|
|
|
destAmt = KyberInterface(getAddressKyber()).trade.value(ethQty)(
|
2019-03-22 22:25:46 +00:00
|
|
|
src,
|
|
|
|
srcAmt,
|
|
|
|
dest,
|
|
|
|
msg.sender,
|
|
|
|
maxDestAmt,
|
|
|
|
slippageRate,
|
2019-03-25 20:44:49 +00:00
|
|
|
getAddressAdmin()
|
2019-03-22 22:25:46 +00:00
|
|
|
);
|
|
|
|
|
2019-03-25 22:56:01 +00:00
|
|
|
// maxDestAmt usecase implementated on user proxy
|
2019-03-27 20:38:13 +00:00
|
|
|
if (address(this).balance > 0) {
|
|
|
|
msg.sender.transfer(address(this).balance);
|
2019-03-25 22:56:01 +00:00
|
|
|
} else if (src != getAddressETH()) {
|
|
|
|
IERC20 srcTkn = IERC20(src);
|
|
|
|
uint srcBal = srcTkn.balanceOf(address(this));
|
2019-03-27 20:38:13 +00:00
|
|
|
if (srcBal > 0) {
|
|
|
|
srcTkn.transfer(msg.sender, srcBal);
|
2019-03-25 22:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 22:25:46 +00:00
|
|
|
emit LogTrade(
|
|
|
|
0,
|
|
|
|
src,
|
|
|
|
srcAmt,
|
|
|
|
dest,
|
|
|
|
destAmt,
|
|
|
|
msg.sender,
|
|
|
|
slippageRate,
|
2019-03-25 20:44:49 +00:00
|
|
|
getAddressAdmin()
|
2019-03-22 22:25:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dev selling token where srcAmt is fixed
|
|
|
|
* @param src - token to sell
|
|
|
|
* @param dest - token to buy
|
2019-03-22 22:33:45 +00:00
|
|
|
* @param srcAmt - token amount to sell
|
2019-03-22 22:25:46 +00:00
|
|
|
*/
|
|
|
|
function sell(
|
|
|
|
address src,
|
|
|
|
address dest,
|
2019-03-26 06:49:20 +00:00
|
|
|
uint srcAmt,
|
2019-03-27 20:38:13 +00:00
|
|
|
uint slippageRate
|
2019-03-22 22:25:46 +00:00
|
|
|
) public payable returns (uint destAmt)
|
|
|
|
{
|
|
|
|
uint ethQty = getToken(msg.sender, src, srcAmt);
|
|
|
|
|
2019-03-27 20:38:13 +00:00
|
|
|
destAmt = KyberInterface(getAddressKyber()).trade.value(ethQty)(
|
2019-03-22 22:25:46 +00:00
|
|
|
src,
|
|
|
|
srcAmt,
|
|
|
|
dest,
|
|
|
|
msg.sender,
|
2019-03-25 20:44:49 +00:00
|
|
|
2**255,
|
2019-03-27 20:38:13 +00:00
|
|
|
slippageRate,
|
2019-03-25 20:44:49 +00:00
|
|
|
getAddressAdmin()
|
2019-03-22 22:25:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
emit LogTrade(
|
|
|
|
1,
|
|
|
|
src,
|
|
|
|
srcAmt,
|
|
|
|
dest,
|
|
|
|
destAmt,
|
|
|
|
msg.sender,
|
2019-03-27 20:38:13 +00:00
|
|
|
slippageRate,
|
2019-03-25 20:44:49 +00:00
|
|
|
getAddressAdmin()
|
2019-03-22 22:25:46 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract InstaTrade is Swap {
|
|
|
|
|
2019-03-25 20:44:49 +00:00
|
|
|
uint public version;
|
|
|
|
|
2019-03-22 22:25:46 +00:00
|
|
|
/**
|
|
|
|
* @dev setting up variables on deployment
|
2019-03-25 20:44:49 +00:00
|
|
|
* 1...2...3 versioning in each subsequent deployments
|
2019-03-22 22:25:46 +00:00
|
|
|
*/
|
2019-03-25 20:44:49 +00:00
|
|
|
constructor(uint _version) public {
|
|
|
|
version = _version;
|
2019-03-22 22:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|