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); } } 

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); } }

  1. Note that the hex type cannot be converted directly to address.
  2. 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; } }

  1. 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)
  2. The constructor constructor is executed when the contract is deployed and is executed only once.
  3. 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.
  4. 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:

  1. 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.
  1. 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.
  1. 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!

Share:

Was this article helpful?

93 out of 132 found this helpful

Discover more

Blockchain

Research Report | Exchange Industry 2019 Q1 Report

In the first quarter of 2019, the digital clearing exchange industry changed dramatically. After a long bear market i...

Blockchain

"New and old" exchanges compete on the same stage, how can you play in the future? | Interview with SheKnows

Exchanges are an important part of the blockchain ecosystem. They interact directly with users and therefore change a...

Blockchain

Latest updates on regulatory events: CZ releases internal memo, Gensler criticizes two exchanges again.

According to Gensler, his agency has obtained internal communications that allegedly indicate intentional illegal beh...

Blockchain

Is the 'big boss' of the cryptocurrency world, Binance, starting to decline?

LianGuaiBitpushNews Mary Liu As 2022 comes to a close, it seems that Zhao Changpeng, co-founder and CEO of Binance, ...

Market

Encryption exchange "moving tide": US SEC "strongly pushed away", Middle East and Hong Kong "welcoming with a smile"

Due to the recent pressure from the SEC, several major exchanges around the world are preparing to flee, with the UAE...

Blockchain

Read the article Bakkt: cryptocurrency of the New York Stock Exchange

At the Bakkt Digital Assets Summit held last week at the New York Stock Exchange, more than 150 investors around the ...