Technical Teaching | Solidity Programming Language: Address
address
The address to be mentioned here is not the memory address in the usual programming language, but the transaction address. We know that the blockchain must pass the value to another address through an address, regardless of the transaction or contract. Therefore, it is very convenient to develop the embedded data type with Address as the Solidity language. More importantly, in Solidity, all contracts inherit the address Type. This is not just for grammatical implementation, but in reality the contract itself is inseparable from the address, including the deployment and execution of the contract requires the participation of the address. Addresses are of two types, one is address and the other is address payable. The address payable has two member methods send and transfer compared to the address type, indicating that the transfer operation can be performed on the variable of the address payable type.
Address type format requirements
The address in Ethereum is 20 bytes, such as 0x52908400098527886E0F7030069857D2E4169EE7 . Since one byte is equal to 8 bits, the address can also be declared using uint160. Addresses can usually be compared.
pragma solidity >=0.4.0 <0.6.0; contract EgAddress{ function testType() public { address _addr = 0x52908400098527886E0F7030069857D2E4169EE7; uint160 _addi = 471360049350540642640628028220064440840608208820; string memory h = hex"52908400098527886E0F7030069857D2E4169EE7"; address addr = address(_addi); //addr = address(h); uint160 addi = uint160(_addr); } }
- Proof of validity and proof of error in Science-2 |
- Economist Ba Shusong: The Application Value of Blockchain Technology in the Financial Field
- The volume is falling, is it a dishwashing or a shipment?
pragma solidity >=0.4.0 <0.6.0; contract EgAddress{ function testType() public { address _addr = 0x52908400098527886E0F7030069857D2E4169EE7; uint160 _addi = 471360049350540642640628028220064440840608208820; string memory h = hex"52908400098527886E0F7030069857D2E4169EE7"; address addr = address(_addi); //addr = address(h); uint160 addi = uint160(_addr); } }
pragma solidity >=0.4.0 <0.6.0; contract EgAddress{ function testType() public { address _addr = 0x52908400098527886E0F7030069857D2E4169EE7; uint160 _addi = 471360049350540642640628028220064440840608208820; string memory h = hex"52908400098527886E0F7030069857D2E4169EE7"; address addr = address(_addi); //addr = address(h); uint160 addi = uint160(_addr); } }
pragma solidity >=0.4.0 <0.6.0; contract EgAddress{ function testType() public { address _addr = 0x52908400098527886E0F7030069857D2E4169EE7; uint160 _addi = 471360049350540642640628028220064440840608208820; string memory h = hex"52908400098527886E0F7030069857D2E4169EE7"; address addr = address(_addi); //addr = address(h); uint160 addi = uint160(_addr); } }
pragma solidity >=0.4.0 <0.6.0; contract EgAddress{ function testType() public { address _addr = 0x52908400098527886E0F7030069857D2E4169EE7; uint160 _addi = 471360049350540642640628028220064440840608208820; string memory h = hex"52908400098527886E0F7030069857D2E4169EE7"; address addr = address(_addi); //addr = address(h); uint160 addi = uint160(_addr); } }
- Note that the hex type cannot be converted directly to address.
- Although address is a hex string, address is formatted, the test method is to refer to the EIP55 contract address, we know that each contract is deployed through an account to the block, and this contract will also generate An address and a transfer operation to this contract address. So how do you get the account address and contract address for this deployment?
Pragma solidity >=0.4.0 <0.6.0;
Contract EgAddress{ address public owner;
Constructor() public { owner = msg.sender; }
Function getOwner() public returns(address){ return owner; }
Function getContractAddr() public view returns(address){ return address(this); }
Function getSenderAddr() public returns (address){ return msg.sender; } }
- Msg.sender is a built-in method, he returns the caller address of the current method, that is, I call the contract method getSenderAddr through the A account, then return the address of A, call the contract method getSenderAddr through the B account, then return B address. (In addition, msg.value and msg.data are data passed in by the caller)
- The constructor constructor is executed when the contract is deployed and is executed only once.
- The creator of the acquisition contract is obtained by "curve". First, declare the state variable owner, and then assign the address of the method caller to the owner in the constructor, because the constructor will only be called when the contract is created. , so the owner saves the address of the contract creator, and finally returns the value of the state variable through the public method getOwner.
- The address of the contract itself is obtained by this. The old version may be the address type itself. You can directly return this, but the version in this example needs to be this type EgAddress. You need to do a type conversion to return the address. Types of. The member variable balance each address represents a value holder, so you can directly view the balance, that is, through the balance attribute member method in addition to the member variable balance, there are the following member methods.
Member Method | | – | | <address>.balance (uint256) | | <address payable>.transfer(uint256 amount) | | <address payable>.send(uint256 amount) returns (bool) | | <address>.call (bytes memory) returns (bool, bytes memory) | | <address>.delegatecall(bytes memory) returns (bool, bytes memory) | | <address>.staticcall(bytes memory) returns (bool, bytes memory) |
example:
pragma solidity >=0.4.0 <0.6.0;
Contract EgAddress{
Function testSend()payable public { address payable to = 0x52908400098527886E0F7030069857D2E4169EE7; to.send(msg.value);
Function testTranser()payable public { address payable to = 0x52908400098527886E0F7030069857D2E4169EE7; to.transfer(msg.value);
Function testStack1024(uint depth) payable public{ address payable to = 0x52908400098527886E0F7030069857D2E4169EE7; bool r = to.send(1 wei); if (depth >1 && r){ testStack1024(depth -1); } }
/*function testCSend() payable public { address payable to = address(this); this.send(msg.value); address(this).send(msg.value); }*/
/*function testCTranser()payable public { // address payable to = 0x52908400098527886E0F7030069857D2E4169EE7; address(this).transfer(msg.value); }*/ }
method:
- Send
- The send method is more "lower" than the transfer method. If the send method fails, it will not throw an exception, but will return false.
- The send call stack depth cannot exceed 1024, otherwise the execution will fail. This problem was verified in the testStack1024 method, but if the incoming number is too large, it will fail to execute, even if it is not 1024, the reason has not been found.
- The send method returns a bool type result to indicate the execution result.
- If the gas is not enough, the execution will fail.
- It is recommended to use the transfer method, which is relatively safer.
- Transfer
- Transfer and send are used in the same way, and are also used for transfer operations. If the current account balance is insufficient or the other account refuses to transfer, the execution will fail.
- If the call address of transfer is a contract address, the contract's callback function will be executed.
- Close call and delegatecall, staticcall use a single frame.
Author: thank HPB Blue Lotus team finishing feeds.
Note: If you have any questions, please leave a message below to contact our technical community.
Wang Xiaoming blog: http://wangxiaoming.com/
Wang Xiaoming: Founder of HPB core chain, Babbitt columnist. More than ten years of experience in financial big data and blockchain technology development, he has participated in the creation of UnionPay big data. The main creative blockchain teaching video program "Ming Shuo" more than 30 issues, compiled the "Ethernet official website document Chinese version", and as the main author wrote the "blockchain development guide", in the Chinese blockchain community with ID "blue lotus "famous.
We will continue to update Blocking; if you have any questions or suggestions, please contact us!
Was this article helpful?
93 out of 132 found this helpful
Related articles
- Bitcoin becomes a sovereign currency? There are three obstacles to overcome
- Blockchain and healthcare: By 2025, the value of the blockchain in the medical market will exceed $1.6 billion
- The technical path of WEB3: from economic logic to the development of blockchain technology architecture
- Analysis | How will fragmentation technology achieve blockchain expansion?
- Heavy Coinbase plans to launch the IEO platform, while waiting for the opportunity to launch STO products
- Order Pizza Win Bitcoin Award, Domino's plan gives 100,000 Euro BTC
- Futures Exchange Industry 2019 Phase II Research Report