nest3.0从架构解析到克隆五(NestAbonus与NodeAssignment)

NestAbonus:Nest分红池合约。NodeAssignment:守护者节点合约。

返回总章 我们之前已经讲了NestOffer,NestToken和VoteContract。其中包含了token,mapping以及报价机部分。 而剩下的四个内容,本节讲的是NestAbonus与NodeAssignment;由于NTokenAuction和NTokenOffer属于额外功能累加,所以暂时就不讲了。 然后我们下一节将讲述如何的对nest进行简单的克隆。然后本系列教程就结束了。

NestAbonus

Nest分红池合约。其中由四个合约组成: NEST 系统收益池合约(Nest_3_Abonus) NEST 系统储蓄合约(Nest_3_Leveling) NEST 系统收益锁仓验证合约(Nest_3_TokenSave) NEST 系统收益分配逻辑合约(Nest_3_TokenAbonus) 我们这里只对最重要的Nest_3_Abonus进行解析。

/**
 * @title ETH bonus pool
 ETH分红池
 * @dev ETH collection and inquiry
 ETH领取与查询
 */
contract Nest_3_Abonus {
    using address_make_payable for address;
    using SafeMath for uint256;

    Nest_3_VoteFactory _voteFactory;                                //  Voting contract
    //投票合约
    address _nestAddress;                                           //  NEST contract address
    //NEST 合约地址
    mapping (address => uint256) ethMapping;                        //  ETH bonus ledger of corresponding tokens
    //  token对应 ETH 分红账本
    uint256 _mostDistribution = 40;                                 //  The highest allocation ratio of NEST bonus pool
    //  最高NEST分红池分配比例
    uint256 _leastDistribution = 20;                                //  The lowest allocation ratio of NEST bonus pool
     //  最低NEST分红池分配比例
    uint256 _distributionTime = 1200000;                            //  The decay time interval of NEST bonus pool allocation ratio 
    //  NEST分红池分配比例每次衰减时间间隔
    uint256 _distributionSpan = 5;                                  //  The decay degree of NEST bonus pool allocation ratio
    ////  NEST分红池分配比例每次衰减程度
    /**
    * @dev Initialization method
    初始化方法
    * @param voteFactory Voting contract address
    投票合约地址
    */
    constructor(address voteFactory) public {
        _voteFactory = Nest_3_VoteFactory(voteFactory);
        _nestAddress = address(_voteFactory.checkAddress("nest"));
    }

    /**
    * @dev Reset voting contract
    重置投票合约
    * @param voteFactory Voting contract address
    投票合约地址
    */
    function changeMapping(address voteFactory) public onlyOwner{
        _voteFactory = Nest_3_VoteFactory(voteFactory);
        _nestAddress = address(_voteFactory.checkAddress("nest"));
    }

    /**
    * @dev Transfer in bonus
    转入分红
    * @param token Corresponding to lock-up Token
    对应锁仓 NToken
    */
    function switchToEth(address token) public payable {
        ethMapping[token] = ethMapping[token].add(msg.value);
    }

    /**
    * @dev Transferin bonus - NToken offering fee
    转入分红-nToken报价手续费
    * @param token Corresponding to lock-up NToken
    对应锁仓 NToken
    */
    function switchToEthForNTokenOffer(address token) public payable {
        Nest_NToken nToken = Nest_NToken(token);
        (uint256 createBlock,) = nToken.checkBlockInfo();
        uint256 subBlock = block.number.sub(createBlock);
        uint256 times = subBlock.div(_distributionTime);
        uint256 distributionValue = times.mul(_distributionSpan);
        uint256 distribution = _mostDistribution;
        if (_leastDistribution.add(distributionValue) > _mostDistribution) {
            distribution = _leastDistribution;
        } else {
            distribution = _mostDistribution.sub(distributionValue);
        }
        uint256 nestEth = msg.value.mul(distribution).div(100);
        ethMapping[_nestAddress] = ethMapping[_nestAddress].add(nestEth);
        ethMapping[token] = ethMapping[token].add(msg.value.sub(nestEth));
    }

    /**
    * @dev Receive ETH
    领取
    * @param num Receive amount 
    领取数量
    * @param token Correspond to locked Token
    对应锁仓 NToken
    * @param target Transfer target
    转账目标
    */
    function getETH(uint256 num, address token, address target) public onlyContract {
        require(num <= ethMapping[token], "Insufficient storage balance");
        ethMapping[token] = ethMapping[token].sub(num);
        address payable addr = target.make_payable();
        addr.transfer(num);
    }

    /**
    * @dev Get bonus pool balance
    获取分红池余额
    * @param token Corresponded locked Token
    对应锁仓 NToken
    * @return uint256 Bonus pool balance
    分红池余额
    */
    function getETHNum(address token) public view returns (uint256) {
        return ethMapping[token];
    }

    // View NEST address
    //查看 NEST 地址
    function checkNestAddress() public view returns(address) {
        return _nestAddress;
    }

    // View the highest NEST bonus pool allocation ratio
    //查看最高 NEST 分红池分配比例
    function checkMostDistribution() public view returns(uint256) {
        return _mostDistribution;
    }

    // View the lowest NEST bonus pool allocation ratio
    // 查看最低 NEST 分红池分配比例
    function checkLeastDistribution() public view returns(uint256) {
        return _leastDistribution;
    }

    // View the decay time interval of NEST bonus pool allocation ratio 
     // 查看 NEST 分红池分配比例每次衰减时间间隔
    function checkDistributionTime() public view returns(uint256) {
        return _distributionTime;
    }

    // View the decay degree of NEST bonus pool allocation ratio
    // 查看 NEST 分红池分配比例每次衰减程度
    function checkDistributionSpan() public view returns(uint256) {
        return _distributionSpan;
    }

    // Modify the highest NEST bonus pool allocation ratio
    // 修改最高 NEST 分红池分配比例
    function changeMostDistribution(uint256 num) public onlyOwner  {
        _mostDistribution = num;
    }

    // Modify the lowest NEST bonus pool allocation ratio
    // 修改 NEST 分红池分配比例每次衰减时间间隔
    function changeLeastDistribution(uint256 num) public onlyOwner  {
        _leastDistribution = num;
    }

    // Modify the decay time interval of NEST bonus pool allocation ratio 
    // 修改 NEST 分红池分配比例每次衰减时间间隔
    function changeDistributionTime(uint256 num) public onlyOwner  {
        _distributionTime = num;
    }

    // Modify the decay degree of NEST bonus pool allocation ratio
    // 修改 NEST 分红池分配比例每次衰减程度
    function changeDistributionSpan(uint256 num) public onlyOwner  {
        _distributionSpan = num;
    }

    // Withdraw ETH
    //取出ETH
    function turnOutAllEth(uint256 amount, address target) public onlyOwner {
        address payable addr = target.make_payable();
        addr.transfer(amount);  
    }

    // Only bonus logic contract
    //// 仅限分红逻辑合约
    modifier onlyContract(){
        require(_voteFactory.checkAddress("nest.v3.tokenAbonus") == address(msg.sender), "No authority");
        _;
    }

    // Administrator only
    // 仅限管理员
    modifier onlyOwner(){
        require(_voteFactory.checkOwners(address(msg.sender)), "No authority");
        _;
    }
}

