Intelligent Contract Language Solidity Tutorial Series 2-Address type introduction __ block chain

Source: Internet
Author: User
Tags documentation modifiers

Intelligent Contract Language Solidity tutorial series the second-solidity address type introduction. It 's written in front .

Solidity is the Ethernet Square Intelligent Contract programming language, before reading this article, you should have an understanding of the ether square, the intelligent contract, if you do not understand, suggest you first look at the etheric square is what

The first half of this article is for reference solidity official documentation (current latest version: 0.4.20) for translation, and the latter part is the actual contract code Instance description type usage (for column subscribers only). address Type (addresses)

Address type addressing is a value type,

Address: 20 bytes (length of an Ethernet square address), the address type also has the member, the address is the foundation of all contracts
Supported operators:

<=,!=, >= and >

Note: Starting with 0.5.0, contracts are no longer inherited from address types, but can still be explicitly converted to addresses. members of the address type

Balance properties and Transfer () functions
This is a quick index of the address type-related members
Balance is used to query the balance of the account, transfer () is used to send the etheric currency (in Wei).
Such as:

Address x = 0x123;
Address myAddress = this;
if (X.balance < && myaddress.balance >=) X.transfer (10);

Note: If X is the contract address, the rollback function (fallback function) of the contract is executed with the transfer call (this is the EVM feature), and if the gas consumption or other reasons fail, the transfer trade fair is restored and the contract is terminated abnormally.

About the fallback function (fallback function), in short, it is a contract without function name function, in the following code case, to further explain the use of the fallback function (fallback).

Send () function
Send corresponds to the transfer, but is much lower. If execution fails, transfer does not stop because of an exception, and send returns false.

Warning: Send () execution has some risks: if the call stack is more than 1024 deep or the gas consumes light, the transaction will fail. Therefore, to ensure security, the return value of send must be checked, and if the transaction fails, the Ethernet currency will be refunded. It would be better if you use transfer.

Call (), Callcode () and Delegatecall () functions
To interact with non-ABI contracts, you can use the call () function, which sends the original data to another contract, supports any number of parameters of any type, and each parameter is packaged into 32 bytes by the rule (ABI Protocol) and spliced together. An exception is: If the first argument is exactly 4 bytes, in this case, it is considered to be used directly as a function signature specified by a function defined by the ABI protocol. If you only want to send a message body, you need to avoid the first argument being 4 bytes. As in the following example:

Address Namereg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2;
Namereg.call ("register", "MyName");
Namereg.call (Bytes4 (keccak256 ("Fun (uint256)")), a);

The call function returns a bool value to indicate whether the execution was successful or not. The normal end returns True, and the exception termination returns FALSE. However, you cannot get the result data, because you need to know in advance the encoding and data size of the returned data (because you do not know the protocol format used by the other party, you will not know how the returned results are parsed).
You can also provide a. Gas () modifier to invoke:

NamReg.call.gas (1000000) ("register", "MyName");

Similar can also be provided with the attached Ethernet currency:

NameReg.call.value (1 ether) ("register", "MyName");

Modifiers can be mixed, and the order of the modifiers is irrelevant.

NameReg.call.gas (1000000). Value (1 ether) ("register", "MyName");

Note: Gas or value modifiers cannot be used on overloaded functions at this time, a workaround is to introduce a special case for gas and value and just re-check whether they Are present at the point of overload resolution. (This sentence I am afraid to translate the inaccurate, quote the original)

We can also use Delegatecall (), which differs from the call method in that only the code executes, while other aspects, such as storage, balance, etc., are the data of the current contract. The purpose of the Delegatecall () method is to execute the library code in another contract. So developers need to ensure that the storage variables in the two contracts are compatible to ensure that delegatecall () can be implemented smoothly. Prior to the Homestead phase, only one restricted Callcode () method was available, but Callcode did not provide access to msg.sender,msg.value.

The above three methods call (), Delegatecall (), Callcode () are low-level messaging calls, preferably used only as a last resort, because they destroy solidity type safety.
. Gas () can be used under call (), Callcode () and Delegatecall () functions, Delegatecall () does not support. Value ()

Note: All contracts inherit the members of the address, so you can use This.balance to query the balance.
Callcode is not encouraged to use and will be removed later.

Warning: All of the above functions are low-level functions and should be used with the exception of caution. When you call an unknown, potentially malicious contract, when you give control to it, it may return your contract, so be prepared to respond to a situation where your state variable may be maliciously tampered with when the call returns. addresses constant (address literals)

A hexadecimal constant that can be checked by address checksum test is considered an address, such as 0XDCAD3A6D3569DF655070DED06CB7A1B2CCD1D3AF. A 39 to 41-bit long hexadecimal constant that cannot be checked by address legality prompts a warning to be treated as a normal rational number constant.

address legality check definition to explain contract case code in EIP-55 contract case

pragma solidity ^0.4.0;
    Contract addrtest{Event logdata (bytes data);
    function () payable {logdata (msg.data);
    function GetBalance () returns (UINT) {return this.balance;
    } UINT score = 0;
    function SetScore (uint s) Public {score = s;
    function Getscore () returns (UINT) {return score;
    } Contract calltest{function deposit () payable {} event Logsendevent (address to, uint value);
        function Transferether (address towho) payable {Towho.transfer (10);
    Logsendevent (towho, 10);
    function Callnofunc (address addr) returns (bool) {return Addr.call ("Tinyxiong", 1234); 
        function Callfunc (address addr) returns (bool) {Bytes4 Methodid = bytes4 (keccak256 ("SetScore (uint256)"));
    Return Addr.call (Methodid, 100);
    function GetBalance () returns (UINT) {return this.balance; }  
}
code operation and explanation

Code to run and explain, please subscribe to the block chain technology view. reference Documentation

Solidity official documents-types

Simple block chain-system learning block chain, to create the best block chain technology Blog

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.