This is the 20th day of my participation in the August Text Challenge.More challenges in August

Supplementary knowledge:

  1. Transfer is not about the deployer, it is about the trader!
  2. Gas is a unit of measurement in the Ethereum network. It is designed to quantify the power consumption. In other words, with gas, we can easily calculate the gas cost for a user to complete a transaction and the gas payment for a miner to complete a block package.

1. Built-in global variables

  • MSG. Sender:

The owner of getOwner is the address of account B. The owner of getOwner is the address of account B. The owner of getOwner is the address of account B. The owner of getOwner is the address of account B.

pragma solidity 0.422.;
contract Owner {
    address public owner;
    function getOwner() public {
        owner = msg.sender;   // Get the address of the caller}}Copy the code
  • MSG. The value:

The amount of ‘payable’ can be accepted, in ‘wei’, with ‘payable’ required

The value received can be used to determine restrictions. For example, the transfer amount must be within a certain range, which Balance cannot do

pragma solidity 0.422.; contract Value { uint256 public money; function getValue() public payable { money = msg.value; }}Copy the code

If you add if to judge:

pragma solidity 0.422.;

contract Value {
    uint256 public money;
    function getValue() public payable {
        if( msg.value == Awesome!) {  // The transfer amount is 666WEImoney = msg.value; }}}Copy the code

Note: transfer money is required, note that the default unit is WEI

Built-in global variables Referred to as” type
blockhash Hash value byte32
block.coinbase Address of the current block miner address
block.difficulty Difficulty of the current block uint
block.gaslimi Gaslimit of the current block uint
block.number The block number of the current block uint
block.timestamp The timestamp of the current block uint
gasleft() The rest of the gas uint
msg.sender Address of the caller address
msg.value Amount transferred (Unit: WEI) uint
msg.data Complete call data calldata bytes
now The timestamp of the current block Uint (same as block.timestamp)
tx.gasprice Transaction gas price uint
tx.origin The sender of the transaction address

2. Error handling

  • The require:

Require exceptions do not consume any gas. The official recommendation is to use require, which is equivalent to if {throw}

require(msg.value == Awesome!);    // If not, an error is reported; otherwise, the execution continues
Copy the code

Something like the following: Notice that the condition is just the opposite

if (msg.value <= 6 * 10支那18)  {
    // Todo involves error handling
    throw;
}
Copy the code
  • Assert:

Even if there is an error, gas is executed and deducted

assert(msg.value == Awesome! );    // If not, an error is reported; otherwise, the execution continues
Copy the code
  • Revert () : Handles scenarios with more complex logic, such asif/else
if (msg.value <= 6 * 10支那18){
    revert();
} else {
    b = Awesome!;
}
Copy the code
  • Require (conditions); If the conditions are met, proceed. If the conditions are not met, an error is reported. Gas is not deducted
  • Assert (conditions); Assert deducts gas and is not recommended
  • Revert (): Responsible for logical use, if(condition){revert(); }else{… }

3. Access functions

In order to return a variable, you have to write a function to return it

pragma solidity 0.422.;
contract Hello {
    string name = "hallen";
    function get_name() public view returns(string){
        returnname; }}Copy the code

If the variable is decorated with public, an accessor function of the same name is generated and can be accessed directly

pragma solidity 0.422.;
contract Fan {
    string public name = "fanone";
    function getName() public view returns(string) {return name;
        // return this.data(); Internal use of this in the contract also calls the accessor function
    }
}
contract One {
    function getHelloName() public view returns (string){
        Fan h = new Fan();   // Address directly to the contract type
        return h.name();    // This must be parenthesized; it is an access function}}Copy the code

Create a contract

  • New: returns an address that needs to be converted to the contract type

So this is the way we started the contract

Fan h = new Fan();  // Force the address directly to the contract type
Copy the code

A bit like a Java contract variable (Fan public H), it is empty and requires an address to be used, otherwise it is used in scenarios where an error contract type is used as a parameter

Fan public h;
// Assign a value to the function
address addr = new Fan();
h = Fan(addr); // Assign the address
Copy the code

Transfer syntax: h.get_money.value(20).gas(800)();

H must be the contract object after the address is assigned

pragma solidity 0.424.;
contract Test1{
    // Get the transfer money
    function contractGetMoney() public payable{
    }
    // Check the balance
    function getBalance() public view returns(uint256){
        return address(this).balance;
    }
}
contract Test2 {
    // Check the balance
    function getBalance() public view returns(uint256){
        return address(this).balance;
    }
    // Get the transfer money
    function contractGetMoney()public payable{
    }
    // Transfer: transfer money to whoever calls
    Test1 public t1;
    function getAddr(address addr) public{
        t1 = Test1(addr);
    }
    // Transfer money to contract Test1
    function payToT1() public{
        t1.contract_get_money.value(5 * 10支那18).gas(200) (); }// Pay is decorated with payable, using an anonymous function
    function () public payable {}
}
Copy the code

5. Contract succession

  • useisKeyword, multiple parent contracts are separated by commas

Parent contract parent contract… {} constract Cat is Animail,Lactation{} // Lactation: if the two parent contracts have the same function, the inheritance principle is followed (inheritance order, the latest in the Animail, the furthest in the Lactation, So in Lactation)

6. modifier

Check whether the preconditions are met before the function is executed

pragma solidity 0.422.;
contract Value {
    uint256 public money;
    modifier check_money(){
        require(msg.value == 10);
        _;  // Modifies the code after entering the function
    }
    function get_value() public payable check_money{  // check_money is the modifier defined earliermoney = msg.value; }}Copy the code

You can use constructors to determine if you are an administrator (deployer), and you can add modifiers to functions that are only accessible to administrators

pragma solidity 0.422.;
contract Value {
    address public owner;
    uint256 public money;
    constructor() public{
        owner = msg.sender;
    }
    modifier check_owner(){
        require(msg.sender == owner);
        _;   // Modifies the code after entering the function} function get_value() public payable check_money{ money = msg.value; }}Copy the code

The last

Xiao Sheng Fan Yi, looking forward to your attention.