solidity合约案例

案例1:群发红包

角色:

  • 发红包者:发红包;红包退回
  • 抢红包者:抢红包

功能:

  • 发红包 :红包类型(随机红包,平均红包)
  • 红包退回
  • 抢红包

代码:

// SPDX-License-Identifier: Apache-2.0  
pragma solidity ^0.8.7;

contract redpacket{
    bool public redType; //true:平均类型;false:随机类型
    uint8 public redCount;//红包个数
    uint256 public redTotalAmount;//红包总金额
    address public sendReder;
    mapping(address=>bool) isStake;

    constructor(bool isAvg,uint8 _count,uint256 _amount) payable {
        redType=isAvg;
        redCount=_count;
        redTotalAmount=_amount;
        sendReder=msg.sender;
        require(_amount==msg.value,"redpacket's balance is ok");
    }

    function getbalance() public view returns(uint256){
        return address(this).balance;
    }
    //抢红包
    function stakePacket() public payable {
        require(redCount>0,"red packet must left");
        require(getbalance()>0,"balance must enough");
        require(!isStake[msg.sender],"user already stake");
        isStake[msg.sender]=true;
        
        if (redType==true){
            uint256 amount=getbalance()/redCount;
            payable (msg.sender).transfer(amount);
        }else{
            if(redCount==1){
                payable (msg.sender).transfer(getbalance());
            }else{
                uint256 randnum=uint256(keccak256(abi.encode(sendReder,redTotalAmount,redCount,block.timestamp,msg.sender)))%10;
                uint256 amount=getbalance()*randnum/10;
                payable (msg.sender).transfer(amount);
            }
            
        }
        redCount--;
    }

    function kill() public payable {
        selfdestruct(payable (sendReder));
    }
}   

注意:
selfdestruct(payable (sendReder)):自毁当前合约,余额转入制定地址

案例2:智能拍卖

角色:

  • 卖方
  • 买房:竞拍-价高者得
  • 平台方:创建合约;结束拍卖
// SPDX-License-Identifier: Apache-2.0  
pragma solidity ^0.8.7;

contract auction {
    
    
    //拍卖公司
    address  payable owner;
    //卖方
    address payable seller;
    //最高价买方
    uint256 public hightestBid;
    address payable hightestBider;
    
    //起拍价
    uint256 public startBid;

    //截止时间
    uint256 public endTime;

    bool isFinshed;

    event BidEvnet(address _higher,uint256 _amount);
    event EndEvent(address _winner,uint256 _amount);

    constructor(address _seller,uint256 _startBid) {
    
    
        owner=payable (msg.sender);
        seller=payable (_seller);
        startBid=_startBid;
        isFinshed=false;
        endTime=block.timestamp+120;
        hightestBid=_startBid;
    }
    //竞拍
    function bid(uint256 _amount) public payable {
    
    
        require(_amount>hightestBid,"amount must > hightestBid");
        require(_amount==msg.value,"amount must = msg.value");
        require(!isFinshed,"auction already finshed");
        require(block.timestamp<endTime,"auction already time out");
        //如果hightestBider不是为空,表示前面有人竞价了,需要把钱退回
        if(hightestBider!=address(0)){
    
    
            hightestBider.transfer(hightestBid);
        }
        hightestBid=_amount;
        hightestBider=payable(msg.sender);

        emit BidEvnet(msg.sender, _amount);
    }
    //结束竞拍
    
    function endAuction() public payable  {
    
    
        require(!isFinshed,"auction already finshed");
        require(msg.sender==owner,"only owner can operate");
        isFinshed=true;
        seller.transfer(hightestBid*90/100);

        emit EndEvent(hightestBider, hightestBid);
    }
}

猜你喜欢

转载自blog.csdn.net/qxjswl/article/details/130058894
今日推荐