Solidity学习笔记
基本类型
- uint
- bool
- address
- string
修饰符 modifier
- public
- private
- view
- pure
自定义modifier
// creator 需事先定义并初始化
modifier onlyCreator{
require (msg.sender == creator)
_;
}
mapping
mapping (address => uint256) public balances;
mapping (address => AddrBean) public userInfo;
mapping (address => bool) public userRegistered;
其中AddrBean
是自定义的结构体struct
结构体struct
struct AddrBean{
address parent_user;
address grand_user;
}
函数
function myfunc(address addr) public view returns(unit256 amount){
}
合约接收Ether
function () external payable{
balances[msg.sender] += msg.value;
}
合约发送Ether
//这里带余额检验,其中balance是合约内存储的地址的余额
function withdraw(uint256 amount) public payable{
require(amount > 0 && balances[msg.sender] >= amount);
msg.sender.transfer(amount);
balances[msg.sender] -= amount;
}