Two Solidity.

1. Introduction of Solidity

  1. Ethereum’s language for writing smart contracts, file extension.sol
  2. Strongly typed programming languages (variable types, such as int byte, bool struct, etc., require strong conversion between types)
  3. Similar to javascript (but javascript is weakly typed and a var keyword automatically recognizes data types)
  4. A pile of pit
  5. API Manual – Solidity Chinese Manual

2. Development environment

  • Mist Wallet – Hard to use, lots of problems

  • Remix programming online

    • Features: Access links, convenient debugging, network dependence
    • Address: remix.ethereum.org
  • Remix Local environment

    • Features: convenient, relatively stable

    • Problem: The compiler occasionally fails to load properly and requires complete disconnection to load

    • [NPM install third-party library can not find “cl.exe” problem]

    • The installation

      npm install remix-ide -g
      Copy the code
    • Start the service: remix-ide

3. The first contract

  • Smart Contract overview
Each contract can contain the following: - State Variables: member Variables, which are up-chain data. - Functions - function modifiers (FunctionStructs Types - Enum Types - Mapping (Structs Types)key= >The value), such as: the map [1] = 2
Copy the code
  • Write a method to receive the message
pragma solidity ^0.425.;

contract Inbox{ 
    string public message; 
    
    function getMessage() public constant returns(string){
        return message;
    }
  
    function setMessage(string newMessage) public{ message = newMessage; }}Copy the code
  • Syntax parsing

4. Compile the contract

4.1 the principle

  • Switch from a high-level language to a machine language using Remix

    1. Solidity –> Bytecode – Machine language, blockchain system read
    2. Solidity –> ABI (Application Binary Interface) – Easy for programmers to call
  • graphic

4.2 the bytecode

6080604052610410806100136000396000f300608060405260043610610057576000357c010000000000000000000000000000000000000000000000 0000000000900463ffffffff168063368b87721461005c578063ce6d41de146100c5578063e21f37ce14610155575b600080fd5b3480156100685760 0080fd5b506100c3600480360381019080803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291 9081815260200183838082843782019150505050505091929192905050506101e5565b005b3480156100d157600080fd5b506100da6101ff565b6040 518080602001828103825283818151815260200191508051906020019080838360005b8381101561011a5780820151818401526020810190506100ff 565b50505050905090810190601f1680156101475780820380516001836020036101000a031916815260200191505b509250505060405180910390f3 5b34801561016157600080fd5b5061016a6102a1565b6040518080602001828103825283818151815260200191508051906020019080838360005b83 8110156101aa57808201518184015260208101905061018f565b50505050905090810190601f1680156101d75780820380516001836020036101000a 031916815260200191505b509250505060405180910390f35b80600090805190602001906101fb92919061033f565b5050565b606060008054600181 600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002 03166002900480156102975780601f1061026c57610100808354040283529160200191610297565b820191906000526020600020905b815481529060 01019060200180831161027a57829003601f168201915b5050505050905090565b60008054600181600116156101000203166002900480601f016020 8091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103375780601f1061030c57 610100808354040283529160200191610337565b820191906000526020600020905b81548152906001019060200180831161031a57829003601f1682 01915b505050505081565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106103805780 5160ff19168380011785556103ae565b828001600101855582156103ae579182015b828111156103ad57825182559160200191906001019061039256 5b5b5090506103bb91906103bf565b5090565b6103e191905b808211156103dd5760008160009055506001016103c5565b5090565b905600a165627a 7a723058201486092ea8103d4816d5aa6177b30372d94ec850544312021159c20f3b7a505b0029Copy the code

4.3 abi

  • (Application binary interface)

    Description file in JSON format:

[{"constant": false."inputs": [{"name": "newMessage"."type": "string"}]."name": "setMessage"."outputs": []."payable": false."stateMutability": "nonpayable"."type": "function"
	},
	{
		"constant": true."inputs": []."name": "getMessage"."outputs": [{"name": ""."type": "string"}]."payable": false."stateMutability": "view"."type": "function"
	},
	{
		"constant": true."inputs": []."name": "message"."outputs": [{"name": ""."type": "string"}]."payable": false."stateMutability": "view"."type": "function"
	},
	{
		"inputs": []."payable": true."stateMutability": "payable"."type": "constructor"}]Copy the code

4.4 call

To facilitate the development, learning and testing of smart contracts, Ethereum has opened up several new blockchains with the same features as the main network, but with a lower value and easier access to Ethereum in the test network. This prevents the development of bugs on the main network from causing loss of ether.

Of course, we can also build a private test network, but the decentralized nature of blockchain requires more nodes to run to achieve the desired effect. Fortunately, Ethereum has an open test network, which is easier for us to access.

4.4.1 Operating Environment
  • vm

    • Account five virtual accounts, each with 100eth by default.
    • No mining, direct return results, easy to develop and test
  • The web environment

    • injected web3
    • Closely integrated with metamask
  • Custom Environment

    • web3 provider
    • Link your own locally launched blockchain environment (Truffle, Ganache, private chain, etc.)
4.4.2 Deployment Contract
  • Return address contract after successful deployment, such as: 0 x27baf63ea560973e9f8f333a39239967f96c5703

  • The deployment contract is also a transaction, except that the TO field is not filled in and the data entry field is filled in as the contract bytecode.

  • Viewing a browser

4.4.3 Invoking the contract
  • Manual invocation, we use manual invocation, there are two types of operations that call contracts

    • Send: All operations that change the state of blockchain data are called SEND operations, which initiate a transaction and typically cost money. The result is returned about 10 to 30 seconds (confirmed by the miner).

    • Call: All operations that read blockchain contract data are called Call operations, which are characterized by quick return at no cost

  • Called via Web3

4.4.4 Comparison and summary
Compare the item Function call The function calls Send Transaction
State of the data Do not modify the smart contract data Modify smart contract data
The return value Functions can return data The function cannot return data because the function takes time to execute
Execution speed immediately It takes more than ten seconds to complete and returns the hash of transaction
The cost of free Cost money!!

4.5 Destructor

function destroy() {
	// Destructor destroys the current contract and sends all its funds to the given address
      //1. Forward the contract balance to the incoming address
      //2. Mark the current contract as destroyed
	selfdestruct(msg.sender);
}
Copy the code

After the call, the contract still exists on the blockchain, but the function cannot be called, and the call throws an exception.

4.6 Rollback function

pragma solidity ^0.425.;

contract Strudent{
    uint public uAge = 10;
    bool public uGender = false;
    
    function changeMsg(uint num,bool _uGender) public{
        uAge += num;
        uGender=_uGender;
    }

}

contract StuInfoManage{
    function callByFun(address addr) public returns (bool){
        bytes4 methodId = bytes4(keccak256("changeMsg(uint256,bool)"));
        return addr.call(methodId,10.true); }}Copy the code
  • Each contract has one and only one function without a name. This function takes no arguments and returns no value.
  • Call scenario: