Ethereum Series-Solidity Smart Contract Study Notes

(1) Account address

To create an account, there is no need to log on. In fact, the address does not exist on the chain. The recipient address of the transfer can be any address code that meets the rules, and the transfer can be successful, but no one has the matching private key to operate this Account

The geth node restarts, and the data on the chain is still saved

(2) Commonly used variables in the contract

  • msg.sender Is the address of the user who is currently calling the method
  • this Refers to the address of the current contract
  • address Support various arithmetic operators
  • tx.origin The sender of the transaction

(3) Visibility of functions public external internal private

  • public : The function is declared as public by default and is part of the contract's external interface

  • external : Declared as external can be called from other contracts or through Transaction, which is part of the contract's external interface

    • Variables in the contract are not named public or external, and cannot be obtained by external dapp
  • internal: In the current contract or inherited contract, only internal calls are allowed

  • private: Can only be accessed in the current contract (cannot be accessed in the inherited contract)

    • Even if it is declared as private, the data inside can still be viewed by everyone . Access rights only prevent other contracts from accessing functions or modifying data.

(4) Inheritance of contract

A contract with an inheritance relationship only needs to arrange the child contract (its abi and bin information already contain the information of the parent contract)

In the dapp, the function of the parent contract can be directly adjusted through the address of the child contract

(5) Contract A calls contract B

  • To define a variable of type contract B in contract A, you need to deploy contract B first, and then assign B's address to the contract B variable in A
  • When the contract B variable is not assigned, it cannot be used
  • After the variable of contract B is assigned, the function of contract B can be indirectly adjusted through the address of contract A in the dapp (encapsulated into an interface function)
  • In contract A, payablethe function of the variable of contract B with the label is called, and the calling method is as follows:
    • . Contract B variable function .value (how much money will be used as the value msg.value) (function parameter);
      for example: siringAuction.bid.value(msg.value - autoBirthFee)(_sireId);

(6) The role of function identification payable

There is a payablekeyword on the function. If a function needs to perform currency operations, it must be accompanied by the payable keyword so that it can be received normally msg.value.

(7) String comparison

Solidity does not support native string comparison, it can only be judged by comparing the keccak256 hash value of two strings

require(keccak256(_name) == keccak256("Vitalik"));

(8) Multiple contract documents

In Solidity, when you have multiple files and want to import one file into another file, you can use the import statement: The same directory must also start with "./"

import "./someothercontract.sol";

(9) Storage

StorageVariables refer to variables that are permanently stored in the blockchain. MemoryVariables are temporary. When an external function is called to a contract, the memory variable is removed

  • When used in a function Storage, it is equivalent to a pointerSandwich storage mySandwich = sandwiches[_index];
  • When used in a function Memory, it is equivalent to copySandwich memory anotherSandwich = sandwiches[_index + 1];

(10) Byte alignment

When uint is defined in a struct, try to use the smallest integer subtype to save space.

And put the variables of the same type together (that is, the variables will be placed in order of type in the struct), so that Solidity can minimize the storage space

(11) If the contract wants to return a struct, it has to use multiple parameters instead

If the contract returns multiple parameters, define a structure in the code to receive,
and use [FunctionOutput] before the defined structure, and use CallDeserializingToObjectAsyncto get

(12) The array subscript starts from 0

(13) Function modifier

  • viewThe function is constantexactly the same, the state variable can be read but cannot be changed;
  • pureIt is more strict, pure modified functions cannot be modified or read state variables, otherwise the compilation will not pass.

(14)require(), assert(), revert()

In the 0.4.10 version of solidity, require(), assert(), revert() have been added.

  • require() Used to check for less serious errors and return unused gas.
  • assert()Used to check for more serious errors, all gasLimit fees will be taken away as before. The writing method is basically the same, but the processing method is different.
  • revert Basically the same as require, but revert does not include status checking.

(15) What is the difference between tx.origin and msg.sender?

  • tx.origin The sender of the transaction.
  • msg.sender The sender of the message.

The transaction transaction can contain one or more contracts.

  1. For a contract, tx.origin and msg.sender are the same address for the contract itself.
  2. When there are multiple contracts, the scenario is: user, contract A, contract B. The user adjusts contract B through contract A. At this point
    • For contracts A: tx.originand msg.senderall users.
    • For contracts B: tx.originthe user msg.senderis a contract A.

Guess you like

Origin blog.csdn.net/wcc19840827/article/details/113859402