罗韭菜的solidity学习笔记与踩过的坑

罗韭菜的solidity学习(五)地址Address

2018-04-09  本文已影响0人  罗雪Melody

基本常识

合约相关

pragma solidity ^0.4.4;


contract Test {
   
   address public _owner;
   
   uint public _number;

   function Test() {
       _owner = msg.sender;
       _number = 100;
   }
   
   function msgSenderAddress() constant returns (address) {
       return msg.sender;
   }
   
   function setNumberAdd1() {
       _number = _number + 5;
   }
   
   function setNumberAdd2() {
       if (_owner == msg.sender) {
           _number = _number + 10;
       }
   }
   
}

// 0xca35b7d915458ef540ade6068dfe2f44e8fa733c

合约地址支持的运算符

<= < == != >= >

成员变量和函数

1. balance

pragma solidity ^0.4.4;

contract addressBalance{
    
    function getBalance(address addr) constant returns (uint){
        return addr.balance;
    }
    // this is current contract address
    function getCurrentAddressBalance()constant returns(uint){
        return this.balance;
    }

}

合约地址也是合法的钱包地址,所以用.balance也可以查看该合约的余额
2. transfer

pragma solidity ^0.4.4;

contract PayableKeyword{ 
    
    
    // 从合约发起方向 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c 地址转入 msg.value 个以太币,单位是 wei
    function deposit() payable{
        
        address Account2 = 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c;
        Account2.transfer(msg.value);
    }
  
  
    // 读取 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c 地址的余额
    function getAccount2Balance() constant returns (uint) {
        
        address Account2 = 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c;

        return Account2.balance;
    }  
    
    // 读取合约发起方的余额
    function getOwnerBalance() constant returns (uint) {
        
        address Owner = msg.sender;
        return Owner.balance;
    } 
    
}

3.send方法转账

pragma solidity ^0.4.4;

contract PayableKeyword{ 
    
    
    function deposit() payable returns (bool){
        
        address Account2 = 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c;
        return Account2.send(msg.value);
    }
  
  
    function getAccount2Balance() constant returns (uint) {
        
        address Account2 = 0x14723a09acff6d2a60dcdf7aa4aff308fddc160c;

        return Account2.balance;
    }  
    
    
    function getOwnerBalance() constant returns (uint) {
        
        address Owner = msg.sender;
        return Owner.balance;
    } 
    
}

注意:

上一篇下一篇

猜你喜欢

热点阅读