mirror of
https://github.com/Instadapp/smart-contract.git
synced 2024-07-29 22:08:07 +00:00
273 lines
7.0 KiB
Solidity
273 lines
7.0 KiB
Solidity
pragma solidity ^0.5.0;
|
|
|
|
|
|
library SafeMath {
|
|
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
if (a == 0) {
|
|
return 0;
|
|
}
|
|
uint256 c = a * b;
|
|
require(c / a == b, "Assertion Failed");
|
|
return c;
|
|
}
|
|
}
|
|
|
|
|
|
contract IERC20 {
|
|
function balanceOf(address who) public view returns (uint256);
|
|
function allowance(address _owner, address _spender) public view returns (uint256);
|
|
function transfer(address to, uint256 value) public returns (bool);
|
|
function approve(address spender, uint256 value) public returns (bool);
|
|
function transferFrom(address from, address to, uint256 value) public returns (bool);
|
|
}
|
|
|
|
|
|
contract KyberInterface {
|
|
function trade(
|
|
address src,
|
|
uint srcAmount,
|
|
address dest,
|
|
address destAddress,
|
|
uint maxDestAmount,
|
|
uint minConversionRate,
|
|
address walletId
|
|
) public payable returns (uint);
|
|
|
|
function getExpectedRate(
|
|
address src,
|
|
address dest,
|
|
uint srcQty
|
|
) public view returns (uint, uint);
|
|
}
|
|
|
|
|
|
contract Helper {
|
|
|
|
using SafeMath for uint;
|
|
using SafeMath for uint256;
|
|
|
|
/**
|
|
* @dev get ethereum address for trade
|
|
*/
|
|
function getAddressETH() public pure returns (address eth) {
|
|
eth = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
|
}
|
|
|
|
/**
|
|
* @dev get kyber proxy address
|
|
*/
|
|
function getAddressKyber() public pure returns (address kyber) {
|
|
kyber = 0x692f391bCc85cefCe8C237C01e1f636BbD70EA4D;
|
|
}
|
|
|
|
/**
|
|
* @dev get admin address
|
|
*/
|
|
function getAddressAdmin() public pure returns (address admin) {
|
|
admin = 0x7284a8451d9a0e7Dc62B3a71C0593eA2eC5c5638;
|
|
}
|
|
|
|
/**
|
|
* @dev get fees to trade // 200 => 0.2%
|
|
*/
|
|
function getUintFees() public pure returns (uint fees) {
|
|
fees = 200;
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
*/
|
|
function getBal(address src) public view returns (uint ethBal, uint tknBal) {
|
|
ethBal = address(this).balance;
|
|
if (src != getAddressETH()) {
|
|
tknBal = IERC20(src).balanceOf(address(this));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @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
|
|
)
|
|
{
|
|
(expectedRate,) = KyberInterface(getAddressKyber()).getExpectedRate(src, dest, srcAmt);
|
|
slippageRate = (expectedRate / 100) * 99; // changing slippage rate upto 99%
|
|
}
|
|
|
|
/**
|
|
* @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) {
|
|
if (src == getAddressETH()) {
|
|
require(msg.value == srcAmt, "not-enough-src");
|
|
ethQty = srcAmt;
|
|
} else {
|
|
manageApproval(src, srcAmt);
|
|
IERC20(src).transferFrom(trader, address(this), srcAmt);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @dev setting allowance to kyber for the "user proxy" if required
|
|
* @param token is the token address
|
|
*/
|
|
function setApproval(address token) internal returns (uint) {
|
|
IERC20(token).approve(getAddressKyber(), 2**255);
|
|
}
|
|
|
|
/**
|
|
* @dev configuring token approval with user proxy
|
|
* @param token is the token
|
|
*/
|
|
function manageApproval(address token, uint srcAmt) internal returns (uint) {
|
|
uint tokenAllowance = IERC20(token).allowance(address(this), getAddressKyber());
|
|
if (srcAmt > tokenAllowance) {
|
|
setApproval(token);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
contract Swap is Helper {
|
|
|
|
/**
|
|
* @param what 0 for BUY & 1 for SELL
|
|
*/
|
|
event LogTrade(
|
|
uint what,
|
|
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,
|
|
uint maxDestAmt
|
|
) public payable returns (uint destAmt)
|
|
{
|
|
uint ethQty = getToken(msg.sender, src, srcAmt);
|
|
(, uint slippageRate) = getExpectedRate(src, dest, srcAmt);
|
|
|
|
(uint ethBal, uint tknBal) = getBal(src);
|
|
|
|
KyberInterface swapCall = KyberInterface(getAddressKyber());
|
|
destAmt = swapCall.trade.value(ethQty)(
|
|
src,
|
|
srcAmt,
|
|
dest,
|
|
msg.sender,
|
|
maxDestAmt,
|
|
slippageRate,
|
|
getAddressAdmin()
|
|
);
|
|
|
|
// maxDestAmt usecase implementated on user proxy
|
|
if (address(this).balance > ethBal) {
|
|
msg.sender.transfer(address(this).balance - ethBal);
|
|
} else if (src != getAddressETH()) {
|
|
IERC20 srcTkn = IERC20(src);
|
|
uint srcBal = srcTkn.balanceOf(address(this));
|
|
if (srcBal > tknBal) {
|
|
srcTkn.transfer(msg.sender, srcBal - tknBal);
|
|
}
|
|
}
|
|
|
|
emit LogTrade(
|
|
0,
|
|
src,
|
|
srcAmt,
|
|
dest,
|
|
destAmt,
|
|
msg.sender,
|
|
slippageRate,
|
|
getAddressAdmin()
|
|
);
|
|
|
|
}
|
|
|
|
/**
|
|
* @dev selling token where srcAmt is fixed
|
|
* @param src - token to sell
|
|
* @param dest - token to buy
|
|
* @param srcAmt - token amount to sell
|
|
*/
|
|
function sell(
|
|
address src,
|
|
address dest,
|
|
uint srcAmt,
|
|
uint minDestAmt
|
|
) public payable returns (uint destAmt)
|
|
{
|
|
uint ethQty = getToken(msg.sender, src, srcAmt);
|
|
|
|
KyberInterface swapCall = KyberInterface(getAddressKyber());
|
|
destAmt = swapCall.trade.value(ethQty)(
|
|
src,
|
|
srcAmt,
|
|
dest,
|
|
msg.sender,
|
|
2**255,
|
|
minDestAmt,
|
|
getAddressAdmin()
|
|
);
|
|
|
|
emit LogTrade(
|
|
1,
|
|
src,
|
|
srcAmt,
|
|
dest,
|
|
destAmt,
|
|
msg.sender,
|
|
minDestAmt,
|
|
getAddressAdmin()
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
contract InstaTrade is Swap {
|
|
|
|
uint public version;
|
|
|
|
/**
|
|
* @dev setting up variables on deployment
|
|
* 1...2...3 versioning in each subsequent deployments
|
|
*/
|
|
constructor(uint _version) public {
|
|
version = _version;
|
|
}
|
|
|
|
} |