silidity经典智能合约(三)

写在前面(及代码解读)

1:系统初始化有86400000000个鸡蛋;

2:项目方怎么挣钱
    1):3%的交易费进入项目的地址:

    2)默认推荐人地址,如果有新玩家假如,那么他每次买卖鸡蛋的时候,推荐者都会收到3%鸡蛋;

3: 对于累计的鸡蛋,玩家可以选择卖掉换成BNB,或者按照864000的比例转换成一只鸡;

4:把鸡蛋孵化成鸡的过程,会增加市场上鸡蛋的数量,增加消耗掉的鸡蛋的数量除以5;

5:用户鸡蛋的数量又两部分组成,自己已有的鸡蛋和距离最近一次把鸡蛋孵化成,到当前能产生的鸡蛋;

6:统计最近一次把鸡蛋孵化成鸡的时候,记录统计时间最大为864000,每只每秒都会产生一个鸡蛋;

7:卖出鸡蛋增加市场鸡蛋的数量,增加的数量为卖出鸡蛋的数量

8:鸡蛋买卖计算公式:

Value = (PSN*bs)/(PSNH+((PSN*rs+PSNH*rt)/rt));

说明:PSN=10000;//固定值

PSNH=5000;//固定值

买的时候计算出来的是鸡蛋的数量,卖的时候计算出俩的是鸡蛋的价值(bnb)数量

当买入鸡蛋的数量:(结果是买入鸡蛋的数量)

Rt = 用户支付的bnb数量;

Rs = 合约地址当前持有bnb余额(不包含当前用户买入支付的bnb数量);

Bs = 市场上鸡蛋的数量;

当卖出鸡蛋价值:(结果是能卖出的bnb数量)

Rt = 用户卖出的鸡蛋数量;

Rs = 市场上鸡蛋的数量;

Bs = 合约地址当前持有bnb的余额;

上代码:

pragma solidity ^0.4.26; // solhint-disable-line

