欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#实战开发:以太坊Geth 命令发布智能合约 (五)

程序员文章站 2022-04-04 10:19:30
Token的合约代码我们参考Token-Factory的代码。 打开 https://remix.ethereum.org/#optimize=false&version=soljson-v0.4.24+commit.e67f0147.js 把运行的abi 或者参数方法拿到 打开 Geth 客户端输 ......

token的合约代码我们参考token-factory的代码。

打开  https://remix.ethereum.org/#optimize=false&version=soljson-v0.4.24+commit.e67f0147.js

把运行的abi 或者参数方法拿到  

打开 geth 客户端输入命令 创建在哪合约

var _supply = 10000 ;
var _name = "rrr" ;//名字
var _symbol = "r&c" ;
var _decimals = 8 ;
var mytokencontract = web3.eth.contract([{"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"}]);

abi

var mytoken = mytokencontract.new(
   _supply,
   _name,
   _symbol,
   _decimals,
   {
     from: eth.accounts[0], 
     data: '0x608060405234801561001057600080fd5b5060405161078438038061078483398101806040528101908080519060200190929190805182019291906020018051820192919060200180519060200190929190505050600084141561006457620f424093505b83600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555082600090805190602001906100be9291906100fa565b5081600190805190602001906100d59291906100fa565b5080600260006101000a81548160ff021916908360ff1602179055505050505061019f565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061013b57805160ff1916838001178555610169565b82800160010185558215610169579182015b8281111561016857825182559160200191906001019061014d565b5b509050610176919061017a565b5090565b61019c91905b80821115610198576000816000905550600101610180565b5090565b90565b6105d6806101ae6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde0314610072578063313ce5671461010257806370a082311461013357806395d89b411461018a578063a9059cbb1461021a575b600080fd5b34801561007e57600080fd5b50610087610267565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100c75780820151818401526020810190506100ac565b50505050905090810190601f1680156100f45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561010e57600080fd5b50610117610305565b604051808260ff1660ff16815260200191505060405180910390f35b34801561013f57600080fd5b50610174600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610318565b6040518082815260200191505060405180910390f35b34801561019657600080fd5b5061019f610330565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101df5780820151818401526020810190506101c4565b50505050905090810190601f16801561020c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561022657600080fd5b50610265600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506103ce565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102fd5780601f106102d2576101008083540402835291602001916102fd565b820191906000526020600020905b8154815290600101906020018083116102e057829003601f168201915b505050505081565b600260009054906101000a900460ff1681565b60036020528060005260406000206000915090505481565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c65780601f1061039b576101008083540402835291602001916103c6565b820191906000526020600020905b8154815290600101906020018083116103a957829003601f168201915b505050505081565b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561041a57600080fd5b600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600360008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110156104a757600080fd5b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a350505600a165627a7a723058208af3ffe7426eddcaffbad28237cd9b1e16ec37f30dd993090a99501d10426aaf0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('contract mined! address: ' + contract.address + ' transactionhash: ' + contract.transactionhash);
    }
 })

以下是整个输入流程

