mcat是一个基于Golang实现的以太坊智能合约开发脚手架,它可以帮助你快速开发、调试以及部署智能合约,同时mact提供一个通过合约交易参数计算调用字节码的功能,可以帮助开发者不受语言限制,无论是Java, Python或是其它支持网络编程的语言与合约进行交互。mcat还在持续迭代中,项目地址 luren5/mcat
这里演示一个基于智能合约的投票项目的开发全过程,在此之前需要先安装geth, mcat, solc
初始化项目
luren5@ubuntu:~$ mcat init --project ballot
Starting the initialization, wait a moment…
Congratulations! You have succeed in initing a new mc project
初始化之后在当前目录下会初始化一个项目`ballot`,以下是目录结构说明
- contracts # 合约源码目录
- compiled # 合约编译后的abi和bin存放目录
- data # 项目配置存储目录
- IDE # mcat提供的一个本地在线IDE
- mcat.yaml # 项目配置文件
修改并加载项目配置
Development:
project_name: "ballot"
ip: "IP"
rpc_port: "RPC_PORT"
account: "DEFAULT_ACCOUNT"
password: "DEFAULT_ACCOUNT_PASSWORD"
ide_port: "50728"
Production:
project_name: "ballot"
ip: "IP"
rpc_port: "RPC_PORT"
account: "DEFAULT_ACCOUNT"
password: "DEFAULT_ACCOUNT_PASSWORD"
ide_port: "50728"
将Development 模式下的 IP, RPC_PORT 改为节点的ip和rpc_port, ACCOUNT改为节点中真实存在的地址,由于此地址是部署和调用合约方法的发起方,所以需要保证此地址有资产,DEFAULT_ACCOUNT_PASSWORD改为此地址的密码,修改后类似如下
Development:
project_name: "ballot"
ip: "localhost"
rpc_port: "8090"
account: "0x14dc9da15bcf9028238899e149b6abf0ed8ad553"
password: "123456"
ide_port: "50728"
加载配置
luren5@ubuntu:~/ballot$ mcat loadConfig --model=Development
Succeed in loading mcat config.
--model缺省模式下为Development,可根据需求加载别的模式
编译合约
luren5@ubuntu:~/ballot$ mcat compile --sol=Ballot.sol
Waiting for compiling contracts…
Succeed in compiling contract Ballot
--sol 为合约源码文件名,如果此合约源码文件中有多个合约可使用 --exc 参数排除不需要编译的合约,多个之间用`,`隔开。编译后,合约对应的`abi`和`bin`会存放在`compiled`目录下
luren5@ubuntu:~/ballot$ ls compiled/
Ballot.abi Ballot.bin
部署合约
luren5@ubuntu:~/ballot$ mcat deploy --contract=Ballot --params="Hillary,Trump"
Succeed in deploying contract Ballot, tx hash: 0x319d4a32b110738bc6d9b4e2cfc576958d4ff9abd9e8320697d3436638a30cbc. Waiting for being mined…
注意由于Ballot合约的构造方法需要传入一个`bytes32[] proposalNames` 类型的参数,这里加上参数 --params="Hillary,Trump"。如果需要多个参数则用`&`隔开,如果参数为T[]或T[k]类型的,则多个元素之间用`,` 隔开。另外别忘了参数值的双引号
此时mcat已经发起了部署合约的请求,它是一笔交易,在你的geth console会打印一条信息
INFO [08-15|22:42:35] Submitted contract creation fullhash=0x319d4a32b110738bc6d9b4e2cfc576958d4ff9abd9e8320697d3436638a30cbc contract=0x659eada17ac70cab818cc31e81399af58f97aad8
开启挖矿,让交易被打包…
luren5@ubuntu:~/ballot$ mcat deploy --contract=Ballot --params="Hillary,Trump"
Succeed in deploying contract Ballot, tx hash: 0x319d4a32b110738bc6d9b4e2cfc576958d4ff9abd9e8320697d3436638a30cbc. Waiting for being mined…
Congratulations! tx has been mined, contract address: 0x659eada17ac70cab818cc31e81399af58f97aad8
打包成功,并返回了合约账户的地址 0x659eada17ac70cab818cc31e81399af58f97aad8
验证合约是否部署成功
通过查看合约账户地址里是否有编译后的字节码可确认合约是否部署成功
> eth.getCode("0x659eada17ac70cab818cc31e81399af58f97aad8")
# 此处会打印合约编译后的字节码
再来确认下构造函数的参数是否成功赋值给合约的状态变量
> abi=… # 合约abi的json数据, 此处太长不再粘贴
> addr="0x659eada17ac70cab818cc31e81399af58f97aad8"
> ballotInstance=eth.contract(abi).at(addr)
{
abi: [{
constant: false,
inputs: [{...}],
name: "vote",
outputs: [],
payable: false,
type: "function"
}, {
……
> ballotInstance.proposals(0)
["0x48696c6c61727900000000000000000000000000000000000000000000000000", 0]
> ballotInstance.proposals(1)
["0x5472756d70000000000000000000000000000000000000000000000000000000", 0]
>web3.toUtf8("0x5472756d70000000000000000000000000000000000000000000000000000000")
"Trump"
可见,合约确实部署成功了!
原文地址: https://zhuanlan.zhihu.com/p/28541643