智能合约中的地址

  1. 合约中的地址等同于uint160类型
  2. 合约当中的地址可以被比较
    pragma solidity ^0.4.0;contract addressTest{address public account1 = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; address public account2 = 0xd9145CCE52D386f254917e481eB44e9943F39138; //0x5B38Da6a701c568545dCfcB03FcB875f56beddC4 uint160//0xd9145CCE52D386f254917e481eB44e9943F39138 合约地址// 520786028573371803640530888255888666801131675076function changeIt() view public returns(uint160){return uint160(account1);}function changeIt2() view public returns(address){return address(520786028573371803640530888255888666801131675076);}function check1()view public returns (bool){return account1>=account2;}function check2()view public returns (bool){return account1

地址中余额的获取与转账操作

  1. 那新建一个地址类型时可以通过address.balance 来获取当前地址的余额
  2. 若一个函数中需要进行转账操作需要在函数中加上payable关键字
  3. this指向的是当前合约的地址
  4. 若对当前的合约进行this.transfer(msg.value)操作需要加一个回滚操作
  5. 对其他账户的转账需要使用账户名称.transfer(msg.value)或者账户名称.transfer(数量 单位,如1 ether)如果在输入value的同时又写的是数量 单位,那么多余的value就自动转账到合约地址里去
  6. 若一个函数有payable关键字没有地址的话会默认转账到当前合约地址中去
    pragma solidity ^0.4.0;contract payAbleTest{//payable关键字代表我们可以通过这个函数给我们的合约地址充值 转账 默认function pay() payable{}//获取账户上的金额function getBalance() view returns(uint){return this.balance;}//0x7b96aF9Bd211cBf6BA5b0dd53aa61Dc5806b6AcE//this 指向合约地址function getThis() view returns(address){return this;}function getrandomBalance(address account) view returns(uint){ return account.balance;}//如果这个函数没有任何操作 但是有payable属性那么msg.value转账给合约地址function transfer() payable{address account = 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db;account.transfer(msg.value);}//往合约地址转账需要一个回滚地址function transfer2() payable{ this.transfer(msg.value);}function () payable{}}pragma solidity ^0.4.0;contract payAbleTest{//payable关键字代表我们可以通过这个函数给我们的合约地址充值 转账 默认function pay() payable{}//获取账户上的金额function getBalance() view returns(uint){return this.balance;}//0x7b96aF9Bd211cBf6BA5b0dd53aa61Dc5806b6AcE//this 指向合约地址function getThis() view returns(address){return this;}function getrandomBalance(address account) view returns(uint){ return account.balance;}//如果这个函数没有任何操作 但是有payable属性那么msg.value转账给合约地址function transfer() payable{address account = 0x4B20993Bc481177ec7E8f571ceCaE8A9e22C02db;account.transfer(msg.value);}//往合约地址转账需要一个回滚地址function transfer2() payable{ this.transfer(msg.value);}function () payable{}function sendMoney() payable{address account = 0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2;account.send(10 ether);}}

solidity中的全局变量

全局变量https://blog.csdn.net/qq_33829547/article/details/80419296常用的全局变量

pragma solidity ^0.4.0;contract grobal{//全局变量function getgrobal() view public returns(address) {//合约的调用者地址return msg.sender;}//返回当前块的困难度function getgrobal2() view public returns(uint) {return block.difficulty;}//当前区块的块号function getgrobal3() view public returns(uint) {return block.number;}function getgrobal4() view public returns(address) {return block.coinbase;}}

mapping映射类型

定义映射类型

mapping (类型1=>类型2) 映射名称;

pragma solidity ^0.4.0;contract mappingTest{//账户ID mapping(address =>uint) idMapping; mapping(uint => string) nameMapping; uint public sum= 0; function register(string name){ //获取到当前合约的调用者 address account = msg.sender; sum++; //将合约的调用者的地址与注册总量id联系在一起idMapping[account]=sum;//将用户id与用户注册的名字绑定在一起 nameMapping[sum] =name; } //通过地址获取到id function getIdByAddress(address are) view public returns(uint) { return idMapping[are]; } //通过id获取到name function getNameById(uint id) view public returns(string) { return nameMapping[id]; }}