directory

I Introduction 2 Details 2.1 Environment 2.2 Basic Syntax 2.3 Array 2.4 Storage and Memory 2.5 Events/Logs 2.6 GAS2.7 Contract Adjustment 2.8 Contract Upgrade 2.9 Problems 2.10 Security-related 2.11 Web3-related Recommendations

A preface

Summing up what we’ve learned about blockchain smart contract development over time is solidity language development

This article is suitable for those with solidity based but not actually watched, both for actual development projects. For blockchain entry basis will not be detailed, if necessary can be private.

The article mainly serves as an introduction and summary, and the reader is left to think about how to use and distinguish.

Please point out any incorrectness or infringement in this chapter

Ii. Details

2.1 environment

Basically using the online version of the IDE both Remix development (self science)

2.2 Grammar Basics

  • Uint8 (-1) == 255
  • Property access is internal by default, and public qualifiers will automatically have the get function
  • Solidity comes with global functions

2.3 an array

Add: push, the return value of push is the new length

Delete: To reduce the array size, remove the last one from the array (version 0.5)

Create a fixed length array:

1uint[5] memory arr = [0.1.2.3.4];
2address[] memory meetIncomeAccounts = new address[](arr.length);
Copy the code

2.4 Storage and Memory

  • Storage stores this variable on the blockchain

  • Memory is both the local machine running the variable (the number of variables in each function is limited, and the specific search page stack is too deep)

A single memory manipulation function (one decorated with pure or View) does not require gas consumption, but a function with memory and memory (one not decorated with Pure or View) will also take memory into account when calculating GAS.

2.5 Events/Logs

Quickly find

To quickly search for an address in logs, you can use the index keyword.

Displays logs for contract B in contract A

Two contract calls, A calls B, to print B’s diary in A, need to write B interface in A with the event declaration.

2.6 gas

The most important consideration in contract development of Ethernet is how to satisfy the business while minimizing gas.

For other chain development contracts, energy may not be consumed or can be secured, depending on which chain the contract is running on. But the aether must belong to a big head.

General ideas for saving gas:

When using structure, select variable type of specified size as far as possible according to the size of the current variable. For example, if a variable declared as uint type cannot exceed 200 in the whole business scope, it should be declared as uint8 variable type to save gas.

For a stored variable, assigning it to a memory variable within a function will reduce gas.

2.7 Contract transfer contract

Call another function on the contract

 1contract Test {
 2    function callHello(address test1Addressspublic pure returns(string memory{
 3        // Enter the address of Test1
 4        Test1 test1 = Test1(test1Addresss);
 5        return test1.hello1();
 6    }
 7}
 8
 9contract Test1 {
10    function hello1(public pure returns(string memory{
11        return "hello1";
12    }
13}
14
15Test1 is the address of Test1, callHello is the address of Test1, and hello1 is returned.
Copy the code

A special call (delegatecall) is code that uses another contract without using its data

 1// delegatecAll calls the contract (take the code from another contract and execute it yourself)
 2JoinTeamTest (address,address) is not allowed in "joinTeamTest(address,address)" for multi-parameter calls.
 3function joinTeamTest(address user, address superior) public returns (bool){
 4        bool status;
 5        bytes memory result;
 6        (status, result) = upgradedAddress.delegatecall(abi.encodeWithSignature("joinTeamTest(address,address)", user, superior));
 7        require(status, "Error: Call 'joinTeamTest' method error");
 8        require(bytesToUint(result) == 1."Error: Call 'joinTeamTest' method result error");
 9        emit TestData(status, result);
10        return status;
11    }
Copy the code

2.8 Contract upgrade

There are two ways

  1. Can refer to the USDT source code, you can know, in short, is all external interface interception reference:
  2. Use contract transfer contract, recommendation method, one contract store data, one contract as implementation code. See delegatecall special call reference: Use Delegatecall to upgrade contracts in version 0.6.8 solidity

2.9 Problems encountered

  • If the stack is too deep, it indicates that the slot is not enough
  • The same contract generates the same contract address on different chains. Deploy the contract on different chains using a user who has never initiated a transfer before. The requirements are all first deployment (same time?). The blogger did not verify this claim

2.10 Security related

Best security development Guide

17 pits and super detailed pit avoidance guide

2.11 web3 related

Abi encoding (ABi.encodePacked) and hash (KECCAK256) calculations

  • Abi into 16 into coding is actually the parameters, and long, the corresponding web3: web3. Eth. Abi. EncodeParameter
  • Hash calculation is sha3, corresponding to web3: web3.utils.keccak256

Iii. Recommendation/reference

  • Ethereum official website

  • Truffle official document

  • Solidity document

  • Truffle Chinese document

  • Web3.js API Chinese documentation

  • Community of Ethereum enthusiasts

  • Ethereum White Paper

  • Uniswap source code full explanation of the first

  • Uniswap White Paper in Chinese

  • Uniswap build (build by yourself, pay attention to the hash code of the pairFor function)

  • Ethernet Gas details table

  • Solidity Starter Series: First line of code (some not recommended anymore)