In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what are the types of Solidity addresses". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the Solidity address types"?
Address type (Address)
Address type address is a value type
Address: 20 bytes (the length of an Ethernet address), the address type also has members, and the address is the basic operator supported by all contracts:
Note: starting from 0.5.0, contracts are no longer inherited from address types, but can still be explicitly converted to addresses.
Members of the address typ
The balance attribute and the transfer () function here is the quick index of the relevant members of the address type balance to query the account balance, and transfer () to send Ethernet coins (in wei). Such as:
Address x = 0x123 * address myAddress = this;if (x.balance
< 10 && myAddress.balance >= 10) x.transfer (10)
Note: if x is the contract address, the fallback function (fallback function) of the contract will be executed with the transfer call (this is the EVM feature). If the transfer transaction fails due to gas depletion or other reasons, the transfer transaction will be restored and the appointment exception will stop.
With regard to the fallback function (fallback function), it is simply an unnamed function in the contract. In the following code example, we will further explain the use of the fallback function (fallback).
The send () function send corresponds to transfer, but at a lower level. If the execution fails, transfer will not stop because of an exception, and send will return false.
Warning: send () execution has some risks: if the depth of the call stack exceeds 1024 or the gas runs out, the transaction will fail. Therefore, in order to ensure security, the return value of send must be checked and the etheric currency will be returned if the transaction fails. It would be better to use transfer.
In order to interact with non-ABI contracts, the call (), callcode () and delegatecall () functions can use the call () function to send raw data to another contract, supporting any number of parameters of any type, each of which is packaged into 32 bytes according to the rule (ABI protocol) and spliced together one by one. One exception is that if the first parameter is exactly 4 bytes, in this case, it is considered to be used directly according to the function signature specified by the function defined by the ABI protocol. If you only want to send the message body, you need to avoid that the first parameter is 4 bytes. Such as the following example:
Address nameReg = 0x72ba7d8e73fe8eb666ea66babc8116a41bfb10e2 nameReg.call ("register", "MyName"); nameReg.call (keccak256 ("fun (uint256)"), a)
The call function returns a Bool value to indicate whether the execution was successful or not. True is returned after normal termination, and false is returned after abnormal termination. However, the result data cannot be obtained because you need to know in advance the encoding and size of the returned data (because you do not know the protocol format used by the other party, you will not know how to parse the returned results). You can also provide a * * .gas () * * modifier to call:
NamReg.call.gas (1000000) ("register", "MyName")
Similarly, an etheric currency can also be provided:
NameReg.call.value (1 ether) ("register", "MyName")
Modifiers can be mixed, and the order in which the modifiers are called does not matter.
NameReg.call.gas (1000000) .value (1 ether) ("register", "MyName")
Note: at present, you cannot use gas or value modifiers on overloaded functions, 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. (I am afraid that this sentence will be translated inaccurately.
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.), use data from the current contract. The purpose of the delegatecall () method is to execute 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 executed 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 (), and callcode () are all underlying messaging calls and are best used only as a last resort, because they break the type safety of Solidity. .gas () can be used under the call (), callcode () and delegatecall () functions. Delegatecall () does not support .value ().
Note: all contracts inherit members of address, so you can use this.balance to query the balance. Callcode is discouraged and will be removed later.
Warning: the above functions are all low-level functions, so you should be very careful when using them. When you call an unknown, possibly malicious contract, it may call back your contract when you give it control, so be prepared to deal with the possibility that your state variable may be maliciously tampered with when the call returns.
Address constant (Address Literals)
A hexadecimal constant that passes the address validity check (address checksum test) is considered an address, such as 0xdCad3a6d3569DF655070DEd06cb7A1b2Ccd1D3AF. A 39-to 41-bit hexadecimal constant that fails the address validity check prompts a warning and is considered a common rational constant.
Address validity check is defined in EIP-55
Contract case description contract case code 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;}} so far, I believe you have a deeper understanding of "Solidity address types". You might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 249
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.