区块链实验室(21) - Go语言采用SDK访问Fisco的案例

本文以Fisco提供的go-sdk工具包,调用Fisco网络中已安装的智能合约。

1、安装Go语言

good@good:~/fisco$ sudo tar -C /usr/local -xzf  /media/good/share/go1.21.0.linux-amd64.tar.gz

2、配置Go语言,在/etc/profile最后添加如下代码

export DISPLAY=:0
export QT_X11_NO_MITSHM=1
export GOROOT=/usr/local/go
export GOARCH=amd64
export GOOS=linux
export GOPATH=/home/good/go
export GOBIN=$GOPATH/bin
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
xhost +

3、下载go-sdk、solc

good@good:~/fisco/nodes/127.0.0.1$ git clone https://gitee.com/FISCO-BCOS/go-sdk.git
good@good:~/fisco/nodes/127.0.0.1/go-sdk/tools$ ./download_solc.sh
good@good:~/fisco/nodes/127.0.0.1/go-sdk/tools$ mv solc-0.4.25 ../../helloworld/

4、编译abigen

good@good:~/fisco/nodes/127.0.0.1/go-sdk/cmd/abigen$ go mod init abigen
good@good:~/fisco/nodes/127.0.0.1/go-sdk/cmd/abigen$ go mod tidy
good@good:~/fisco/nodes/127.0.0.1/go-sdk/cmd/abigen$ go build .
good@good:~/fisco/nodes/127.0.0.1/go-sdk/cmd/abigen$ mv abigen ../../../helloworld/

5、编译智能合约

good@good:~/fisco/nodes/127.0.0.1/helloworld$ cp ../console/contracts/solidity/HelloWorld.sol .
good@good:~/fisco/nodes/127.0.0.1/helloworld$ ./solc-0.4.25 --abi --bin -o ./ ./HelloWorld.sol
good@good:~/fisco/nodes/127.0.0.1/helloworld$ ./abigen --bin ./HelloWorld.bin --abi ./HelloWorld.abi --pkg helloworld --type HelloWorld --out ./HelloWorld.go

6、准备配置文件和证书

good@good:~/fisco/nodes/127.0.0.1/helloworld$ mkdir conf
good@good:~/fisco/nodes/127.0.0.1/console/conf$ cp ca.crt ../../helloworld/conf/
good@good:~/fisco/nodes/127.0.0.1/console/conf$ cp sdk.crt ../../helloworld/conf/
good@good:~/fisco/nodes/127.0.0.1/console/conf$ cp sdk.key ../../helloworld/conf/
good@good:~/fisco/nodes/127.0.0.1/helloworld/conf$ gedit config.toml &

配置文件config.toml如下所示。

[Network]
Type="channel"
CAFile="conf/ca.crt"
Cert="conf/sdk.crt"
Key="conf/sdk.key"
[[Network.Connection]]
NodeURL="127.0.0.1:20200"
GroupID=1
# [[Network.Connection]]
# NodeURL="127.0.0.1:20200"
# GroupID=2

[Account]
KeyFile="/home/good/fisco/nodes/127.0.0.1/console/account/ecdsa/0x2d2e77cb1849268e5859e733bfbe364f41ace095.pem"

[Chain]
ChainID=1
SMCrypto=false

重要的地方是4个文件的路径,尤其是最后1个。本文直接用console控制台默认的私钥文件。

7、编写并编译调用智能合约的Go程序call

good@good:~/fisco/nodes/127.0.0.1/helloworld$ gedit call.go &
good@good:~/fisco/nodes/127.0.0.1/helloworld$ go mod init call
good@good:~/fisco/nodes/127.0.0.1/helloworld$ go mod tidy
good@good:~/fisco/nodes/127.0.0.1/helloworld$ go build .

call.go代码如下。

package main

import (
    "fmt"
    "log"

    "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("conf/config.toml")
    if err != nil {
        log.Fatal(err)
    }
    config := &configs[0]
    client, err := client.Dial(config)
    if err != nil {
        log.Fatal(err)
    }

    // load the contract
    contractAddress := common.HexToAddress("0x665c4d8bc52217ad9c884b857bcabf37fe464bf2") // 0x481D3A1dcD72cD618Ea768b3FbF69D78B46995b0
    instance, err := NewHelloWorld(contractAddress, client)
    if err != nil {
        log.Fatal(err)
    }

    helloworldSession := &HelloWorldSession{Contract: instance, CallOpts: *client.GetCallOpts(), TransactOpts: *client.GetTransactOpts()}

    value, err := helloworldSession.Get()    // call Get API
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("value :", value)

    value = "Hello, FISCO BCOS"
    tx, receipt, err := helloworldSession.Set(value)  // call set API
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("tx sent: %s\n", tx.Hash().Hex())
    fmt.Printf("transaction hash of receipt: %s\n", receipt.GetTransactionHash())
}

8、编译成功之后调用call

good@good:~/fisco/nodes/127.0.0.1/helloworld$ ./call
value : China
tx sent: 0x0667fcd754dc11a2879799b3e4a5ff7d7137dfddc17f1044bf159e8bbb97547b
transaction hash of receipt: 0x0667fcd754dc11a2879799b3e4a5ff7d7137dfddc17f1044bf159e8bbb97547b
good@good:~/fisco/nodes/127.0.0.1/helloworld$ ./call
value : Hello, FISCO BCOS
tx sent: 0xaada3383142c4d7c928c08a329e526e20112b6b9216068a6d0d72fb0b401fbb7
transaction hash of receipt: 0xaada3383142c4d7c928c08a329e526e20112b6b9216068a6d0d72fb0b401fbb7
good@good:~/fisco/nodes/127.0.0.1/helloworld$ 

每调用1次,产生1个区块,可以在控制台中观察到。

区块链实验室(21) - Go语言采用SDK访问Fisco的案例_第1张图片

写在最后,上述过程的代码主要来自Fisco技术文档,本文对源程序作了少量修改,使之能够编译、运行。上述过程不是一帆风顺的,中间经历了许多失败。最后把案例打包成1个压缩文件放在网盘中供下载。调整其中的一些路径,合约地址等参数,就可以使用。

实验环境如下

虚拟机文件:ubuntu20.04-Fisco

获取地址:https://pan.baidu.com/s/1RlrSy_OuT3B7TMT5AJEGmA?pwd=oava

联系方式:[email protected]

你可能感兴趣的:(FISCO,区块链,区块链,golang,开发语言)