current position:Home>Solidity lesson nine - events and inheritance

Solidity lesson nine - events and inheritance

2022-09-23 08:50:01volcano_op

Coming to the difficult and key courses, events and inheritance in the basic course.

One, the event

Events are a mechanism for contract blockchain communication.The application "listens" for certain events, and reacts to them.Events in solidity have two characteristics:

1. In response, the application (ether.js) can subscribe and listen to these events through the RPC interface, and react on the front end.

2. Economic, events are a relatively economical way to store data on the EVM.

The general format of a token ERC20 contract instance is:

event Transfer(address indexed from, address indexed to, uint256 value);

We can see that the Transfer event records a total of 3 variables from, to and value,Corresponding to the transfer address, receiving address and transfer amount of the token, respectively.At the same time, from and to are preceded by the indexed keyword, and each variable marked with indexed can be understood as a retrieval eventThe index "key" of , is stored and indexed separately as a topic on Ethereum, and the program can easily filter out the transfer events of a specific transfer address and receiving address.Each event has up to 3 variables with indexed.Each indexed variable has a fixed size of 256 bits.The hash of the event and these three variables with indexed are usually stored as topic in the EVM log.where topic[0] is the keccak256 hash of this event, topic[1] to topic[3]The keccak256 hash with the indexed variable is stored.

value without the indexed keyword will be stored in the data section of the event, which can be understood as the "value" of the event.Variables in the data section cannot be retrieved directly, but can store data of any size.Therefore the general data section can be used to store complex data structures such as arrays and strings, etc., since these data exceed 256 bits, even if stored in the topic section of the event, which is also stored in a hash format.In addition, variables in the data section consume less gas in storage than topic .We can release events in functions.In the following example, each time the _transfer() function is used to perform a transfer operation, the Transfer event will be released and the corresponding variable will be recorded.

 // Define the _transfer function to execute the transfer logicfunction _transfer(address from,address to,uint256 amount) external {_balances[from] = 10000000; // Give the transfer address some initial tokens_balances[from] -= amount; // from address minus transfer amount_balances[to] += amount; // to address plus transfer amount// release eventemit Transfer(from, to, amount);}

Second, inheritance

Inheritance in solidity is introduced here, including simple inheritance, multiple inheritance, and inheritance of modifier keywords and constructors.Inheritance is an important part of object-oriented programming and can significantly reduce duplication of code.If you think of contracts as objects, solidity is also an object-oriented language that can be inherited.

virtual: The function in the parent contract. If you want the child contract to be rewritten, you need to add the virtual keyword.

override: The child contract overrides the function in the parent contract, and the override keyword needs to be added.

Look at a bunch of code!

contract Doge{function catchphrase() public returns(String){return "So Wow cryptoDoge";}}contract BabyDoge is Doge{function anotherCatchphrase() public returns(String){return "Such Moon BabyDoge";}}

Since BabyDoge is inherited from Doge.Means that when you compile and deploy BabyDoge, it will have access to catchphrase() and anotherCatchphrase() and other public functions we have in Doge, expressing subclasses with inheritance logic.

The modifier in solidity can also be inherited. The usage is similar to that of function inheritance. Just add the virtual and override keywords in the corresponding places.

contract Base1 {modifier exactDividedBy2And3(uint _a) virtual {require(_a % 2 == 0 && _a % 3 == 0);_;}}contract Identifier is Base1 {/ / Calculate the value of a number divided by 2 and 3, respectively, but the incoming parameters must be multiples of 2 and 3function getExactDividedBy2And3(uint _dividend) public exactDividedBy2And3(_dividend) pure returns(uint, uint) {return getExactDividedBy2And3WithoutModifier(_dividend);}//Calculate the value of a number divided by 2 and 3 respectivelyfunction getExactDividedBy2And3WithoutModifier(uint _dividend) public pure returns(uint, uint){uint div2 = _dividend / 2;uint div3 = _dividend / 3;return (div2, div3);}}

Constructor inheritance:

There are two ways for the child contract to inherit the constructor of the parent contract.For a simple example, the parent contract A has a state variable a, which is determined by the parameters of the constructor:

// Constructor inheritanceabstract contract A {uint public a;constructor(uint _a) {a = _a;}}

1. Declare the parameters of the parent constructor when inheriting, for example: contract B is A(1)

2. Declare the parameters of the constructor in the constructor of the sub-contract, for example:

contract C is A {constructor(uint _c) A(_c * _c) {}}

Call the function of the parent contract

There are two ways for the child contract to call the function of the parent contract, directly calling and using the super keyword.

1. Direct call: the format is parent contract. function name ()

2.super keyword: child contracts can use super.function name() to call the nearest parent contract function.Solidity inheritance relationships are declared from right to left.

function catchphraseSuper() public{super.__()}

copyright notice
author[volcano_op],Please bring the original link to reprint, thank you.

Random recommended