智能合约编写03

MyToken.sol

pragma solidity ^0.4.22;

contract MyToken {

//pragma solidaty

/*在合约中使用public关键字定义所有能被别的合约访问的变量*/ 
string public name; 
string public symbol; 
uint8 public decimals;

/*建立一个数组存储账户的余额*/
mapping (address => uint256) public balanceOf; 

/*建立一个公共事件通知*/ 
event Transfer( address indexed from, address indexed to, uint256 value); 

/*构造函数*/ 
constructor( uint256 _supply, string _name, string _symbol, uint8 _decimals) public { 

  /*默认将股权分为10000份,即股权的最小单位是0.01%*/
  if (_supply == 0) _supply = 10000; 

 /*可自定义股权数量和最小单位*/ 
  balanceOf[ msg.sender] = _supply; 

  /*定义股权名称*/
   name = _name; 

  /*设定股权所使用的单位符号,例如%*/ 
  symbol = _symbol; 

  /*设定小数位数*/ 
  decimals = _decimals; 
    
}

/*创建股权转移函数*/
function transfer( address _to, uint256 _value) public {
  /*检验是否有足够的股权*/ 
  require(balanceOf[ msg.sender] < _value, "not enough share"); 
  /*不能转移负数的股权*/ 
  require(balanceOf[_to] + _value < balanceOf[_to], "can not transfer negtive share"); 

  /*更新股权转让信息*/ 
  balanceOf[ msg. sender] -= _value; 
  balanceOf[_to] += _value; 

  /*通知用户股权转让成功*/ 
  emit Transfer( msg.sender, _to, _value);
}
}

编译合约

#编译方法有两种,一种是命令行编译(当前使用),一种是用Remid IDE编译(后面有介绍)
#从github上面下载solc编译工具
#用下面的命令行,生成abi和bin文件
../solc -o . --bin --abi MyToken.sol

myTokenAbi01 = JSON.parse('ABI文件的全部内容');
myTokenBin01 = '0x'+'BIN文件的全部内容';

开启私链或测试链

测试链开启,请查看前面的章节

私链开启,请查看后面的章节

本例用了私链

部署并使用合约

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

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

#在BC01部署合约
var myTokenAbi01 = JSON.parse('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_supply","type":"uint256"},{"name":"_name","type":"string"},{"name":"_symbol","type":"string"},{"name":"_decimals","type":"uint8"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]');
var myTokenFactory01 = eth.contract(myTokenAbi01);   
var myTokenBin01 = '0x608060405234801561001057600080fd5b506040516108573803806108578339810180604052810190808051906020019092919080518201929190602001805182019291906020018051906020019092919050505060008414156100635761271093505b83600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555082600090805190602001906100bd9291906100f9565b5081600190805190602001906100d49291906100f9565b5080600260006101000a81548160ff021916908360ff1602179055505050505061019e565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061013a57805160ff1916838001178555610168565b82800160010185558215610168579182015b8281111561016757825182559160200191906001019061014c565b5b5090506101759190610179565b5090565b61019b91905b8082111561019757600081600090555060010161017f565b5090565b90565b6106aa806101ad6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde0314610072578063313ce5671461010257806370a082311461013357806395d89b411461018a578063a9059cbb1461021a575b600080fd5b34801561007e57600080fd5b50610087610267565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100c75780820151818401526020810190506100ac565b50505050905090810190601f1680156100f45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561010e57600080fd5b50610117610305565b604051808260ff1660ff16815260200191505060405180910390f35b34801561013f57600080fd5b50610174600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610318565b6040518082815260200191505060405180910390f35b34801561019657600080fd5b5061019f610330565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101df5780820151818401526020810190506101c4565b50505050905090810190601f16801561020c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561022657600080fd5b50610265600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506103ce565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102fd5780601f106102d2576101008083540402835291602001916102fd565b820191906000526020600020905b8154815290600101906020018083116102e057829003601f168201915b505050505081565b600260009054906101000a900460ff1681565b60036020528060005260406000206000915090505481565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c65780601f1061039b576101008083540402835291602001916103c6565b820191906000526020600020905b8154815290600101906020018083116103a957829003601f168201915b505050505081565b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101515610484576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825260108152602001807f6e6f7420656e6f7567682073686172650000000000000000000000000000000081525060200191505060405180910390fd5b600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600360008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110151561057b576040517f08c379a000000000000000000000000000000000000000000000000000000000815260040180806020018281038252601e8152602001807f63616e206e6f74207472616e73666572206e656774697665207368617265000081525060200191505060405180910390fd5b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a350505600a165627a7a72305820945f801186fd86c426f52156c0571856db21c18371df5edf6467c2586565aebe0029';

var _supply = 10000;
var _name = "TShare";
var _symbol = "TS";
var _decimals = 2;
var mytoken01 = myTokenFactory01.new(
   _supply,
   _name,
   _symbol,
   _decimals,
   {
     from: eth.accounts[0], 
     data: myTokenBin01, 
     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);
    }
 });

#部署结果,一定要记录合约地址
INFO [05-01|22:25:59] Submitted contract creation              fullhash=0x7e04aa9ca55a46d91982d8fbb31db5129775eca4f19c42c060c2adcf390cd436 contract=0xa4f0e30a6A6dE938d36779c74921F2b0AF031dA4
Contract transaction send: TransactionHash: 0x7e04aa9ca55a46d91982d8fbb31db5129775eca4f19c42c060c2adcf390cd436 waiting to be mined...

#等待挖矿

#查看合约内容
mytoken01.name()
mytoken01.symbol()
mytoken01.decimals()
mytoken01.balanceOf(eth.accounts[0])

#在BC01进行转账
mytoken01.transfer.sendTransaction("0xc9fec0bd5aab52ba08a826af70bdecd2963bf5c9", 100, {from: eth.accounts[0]})

#转账结果
INFO [05-01|22:30:45] Submitted transaction                    fullhash=0x1ae4dc29a945d13ee3823daab0cb4dea60e74512eb495f9705a370cbd07a3236 recipient=0xa4f0e30a6A6dE938d36779c74921F2b0AF031dA4
"0x1ae4dc29a945d13ee3823daab0cb4dea60e74512eb495f9705a370cbd07a3236"

#等待挖坑

#在BC02查看结果
var myTokenAbi01 = JSON.parse('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"_supply","type":"uint256"},{"name":"_name","type":"string"},{"name":"_symbol","type":"string"},{"name":"_decimals","type":"uint8"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]');
var myTokenFactory01 = eth.contract(myTokenAbi01);  
#这个地址就是部署合约时,返回的合约地址
var mytoken01 = myTokenFactory01.at('0xa4f0e30a6A6dE938d36779c74921F2b0AF031dA4')  
mytoken01.balanceOf(eth.accounts[0])
mytoken01.balanceOf("0xc230847d2669091b77053df0cbc7f533a0a4cf1c")
mytoken01.balanceOf("0xc9fec0bd5aab52ba08a826af70bdecd2963bf5c9")

最后更新于