环境: fisco2.8.0
go 1.17
go-sdk 1.0.0
solidity 0.4.25
Preface
Please start the four fisco nodes in advance.
Please prepare an account private key belonging to this fisco node [will be used later to call the contract and deploy the contract]
This article will explain how official documents use gosdk to deploy the helloworld contract and call its methods Contract development examples
Official website tips
Golang, the version must be no less than 1.13.6. This project uses go module for package management. For details, please refer to Using Go Modules, environment configuration
FISCO BCOS 2.2.0+, you need to run the FISCO BCOS blockchain platform in advance, please refer to the installation and construction
Solidity compilation Server, default version 0.4.25
gosdk pull
# 拉取代码
git clone https://github.com/FISCO-BCOS/go-sdk.git
# 若因为网络问题导致长时间无法执行上面的命令,请尝试以下命令:
git clone https://gitee.com/FISCO-BCOS/go-sdk.git
root@192-168-19-133:/usr/project/goproject# ll
drwxr-xr-x 17 root root 4096 11月 16 10:29 go-sdk/
tag version of go-sdk
root@192-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 <[email protected]>
Date: Thu May 12 17:22:46 2022 +0800
Merge pull request #148 from FISCO-BCOS/release-v1.0.0
Release v1.0.0
Create helloworld project
I chose not to follow the steps on the official website and not create it in the go-sdk directory. I chose to create the project under the working directory of gopath [this is more convenient for management]
root@192-168-19-133:/usr/project/goproject# mkdir helloworld
Then initialize the go project
root@192-168-19-133:/usr/project/goproject/helloworld# go mod init helloworld
go: creating new go.mod: module helloworld
Then in the helloworld directory, write a HelloWorld.sol contract
pragma solidity>=0.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;
}
}
Install solc compiler
To execute this command under the helloworld project, you need to use the download compiler script file in go-sdk. The directory of go-sdk needs to be selected according to the actual situation.
bash /usr/project/goproject/go-sdk/tools/download_solc.sh -v 0.4.25 # 它会做链接到当前目录下
root@192-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.25+commit.46d177ad.mod.Linux.g++
root@192-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*
Generate abi,bin statement
Use solc to compile. The command means to generate abi and bin files according to HelloWorld.sol to the current directory.
root@192-168-19-133:/usr/project/goproject/helloworld# ./solc-0.4.25 --bin --abi -o ./ ./HelloWorld.sol #
root@192-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*
Code generation tool abigen for building go-sdk
This tool is used to convert abi and bin files into go files
Enter the go-sdk directory, compile and generate the abigen tool [an abigen binary file will be generated]
root@192-168-19-133:/usr/project/goproject/go-sdk# go build ./cmd/abigen/
root@192-168-19-133:/usr/project/goproject/go-sdk# ll | grep abigen
-rwxr-xr-x 1 root root 21519720 11月 16 11:33 abigen*
If an error is reported in the above steps: You can execute this command in the go-sdk directory and re-download the dependencies and import them.
go mod tidy
Then copy this file to the helloworld project. [This abigen can be reused. If you want to use abigen in the future, just copy it directly. There is no need to go to go-sdk to compile again]
cp -r abigen ../helloworld/`
Compile and generate go files
Execute the command compilation in the helloworld directory
./abigen --bin ./HelloWorld.bin --abi ./HelloWorld.abi --pkg helloworld --type HelloWorld --out ./HelloWorld.go
The meaning of the command is to use the bin and abi files to generate a HelloWorld.go file with the package helloworld and the type HelloWorld in the current directory.
root@192-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*
Prepare relevant documents for deployment contract
Copy config.toml and modify the configuration
Copy config.toml in the go-sdk directory to the helloworld directory
cp -r ../go-sdk/config.toml .
Copy the node's sdk file directory to the helloworld directory [execute the command according to your own machine storage]
Copy the account private key to the helloworld directory [explanation in the preface] a>
Files that exist in the current directory
root@192-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*
Fix the config.toml
[Network]
#type rpc or channel
Type="channel"
# 三个节点证书,使用相对路径
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]]
NodeURL="127.0.0.1:20200" # 节点的地址
GroupID=1 # 群组id
# [[Network.Connection]]
# NodeURL="127.0.0.1:20200"
# GroupID=2
[Account]
# only support PEM format for now
KeyFile="./admin.pem" #使用什么账户调用合约
[Chain]
ChainID=1 #链id
SMCrypto=false # 费国密
[log]
Path="./"
Write the go file that deploys the contract and deploy it
Create a cmd folder in the helloworld directory, and create a main.go file under the cmd folder
The code is as follows
package main
import (
"fmt"
"log"
"helloworld" //导入本地项目helloworld
"github.com/FISCO-BCOS/go-sdk/client"
"github.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
}
Then execute the dependency package import command in the helloworld directory
go mod tidy
If the import fails, you can copy the go.mod and go.sum files of go-sdk, modify the project name, and proceedgo mod tidy
and make sureGO111MODULE
is on
The directory of the project at this time
root@192-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*
Execute the command under the helloworld foldergo run cmd/main.go
root@192-168-19-133:/usr/project/goproject/helloworld# go run cmd/main.go
contract address: 0xA2132f9E796F954f4483A7078a357114F54D2f1B
transaction hash: 0x741fe11c3f1da7ad2ca43deb3b7045c170ce43aa5b3581bdfbf86a6fc323e331
Then the address of the deployment contract is obtained, and the deployment is successful.
Write the go file of the get/set method and call it
According to the example on the official website, create the contract folder under the helloworld folder, and write the go file under the contract folderhelloworld_set_get.go
File
package main
import (
"fmt"
"log"
"helloworld" //导入本地项目helloworld
"github.com/FISCO-BCOS/go-sdk/client"
"github.com/FISCO-BCOS/go-sdk/conf"
"github.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) //加载配置文件,生成client
if err != nil {
log.Fatal(err)
}
// load the contract
contractAddress := 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进行实例化一个合约通信对象 helloworldSession
value, err := helloworldSession.Get() // 调用get方法
if err != nil {
log.Fatal(err)
}
fmt.Println("value :", value)
value = "Hello, Hello,Hello"
tx, receipt, err := helloworldSession.Set(value) // 调用set方法
if err != nil {
log.Fatal(err)
}
fmt.Printf("tx sent: %s\n", tx.Hash().Hex()) //调用set方法的交易hash
fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash()) //调用set方法的交易hash ,与上面的hash是一样,只是存在不同的地方而已
}
Execute the go file in the helloworld directory
go run contract/helloworld_set_get.go
result
root@192-168-19-133:/usr/project/goproject/helloworld# go run contract/helloworld_set_get.go
value : 你好,golang!
tx sent: 0x5a49ac1b48ebe717c75cbeb3a3144a031db8cfd5a0cdf7c38a151a87f1454583
transaction hash of receipt: 0x5a49ac1b48ebe717c75cbeb3a3144a031db8cfd5a0cdf7c38a151a87f1454583
Conclusion
Compared with fisco's java-sdk, go-sdk is still more difficult to use, because webase also integrates java-sdk, and all required java project files can be exported with one click.
If I have time, I will explain how to combine the goweb framework, integrate fisco and use go files to call contracts to generate web projects [I prefer to explain the beego framework].