> var _supply = 10000 ;
undefined
> var _name = rrr;
undefined
> var _symbol = "r&c" ;
undefined
> var _decimals = 8 ;
undefined
> var mytokencontract = web3.eth.contract([{"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"}]);
undefined
> personal.unlockaccount(eth.accounts[0],123456)
password must be a string
> var mytokencontract = web3.eth.contract([{"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"}]);
121212
> personal.unlockaccount(eth.accounts[0],"123456")
true
> var mytoken = mytokencontract.new(
...    _supply,
...    _name,
...    _symbol,
...    _decimals,
...    {
......      from: eth.account[0],
......      data: '0x608060405234801561001057600080fd5b5060405161078438038061078483398101806040528101908080519060200190929190805182019291906020018051820192919060200180519060200190929190505050600084141561006457620f424093505b83600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555082600090805190602001906100be9291906100fa565b5081600190805190602001906100d59291906100fa565b5080600260006101000a81548160ff021916908360ff1602179055505050505061019f565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061013b57805160ff1916838001178555610169565b82800160010185558215610169579182015b8281111561016857825182559160200191906001019061014d565b5b509050610176919061017a565b5090565b61019c91905b80821115610198576000816000905550600101610180565b5090565b90565b6105d6806101ae6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde0314610072578063313ce5671461010257806370a082311461013357806395d89b411461018a578063a9059cbb1461021a575b600080fd5b34801561007e57600080fd5b50610087610267565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100c75780820151818401526020810190506100ac565b50505050905090810190601f1680156100f45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561010e57600080fd5b50610117610305565b604051808260ff1660ff16815260200191505060405180910390f35b34801561013f57600080fd5b50610174600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610318565b6040518082815260200191505060405180910390f35b34801561019657600080fd5b5061019f610330565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101df5780820151818401526020810190506101c4565b50505050905090810190601f16801561020c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561022657600080fd5b50610265600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506103ce565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102fd5780601f106102d2576101008083540402835291602001916102fd565b820191906000526020600020905b8154815290600101906020018083116102e057829003601f168201915b505050505081565b600260009054906101000a900460ff1681565b60036020528060005260406000206000915090505481565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c65780601f1061039b576101008083540402835291602001916103c6565b820191906000526020600020905b8154815290600101906020018083116103a957829003601f168201915b505050505081565b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561041a57600080fd5b600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600360008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110156104a757600080fd5b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a350505600a165627a7a723058208af3ffe7426eddcaffbad28237cd9b1e16ec37f30dd993090a99501d10426aaf0029',
......      gas: '4700000'
......    }, function (e, contract){
......     console.log(e, contract);
......     if (typeof contract.address !== 'undefined') {
.........          console.log('contract mined! address: ' + contract.address + ' transactionhash: ' + contract.transactionhash);
.........     }
......  })
typeerror: cannot access member '0' of undefined
    at <anonymous>:7:12

