Red envelope distribution of classic smart contract cases

Classic smart contract case: sending red envelopes

Role analysis: those who send red envelopes and those who grab red envelopes

Functional Analysis:

  • Sending red envelopes: the function of sending red envelopes can be realized with the help of the constructor, the core is to put ether into the contract;

  • Grab the red envelope: the function of grabbing the red envelope, the success of the grab requires some assertions and judgments, the core operation is to transfer the contract to the person who grabs the red envelope;

  • Refund: When the red envelope is left, the person who sent the red envelope is allowed to take back the balance, which can be realized by destroying the contract;

Realize the function of sending red envelopes

  • An address with a payment function is required for sending red envelopes (whoever creates the contract is the one who sends out red envelopes)

  • The number of a red packet (number) needs to be passed in, and the amount of the red packet is passed in from msg.value

  • In the constructor, specify the person who sends the red envelope and the number of red envelopes

  • Need a function to query the balance of the red envelope (hint: address(this).balance)

Realize the function of grabbing red envelopes

  • Need a function to transfer money to the person who grabs the red envelope

  • Judgment is required in the function: 1. The balance of the red envelope is greater than 0; 2. The remaining number of red envelopes is greater than 0; (Hint: Assert)

  • The number of red envelopes decreases accordingly with the number of times the function is executed;

  • The amount of the red envelope is randomized (reminder: use the keccak256 function to calculate the hash of the current timestamp), and the amount of the red envelope is a number within 100 (reminder: the hash value is modulo 100)

  • Transfer function: msg.sender.transfer(amount)(amount is the amount);

Realize the refund of the red envelope balance

  • The selfdestruct function can be used to destroy the contract, and its prototype is as follows:

    function selfdestruct(address user)

  • user represents the beneficiary when the contract is destroyed;

  • Implement a kill function, use it to destroy the contract, and designate the person who sent the red envelope as the beneficiary;

Contract code:

pragma solidity ^0.6.1;

contract red_pocket{
    uint256 public number;
    address payable public pocket_sender;
    mapping(address => bool) isGot;
    // Send red packets
    // Specify the person and the number of red envelopes
    constructor(uint256 count) public payable{
        require(msg.value > 0, "msg.value must >0");
        require(count > 0, "count must > 0");
        number = count;
        pocket_sender = msg.sender;
    }

    // Query the balance of the red envelope
    function getBalance() public view returns(uint256){
        return address(this).balance;
    }

    // GetPocket
    function getPocket() public payable {
        require(!isGot[msg.sender],"msg.sender must not get");
        require(number > 0, "number must >0");
        require(getBalance() > 0, "getBalance() must > 0");
        uint256 amount = uint256 (keccak256(abi.encode(msg.sender,pocket_sender,now,number)))%100;
        msg.sender.transfer(amount);
        number --;
        isGot[msg.sender] = true;
    }

    // Refund the balance of the red envelope
    function kill() public{
        selfdestruct(pocket_sender);
    }
}

Contract execution screenshot:

First deploy the red envelope contract:

deploy.png

Check the relevant information before grabbing the red envelope:

It can be seen that the total amount is 20230324wei. According to delpoy, there are 6 red envelopes in total. The address of the sender is as follows:

prequery.png

Then grab the red envelope:

It can be seen that the total quota is reduced accordingly, and the number of red envelopes is also reduced by 1.

getpocket.png

Finally, kill destroys the contract and returns the balance of the red envelope.

kill.png

Guess you like

Origin blog.csdn.net/m0_52316372/article/details/130917337