neo blockchain book
  • NEOHOPE's Blockchain Book
  • 概述
    • 本书内容
    • 读者定位
    • 本书约定
    • 项目对比
    • 名词释义
  • 基本原理
    • 区块链
    • 智能合约
    • 交易所
    • 系统架构
  • 基础算法
    • 默克尔树
    • Bloom过滤器
    • 公钥私钥及椭圆曲线
  • 共识算法
    • 分布式系统
    • Paxos与Raft算法
    • 拜占庭容错算法
  • Bitcoin
    • 整体架构
    • 状态转换
    • 区块确认
    • 分叉处理
    • P2P
    • 脚本
    • 钱包
    • 使用
    • 编译(Ubuntu)
    • 私链搭建(Linux)
    • 私链搭建(Docker)
  • Ethereum
    • 整体架构
    • 状态转换
    • 区块确认
    • 使用
    • Remix IDE环境搭建(Ubuntu16)
    • 智能合约编写01
    • 智能合约编写02
    • 智能合约编写03
    • 智能合约编写04
    • 编译(MacOS)
    • 私链搭建
  • EOS
    • 整体架构
    • 使用
    • 智能合约使用(单机单节点单钱包)
    • 智能合约使用(单机单节点多钱包)
    • 编写简单的智能合约
    • 编写有数据存储的智能合约
    • 编译(Linux)
    • 私链搭建(单机多节点)
    • 私链搭建(多机多节点)
  • Fabric
    • 整体架构
    • 身份管理
    • 共识达成
    • 使用
    • 智能合约介绍
    • 智能合约使用01
    • 智能合约使用02
    • 编译(MacOS)
    • 私链搭建(Ubuntu)
    • 在私链中添加一个机构
  • Cosmos
    • 整体架构
    • Zone与Hub
    • 使用
  • IPFS
    • 整体架构
    • 使用
    • 编译(MacOS)
    • 私链搭建
  • 然后呢?
由 GitBook 提供支持
在本页
  • MyRating.sol
  • 编译合约
  • 部署合约
  1. Ethereum

智能合约编写04

上一页智能合约编写03下一页编译(MacOS)

最后更新于7年前

MyRating.sol

pragma solidity ^0.4.22;

contract MyRating {

  /*存储特定商品的分数*/
  mapping (bytes32 => uint256) public ratings; 
  
  function setRating( bytes32 _key, uint256 _value) public { 
    /*为特定编号的商品打分*/
    ratings[_key] = _value; 
  } 
}

编译合约

编译方法有两种,一种是命令行编译(签名有介绍),一种是用Remid IDE编译(当前使用)

访问或者官方地址

将MyRating.sol中内容拷贝到编辑器

点击Compile

编译成功后,点击detials,拷贝web3部署一节,并按自己的需要进行修改

var myratingContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"_key","type":"bytes32"},{"name":"_value","type":"uint256"}],"name":"setRating","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"ratings","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]);

var myrating = myratingContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x608060405234801561001057600080fd5b50610138806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806350b7b7a2146100515780636c71d01d1461008c575b600080fd5b34801561005d57600080fd5b5061008a6004803603810190808035600019169060200190929190803590602001909291905050506100d1565b005b34801561009857600080fd5b506100bb60048036038101908080356000191690602001909291905050506100f4565b6040518082815260200191505060405180910390f35b806000808460001916600019168152602001908152602001600020819055505050565b600060205280600052604060002060009150905054815600a165627a7a723058206415d05464d1e60986e0680c491ee824cdf7f655411fc3d7c1b6149b429bdfcc0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 });

部署合约

#在BC01开启geth
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

#在BC01解锁账号
personal.unlockAccount('c230847d2669091b77053df0cbc7f533a0a4cf1c','patient1',3600)

#在BC01部署合约
var myRatingAbi01 = JSON.parse('[{"constant":false,"inputs":[{"name":"_key","type":"bytes32"},{"name":"_value","type":"uint256"}],"name":"setRating","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"bytes32"}],"name":"ratings","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}]');
var myRatingFactory01 = eth.contract(myRatingAbi01);   
var myRatingBin01 = '0x608060405234801561001057600080fd5b50610138806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806350b7b7a2146100515780636c71d01d1461008c575b600080fd5b34801561005d57600080fd5b5061008a6004803603810190808035600019169060200190929190803590602001909291905050506100d1565b005b34801561009857600080fd5b506100bb60048036038101908080356000191690602001909291905050506100f4565b6040518082815260200191505060405180910390f35b806000808460001916600019168152602001908152602001600020819055505050565b600060205280600052604060002060009150905054815600a165627a7a723058206415d05464d1e60986e0680c491ee824cdf7f655411fc3d7c1b6149b429bdfcc0029';

var myrating01 = myRatingFactory01.new(
   {
     from: eth.accounts[0], 
     data: myRatingBin01, 
     gas: '100000000'
   }, function (e, contract){
    if(e) {
      console.error(e);
      return;
    }

    if(!contract.address) {
      console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");

    } else {
      console.log("Contract mined! Address: " + contract.address);
      console.log(contract);
    }
 })

#部署结果,一定要记录合约地址

#等待挖坑

#进行评价
myrating01.setRating.sendTransaction(1, 3, {from: eth.accounts[0]})

#等待挖坑

#查看评价结果
myrating01.ratings(1)
http://localhost:8080
http://remix.ethereum.org/