以太坊智能合约(模拟积分兑换商品)

pragma solidity ^0.4.25;

interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }

contract JIfen {
    string public name; // 发放积分全称
    string public abbreviation; // 发放积分的简称
    uint256 public totalSupply; // 总发行量
    uint private deadline; //设置活动的截至日期

    // 用mapping保存每个地址对应的余额
    mapping (address => uint256) public getAccountPoint;
    mapping (address => mapping (address => uint256))  allowance;

    //交易调用的事件
    event Transfer(address indexed from, address indexed to, uint256 value);

    //兑换商品的事件
    event Burn(address indexed from, uint256 value);

   constructor (uint256 supply, string tokenName, string tokenSymbol) public {
        totalSupply = supply;                                // 供应的积分数量
        getAccountPoint[msg.sender] = totalSupply;           // 官方目前的剩余积分
        name = tokenName;                                    // 积分名称
        abbreviation = tokenSymbol;                          // 积分符号
    }

   //购买商品时候
    function _transfer(address _from, address _to, uint _value) internal {
        require(_to != 0x0); //验证地址
        require(getAccountPoint[_from] >= _value); //对发送者余额进行校验
        require(getAccountPoint[_to] + _value > getAccountPoint[_to]);  //确保转账的金额为正数
        uint previousBalances = getAccountPoint[_from] + getAccountPoint[_to];
        getAccountPoint[_from] -= _value;
        getAccountPoint[_to] += _value;
        Transfer(_from, _to, _value);
        // 断言函数检查逻辑。
        assert(getAccountPoint[_from] + getAccountPoint[_to] == previousBalances);
        
    }

   //此函数是类似于用户购买商品、登录、浏览商品时候的官方发放积分给用户
    function distributePointsToUsers(address _to, uint256 _value) public {
        _transfer(msg.sender, _to, _value);
    }

    /**
     * 账号之间进行交易积分的奖励
     *  _from 发送者地址
     *  _to 接收者地址
     *  _value 转移数额
     */
    function transferMoneyByProduct(address _to, uint _value) public payable returns (bool success) {
        address account=0x59ABBBbC651159328BC3d9Af70375a60D1755927;
        address account1=0xeb4E0504d69FBcc9AC594C225b2bF3C0142dccf6;
        _to.transfer(msg.value);
        require(_to != 0x0); //验证地址
        require(getAccountPoint[account] >= _value); //对发送者余额进行校验
        require(getAccountPoint[account1] + _value > getAccountPoint[account1]);  //确保转账的积分为正数
        uint previousBalances = getAccountPoint[account] + getAccountPoint[account1];
        getAccountPoint[account] -= _value;
        getAccountPoint[account1] += _value;
        Transfer(account, account1, _value);
        // 断言函数检查逻辑。
        assert(getAccountPoint[account] + getAccountPoint[account1] == previousBalances);
         return true;
       }
      
    /**
     * 设置某个地址(合约)可以创建交易者名义花费的积分或者代币。
     * 允许发送者`_spender` 花费不多于 `_value` 个代币
     */
    function approve(address _spender, uint256 _value) public
    returns (bool success) {
        allowance[msg.sender][_spender] = _value;
        return true;
    }

    /**
     * 设置允许一个地址(合约)以我(创建交易者)的名义可最多花费的代币数。
     * @param _spender 被授权的地址(合约)
     * @param _value 最大可花费代币数
     * @param _extraData 发送给合约的附加数据
     */
    function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) {
        tokenRecipient spender = tokenRecipient(_spender);
        if (approve(_spender, _value)) {
            // 通知合约
            spender.receiveApproval(msg.sender, _value, this, _extraData);
            return true;
        }
    }

    //(相当于用户兑换了商品等)
    function reduceOfPoints(address _from,uint256 _value) public returns (bool success) {
        require(getAccountPoint[_from] >= _value);   
        getAccountPoint[_from] -= _value;           
        totalSupply -= _value;                     
        Burn(_from, _value);
        return true;
    }
    
    //模拟的是到达一定时间后积分兑换活动的结束
    function gameOverPoint() public notExpired returns (bool success) {
        address account=0x59ABBBbC651159328BC3d9Af70375a60D1755927;
        getAccountPoint[account] -=getAccountPoint[account] ;                              
        return true;
    }

     function getBalance(address _from) public view returns (uint256) {
        return _from.balance;
     }


     function setDeadline(uint time) public {
        deadline = time;
    }

     modifier notExpired(){
        require(block.timestamp <= deadline);
        _;
    }

    function getBlockTimestamp() public view returns(uint) {
     // 获取当前块的时间戳
        return block.timestamp;
    }

}

posted @ 2022-06-27 11:29  要买CT5的小曹  阅读(28)  评论(0)    收藏  举报  来源