第06课:Token

第06课:Token 的发布及操作

在上篇文章中我们学习了 ERC-20 代币标准,并在最后附上了一套代币的代码。本篇内容将针对那套代码来继续延伸,带领大家学习一下如何通过 Geth 来发布此代币的智能合约及相关的操作。

编译工具

针对 Solidity 语言编写的智能合约有一套比较好的在线编写、编译、发布的神器——Remix。

此在线工具原网址详见这里,目前已不提供更新,改为新网址

enter image description here

下面简单介绍一下此工具支持的功能。

文件夹管理

最左边是文件夹管理,里面列出了当前工作区里的文件,Remix 可以支持从本地文件夹读取文件。上面截图中对应列出了本人写的一些智能合约的 sol 文件。

工作区

中间部分为工作区,其中上半部是代码编辑区,如图所示的 Solidity 代码。下半部是日志区,在执行智能合约时,会显示 transaction 相关的信息。在输出日志的时候还可以查看 Details 和 Debug 信息。

功能区

最后边为可操作的功能区,包含编译(Complie)、运行(Run)、设置(Settings)、分析(Analysis)、调试(Debugger)和支持(Support)。

值得注意的是编译功能中的详情(Details),这里展示了编译的各种细节,后面我们代码的发布内容也是从此部分进行提取,这里面提供了 NAME、METADATA、BYTECODE、ABI 等一些相关信息。

发布步骤

代码的编译

首选,将上篇的代币代码复制到 Remix 的代码区域内。如果有语法错误,或版本错误,Remix 的代码区会以红线提示,同时在右边编译的过程中也会发生错误。比如我们随便在代码中添加一些无效字符“abc”,那么会出现下面的情况:

enter image description here

通过这些提示内容,可以很快的定位到问题的原因。

编译通过之后,在详情(Details)中拿到待发布的编译之后的代码内容。在 Geth 的 Console 中发布属于通过 Web3 部署的模式。在编译(Compile)功能的下拉框中找到智能合约的名字 AC,选中。然后单击右边的 Details 选项,弹出浮动层,里面包含了各种细节。

enter image description here

比如 Name 栏中显示为 AC,METADATA 中显示了编译相关的信息,BYTECODE 中显示了字节码的信息,ABI 中显示了此智能合约对应的 ABI 等,但这些都不是目前我们需要的。我们需要的在下面 WEB3DEPLOY 中,此智能合约编译之后的发布命令内容为:

