contract Demo{
uint[] public array
function testDemo(uint a) returns (uint lenth){
array.push(a);
return array.length;
}
}
2. 数组的增删改查:
contract ArrayOperation{
uint[] public array;
//数组添加元素
function add(uint a) returns (uint result){
array.push(a);
}
//获取数组下标元素值
function valueOf(uint index) returns (uint value){
if(index >= array.length) throw;
return array[index];
}
//更新元素
function updata(uint index, uint value){
if(index >= array.length) throw;
array[index] = value;
}
//删除元素
function delete(uint index){
uint len = array.length
if(index >= len ) throw;
for(uint i = index; i < len - 1; i++){
array[index] = array[index + 1]
}
delete (array[len - 1]);
array.length --;
}
}
3. 简单模拟代币合约, 包含(转账, 挖矿, 构造):
pragma solidity ^0.4.0;
contract Token{
uint[] public balanceOf;
//初始化两个模拟账户
function Token(){
balanceOf.push(4500);
balanceOf.push(1000);
}
//两个账户进行转账
function transfer(uint _from, uint _to, uint amount){
if(_from >= balanceOf.length || _to >= balanceOf.length || _from == _to ) return;
if(balanceOf[_from] < amount) return;
balanceOf[_from] -= amount;
balanceOf[_to] += amount;
}
//挖矿,所有的收入都放入第一个账户中
function mint(uint amount){
balanceOf[0] += amount;
}
}
4. address, mapping 类型的使用:
* 1 mapping的使用, 一般键为地址, 值为余额 mapping(address => uint);
* 2 address : 以太坊账户地址;
* 3 address: Holds a 20 byte value (size of an Ethereum address). Address types also have members and serve as base for all contracts.
* 4 address 含有两个成员, balance 和 send;
5. 合约账户中 msg 的定义
* 1 msg 指谁调用该智能合约发来的信息。 注意:智能合约也是一个账户
* 2 msg 包含的成员:
* msg.data(bytes): 获取调用者传入的数据信息
* msg.gas(uint): 剩余的以太币
* msg.sender(address): 当前调用者的以太坊账户地址
* msg.sig(bytes4): first four bytes of cakkData
* msg.value(uint): number of wei sent with the message;注意: 只有当有以太坊币的转义时, 该变量才有值(除调用合约所花费以太币这种情况)
pragma solidity ^0.4.0;
contract Token{
mapping (address => uint) public balanceOf;
address public owner;
event Sent(address sender, address receiver, uint amount); //定义一个时间
//获取部署合约的账户地址, 并初始化该地址的余额
function Token(){
owner = msg.sender;
balanceOf[owner] = 10000;
}
//账户地址交易
function transfer(address _to, uint amount){
if(balanceOf[msg.sender] < amount) throw; //进行判断,防止发送账户的余额不足
if(balanceOf[_to] + amount < balanceOf[_to]) throw; //防止自己给自己转账,或递归调用(因为每次调用都有额外的花费)
balanceOf[_to] += amount;
balanceOf[msg.sender] -= amount;
Sent(msg.sender, _to, amount); ///调用事件
}
//挖矿
function mint(uint amount){
balanceOf[owner] += amount;
}
}
6. enum 类型
contract Demo{
enum ActionCode{ Left, Right, down}
ActionCode public enumType;
function Demo(){
enumType = ActionCode.Left;
}
function Left(){
enumType = ActionCode.Left;
}
function Right(){
enumType = ActionCode.Right;
}
function Down(){
enumType = ActionCode.down;
}
function f() returns(string str){
if(enumType == ActionCode.Left) return "Left";
if(enumType == ActionCode.Right) return "Right";
if(enumType == ActionCode.down) return "down";
}
}
7. struct 类型
contract DemoStruct{
struct Person{
uint age;
string name;
uint sexy;
string phone;
}
Person[] public PersonLists;
function DemoStruct(){
uint id = PersonLists.length++;
Person p = PersonLists[id];
p.age = 20;
p.sexy = 0;
p.name = "wukong";
p.phone = "1235432328";
}
function addPerson(uint age, uint sexy, string name, string phone){
uint id = PersonLists.length++;
Person p = PersonLists[id];
p.age = age;
p.sexy = sexy;
p.name = name;
p.phone = phone;
}
function addPersontwo(uint _age, uint _sexy, string _name, string _phone){
uint id = PersonLists.length++;
PersonLists[id] = Person({age: _age, name: _name, sexy: _sexy, phone: _phone});
}
}
8. 以太坊中的数据单位
* ether
* finney
* szabo
* wei
* 1 ether = 1 * 10^18 wei;
* 1 ether = 1 * 10^6 szabo;
* 1 ehter = 1* 10^3 finney;
9. 时间类型
* seconds
* minutes
* hours
* days
* weeks
* years
* now
10. mapping 类型