NodeAssignment

守护者节点合约。 NEST_NodeAssignment:节点分配合约 NEST_NodeAssignmentData:NestNode 领取数据 NEST_NodeSave:分配nest存储

NEST_NodeSave

/**
 * @title Guardian node nest storage
 分配nest存储
 */
contract NEST_NodeSave {
    Nest_3_VoteFactory mappingContract;                  
    IBNEST nestContract;                         

    /**
    * @dev Initialization method
    //  初始化方法
    * @param map Mapping contract address
    //  投票合约地址
    */
    constructor (address map) public {
        mappingContract = Nest_3_VoteFactory(address(map));          
        nestContract = IBNEST(address(mappingContract.checkAddress("nest")));        
    }

    /**
    * @dev Change mapping contract
    重置投票合约
    * @param map Mapping contract address
    投票合约地址
    */
    function changeMapping(address map) public onlyOwner {
        mappingContract = Nest_3_VoteFactory(address(map));          
        nestContract = IBNEST(address(mappingContract.checkAddress("nest")));        
    }

    /**
    * @dev Transfer out nest
     // 转出nest
    * @param amount Transfer out quantity
    * @param to Transfer out target
    * @return Actual transfer out quantity
    */
    function turnOut(uint256 amount, address to) public onlyMiningCalculation returns(uint256) {
        uint256 leftNum = nestContract.balanceOf(address(this));
        if (leftNum >= amount) {
            nestContract.transfer(to, amount);
            return amount;
        } else {
            return 0;
        }
    }
    // 仅限管理员
    modifier onlyOwner(){
        require(mappingContract.checkOwners(msg.sender) == true);
        _;
    }
// 仅限分配合约
    modifier onlyMiningCalculation(){
        require(address(mappingContract.checkAddress("nodeAssignment")) == msg.sender);
        _;
    }

}