var initialSupply = /* var of type uint256 here */ ;
var tokenName = /* var of type string here */ ;
var tokenSymbol = /* var of type string here */ ;
var holder = /* var of type address here */ ;
var acContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"burn","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"unfreeze","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"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"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"freezeOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_value","type":"uint256"}],"name":"freeze","outputs":[{"name":"success","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"initialSupply","type":"uint256"},{"name":"tokenName","type":"string"},{"name":"tokenSymbol","type":"string"},{"name":"holder","type":"address"}],"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"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Burn","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Freeze","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Unfreeze","type":"event"}]);
var ac = acContract.new(
   initialSupply,
   tokenName,
   tokenSymbol,
   holder,
   {
     from: web3.eth.accounts[0], 
     data: '0x60606040526012600260006101000a81548160ff021916908360ff16021790555034156200002c57600080fd5b604051620015fb380380620015fb83398101604052808051906020019091908051820191906020018051820191906020018051906020019091905050600260009054906101000a900460ff1660ff16600a0a8402600381905550600354600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508260009080519060200190620000e492919062000149565b508160019080519060200190620000fd92919062000149565b5080600460006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555050505050620001f8565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106200018c57805160ff1916838001178555620001bd565b82800160010185558215620001bd579182015b82811115620001bc5782518255916020019190600101906200019f565b5b509050620001cc9190620001d0565b5090565b620001f591905b80821115620001f1576000816000905550600101620001d7565b5090565b90565b6113f380620002086000396000f3006060604052600436106100d0576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806306fdde03146100d5578063095ea7b31461016357806318160ddd146101bd57806323b872dd146101e6578063313ce5671461025f57806342966c681461028e5780636623fc46146102c957806370a08231146103045780638da5cb5b1461035157806395d89b41146103a6578063a9059cbb14610434578063cd4217c114610476578063d7a78db8146104c3578063dd62ed3e146104fe575b600080fd5b34156100e057600080fd5b6100e861056a565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561012857808201518184015260208101905061010d565b50505050905090810190601f1680156101555780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561016e57600080fd5b6101a3600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610608565b604051808215151515815260200191505060405180910390f35b34156101c857600080fd5b6101d06106a3565b6040518082815260200191505060405180910390f35b34156101f157600080fd5b610245600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff169060200190919080359060200190919050506106a9565b604051808215151515815260200191505060405180910390f35b341561026a57600080fd5b610272610ad5565b604051808260ff1660ff16815260200191505060405180910390f35b341561029957600080fd5b6102af6004808035906020019091905050610ae8565b604051808215151515815260200191505060405180910390f35b34156102d457600080fd5b6102ea6004808035906020019091905050610c3c565b604051808215151515815260200191505060405180910390f35b341561030f57600080fd5b61033b600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050610e0a565b6040518082815260200191505060405180910390f35b341561035c57600080fd5b610364610e22565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b34156103b157600080fd5b6103b9610e48565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156103f95780820151818401526020810190506103de565b50505050905090810190601f1680156104265780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b341561043f57600080fd5b610474600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091908035906020019091905050610ee6565b005b341561048157600080fd5b6104ad600480803573ffffffffffffffffffffffffffffffffffffffff16906020019091905050611179565b6040518082815260200191505060405180910390f35b34156104ce57600080fd5b6104e46004808035906020019091905050611191565b604051808215151515815260200191505060405180910390f35b341561050957600080fd5b610554600480803573ffffffffffffffffffffffffffffffffffffffff1690602001909190803573ffffffffffffffffffffffffffffffffffffffff1690602001909190505061135f565b6040518082815260200191505060405180910390f35b60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156106005780601f106105d557610100808354040283529160200191610600565b820191906000526020600020905b8154815290600101906020018083116105e357829003601f168201915b505050505081565b6000808211151561061857600080fd5b81600760003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506001905092915050565b60035481565b6000808373ffffffffffffffffffffffffffffffffffffffff16141515156106d057600080fd5b6000821115156106df57600080fd5b81600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020541015151561072d57600080fd5b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205401101515156107bc57600080fd5b600760008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054821115151561084757600080fd5b610890600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205483611384565b600560008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000208190555061091c600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548361139d565b600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506109e5600760008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205483611384565b600760008673ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002060003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508273ffffffffffffffffffffffffffffffffffffffff168473ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef846040518082815260200191505060405180910390a3600190509392505050565b600260009054906101000a900460ff1681565b600081600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610b3857600080fd5b600082111515610b4757600080fd5b610b90600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205483611384565b600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610bdf60035483611384565b6003819055503373ffffffffffffffffffffffffffffffffffffffff167fcc16f5dbb4873280815c1ee09dbd06736cffcc184412cf7a71a0fdb75d397ca5836040518082815260200191505060405180910390a260019050919050565b600081600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610c8c57600080fd5b600082111515610c9b57600080fd5b610ce4600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205483611384565b600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002081905550610d70600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548361139d565b600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055503373ffffffffffffffffffffffffffffffffffffffff167f2cfce4af01bcb9d6cf6c84ee1b7c491100b8695368264146a94d71e10a63083f836040518082815260200191505060405180910390a260019050919050565b60056020528060005260406000206000915090505481565b600460009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b60018054600181600116156101000203166002900480601f016020809104026020016040519081016040528092919081815260200182805460018160011615610100020316600290048015610ede5780601f10610eb357610100808354040283529160200191610ede565b820191906000526020600020905b815481529060010190602001808311610ec157829003601f168201915b505050505081565b60008273ffffffffffffffffffffffffffffffffffffffff1614151515610f0c57600080fd5b600081111515610f1b57600080fd5b80600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410151515610f6957600080fd5b600560008373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205481600560008573ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020540110151515610ff857600080fd5b611041600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205482611384565b600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506110cd600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548261139d565b600560008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055508173ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff167fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef836040518082815260200191505060405180910390a35050565b60066020528060005260406000206000915090505481565b600081600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200190815260200160002054101515156111e157600080fd5b6000821115156111f057600080fd5b611239600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205483611384565b600560003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055506112c5600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020548361139d565b600660003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020819055503373ffffffffffffffffffffffffffffffffffffffff167ff97a274face0b5517365ad396b1fdba6f68bd3135ef603e44272adba3af5a1e0836040518082815260200191505060405180910390a260019050919050565b6007602052816000526040600020602052806000526040600020600091509150505481565b600082821115151561139257fe5b818303905092915050565b60008082840190508381101580156113b55750828110155b15156113bd57fe5b80915050929150505600a165627a7a723058207f7d11ace528a3379c60e83b754f662603c5b7e3fc64a659a165722a5dea35880029', 
     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 定义的变量就是在智能合约中构造函数需要的参数变量。在发布之前,需要对其进行修改,比如:

var initialSupply = 100000000; /* 先来一个亿 */ 
var tokenName = 'Abc Coin' /*代币名称为Abc Coin*/ ;
var tokenSymbol = 'AC' /* 代币简称AC */ ;
var holder = '0x8b275d360a5d435e9dfcf27d610362ee2fdf460c' /* 持有者地址,可为当前发布者,也可为其他拥有私钥的地址 */ ;

智能合约发布

上面的基础工作都已经做好,剩下的就是在节点 Web3j 交互中发布智能合约,这里涉及到发布的手续费问题,在上面智能合约 new 方法里面只传递了 Gas,这里的 Gas 指的就是 gasLimit,如果想指定 Gas 的价格可再添加 gasPrice 参数项目,可根据市场行情调整,目前 20-50Gwei 都是可以成功的,注意单位参数的最小计量单位是 wei,比如 gasPrice:'20000000000'。

然后进入 Console 控制台,依次执行上面的命令,也可以全部复制进入执行。当然在执行之前是需要确保发布智能合约的账户里面有足够的手续费和账户的解锁操作的,执行成功输入类似以下内容:

Contract mined! address: 0x5eb272fd3eb123a46fa56a0d189351066633d845 transactionHash: 0x175529a4fb21ed96a9f244569d14f45b39cb5898cf8e56b1ca496cd6827b3d73

说明智能合约发布成功。打印的结果中第一个为智能合约的地址,也就是以后我们用来调用智能合约的进行转账或查询的 to 地址。transactionHash 为当前智能合约发布所在的交易的 hash 值。

下面看一下操作过程中的效果图:

enter image description here

enter image description here

enter image description here

方法执行

上面已经完成的合约的发布,确认智能合约已经被打包之后就可以简单验证一下金额等信息。

核实一下接收账户是否收到了指定金额的代币,直接在控制台执行以下命令:

> ac.balanceOf("0xadce6e0e1ee491e7c1945e76d3dc5975418c4e45")
1e+26

显然,代币的余额就是我们发行的代币总量,这里是按照最小单位显示,使用了科学计数法。

上面只是简单验证,真正在 console 中调用时还需要根据 abi 来调用,我们看一下具体步骤,定义了 acCall 用来接收已经部署成功合约的 abi 信息,注意 at 方法的参数为智能合约的地址:

> acCall = acContract.at("0x5cd36b04243b66f646db8c38427321432c66b6af")
{
  abi: [{
      constant: true,
      inputs: [],
      name: "name",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}, {...}],
      name: "approve",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "totalSupply",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}, {...}, {...}],
      name: "transferFrom",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "decimals",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "burn",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "unfreeze",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: true,
      inputs: [{...}],
      name: "balanceOf",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: true,
      inputs: [],
      name: "owner",
      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"
  }, {
      constant: true,
      inputs: [{...}],
      name: "freezeOf",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "freeze",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }, {
      constant: true,
      inputs: [{...}, {...}],
      name: "allowance",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      inputs: [{...}, {...}, {...}, {...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "constructor"
  }, {
      anonymous: false,
      inputs: [{...}, {...}, {...}],
      name: "Transfer",
      type: "event"
  }, {
      anonymous: false,
      inputs: [{...}, {...}],
      name: "Burn",
      type: "event"
  }, {
      anonymous: false,
      inputs: [{...}, {...}],
      name: "Freeze",
      type: "event"
  }, {
      anonymous: false,
      inputs: [{...}, {...}],
      name: "Unfreeze",
      type: "event"
  }],
  address: "0x5cd36b04243b66f646db8c38427321432c66b6af",
  transactionHash: null,
  Burn: function(),
  Freeze: function(),
  Transfer: function(),
  Unfreeze: function(),
  allEvents: function(),
  allowance: function(),
  approve: function(),
  balanceOf: function(),
  burn: function(),
  decimals: function(),
  freeze: function(),
  freezeOf: function(),
  name: function(),
  owner: function(),
  symbol: function(),
  totalSupply: function(),
  transfer: function(),
  transferFrom: function(),
  unfreeze: function()
}

