【2022 元宇宙基础NFT之Solidity OOP编程第13篇】3分钟了解Solidity Types – 地址(Address)
以太坊钱包地址位数验证
以太坊中的地址的长度为20
字节,一字节等于8位,一共160
位,所以address
其实亦可以用uint160
来声明。
我的以太坊钱包的地址为0xF055775eBD516e7419ae486C1d50C682d4170645
,0x
代表十六进制,我们将F055775eBD516e7419ae486C1d50C682d4170645
拷贝,如下图所示,将其进行二进制转换,不难发现,它的二进制刚好160
位。
备注:以太坊钱包地址是以16进制的形式呈现,我们知道一个十六进制的数字等于4
个字节,160 / 4 = 40
,你自己验证一下,钱包地址F055775eBD516e7419ae486C1d50C682d4170645
的长度为40。
我的以太坊钱包地址:
0xF055775eBD516e7419ae486C1d50C682d4170645
对应的二进制为:
0b1111 0000 0101 0101 0111 0111 0101 1110 1011 1101 .....0100 0101
PS:通过工具进行转换完的二进制有问题,所以自己去计算一下即可。
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract AddressTest{
address _owner;
uint160 _ownerUint;
constructor() {
_owner = 0xF055775eBD516e7419ae486C1d50C682d4170645;
_ownerUint = 1372063746939811866332913075223978746532890871365;
}
function owner() view public returns (address) {
return _owner; //0xF055775eBD516e7419ae486C1d50C682d4170645;
}
function ownerUint160() view public returns (uint160) {
return uint160(_owner);
// 1372063746939811866332913075223978746532890871365
}
function ownerUintToAddress() view public returns (address) {
return address(_ownerUint); //0xF055775eBD516e7419ae486C1d50C682d4170645;
}
function addressToOwnerUint() view public returns (uint160) {
return uint160(_owner); //1372063746939811866332913075223978746532890871365;
}
}
// 0x 45
// 0b 0100 0101
// 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128
// 1 + 0 + 4 + 0 + 0 + 0 + 64 + 0 = 69
// 1 2 3 4 5 6 7 8 9 A B C D E F
// 0xF055775eBD516e7419ae486C1d50C682d4170645
// 1372063746939811866332913075223978746532890871365
// 0b1111 0000 0101 0101 0111 0111 0101 1110 1011 1101 .....0100 0101
//
// 40 * 4 = 160
// uint160
// address
// uint160
由上不难看出address
和uint160
之间可以来回相互转换,其实address
本身就不是一个什么新的特殊类型,它其实就是uint160
的别名或者变体,相互等价。
不可不知的几个常识
msg.sender
是什么?
当前哪个地址身份在操作或者调用合约,谁就是当前的msg.sender
,我们来测试一下。
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Msg_Sender_Test{
function msgSenderAddress() view public returns (address) {
return msg.sender;
}
}
// 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
// 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4
// 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db
// 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db
- 合约地址
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ContractAddress{
function msgSenderAddress() view public returns (address) {
return msg.sender;
}
function returnContractAddress() view public returns (ContractAddress) {
return this;
}
}
msg.sender
等价于from
,而this
就是当前合约实例对象的地址,通过合约地址,可以调用合约对象的相关行为和方法。
支持的运算符
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ContractAddress{
address address1;
address address2;
// <=,<,==,!=,>=和>
constructor() {
address1 = 0xF055775eBD516e7419ae486C1d50C682d4170645;
address2 = 0xEAEC9B481c60e8cDc3cdF2D342082C349E5D6318;
}
// <=
function test1() view public returns (bool) {
return address1 <= address2; //0: bool: false
}
// <
function test2() view public returns (bool) {
return address1 < address2; //0: bool: false
}
// !=
function test3() view public returns (bool) {
return address1 != address2; //0: bool: true
}
// >=
function test4() view public returns (bool) {
return address1 >= address2; //0: bool: true
}
// >
function test5() view public returns (bool) {
return address1 > address2; //0: bool: true
}
}
成员变量(属性)和行为(函数)
一、 balance
如果我们需要查看一个地址的余额,我们可以使用balance
属性进行查看。
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract addressBalance{
function getBalance(address addr) view public returns (uint){
return addr.balance;
}
}
// 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db
// 99999999999996283915
// 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C
// uint256: 100000000000000000000
99999999999999587411 wei => 99.999999999999587411 ether
100000000000000000000 wei => 100 ether
二、this 查看当前合约地址余额
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract addressBalance{
function getThisBalance() view public returns (uint){
return address(this).balance; // 1. 直接通过this查看当前合约地址余额
}
function getContractAddrees() view public returns (addressBalance){
return this; //2. 查看当前合约地址
}
function getBalance(address addr) view public returns (uint){
return addr.balance; // 3. 传入合约地址查看合约地址的余额
}
}
三、transfer
温馨提示:此处内容已隐藏,您必须消耗1个积分后才能查看。 
或者

关注微信公众号: 程序咖元宇宙实验室
回复:程序咖巴士 ,获取验证码。
验证码:
验证码: