当前位置:   article > 正文

【4】私有链发币_4、 私有链如何换币

4、 私有链如何换币

实现源码:

  1. /*************************************
  2. * web3.js version : 1.0.0-beta.35 *
  3. * */
  4. // require filestream to read solidity file
  5. const fs=require("fs")
  6. const Web3=require("web3")
  7. const solc=require("solc")
  8. const web3Admin=require("web3admin")
  9. init Web3 rpc-http Connection through private port :8545
  10. let web3=new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"))
  11. read contract file
  12. const solcfile=fs.readFileSync("TokenERC20.sol");
  13. compile solc file to json ABI
  14. const output= solc.compile(solcfile.toString(),1)
  15. var tokenContract=output.contracts[':TokenERC20']
  16. get token contract abi
  17. const tokenContractABI=tokenContract.interface
  18. get token contract bytecode recognized by js code
  19. const bytecode=tokenContract.bytecode
  20. asynchronized unlock default account for depolying token20Contract
  21. web3.eth.getAccounts().then(data=>{
  22. web3.eth.personal.unlockAccount(data[0],'liujp')
  23. console.log("unlock account:"+data[0])
  24. /********************* deploy contract **********************
  25. * new web3.eth.Contract(param1,param2,param3) *
  26. * param1: JSON.parse(tokenContractABI), *
  27. * param2: address:null, *
  28. * param3: options{ *
  29. * data:"0x"+bytecode, *
  30. * from: account address, *
  31. * gas:1000000 *
  32. * } */
  33. console.log("deploying the contract")
  34. const tokenContract20=new web3.eth.Contract(JSON.parse(tokenContractABI))
  35. tokenContract20.deploy({
  36. data:'0x'+bytecode, //0x开头
  37. //传递构造函数的参数(发行量:10000000000000,token名称:'XXXX',符号:"XXX")
  38. arguments:[10000000000000,'XXXX','XXX']
  39. }).send({
  40. from:"0x04a78a0ef3d868fdb9d7532c8b1ef7f49167f5a4",
  41. gas:1000000,
  42. gasPrice:'30000000'
  43. },function(error,transactionHash){
  44. console.log("send回调");
  45. console.log("error:"+error);
  46. console.log("send transactionHash:"+transactionHash);
  47. }).on('error', function(error){ console.error(error) })
  48. .then(function(newContractInstance){
  49. var newContractAddress=newContractInstance.options.address
  50. console.log("新合约地址:"+newContractAddress);
  51. });
  52. web3.eth.getBalance(data[0])
  53. .then(function(balance){
  54. console.log(balance)
  55. });
  56. // sendTransaction
  57. // web3.eth.sendTransaction({
  58. // from:data[0],
  59. // to: data[1],
  60. // value:web3.utils.toWei("1")
  61. // });
  62. miner start mining
  63. // web3Admin.extend(web3)
  64. // console.log("turning on mining",web3.miner.start())
  65. // console.log("isMining?",web3.eth.mining)
  66. //执行结果:
  67. //send transactionHash:0x9bfb166a8abb3211b4c5303ae905e40c655e0480d8a5246bc3a8a260eb68333e
  68. ///新合约地址:0xFa5d8686fA1f899E88CD6d45B7Deab57d3F32caC
  69. });
ERC20代币源码:
  1. pragma solidity ^0.4.16;
  2. interface tokenRecipient { function receiveApproval(address _from, uint256 _value, address _token, bytes _extraData) public; }
  3. contract TokenERC20 {
  4. string public name="LJP";
  5. string public symbol="LJP";
  6. uint8 public decimals = 18; // 18 是建议的默认值
  7. uint256 public totalSupply=1000000000000;
  8. mapping (address => uint256) public balanceOf; //
  9. mapping (address => mapping (address => uint256)) public allowance;
  10. event Transfer(address indexed from, address indexed to, uint256 value);
  11. event Burn(address indexed from, uint256 value);
  12. function TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) public {
  13. totalSupply = initialSupply * 10 ** uint256(decimals);
  14. balanceOf[msg.sender] = totalSupply;
  15. name = tokenName;
  16. symbol = tokenSymbol;
  17. }
  18. function _transfer(address _from, address _to, uint _value) internal {
  19. require(_to != 0x0);
  20. require(balanceOf[_from] >= _value);
  21. require(balanceOf[_to] + _value > balanceOf[_to]);
  22. uint previousBalances = balanceOf[_from] + balanceOf[_to];
  23. balanceOf[_from] -= _value;
  24. balanceOf[_to] += _value;
  25. Transfer(_from, _to, _value);
  26. assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
  27. }
  28. function transfer(address _to, uint256 _value) public {
  29. _transfer(msg.sender, _to, _value);
  30. }
  31. function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) {
  32. require(_value <= allowance[_from][msg.sender]); // Check allowance
  33. allowance[_from][msg.sender] -= _value;
  34. _transfer(_from, _to, _value);
  35. return true;
  36. }
  37. function approve(address _spender, uint256 _value) public
  38. returns (bool success) {
  39. allowance[msg.sender][_spender] = _value;
  40. return true;
  41. }
  42. function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) {
  43. tokenRecipient spender = tokenRecipient(_spender);
  44. if (approve(_spender, _value)) {
  45. spender.receiveApproval(msg.sender, _value, this, _extraData);
  46. return true;
  47. }
  48. }
  49. function burn(uint256 _value) public returns (bool success) {
  50. require(balanceOf[msg.sender] >= _value);
  51. balanceOf[msg.sender] -= _value;
  52. totalSupply -= _value;
  53. Burn(msg.sender, _value);
  54. return true;
  55. }
  56. function burnFrom(address _from, uint256 _value) public returns (bool success) {
  57. require(balanceOf[_from] >= _value);
  58. require(_value <= allowance[_from][msg.sender]);
  59. balanceOf[_from] -= _value;
  60. allowance[_from][msg.sender] -= _value;
  61. totalSupply -= _value;
  62. Burn(_from, _value);
  63. return true;
  64. }
  65. }
 
  1. 安装webstorm 平台 ,web3 0.0-beta35最新版、solc、等js库
  2. 准备erc20代币token合约
  3. 具体实现过程见源码
  4. 成功之后注意在控制台(geth console)处调用miner.start(),启动挖矿(当前web3Admin 调用miner仍存在问题,暂通过控制台执行)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/608368
推荐阅读
相关标签
  

闽ICP备14008679号