Solidity 地址类型,balance余额,sender调用者,this合约地址,transfer、send转账

 

demo.sol(地址类型与整型转换):

pragma solidity ^0.4.20;


contract AddressTest {
    address _add1 = 0x4b0897b0513fdc7c541b6d9d7e929c4e5364d2db;  // 地址类型
    address _add2 = 0x583031d1113ad414f02576bd6afabfb302140225;
    uint160 public _num = 0;
    address public _add;

    function address2uint160() public returns(uint160){
        _num = uint160(_add1);   // 地址类型->整型类型
        return _num;
    }
    
    
    function uint160ToAddress() public returns(address){
        _add = address(_num);   // 整型类型->地址类型
        return _add;
    }
    
    function comAddress() constant public returns(bool){
        return _add1 == _add2;
    }
    
    function isGreater() constant public returns(bool){
        return _add1 > _add2;
    }
}

demo.sol(balance属性,获取地址余额):

pragma solidity ^0.4.20;

contract addressBalance {

    function getBalance(address addr) constant public returns (uint){
        return addr.balance;   // balance属性(余额)
    }
}

demo.sol(sender属性,获取消息发起人地址):

pragma solidity ^0.4.20;


contract senderTest {
    address public _owner;
    
    constructor() public {
        _owner = msg.sender;
    }
    
    function getInvoker() constant public returns (address){
        return msg.sender;  // sender 获取部署合约或调用合约的用户地址
    }
    
    function getOwnerBalance() constant public returns (uint256){
        return msg.sender.balance;
    }
    
}

demo.sol(this,合约地址):

pragma solidity ^0.4.20;


contract addressThis {
    
    uint _money;
    
    // 构造函数
    constructor() payable public {  // payable 表示合约地址可以接收转账
        _money = msg.value;  // 获取转给合约地址的金额
    }
    
    function getThis() constant public returns (address){
        return this;   // this 指向合约地址的指针(合约本身)
    }
    
    function getBalance () constant public returns (uint256){
        return this.balance;  // 获取合约地址的余额
    }
    
    function getMoney() constant public returns(uint){
        return _money;
    }
    
}

demo.sol(transfer,转账):

pragma solidity ^0.4.20;


contract TranferTest {
    
    address AliceAddress = 0xa446742e4845CFbA8EC7d93226eE6ceB6C7C91ab;
    
    function transfer() payable public {
        AliceAddress.transfer(msg.value);  // transfer 转账。 调用者转给AliceAddress
    }
}

demo.sol(send,转账):

pragma solidity ^0.4.20;


contract TranferTest {
    
    address AliceAddress = 0xa446742e4845CFbA8EC7d93226eE6ceB6C7C91ab;
    bool res;
    
    function transfer() payable public returns (bool){
        res = AliceAddress.send(msg.value);   // 转账(类似transfer),返回bool类型。(转给AliceAddress)
        return res;
    }
}

Solidity 地址类型,balance余额,sender调用者,this合约地址,transfer、send转账_第1张图片

Solidity 地址类型,balance余额,sender调用者,this合约地址,transfer、send转账_第2张图片

Solidity 地址类型,balance余额,sender调用者,this合约地址,transfer、send转账_第3张图片

 

 

你可能感兴趣的:(Solidity)