区块链实验室(22) - go-sdk访问Fisco的案例

在前面的案例中(区块链实验室(21) - Go语言采用SDK访问Fisco的案例),go程序调用FISCO SDK的参数固化在程序中,现将其改造如下。

go 复制代码
package main

import (
    "flag"
    "fmt"
    "log"

    "github.com/FISCO-BCOS/go-sdk/client"
    "github.com/FISCO-BCOS/go-sdk/conf"
    "github.com/ethereum/go-ethereum/common"
)

func main() {
    var nodeURL string
    var address string
    flag.StringVar(&nodeURL,"u","127.0.0.1:20200","node URL and port")
    flag.StringVar(&address,"a","","contract address")
    flag.Parse()

    if len(address) <= 0{
        log.Fatal("contract address is required.")
    }


    configs, err := conf.ParseConfigFile("conf/config.toml")
    if err != nil {
        log.Fatal(err)
    }
    config := &configs[0]
    config.NodeURL = nodeURL
    //fmt.Println("nodeurl:",config.NodeURL)
    client, err := client.Dial(config)
    if err != nil {
        log.Fatal(err)
    }

    // load the contract
    //contractAddress := common.HexToAddress("0xd70eac97a59b8317546f60618d1f9c2bb04d14ef") // 0x481D3A1dcD72cD618Ea768b3FbF69D78B46995b0
    contractAddress := common.HexToAddress(address)
    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"
    _, receipt, err := helloworldSession.Set(value)  // call set API
    if err != nil {
        log.Fatal(err)
    }

    //fmt.Printf("tx sent: %s\n", tx.Hash().Hex())
    fmt.Print("nodeurl ",config.NodeURL, " transactionhash ", receipt.GetTransactionHash())
}

该程序接收2个参数。第1个参数是节点的IP地址和channel端口,将其参数化,便可任意指定要连接的节点。第2个参数是智能合约的地址,可以在FISCO控制台获取。

相关推荐
大麦大麦19 分钟前
深入剖析 Sass:从基础到进阶的 CSS 预处理器应用指南
开发语言·前端·css·面试·rust·uni-app·sass
hhw19911241 分钟前
c#面试题整理6
java·开发语言·c#
蠟筆小新工程師1 小时前
Deepseek可以通过多种方式帮助CAD加速工作
开发语言·python·seepdeek
dingzd952 小时前
Web3 的去中心化治理:如何实现透明与公正
web3·去中心化·区块链·互联网·instagram·指纹浏览器
Benaso2 小时前
Java,Golang,Rust 泛型的大体对比小记
java·golang·rust
天道有情战天下2 小时前
python flask
开发语言·python·flask
帅弟1503 小时前
Day4 C语言与画面显示练习
c语言·开发语言
qhs15733 小时前
Kotlin字符串操作在Android开发中的应用示例
android·开发语言·kotlin
Stack Overflow?Tan904 小时前
c++实现在同一台主机两个程序实现实时通信
开发语言·c++
MZWeiei4 小时前
Scala:case class(通俗易懂版)
开发语言·后端·scala