智能合约编写04

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编译(当前使用)

访问http://localhost:8080或者官方地址http://remix.ethereum.org/

将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)

最后更新于