NEST_NodeAssignmentData

/**
 * @title Guardian node receives data
 NestNode 领取数据
 */
contract NEST_NodeAssignmentData {
    using SafeMath for uint256;
    Nest_3_VoteFactory mappingContract;    ////  映射合约      
    uint256 nodeAllAmount = 9546345842385995696603;   //  节点分配 NEST 数量   
        //这个没搞清楚是什么
    mapping(address => uint256) nodeLatestAmount;         // //  上次领取数量  

    /**
    * @dev Initialization method
    初始化方法
    * @param map Mapping contract address
    投票合约地址
    */
    constructor (address map) public {
        mappingContract = Nest_3_VoteFactory(map); 
    }

    /**
    * @dev Change mapping contract
    重置投票合约
    * @param map Mapping contract address
    投票合约地址
    */
    function changeMapping(address map) public onlyOwner{
        mappingContract = Nest_3_VoteFactory(map); 
    }

    //  Add nest
    //增加nest
    function addNest(uint256 amount) public onlyNodeAssignment {
        nodeAllAmount = nodeAllAmount.add(amount);
    }

    //  View cumulative total
    //查看累计总数
    function checkNodeAllAmount() public view returns (uint256) {
        return nodeAllAmount;
    }

    //  Record last received quantity
    // 记录上次数量
    function addNodeLatestAmount(address add ,uint256 amount) public onlyNodeAssignment {
        nodeLatestAmount[add] = amount;
    }

    //  View last received quantity
    // 查看上次数量
    function checkNodeLatestAmount(address add) public view returns (uint256) {
        return nodeLatestAmount[address(add)];
    }
    // 仅限管理员
    modifier onlyOwner(){
        require(mappingContract.checkOwners(msg.sender) == true);
        _;
    }
    // 仅限分配合约
    modifier onlyNodeAssignment(){
        require(address(msg.sender) == address(mappingContract.checkAddress("nodeAssignment")));
        _;
    }
}

NEST_NodeAssignment

/**
 * @title Node assignment contract
 节点分配合约
 */