> var mytoken = mytokencontract.new(
...    _supply,
...    _name,
...    _symbol,
...    _decimals,
...    {
......      from: eth.accounts[0],
......      data: '0x608060405234801561001057600080fd5b5060405161078438038061078483398101806040528101908080519060200190929190805182019291906020018051820192919060200180519060200190929190505050600084141561006457620f424093505b83600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555082600090805190602001906100be9291906100fa565b5081600190805190602001906100d59291906100fa565b5080600260006101000a81548160ff021916908360ff1602179055505050505061019f565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061013b57805160ff1916838001178555610169565b82800160010185558215610169579182015b8281111561016857825182559160200191906001019061014d565b5b509050610176919061017a565b5090565b61019c91905b80821115610198576000816000905550600101610180565b5090565b90565b6105d6806101ae6000396000f30060806040526004361061006d576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde0314610072578063313ce5671461010257806370a082311461013357806395d89b411461018a578063a9059cbb1461021a575b600080fd5b34801561007e57600080fd5b50610087610267565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100c75780820151818401526020810190506100ac565b50505050905090810190601f1680156100f45780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561010e57600080fd5b50610117610305565b604051808260ff1660ff16815260200191505060405180910390f35b34801561013f57600080fd5b50610174600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610318565b6040518082815260200191505060405180910390f35b34801561019657600080fd5b5061019f610330565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101df5780820151818401526020810190506101c4565b50505050905090810190601f16801561020c5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561022657600080fd5b50610265600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506103ce565b005b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102fd5780601f106102d2576101008083540402835291602001916102fd565b820191906000526020600020905b8154815290600101906020018083116102e057829003601f168201915b505050505081565b600260009054906101000a900460ff1681565b60036020528060005260406000206000915090505481565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103c65780601f1061039b576101008083540402835291602001916103c6565b820191906000526020600020905b8154815290600101906020018083116103a957829003601f168201915b505050505081565b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101561041a57600080fd5b600360008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600360008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110156104a757600080fd5b80600360003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600360008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a350505600a165627a7a723058208af3ffe7426eddcaffbad28237cd9b1e16ec37f30dd993090a99501d10426aaf0029',
......      gas: '4700000'
......    }, function (e, contract){
......     console.log(e, contract);
......     if (typeof contract.address !== 'undefined') {
.........          console.log('contract mined! address: ' + contract.address + ' transactionhash: ' + contract.transactionhash);
.........     }
......  })
null [object object]
undefined
> miner.start(1)
null
> eth.blocknumber
375
> null [object object]
contract mined! address: 0x7c4c068c526dfd1cf5ebabd39020be9653947d7d transactionhash: 0x096c7a97f180bf80e36eac492a79ab3f0c240ac1628493c49c9c6091203d8471
> eth.blocknumber
377
> miner.stop()
true
> var ruiec = mytokencontract
undefined
> ruiec
{
  abi: [{
      constant: true,
      inputs: [],
      name: "name",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "decimals",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [{...}],
      name: "balanceof",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "symbol",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}, {...}],
      name: "transfer",
      outputs: [],
      payable: false,
      statemutability: "nonpayable",
      type: "function"
  }, {
      inputs: [{...}, {...}, {...}, {...}],
      payable: false,
      statemutability: "nonpayable",
      type: "constructor"
  }, {
      anonymous: false,
      inputs: [{...}, {...}, {...}],
      name: "transfer",
      type: "event"
  }],
  eth: {
    accounts: ["0xd21b8fce5b460060510b4ca4a9ffb55158cfb031", "0xce69218e9a098c5fe02f1c376ff22fb50bce6546", "0x216d2d677c210f7634f0b388d0495fb967595e34", "0x3397cd9a1b503f06ba4a5b1b6353276a415b7be1", "0x1ef09e6aac4dc729ac4a887f1bbad29e298d94b7"],
    blocknumber: 378,
    coinbase: "0xd21b8fce5b460060510b4ca4a9ffb55158cfb031",
    compile: {
      lll: function(),
      serpent: function(),
      solidity: function()
    },
    defaultaccount: undefined,
    defaultblock: "latest",
    gasprice: 18000000000,
    hashrate: 0,
    mining: false,
    pendingtransactions: [],
    protocolversion: "0x3f",
    syncing: false,
    call: function(),
    contract: function(abi),
    estimategas: function(),
    filter: function(options, callback, filtercreationerrorcallback),
    getaccounts: function(callback),
    getbalance: function(),
    getblock: function(),
    getblocknumber: function(callback),
    getblocktransactioncount: function(),
    getblockunclecount: function(),
    getcode: function(),
    getcoinbase: function(callback),
    getcompilers: function(),
    getgasprice: function(callback),
    gethashrate: function(callback),
    getmining: function(callback),
    getpendingtransactions: function(callback),
    getprotocolversion: function(callback),
    getrawtransaction: function(),
    getrawtransactionfromblock: function(),
    getstorageat: function(),
    getsyncing: function(callback),
    gettransaction: function(),
    gettransactioncount: function(),
    gettransactionfromblock: function(),
    gettransactionreceipt: function(),
    getuncle: function(),
    getwork: function(),
    iban: function(iban),
    icapnamereg: function(),
    issyncing: function(callback),
    namereg: function(),
    resend: function(),
    sendibantransaction: function(),
    sendrawtransaction: function(),
    sendtransaction: function(),
    sign: function(),
    signtransaction: function(),
    submittransaction: function(),
    submitwork: function()
  },
  at: function(address, callback),
  getdata: function(),
  new: function()
}
> var rc = ruiec.at("0x7c4c068c526dfd1cf5ebabd39020be9653947d7d");
undefined
> rc
{
  abi: [{
      constant: true,
      inputs: [],
      name: "name",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "decimals",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [{...}],
      name: "balanceof",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "symbol",
      outputs: [{...}],
      payable: false,
      statemutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}, {...}],
      name: "transfer",
      outputs: [],
      payable: false,
      statemutability: "nonpayable",
      type: "function"
  }, {
      inputs: [{...}, {...}, {...}, {...}],
      payable: false,
      statemutability: "nonpayable",
      type: "constructor"
  }, {
      anonymous: false,
      inputs: [{...}, {...}, {...}],
      name: "transfer",
      type: "event"
  }],
  address: "0x7c4c068c526dfd1cf5ebabd39020be9653947d7d",
  transactionhash: null,
  transfer: function(),
  allevents: function(),
  balanceof: function(),
  decimals: function(),
  name: function(),
  symbol: function(),
  transfer: function()
}
> rc.balanceof(eth.accounts[0])
10000
>