diff --git a/contracts/connectors/kyber.sol b/contracts/connectors/kyber.sol new file mode 100644 index 0000000..05b5316 --- /dev/null +++ b/contracts/connectors/kyber.sol @@ -0,0 +1,97 @@ +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 { + /** + * @dev Get Kyber Proxy Address + */ + function getAddressKyber() public pure returns (address) { + return 0x818E6FECD516Ecc3849DAf6845e3EC868087B755; + } + + /** + * @dev Get Admin Address + */ + function getAddressAdmin() public pure returns (address) { + return 0x7284a8451d9a0e7Dc62B3a71C0593eA2eC5c5638; + } +} + + +contract KyberResolver is KyberHelpers { + event LogSell( + address indexed buyToken, + address indexed sellToken, + uint256 buyAmt, + uint256 sellAmt, + uint256 getId, + uint256 setId + ); + + 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; + sellContract.approve(getAddressKyber(), _sellAmt); + } + + uint _buyAmt = KyberInterface(getAddressKyber()).trade.value(ethAmt)( + sellAddr, + _sellAmt, + buyAddr, + address(this), + uint(-1), + unitAmt, + getAddressAdmin() + ); + + 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); + } +} + + +contract InstaKyber is KyberResolver { + string public name = "Kyber-v1"; +} \ No newline at end of file