contract NEST_NodeAssignment {

    using SafeMath for uint256;
    Nest_3_VoteFactory mappingContract;                              //  Mapping contract
    //  映射合约
    IBNEST nestContract;                                    //  NEST contract
    //  NEST 合约
    SuperMan supermanContract;                              //  NestNode contract
    //  NestNode 合约
    NEST_NodeSave nodeSave;                                 //  NestNode save contract
    //  NestNode NEST锁仓
    NEST_NodeAssignmentData nodeAssignmentData;             //  NestNode data assignment contract
    //  NestNode 领取数据

    /**
    * @dev Initialization method
    初始化方法
    * @param map Voting contract address
    投票合约地址
    */
    constructor (address map) public {
        mappingContract = Nest_3_VoteFactory(map); 
        nestContract = IBNEST(address(mappingContract.checkAddress("nest")));
        supermanContract = SuperMan(address(mappingContract.checkAddress("nestNode")));
        nodeSave = NEST_NodeSave(address(mappingContract.checkAddress("nestNodeSave")));
        nodeAssignmentData = NEST_NodeAssignmentData(address(mappingContract.checkAddress("nodeAssignmentData")));
    }

    /**
    * @dev Reset voting contract
    重置投票合约
    * @param map Voting contract address
    投票合约地址
    */
    function changeMapping(address map) public onlyOwner{
        mappingContract = Nest_3_VoteFactory(map); 
        nestContract = IBNEST(address(mappingContract.checkAddress("nest")));
        supermanContract = SuperMan(address(mappingContract.checkAddress("nestNode")));
        nodeSave = NEST_NodeSave(address(mappingContract.checkAddress("nestNodeSave")));
        nodeAssignmentData = NEST_NodeAssignmentData(address(mappingContract.checkAddress("nodeAssignmentData")));
    }

    /**
    * @dev Deposit NEST token
    存入 NEST
    * @param amount Amount of deposited NEST
    amount 存入 NEST 数量
    */
    function bookKeeping(uint256 amount) public {
        require(amount > 0);
        require(nestContract.transferFrom(address(msg.sender), address(nodeSave), amount));
        nodeAssignmentData.addNest(amount);
    }

    // NestNode receive and settlement
    //领取结算
    function nodeGet() public {
        require(address(msg.sender) == address(tx.origin));
        require(supermanContract.balanceOf(address(msg.sender)) > 0);
        uint256 allAmount = nodeAssignmentData.checkNodeAllAmount();
        uint256 amount = allAmount.sub(nodeAssignmentData.checkNodeLatestAmount(address(msg.sender)));
        uint256 getAmount = amount.mul(supermanContract.balanceOf(address(msg.sender))).div(1500);
        require(nestContract.balanceOf(address(nodeSave)) >= getAmount);
        nodeSave.turnOut(getAmount,address(msg.sender));
        nodeAssignmentData.addNodeLatestAmount(address(msg.sender),allAmount);
    }

    // NestNode transfer settlement
    // NestNode 转账结算
    function nodeCount(address fromAdd, address toAdd) public {
        require(address(supermanContract) == address(msg.sender));
        require(supermanContract.balanceOf(address(fromAdd)) > 0);
        uint256 allAmount = nodeAssignmentData.checkNodeAllAmount();
        uint256 amountFrom = allAmount.sub(nodeAssignmentData.checkNodeLatestAmount(address(fromAdd)));
        uint256 getAmountFrom = amountFrom.mul(supermanContract.balanceOf(address(fromAdd))).div(1500);
        if (nestContract.balanceOf(address(nodeSave)) >= getAmountFrom) {
            nodeSave.turnOut(getAmountFrom,address(fromAdd));
            nodeAssignmentData.addNodeLatestAmount(address(fromAdd),allAmount);
        }
        uint256 amountTo = allAmount.sub(nodeAssignmentData.checkNodeLatestAmount(address(toAdd)));
        uint256 getAmountTo = amountTo.mul(supermanContract.balanceOf(address(toAdd))).div(1500);
        if (nestContract.balanceOf(address(nodeSave)) >= getAmountTo) {
            nodeSave.turnOut(getAmountTo,address(toAdd));
            nodeAssignmentData.addNodeLatestAmount(address(toAdd),allAmount);
        }
    }

    // NestNode receivable amount
    // 超级节点可领取金额
    function checkNodeNum() public view returns (uint256) {
         uint256 allAmount = nodeAssignmentData.checkNodeAllAmount();
         uint256 amount = allAmount.sub(nodeAssignmentData.checkNodeLatestAmount(address(msg.sender)));
         uint256 getAmount = amount.mul(supermanContract.balanceOf(address(msg.sender))).div(1500);
         return getAmount; 
    }

    // Administrator only
    // 仅限管理员
    modifier onlyOwner(){
        require(mappingContract.checkOwners(msg.sender));
        _;
    }
}

本节总结

本节主要是对NestAbonus(Nest分红池合约)和NodeAssignment(守护者节点合约)进行描述。

  • 学分: 5
  • 分类: NEST
  • 标签:
点赞 0
收藏 0
分享
本文参与登链社区写作激励计划 ,好文好收益,欢迎正在阅读的你也加入。

0 条评论

请先 登录 后评论
问答区块链
问答区块链
致力于人工智能与区块链相结合下的智能设备自治