智能合约示例

一个简单的 TodoList 的合约示例:

pragma solidity ^0.4.25;

contract TodoList {
  uint public taskCount = 0;

  // 定义待办事项
  struct Task {
    uint id;
    string content;
    bool completed;
  }

  mapping(uint => Task) public tasks;

  event TaskCreated(
    uint id,
    string content,
    bool completed
  );

  event TaskCompleted(
    uint id,
    bool completed
  );

  // 定义构造函数
  constructor() public {
    createTask("Check out sample");
  }

  // 新建任务
  function createTask(string memory _content) public {
    taskCount ++;
    tasks[taskCount] = Task(taskCount, _content, false);
    emit TaskCreated(taskCount, _content, false);
  }

  // 勾选已完成的任务
  function toggleCompleted(uint _id) public {
    Task memory _task = tasks[_id];
    _task.completed = !_task.completed;
    tasks[_id] = _task;
    emit TaskCompleted(_id, _task.completed);
  }
}

通过编译器编译后得到 Bytecode

60806040526000805534801561001457600080fd5b506100596040805190810160405280601081526020017f436865636b206f75742073616d706c650000000000000000000000000000000081525061005e60201b60201c565b610249565b600080815480929190600101919050555060606040519081016040528060005481526020018281526020016000151581525060016000805481526020019081526020016000206000820151816000015560208201518160010190805190602001906100ca9291906101a4565b5060408201518160020160006101000a81548160ff0219169083151502179055509050507f05d0fb833127fc08168556d0e7ca9554fc3f6bc843b3b7d2bf1c35aea6bab660600054826000604051808481526020018060200183151515158152602001828103825284818151815260200191508051906020019080838360005b8381101561016557808201518184015260208101905061014a565b50505050905090810190601f1680156101925780820380516001836020036101000a031916815260200191505b5094505050505060405180910390a150565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106101e557805160ff1916838001178555610213565b82800160010185558215610213579182015b828111156102125782518255916020019190600101906101f7565b5b5090506102209190610224565b5090565b61024691905b8082111561024257600081600090555060010161022a565b5090565b90565b610681806102586000396000f3006080604052600436106100455760003560e01c63ffffffff168063111002aa1461004a578063455f5024146100b35780638d977672146100e0578063b6cb58a514610198575b600080fd5b34801561005657600080fd5b506100b1600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101c3565b005b3480156100bf57600080fd5b506100de60048036038101908080359060200190929190505050610309565b005b3480156100ec57600080fd5b5061010b600480360381019080803590602001909291905050506104b7565b604051808481526020018060200183151515158152602001828103825284818151815260200191508051906020019080838360005b8381101561015b578082015181840152602081019050610140565b50505050905090810190601f1680156101885780820380516001836020036101000a031916815260200191505b5094505050505060405180910390f35b3480156101a457600080fd5b506101ad610586565b6040518082815260200191505060405180910390f35b6000808154809291906001019190505550606060405190810160405280600054815260200182815260200160001515815250600160008054815260200190815260200160002060008201518160000155602082015181600101908051906020019061022f92919061058c565b5060408201518160020160006101000a81548160ff0219169083151502179055509050507f05d0fb833127fc08168556d0e7ca9554fc3f6bc843b3b7d2bf1c35aea6bab660600054826000604051808481526020018060200183151515158152602001828103825284818151815260200191508051906020019080838360005b838110156102ca5780820151818401526020810190506102af565b50505050905090810190601f1680156102f75780820380516001836020036101000a031916815260200191505b5094505050505060405180910390a150565b61031161060c565b600160008381526020019081526020016000206060604051908101604052908160008201548152602001600182018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103d35780601f106103a8576101008083540402835291602001916103d3565b820191906000526020600020905b8154815290600101906020018083116103b657829003601f168201915b505050505081526020016002820160009054906101000a900460ff1615151515815250509050806040015115816040019015159081151581525050806001600084815260200190815260200160002060008201518160000155602082015181600101908051906020019061044892919061058c565b5060408201518160020160006101000a81548160ff0219169083151502179055509050507fe21fa966ca5cd02748c0752352d18c48165e61cb55b4c29cccf924b5a95fcff182826040015160405180838152602001821515151581526020019250505060405180910390a15050565b6001602052806000526040600020600091509050806000015490806001018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156105695780601f1061053e57610100808354040283529160200191610569565b820191906000526020600020905b81548152906001019060200180831161054c57829003601f168201915b5050505050908060020160009054906101000a900460ff16905083565b60005481565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106105cd57805160ff19168380011785556105fb565b828001600101855582156105fb579182015b828111156105fa5782518255916020019190600101906105df565b5b5090506106089190610630565b5090565b60606040519081016040528060008152602001606081526020016000151581525090565b61065291905b8082111561064e576000816000905550600101610636565b5090565b905600a165627a7a72305820bb245c01d0ac150cfe32fb116118f0cec3c3217e7082fc0d052907f7cf28818c0029

接下来就可以结合 DApp 调用合约中的方法

// 获取合约的 abi
var abi = [{"constant":false,"inputs":[{"name":"_content","type":"string"}],"name":"createTask","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_id","type":"uint256"}],"name":"toggleCompleted","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"tasks","outputs":[{"name":"id","type":"uint256"},{"name":"content","type":"string"},{"name":"completed","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"taskCount","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"content","type":"string"},{"indexed":false,"name":"completed","type":"bool"}],"name":"TaskCreated","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"id","type":"uint256"},{"indexed":false,"name":"completed","type":"bool"}],"name":"TaskCompleted","type":"event"}];

// 获取合约示例
var contractInstance = youchain.you.Contract(abi, '<--部署后的合约地址-->');

// 调用合约方法
contractInstance.methods.toggleCompleted(0).send({from: myAccount}, (error, result) => {
    console.log(result);
});