[Ant Chain Learning 1] First understanding of Ant Chain's smart contracts (solidity)

1. Version directive and contract keyword

Below you will learn what a contract is and try how to create one.

contract

A smart contract is a computer protocol designed to disseminate, verify or execute contracts in an informative manner. A smart contract is essentially a set of digitally defined promises (Promises), including agreements on which contract participants can execute those promises.

Solidity's code is wrapped in contracts, and all variables and functions belong to a contract. The contract keyword is used to declare a contract.

Here is an empty contract called HelloWorld:

contract HelloWorld { 

} 

version directive

All Solidity source code must indicate the version, which is used to indicate the version of the Solidity compiler, so as to avoid future new compilers that may break your code. The pragma directive is used to declare the version of Solidity.

Example: pragma solidity ^0.4.20;

The following is a basic contract example:

pragma solidity ^0.4.20; 

contract HelloWorld { 

}

Note: Solidity's expression specifying version number follows " NPM version semantics ". In most cases, we can use ^.

actual combat

To create our little ant, let's first create a base contract called AntFamily.

  1. Enter ^0.4.20 in the input box on the right, our contract is based on this version of the compiler.
  2. Create an empty contract AntFamily.
pragma solidity ^0.4.20;

contract AntFamily {
  
}

2. State variables and integers

In the previous chapter, we have made a shell for our contract, let's learn how to use variables in Solidity.

State variables

State variables defined inside the contract, but not inside the function, will be permanently stored in the contract storage space, which means they will be written to the blockchain , which is like writing data to a database. Here is an example of a state variable:

contract HelloWorld {
  // 这个无符号整数将会永久的被保存在区块链中
  uint myUnsignedInteger = 10;
}

In the above example, myUnsignedInteger is defined as a uint type and assigned the value 10.

unsigned integer uint

uint is one of the basic data types supported by Solidity, which means unsigned integer, which means that its value cannot be a negative number. For signed integers, there is a data type named int.

actual combat

Our ant DNA will consist of a twelve digit number.

  1. Define dnaDigits as uint data type, and assign 12.
pragma solidity ^0.4.20;

contract AntFamily { 
  uint dnaDigits = 12; 
}

3. Mathematical operations

In Solidity, mathematical operations are the same as in other programming languages:

  • Addition: x + y
  • Subtraction: x - y
  • Multiplication: x * y
  • Division: x / y
  • Modulo: x % y
  • Exponentiation: x**y

Here is an example of exponentiation:

// 表示6的2次方
uint a = 6 ** 2; 

actual combat

Each little ant has its own DNA gene. In order to ensure that our ant's DNA contains only 12 characters, we first create a uint data and make it equal to 10^12. This way we can then use the modulo operator % to turn an integer into 12 bits.

  1. Create a variable of type uint named dnaModulus equal to 10 raised to the power of dnaDigits.
pragma solidity ^0.4.20;

contract AntFamily {
	uint dnaDigits = 12;
	uint dnaModulus = 10 ** dnaDigits;
}

4. Structure

Sometimes you need more complex data types. Solidity supports defining new types through structures. Use the struct keyword to define structures:

struct Book {
    string name;
    uint page;
}

Structs allow you to generate a more complex data type that can have multiple properties.

Note: We have just introduced a new primitive data type string . The string type is used to hold UTF-8 encoded data of any length. For example: string message = "Hello world".

actual combat

In our program, we're finally going to create ants, and each ant will have multiple properties, so this is a perfect example of a structure.

  1. Create a struct named Ant.
  2. Our Ant struct has two properties: name (of type string), and dna (of type uint).
pragma solidity ^0.4.20;

contract AntFamily {
  uint dnaDigits = 12;
  uint dnaModulus = 10 ** dnaDigits;
  
  struct Ant {
    string name;
    uint dna;
  }  
}

5. Define the function

Use the function keyword to define a function in Solidity. The function is the executable unit of the contract code. The basic syntax of the function definition is as follows:

function buyBook(string _name, uint _count) { 
  
} 

This is a function called buyBook, which takes two parameters: one is of type string and the other is of type uint. Now the inside of the function is still empty.

Note: It is customary for variables in functions to start with (_) (but not mandatory) to distinguish global variables. We'll use this habit throughout the tutorial.

Our function is defined as follows:

buyBook(“fishBook”, 100);

actual combat

In our application, we want to be able to create ants, let's write a function to do this!

  1. Create a function createAnt. It has two parameters: _name (of type string), and _dna (of type uint).
pragma solidity ^0.4.20;

contract AntFamily {
  
  uint dnaDigits = 12;
  uint dnaModulus = 10 ** dnaDigits;
  
  struct Ant {
    string name;
    uint dna;
  }
  
  function createAnt(string _name, uint _dna) {
    
  }
}

Guess you like

Origin blog.csdn.net/qq_44528283/article/details/123936101
ANT
ANT