dsa-connectors-old/contracts/connectors/kyber.sol

106 lines
3.1 KiB
Solidity
Raw Permalink Normal View History

2020-05-04 01:54:26 +00:00
pragma solidity ^0.6.0;
// import files from common directory
import { TokenInterface , MemoryInterface, EventInterface} from "../common/interfaces.sol";
import { Stores } from "../common/stores.sol";
import { DSMath } from "../common/math.sol";
interface KyberInterface {
function trade(
address src,
uint srcAmount,
address dest,
address destAddress,
uint maxDestAmount,
uint minConversionRate,
address walletId
) external payable returns (uint);
function getExpectedRate(
address src,
address dest,
uint srcQty
) external view returns (uint, uint);
}
contract KyberHelpers is DSMath, Stores {
/**
2020-05-04 02:15:49 +00:00
* @dev Kyber Proxy Address
2020-05-04 01:54:26 +00:00
*/
2020-05-04 02:15:49 +00:00
function getKyberAddr() internal pure returns (address) {
2020-05-04 01:54:26 +00:00
return 0x818E6FECD516Ecc3849DAf6845e3EC868087B755;
}
/**
2020-05-04 02:15:49 +00:00
* @dev Referral Address
2020-05-04 01:54:26 +00:00
*/
2020-05-04 02:15:49 +00:00
function getReferralAddr() internal pure returns (address) {
2020-05-04 01:54:26 +00:00
return 0x7284a8451d9a0e7Dc62B3a71C0593eA2eC5c5638;
}
}
contract KyberResolver is KyberHelpers {
event LogSell(
address indexed buyToken,
address indexed sellToken,
uint256 buyAmt,
uint256 sellAmt,
uint256 getId,
uint256 setId
);
2020-05-04 02:09:44 +00:00
/**
* @dev Sell ETH/ERC20_Token.
* @param buyAddr buying token address.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAddr selling token amount.(For ETH: 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE)
* @param sellAmt selling token amount.
* @param unitAmt unit amount of buyAmt/sellAmt with slippage.
* @param getId Get token amount at this ID from `InstaMemory` Contract.
* @param setId Set token amount at this ID in `InstaMemory` Contract.
*/
2020-05-04 01:54:26 +00:00
function sell(
address buyAddr,
address sellAddr,
uint sellAmt,
uint unitAmt,
uint getId,
uint setId
) external payable
{
uint _sellAmt = getUint(getId, sellAmt);
uint ethAmt;
if (sellAddr == getEthAddr()) {
_sellAmt = _sellAmt == uint(-1) ? address(this).balance : _sellAmt;
ethAmt = _sellAmt;
} else {
TokenInterface sellContract = TokenInterface(sellAddr);
_sellAmt = _sellAmt == uint(-1) ? sellContract.balanceOf(address(this)) : _sellAmt;
2020-05-04 02:15:49 +00:00
sellContract.approve(getKyberAddr(), _sellAmt);
2020-05-04 01:54:26 +00:00
}
2020-05-04 02:15:49 +00:00
uint _buyAmt = KyberInterface(getKyberAddr()).trade.value(ethAmt)(
2020-05-04 01:54:26 +00:00
sellAddr,
_sellAmt,
buyAddr,
address(this),
uint(-1),
unitAmt,
2020-05-04 02:15:49 +00:00
getReferralAddr()
2020-05-04 01:54:26 +00:00
);
setUint(setId, _buyAmt);
emit LogSell(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
bytes32 eventCode = keccak256("LogSell(address,address,uint256,uint256,uint256,uint256)");
bytes memory eventData = abi.encode(buyAddr, sellAddr, _buyAmt, _sellAmt, getId, setId);
emitEvent(eventCode, eventData);
}
}
2020-05-04 02:19:40 +00:00
contract ConnectKyber is KyberResolver {
2020-05-04 01:54:26 +00:00
string public name = "Kyber-v1";
}