从零开始以太坊区块链开发指南三

前言

上一篇我们介绍了如何部署智能合约,这一章我们将演示如何在Geth中调用智能合约。如果你还没有看上一篇介绍的内容,我建议你先看看上一篇从零开始以太坊区块链开发指南二。


获取合约实例
有过编程经历的都知道,想要调用一个类里面的方法必须先获取这个类的实例,那么以太坊里面如何获取智能合约的实例呢,我们需要abi和上一篇我们部署的合约的地址

abi全称Application Binary Interface,它定义了如何与智能合约进行交互,我们可以把它理解成一个智能合约类的接口描述文件,当我们部署完智能合约之后,智能合约被编译成机器码,我们通过这个描述文件来知道这个智能合约有哪些接口,需要传递什么参数,返回什么值。

  1. 如何获取abi呢
    其实,当我们使用Truffle部署的时候,它已经帮我们生成了abi
从零开始以太坊区块链开发指南三_第1张图片
目录结构

打开build文件夹下的contracts文件夹,里面生成了每个合约对应的json文件,打开这个json文件。我们就可以看到abi的具体描述


从零开始以太坊区块链开发指南三_第2张图片
abi截图

我们copy出这个json里面abi的value,即"abi:"的值,"""需要注意的是"""我们使用的时候abi不能有空格和换行符,我使用bejson来压缩abi。压缩完的abi应该只有一行

[{"constant":false,"inputs":[{"name":"proposalIndex","type":"uint256"}],"name":"vote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"proposals","outputs":[{"name":"name","type":"bytes32"},{"name":"voteCount","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"chairperson","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"winningProposalIndex","outputs":[{"name":"winningProposalIndex","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"voters","outputs":[{"name":"name","type":"bytes32"},{"name":"voted","type":"bool"},{"name":"vote","type":"uint256"},{"name":"givenRightTime","type":"uint256"},{"name":"votetime","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"winnerName","outputs":[{"name":"winnerName","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"voter","type":"address"},{"name":"voterName","type":"bytes32"}],"name":"giveRightToVote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"proposalNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
  1. 如何获取智能合约地址
    我们在上一步部署合约的时候
从零开始以太坊区块链开发指南三_第3张图片
合约地址

红线框出的16进制代码 就是合约的地址

生成合约实例

从零开始以太坊区块链开发指南三_第4张图片
步骤截图

具体命令

var addr = '0x5c99e3033ec4f8949df2b3cf391f9f3be64e0f823207af245d23ca7d9a96fe6c'
var abi = [{"constant":false,"inputs":[{"name":"proposalIndex","type":"uint256"}],"name":"vote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"proposals","outputs":[{"name":"name","type":"bytes32"},{"name":"voteCount","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"chairperson","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"winningProposalIndex","outputs":[{"name":"winningProposalIndex","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"voters","outputs":[{"name":"name","type":"bytes32"},{"name":"voted","type":"bool"},{"name":"vote","type":"uint256"},{"name":"givenRightTime","type":"uint256"},{"name":"votetime","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"winnerName","outputs":[{"name":"winnerName","type":"bytes32"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"voter","type":"address"},{"name":"voterName","type":"bytes32"}],"name":"giveRightToVote","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"inputs":[{"name":"proposalNames","type":"bytes32[]"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
var ballotInstance= eth.contract(abi).at(addr)

我们在命令行中查看ballotInstance

从零开始以太坊区块链开发指南三_第5张图片
打印合约实例

我们尝试调用合约的方法proposals

从零开始以太坊区块链开发指南三_第6张图片
调用截图

好了,如何调用合约,我们已经演示完了。如果你有什么疑问欢迎在

你可能感兴趣的:(从零开始以太坊区块链开发指南三)