官术网_书友最值得收藏!

Addresses

In Solidity 0.4.0, there was only one type of address. Now, we have two types to define whether an Ethereum address should be payable or not.

An address contains the account number of each user in Ethereum. It's a 42-character piece of hexadecimal text, such as this one:

0xeF5781A2c04113e29bE5724ae6E30bC287610007

To create an address variable in your contract, you have to define it as follows:

pragma solidity 0.5.0;
contract Example {
address public myAddress = 0xeF5781A2c04113e29bE5724ae6E30bC287610007;
}

Addresses don't have quotes because they are not strings of text. In this version of Solidity, you must define the type of address, which could be one of the following:

  • Address payable: A payable address is a new type of variable introduced in Solidity 0.5 that allows the address to receive and store Ether inside. Previously, all addresses were payable, now only those explicitly marked as payable will be able to receive or send Ether and use functions that deal with Ether, such as .transfer() or .send().
  • Address: A normal address that can't receive or send Ether to prevent users from doing so.

You define the payable addresses as follows:

address payable public myAddress;

It's useful to have payable addresses when you want to send Ether to that address. For instance, let's say that user A wants to receive 10 ether from the balance stored in the smart contract. They would do something like the following:

pragma solidity 0.5.0;
contract TransferExample {
address payable public userAAddress;
function transferFunds() public {
userAAddress.transfer(10 ether);
}
}

So, user A would receive 10 ether from the funds stored in this smart contract.

Another important aspect of the addresses is that you sometimes need to access the address of the current smart contract, because, as you know, smart contracts can hold Ether inside.

To get the address of your smart contract, use the following code:

address public myContractAddress = address(this);

Here, this is the special keyword used to reference the active smart contract being used at that moment. But because it is an instance of a smart contract, you need to convert that instance to an address with the type conversion function, which essentially gets the address of this smart contract.

You can also access the balance of this smart contract with the .balance function as follows:

uint256 public myContractBalance = address(this).balance;

That will return the number of wei in the smart contract, useful for making transfers with the transfer() function:

myUserAddress.transfer(address(this).balance);

That will send myUserAddress all the Ether stored inside this contract.

You can convert payable addresses to normal addresses, but not the other way around, based on the fact that payable addresses are an augmented version with additional functions that can't be passed easily.

主站蜘蛛池模板: 临西县| 焉耆| 比如县| 诸暨市| 平泉县| 绍兴县| 勐海县| 邳州市| 曲水县| 河南省| 武强县| 富阳市| 普宁市| 育儿| 分宜县| 宜宾市| 广河县| 康平县| 廊坊市| 连云港市| 亚东县| 太原市| 陵川县| 东乌珠穆沁旗| 合江县| 昭通市| 西乡县| 平谷区| 麻阳| 大兴区| 渭源县| 民乐县| 新津县| 盐山县| 泽普县| 临夏市| 礼泉县| 吉首市| 静乐县| 西宁市| 重庆市|