林业建设协会网站,公众号公众平台,知名网站制作公司青岛分公司,北京门户网站建设公司环境#xff1a; fisco2.8.0 go 1.17 go-sdk 1.0.0 solidity 0.4.25 前言
请提前启动好四个fisco节点。
请准备好一个属于此fisco节点的账户私钥【待会调用合约和部署合约会用到】
此文章将讲解 官方文档使用gosdk部署helloworld合约并调用其方法 合约开发样例
官网提示 G… 环境 fisco2.8.0 go 1.17 go-sdk 1.0.0 solidity 0.4.25 前言
请提前启动好四个fisco节点。
请准备好一个属于此fisco节点的账户私钥【待会调用合约和部署合约会用到】
此文章将讲解 官方文档使用gosdk部署helloworld合约并调用其方法 合约开发样例
官网提示 Golang, 版本需不低于1.13.6本项目采用go module进行包管理。具体可查阅Using Go Modules环境配置 FISCO BCOS 2.2.0, 需要提前运行 FISCO BCOS 区块链平台可参考安装搭建 Solidity编译器默认0.4.25版本 gosdk的拉取
# 拉取代码
git clone https://github.com/FISCO-BCOS/go-sdk.git# 若因为网络问题导致长时间无法执行上面的命令请尝试以下命令
git clone https://gitee.com/FISCO-BCOS/go-sdk.gitroot192-168-19-133:/usr/project/goproject# ll
drwxr-xr-x 17 root root 4096 11月 16 10:29 go-sdk/go-sdk的tag版本
root192-168-19-133:/usr/project/goproject/go-sdk# git show
commit d1a8411d0e7600e2ece7c50c6da523ee52f32a45 (HEAD - master, tag: v1.0.0, origin/master, origin/HEAD)
Merge: 5ca92a0 797900f
Author: XingQiang Bai bxq2011hustqq.com
Date: Thu May 12 17:22:46 2022 0800Merge pull request #148 from FISCO-BCOS/release-v1.0.0Release v1.0.0创建 helloworld的项目
我选择不跟官网的步骤不在go-sdk目录进行创建选择在gopath的工作目录下面进行项目创建【这样比较方便管理】
root192-168-19-133:/usr/project/goproject# mkdir helloworld然后进行go项目初始化
root192-168-19-133:/usr/project/goproject/helloworld# go mod init helloworld
go: creating new go.mod: module helloworld然后在helloworld目录下编写一个HelloWorld.sol的合约
pragma solidity0.4.24 0.6.11;contract HelloWorld {string value;constructor() public {value 你好golang!;}function get() public view returns (string memory) {return value;}function set(string v) public {value v;}
}进行安装solc编译器
在helloworld项目下执行此命令需要用到在go-sdk里面的下载编译器脚本文件 go-sdk的目录需要根据各自的实际情况来选择 bash /usr/project/goproject/go-sdk/tools/download_solc.sh -v 0.4.25 # 它会做链接到当前目录下root192-168-19-133:/usr/project/goproject/helloworld# bash /usr/project/goproject/go-sdk/tools/download_solc.sh -v 0.4.25
Downloading solc 0.4.25 solc-linux.tar.gz from https://github.com/FISCO-BCOS/solidity/releases/download/v0.4.25/solc-linux.tar.gz[INFO] os : linux
[INFO] solc version : 0.4.25
[INFO] solc location : ./solc-0.4.25[INFO] ./solc-0.4.25 --version
solc, the solidity compiler commandline interface
Version: 0.4.25commit.46d177ad.mod.Linux.g
root192-168-19-133:/usr/project/goproject/helloworld# ll
总用量 12
drwxr-xr-x 2 root root 4096 11月 16 10:46 ./
drwxrwxrwx 8 root root 4096 11月 16 10:38 ../
-rw-r--r-- 1 root root 294 11月 16 10:43 HelloWorld.sol
lrwxrwxrwx 1 root root 29 11月 16 10:46 solc-0.4.25 - /root/.fisco/solc/solc-0.4.25*生成abi,bin文件
使用solc 进行编译 命令的意思是根据HelloWorld.sol生成abi,bin文件到当前目录下
root192-168-19-133:/usr/project/goproject/helloworld# ./solc-0.4.25 --bin --abi -o ./ ./HelloWorld.sol #
root192-168-19-133:/usr/project/goproject/helloworld# ll
总用量 20
drwxr-xr-x 2 root root 4096 11月 16 10:49 ./
drwxrwxrwx 8 root root 4096 11月 16 10:38 ../
-rw-r--r-- 1 root root 375 11月 16 10:49 HelloWorld.abi
-rw-r--r-- 1 root root 2010 11月 16 10:49 HelloWorld.bin
-rw-r--r-- 1 root root 294 11月 16 10:43 HelloWorld.sol
lrwxrwxrwx 1 root root 29 11月 16 10:46 solc-0.4.25 - /root/.fisco/solc/solc-0.4.25*构建go-sdk的代码生成工具abigen
该工具用于将 abi 和 bin 文件转换为 go 文件 进入go-sdk目录编译生成abigen工具 [会生成一个abigen的二进制文件]
root192-168-19-133:/usr/project/goproject/go-sdk# go build ./cmd/abigen/
root192-168-19-133:/usr/project/goproject/go-sdk# ll | grep abigen
-rwxr-xr-x 1 root root 21519720 11月 16 11:33 abigen*若 上面步骤报错可以在go-sdk目录执行此命令把依赖重新下载导入一下
go mod tidy然后将此文件复制到helloworld项目,[此abigen可以复用以后想用abigen直接复制就可以了不需要再去go-sdk里再编译] cp -r abigen ../helloworld/编译生成go文件
在helloworld目录下执行命令编译
./abigen --bin ./HelloWorld.bin --abi ./HelloWorld.abi --pkg helloworld --type HelloWorld --out ./HelloWorld.go命令的意思是使用bin,abi文件在当前目录下 生成一个包为helloworld 类型为HelloWorld的HelloWorld.go文件
root192-168-19-133:/usr/project/goproject/helloworld# ll
drwxrwxrwx 4 root root 4096 11月 16 11:36 ./
drwxrwxrwx 8 root root 4096 11月 16 11:35 ../
-rwxrwxrwx 1 root root 21519720 11月 16 11:37 abigen*
-rwxrwxrwx 1 root root 531 11月 16 11:06 config.toml*
-rw-r--r-- 1 root root 1970 11月 16 11:31 go.mod
-rwxrwxrwx 1 root root 375 11月 16 10:49 HelloWorld.abi*
-rwxrwxrwx 1 root root 2010 11月 16 10:49 HelloWorld.bin*
-rwxrwxrwx 1 root root 13739 11月 16 11:03 HelloWorld.go*
-rwxrwxrwx 1 root root 294 11月 16 10:43 HelloWorld.sol*
lrwxrwxrwx 1 root root 29 11月 16 10:46 solc-0.4.25 - /root/.fisco/solc/solc-0.4.25*准备部署合约的相关文件
拷贝 config.toml 并修改配置
将 go-sdk目录下config.toml拷贝到helloworld目录下
cp -r ../go-sdk/config.toml .拷贝节点的sdk文件目录到helloworld目录下【根据自己的机子存放情况来执行命令】 拷贝账户私钥到helloworld目录下【前言有说明】
当前目录存在的文件
root192-168-19-133:/usr/project/goproject/helloworld# ll
总用量 21120
drwxrwxrwx 4 root root 4096 11月 16 12:41 ./
drwxrwxrwx 8 root root 4096 11月 16 11:35 ../
-rwxrwxrwx 1 root root 21519720 11月 16 11:37 abigen*
-rwxrwxrwx 1 root root 249 11月 16 11:05 admin.pem* ## 私钥
-rwxrwxrwx 1 root root 531 11月 16 11:06 config.toml* ## 配置文件
-rw-r--r-- 1 root root 1970 11月 16 11:31 go.mod # go mod文件
-rwxrwxrwx 1 root root 375 11月 16 10:49 HelloWorld.abi*
-rwxrwxrwx 1 root root 2010 11月 16 10:49 HelloWorld.bin*
-rwxrwxrwx 1 root root 13739 11月 16 11:03 HelloWorld.go*
-rwxrwxrwx 1 root root 294 11月 16 10:43 HelloWorld.sol*
drwxrwxrwx 2 root root 4096 11月 16 11:03 sdk/ # 节点连接证书
lrwxrwxrwx 1 root root 29 11月 16 10:46 solc-0.4.25 - /root/.fisco/solc/solc-0.4.25*修该config.toml [Network]
#type rpc or channel
Typechannel
# 三个节点证书使用相对路径
CAFile./sdk/ca.crt
Cert./sdk/sdk.crt
Key./sdk/sdk.key
# if the certificate context is not empty, use it, otherwise read from the certificate file
# multi lines use triple quotes
CAContext
KeyContext
CertContext[[Network.Connection]]
NodeURL127.0.0.1:20200 # 节点的地址
GroupID1 # 群组id
# [[Network.Connection]]
# NodeURL127.0.0.1:20200
# GroupID2[Account]
# only support PEM format for now
KeyFile./admin.pem #使用什么账户调用合约[Chain]
ChainID1 #链id
SMCryptofalse # 费国密[log]
Path./编写部署合约的go文件 并部署
在helloworld目录下创建cmd文件夹在cmd文件夹下创建一个main.go文件 代码如下
package mainimport (fmtloghelloworld //导入本地项目helloworldgithub.com/FISCO-BCOS/go-sdk/clientgithub.com/FISCO-BCOS/go-sdk/conf
)func main(){configs, err : conf.ParseConfigFile(config.toml) //读取config.toml文件if err ! nil {log.Fatal(err)}config : configs[0]client, err : client.Dial(config) //加载配置文件生成client进行相关链操作if err ! nil {log.Fatal(err)}address, tx, instance, err : helloworld.DeployHelloWorld(client.GetTransactOpts(), client) // 调用hellowrold的部署合约方法if err ! nil {log.Fatal(err)}fmt.Println(contract address: , address.Hex()) // 合约的地址fmt.Println(transaction hash: , tx.Hash().Hex()) //此次部署合约的交易hash_ instance
}然后在helloworld目录下执行依赖包导入命令
go mod tidy如果导入失败了可以复制go-sdk的go.mod和go.sum文件修改好项目名进行go mod tidy 还有确保GO111MODULE是on
此时项目的目录
root192-168-19-133:/usr/project/goproject/helloworld# ll
总用量 21120
drwxrwxrwx 4 root root 4096 11月 16 12:50 ./
drwxrwxrwx 8 root root 4096 11月 16 11:35 ../
-rwxrwxrwx 1 root root 21519720 11月 16 11:37 abigen*
-rwxrwxrwx 1 root root 249 11月 16 11:05 admin.pem*
drwxrwxrwx 2 root root 4096 11月 16 12:47 cmd/
-rwxrwxrwx 1 root root 531 11月 16 12:42 config.toml*
-rw-r--r-- 1 root root 1970 11月 16 11:31 go.mod
-rw-r--r-- 1 root root 46323 11月 16 11:31 go.sum
-rwxrwxrwx 1 root root 375 11月 16 10:49 HelloWorld.abi*
-rwxrwxrwx 1 root root 2010 11月 16 10:49 HelloWorld.bin*
-rwxrwxrwx 1 root root 13739 11月 16 11:03 HelloWorld.go*
-rwxrwxrwx 1 root root 294 11月 16 10:43 HelloWorld.sol*
drwxrwxrwx 2 root root 4096 11月 16 11:03 sdk/
lrwxrwxrwx 1 root root 29 11月 16 10:46 solc-0.4.25 - /root/.fisco/solc/solc-0.4.25*在helloworld 文件夹下执行命令 go run cmd/main.go
root192-168-19-133:/usr/project/goproject/helloworld# go run cmd/main.go
contract address: 0xA2132f9E796F954f4483A7078a357114F54D2f1B
transaction hash: 0x741fe11c3f1da7ad2ca43deb3b7045c170ce43aa5b3581bdfbf86a6fc323e331然后就获得了部署合约的地址部署成功
编写get/set方法的go文件并调用
根据官网的例子在helloworld文件夹下创建contract文件夹并在在contract文件夹下编写go文件helloworld_set_get.go文件
package mainimport (fmtloghelloworld //导入本地项目helloworldgithub.com/FISCO-BCOS/go-sdk/clientgithub.com/FISCO-BCOS/go-sdk/confgithub.com/ethereum/go-ethereum/common
)func main() {configs, err : conf.ParseConfigFile(config.toml) //读取配置文件if err ! nil {log.Fatal(err)}config : configs[0] client, err : client.Dial(config) //加载配置文件生成clientif err ! nil {log.Fatal(err)}// load the contractcontractAddress : common.HexToAddress(0xA2132f9E796F954f4483A7078a357114F54D2f1B) // 这里请放入刚刚部署的合约地址注意是你自己的机子部署的地址instance, err : helloworld.NewHelloWorld(contractAddress, client) //根据地址和client生成helloworld合约对象if err ! nil {log.Fatal(err)}helloworldSession : helloworld.HelloWorldSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()} //根据合约对象和client的call和transact进行实例化一个合约通信对象 helloworldSessionvalue, err : helloworldSession.Get() // 调用get方法if err ! nil {log.Fatal(err)}fmt.Println(value :, value)value Hello, HelloHellotx, receipt, err : helloworldSession.Set(value) // 调用set方法if err ! nil {log.Fatal(err)}fmt.Printf(tx sent: %s\n, tx.Hash().Hex()) //调用set方法的交易hashfmt.Printf(transaction hash of receipt: %s\n, receipt.GetTransactionHash()) //调用set方法的交易hash ,与上面的hash是一样只是存在不同的地方而已
}执行go文件,在helloworld目录下 go run contract/helloworld_set_get.go结果
root192-168-19-133:/usr/project/goproject/helloworld# go run contract/helloworld_set_get.go
value : 你好golang!
tx sent: 0x5a49ac1b48ebe717c75cbeb3a3144a031db8cfd5a0cdf7c38a151a87f1454583
transaction hash of receipt: 0x5a49ac1b48ebe717c75cbeb3a3144a031db8cfd5a0cdf7c38a151a87f1454583结语
相较于fisco的java-sdk go-sdk的使用起来还是比较困难因为webase也集成了java-sdk一键就能导出所有所需要的java项目文件。 若有空我将讲解如果结合goweb的框架集成fisco和使用go文件调用合约生成web项目【我比较倾向讲解beego框架】。