pragma solidity ^0.4.21; // 程序版本 contract SimpleStorage { // 合约声明 uint storedData; // 状态变量 function set(uint x) { // 合约的方法 storedData = x; } function get() constant returns (uint){ return storedData; } }
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
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]); } }
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 Employee { address id; uint salary; uint lastPayday; }
storage 永久的,区块链上的
memory EVM临时空间,function运行完成后抹除
calldata 类似memory
private 只有当前合约可见
状态变量: public, internal, private
Private 不代表别的无法肉眼看到,只代表别的区块链智能合约无法看到
合约的所有成员变量都是肉眼可见的
函数public,external,internal,private
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); } }
nvm install v8 nvm alias default v8 npm install --dotenv-extended truffle migrate --reset && truffle compile && npm start
import account 是不受keystone保护,即重装或换电脑回丢失