contract Vegetable{
    //uint256 EGGS_PER_MINERS_PER_SECOND=1;
    uint256 public EGGS_TO_HATCH_1MINERS=864000;//for final version should be seconds in a day
    uint256 PSN=10000;
    uint256 PSNH=5000;
    bool public initialized=false;
    address public ceoAddress;
    mapping (address => uint256) public hatcheryMiners;
    mapping (address => uint256) public claimedEggs;
    mapping (address => uint256) public lastHatch;
    mapping (address => address) public referrals;
    uint256 public marketEggs;
    constructor() public{
        ceoAddress=msg.sender;
    }
    function hatchEggs(address ref) public{
        require(initialized);
        if(ref == msg.sender || ref == address(0) || hatcheryMiners[ref] == 0) {
            ref = ceoAddress;
        }
        if(referrals[msg.sender] == address(0)){
            referrals[msg.sender] = ref;
        }
        uint256 eggsUsed=getMyEggs();
        uint256 newMiners=SafeMath.div(eggsUsed,EGGS_TO_HATCH_1MINERS);
        hatcheryMiners[msg.sender]=SafeMath.add(hatcheryMiners[msg.sender],newMiners);
        claimedEggs[msg.sender]=0;
        lastHatch[msg.sender]=now;

        //send referral eggs
        var refer1 = referrals[msg.sender];
        claimedEggs[refer1]=SafeMath.add(claimedEggs[refer1],SafeMath.div(SafeMath.mul(eggsUsed,13),100));
        var refer2 = referrals[refer1];
        if(refer2 != address(0)){
            claimedEggs[refer2]=SafeMath.add(claimedEggs[refer2],SafeMath.div(SafeMath.mul(eggsUsed,2),100));
        }



        //boost market to nerf miners hoarding
        marketEggs=SafeMath.add(marketEggs,SafeMath.div(eggsUsed,5));
    }
    function sellEggs() public{
        require(initialized);
        uint256 hasEggs=getMyEggs();
        uint256 eggValue=calculateEggSell(hasEggs);
        uint256 fee=devFee(eggValue);
        claimedEggs[msg.sender]=0;
        lastHatch[msg.sender]=now;
        marketEggs=SafeMath.add(marketEggs,hasEggs);
        ceoAddress.transfer(fee);
        msg.sender.transfer(SafeMath.sub(eggValue,fee));
    }
    function buyEggs(address ref) public payable{
        require(initialized);
        uint256 eggsBought=calculateEggBuy(msg.value,SafeMath.sub(address(this).balance,msg.value));
        eggsBought=SafeMath.sub(eggsBought,devFee(eggsBought));
        uint256 fee=devFee(msg.value);
        ceoAddress.transfer(fee);
        claimedEggs[msg.sender]=SafeMath.add(claimedEggs[msg.sender],eggsBought);
        hatchEggs(ref);
    }

    //magic trade balancing algorithm
    function calculateTrade(uint256 rt,uint256 rs, uint256 bs) public view returns(uint256){
        //(PSN*bs)/(PSNH+((PSN*rs+PSNH*rt)/rt));  bs*rt/(rs+rt)
        return SafeMath.div(SafeMath.mul(PSN,bs),SafeMath.add(PSNH,SafeMath.div(SafeMath.add(SafeMath.mul(PSN,rs),SafeMath.mul(PSNH,rt)),rt)));
    }
    function calculateEggSell(uint256 eggs) public view returns(uint256){
        return calculateTrade(eggs,marketEggs,address(this).balance);
    }
    function calculateEggBuy(uint256 eth,uint256 contractBalance) public view returns(uint256){
        return calculateTrade(eth,contractBalance,marketEggs);
    }
    function calculateEggBuySimple(uint256 eth) public view returns(uint256){
        return calculateEggBuy(eth,address(this).balance);
    }
    function devFee(uint256 amount) public pure returns(uint256){
        return SafeMath.div(SafeMath.mul(amount,3),100);
    }
    function seedMarket() public payable{
        require(msg.sender == ceoAddress, 'invalid call');
        require(marketEggs==0);
        initialized=true;
        marketEggs=86400000000;
    }
    function getBalance() public view returns(uint256){
        return address(this).balance;
    }
    function getMyMiners() public view returns(uint256){
        return hatcheryMiners[msg.sender];
    }
    function getMyEggs() public view returns(uint256){
        return SafeMath.add(claimedEggs[msg.sender],getEggsSinceLastHatch(msg.sender));
    }
    function getEggsSinceLastHatch(address adr) public view returns(uint256){
        uint256 secondsPassed=min(EGGS_TO_HATCH_1MINERS,SafeMath.sub(now,lastHatch[adr]));
        return SafeMath.mul(secondsPassed,hatcheryMiners[adr]);
    }
    function min(uint256 a, uint256 b) private pure returns (uint256) {
        return a < b ? a : b;
    }
}

library SafeMath {

    /**
    * @dev Multiplies two numbers, throws on overflow.
    */
    function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        if (a == 0) {
            return 0;
        }
        uint256 c = a * b;
        assert(c / a == b);
        return c;
    }

    /**
    * @dev Integer division of two numbers, truncating the quotient.
    */
    function div(uint256 a, uint256 b) internal pure returns (uint256) {
        // assert(b > 0); // Solidity automatically throws when dividing by 0
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold
        return c;
    }

    /**
    * @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
    */
    function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        assert(b <= a);
        return a - b;
    }

    /**
    * @dev Adds two numbers, throws on overflow.
    */
    function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        assert(c >= a);
        return c;
    }
}

 申明:本人只研究分享有意思的智能合约,对于读者去部署使用以上代码造成的任何后果,不予负责,区块链智能合约具有不可篡改的性质,对于读者或使用者造成的经济价值,更不予负责,如果你也感兴趣,可以私信或微信或邮件作者

猜你喜欢

转载自blog.csdn.net/weixin_39842528/article/details/124763205
今日推荐