Solidity Overview and Basic code display

Source: Internet
Author: User

Solidity is the high-level language of contract-oriented implementation of smart contracts. It is influenced by C ++,python and JavaScript and is designed for Ethereum virtual machines (EVM).

Solidity is a static type that supports features such as inheritance, libraries, and complex user-defined types.

You will see that you can create contracts for voting, crowdfunding, blind auctions, multi-signature wallets, and more.

1 Solidity Smart Contract example

Let's start with a basic solidity example. At the beginning, you may not understand the exact meaning of each line, but it doesn't matter, we will introduce each detail in the follow-up explanation.

The first line tells the contract to be written in 0.4.0 version of solidity, and the code has up-to-date compatibility. It is guaranteed that there will be no different behavior for compiling under different solidity compiled versions.

From a solidity point of view, a contract is a collection of code and data that exists in a particular address in the Ethereum blockchain. UINT Storeddata declares a variable of type UINT (256-bit unsigned integer) with the variable name Storeddata. You can think of it as a field in a database that can be queried and modified by the methods in the database. In Ethereum, this field belongs to a contract field. In this example, the variable can be obtained or modified by the Get,set method provided. In solidity, accessing a variable is not required to be referenced by this.

The contract is simple enough to allow anyone on the Ethereum to store a data to a node and publish it to Ethereum, and of course other nodes on the Ethereum can also modify the values you have stored by invoking the Set method. Although there are modifications, any history of the value operation is saved in the Ethereum. Do not worry about your storage records or changes to the record will be lost. We'll go back to how to limit the contract, and only allow you to modify the data on your own.

2 solidity Sub-currency example

The following example will implement a simple crypto currency example. There is no such thing as a dream, but only the creator of the contract has the privilege. In addition, anyone who has an ethereum key pair can trade money without having to register a username and password at all. This contract introduces some new concepts, let's go through each one.

  Address public Minter;

Declares a public, state variable of type address. The address type is a 160-bit value that does not allow any arithmetic operations. It is suitable for storing the contract address or other person's key pair. The Public keyword automatically generates a method for external access to the variable value. If you do not declare public, other contracts cannot access the variable. The automatically generated method is similar to the following:

  function Minter () returns (address) {return minter;}

Of course, if you add a method that is exactly the same as above, it doesn't work, we need the variable to be exactly the same as the method name produced. This is actually a compiler that will help us do it, we don't need to write it ourselves, we just have to know the concept.

  Mapping (address = uint) public balances;

or create a public state variable, which is a more complex data type than address, similar to Map<address,uint> in Java, which describes a map relationship between an address and a Uinit data type. Mappings's relationship can be seen as a hash table, and all possible keys correspond to a value of 0. Of course there is no case where there is only a key value or only value in the map. So we need to remember what kind of map relationship to add or how to use it like this example. Because this is a public variable, the system automatically generates a get method for it, similar to the following:

  function balances (address _account) returns (UINT) {

return Balances[_account];

}

Through the above method we can easily query the balance of an account.

  Event Sent (address from, address to, uint amount);

This line creates an event that is known as event. The event is triggered on the last line of the example. The user or server application can listen to the triggering of the event at a very low cost, which is what the price will be later. Once this event is triggered, the listener receives three parameters: From, To,amount. Which account, to which account, the amount is. These three parameters can easily be traced to a specific transaction. To listen to this event, we need to use the following code:

Note: How the user calls the system auto-generated balances method.

The coin method is a construction method that is called when the contract is generated and is not allowed to be invoked. MSG (as well as TX and block) is a global variable that holds some properties that can be accessed by the blockchain. It persists the address of the node where the contract was created. Msg.sender is the address of the caller of the method that is valued.

Finally, the actual completion of the contract function, and is called by other users is the Mint and send method. If Mint is called by an account that is not created by the contract, it will not work. However, send can be called by any account (which must have an Ethernet account) and send the etheric currency to another account. Note that if you use a contract to send the etheric currency to another account, viewing through the blockchain browser is not a change, as the process of sending the etheric currency and the amount of the change are stored in a special Ethereum contract. Not on the account. By using events, it is easy to create a blockchain browser that can be used to view transactions and account balances.

original link:http://wangxiaoming.com/blog/2018/04/20/HPB-45-ETH-Solidity-code/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.