通过 acCall 再来查询一下余额信息:

> acCall.balanceOf(eth.accounts[0])
1e+26

也是可以正在获取的。

再来一笔转账,并查询余额:

> acCall.transfer.sendTransaction("0x1ece51bdb1f056f929da51c2fe0a31beeea89ab3",88888,{from:"0xadce6e0e1ee491e7c1945e76d3dc5975418c4e45",gas:300000})
"0x49f0a7cccf65548d90c87ba71c9e70a4f3211c9fdec910cb9d87fa441cc3db56"
> acCall.balanceOf("0x1ece51bdb1f056f929da51c2fe0a31beeea89ab3")
88888

很显然转账成功。这里解释一下转账方法的调用,acCall.tranfer 是指明调用智能合约的哪个方法,sendTransaction 用来发送一笔交易,sendTransaction 的前两个参数是 transfer 所需的两个参数,可对照 abi 或源代码查看,后面 {} 中的参数为发送交易所需的手续费等参数。其他方法的调用依次类推,不再赘述。

Etherscan 智能合约的配置

当智能合约已经被执行,如果希望将源代码信息发布到 Etherscan 网站上,我们可以进行以下操作。

首先访问这里,找到发布成功的智能合约,可根据合约地址查询到。未上传源代码的智能合约在该网站查到是这样的:

enter image description here

如果我们要上传原代码,单击“Verify And Publish”按钮,进入下面页面:

enter image description here

在 Contract Name 处填写发布合约时指定的名字,比如 AC,在 Complier 选择编译的版本,也就是智能合约源代码最上面指定的,Optimization 选择 No,然后将源代码粘贴到下面输入域中,进行人机身份验证之后,单击 Verify And Pulish 按钮,即可完成源代码的发布。

需要注意的是这里的源代码并未上传到区块链上,而只是放置到该区块链浏览器上了。

总结

本篇内容给大家介绍了 Token 的编译发布以及相关的命令操作,希望大家多做练习、熟能生巧。下篇的内容是将带领大家操作如何用 Java 来调用智能合约。

上一篇
下一篇
目录