kyber

pragma solidity 0.6.2;

// ETH/USDT
contract Kyber{         
    using SafeMath for uint256;
    ERC20 erc;
    mapping(address => Amount) public deposit;     // 每个用户存储金额
    string public prefix = "\x19Ethereum Signed Message:\n32";

    struct Amount{
        uint256 ethAmount;
        uint256 usdtAmount;
    }

    constructor(address _ercContractAddress) public{
        erc = ERC20(_ercContractAddress);
    }
    // 交易 (_token:想要买卖ETH的数量)
    function exchange(bytes memory _msg,bytes32 _r,bytes32 _s,uint8 _v) public payable{
        address from = ecrecover(keccak256(abi.encodePacked(prefix,keccak256(abi.encodePacked(_msg)))),_v,_r,_s);
        (uint256 _ethAmount,uint256 _usdtAmount,uint256 _status,uint256 _endTime) = _msgInfo(_msg);
        require(now > _endTime,"The time has expired");
        if(_status == 1){
            _sellEth(msg.value,_ethAmount,_usdtAmount,from);  
        }else{
            _buyEth(msg.value,_ethAmount,_usdtAmount,from);
        }
    }

    function _sellEth(uint256 _sellEthAmount,uint256 _ethAmount,uint256 _usdtAmount,address _from) private{
        require(_sellEthAmount == _ethAmount,"usdt balance is error");
        Amount memory amount = deposit[_from];
        amount.ethAmount = amount.ethAmount.add(_sellEthAmount);
        amount.usdtAmount = amount.usdtAmount.sub(_usdtAmount);
        deposit[_from] = amount;
        bool success = erc.transfer(msg.sender,_usdtAmount);
        require(success,"transfer usdt fail");
    }

    function _buyEth(uint256 _buyEthAmount,uint256 _ethAmount,uint256 _usdtAmount,address _from) private {
        require(_buyEthAmount == _ethAmount,"eth balance is error");
        Amount memory amount = deposit[_from];
        amount.ethAmount = amount.ethAmount.sub(_buyEthAmount);
        amount.usdtAmount = amount.usdtAmount.add(_usdtAmount);
        deposit[_from] = amount;
        msg.sender.transfer(_buyEthAmount);
    }

    function _msgInfo(bytes memory _msg) public pure returns (uint256 _ethAmount,uint256 _usdtAmount,uint256 _status,uint256 _endTime) {
        assembly {
           _ethAmount := mload(add(_msg,32))
           _usdtAmount := mload(add(_msg,64))
           _status := mload(add(_msg,96))
           _endTime := mload(add(_msg,128))
       }
    }
}

interface ERC20 {
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns(bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns(bool);
}

library SafeMath {

    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a);
        uint256 c = a - b;
        return c;
    }

    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a);
        return c;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,566评论 16 22
  • 今天感恩节哎,感谢一直在我身边的亲朋好友。感恩相遇!感恩不离不弃。 中午开了第一次的党会,身份的转变要...
    迷月闪星情阅读 10,610评论 0 11
  • 可爱进取,孤独成精。努力飞翔,天堂翱翔。战争美好,孤独进取。胆大飞翔,成就辉煌。努力进取,遥望,和谐家园。可爱游走...
    赵原野阅读 2,774评论 1 1
  • 在妖界我有个名头叫胡百晓,无论是何事,只要找到胡百晓即可有解决的办法。因为是只狐狸大家以讹传讹叫我“倾城百晓”,...
    猫九0110阅读 3,346评论 7 3