Smart_contracts
Table of Contents

Smart Contracts

Hello World 代码

pragma solidity ^0.4.21; // 程序版本

contract SimpleStorage {       // 合约声明
    uint storedData;    // 状态变量

    function set(uint x) {      // 合约的方法
        storedData = x;
    }

    function get() constant returns (uint){     
        return storedData;
    }
}

合约方法

constant(视觉上的警示) -> view

pure

remix 在线编辑器

eth gas

solidity

bool

uint (无符号整型) / int

地址address

成员变量函数

ETHER 单位

pragma solidity ^0.4.14;

contract Payroll {
    uint salary = 1 ether;
    address frank = 0x7eC9938C53e6DE7B4B502B5e6fc388084b934577;
    uint payDuration = 30 days;
    uint lastPayday = now;

    function test() returns (bool) {
        return 1 wei == 1;
    }

}
>>>
to:Payroll.test() 0xdc0...46222value:0 weidata:0xf8a...8fd6dlogs:0hash:0x487...e0f91
Debug
 status     0x1 Transaction mined and execution succeed
 from   0xca35b7d915458ef540ade6068dfe2f44e8fa733c
 to     Payroll.test() 0xdc04977a2078c8ffdf086d618d1f961b6c546222
 gas    3000000 gas

 transaction cost   21492 gas 
 execution cost     220 gas 
 input  0xf8a8fd6d
 decoded input  {}
 decoded output     {
    "0": "bool: true"
}
 logs   []
 value  0 wei

block 块

消息

revert 返回

数组

固定长度(uint[5] a)

pragma solidity ^0.4.14;

contract Test {
    uint[2] a;

    function test() returns (uint, uint) {
        a[0] = 1;
        a[1] = 2;

        return (a[0], a[1]);
    }
}

动态长度(uint[] a)

pragma solidity ^0.4.14;

contract Test {
    uint[] a;

    function test() returns (uint, uint) {
        a.push(1);
        a.push(2);

        a[1] = 3;

        return (a[0], a[1]);
    }
}

struct

struct Employee {
    address id;
    uint salary;
    uint lastPayday;
}

循环

for

while

data location

强制

默认

MAPPING

mapping 底层实现

可视度

继承

设计 design

alt

单人设计

code

pragma solidity ^0.4.14;

contract Payroll {
    uint constant payDuration =  10 seconds;

    address owner;
    uint salary;
    address employee;
    uint lastPayday;

    function Payroll() {
        owner = msg.sender;
    }

    function updateEmployee(address e, uint s) {
        require(msg.sender == owner);

        if (employee != 0x0) {
            uint payment = salary * (now - lastPayday) / payDuration;
            employee.transfer(payment);
        }

        employee = e;
        salary = s * 1 ether;
        lastPayday = now;
    }

    function addFund() payable returns (uint) {
        return this.balance;
    }

    function calculateRunway() returns (uint) {
        return this.balance / salary;
    }

    function hasEnoughFund() returns (bool) {
        return calculateRunway() > 0;
    }

    function getPaid() {
        require(msg.sender == employee);

        uint nextPayday = lastPayday + payDuration;
        assert(nextPayday < now);
        lastPayday = nextPayday;
        employee.transfer(salary);

    }

}

错误检测

assert(bool)

require(bool)

Truffle

truffle box 开发前端应用

测试

nvm install v8
nvm alias default v8
npm install --dotenv-extended
truffle migrate --reset && truffle compile && npm start

metamask

import account 是不受keystone保护,即重装或换电脑回丢失

ganache-cli

web3.js

web3 调用合约方程

合约部署

合约安全

外部函数调用安全性

函数可见性

数学运算

随机数与系统时间的依赖性

合约数据可见性

谨慎使用汇编注入