区块链实验室(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控制台获取。

相关推荐
掘根44 分钟前
【消息队列项目】客户端四大模块实现
开发语言·后端·ruby
疯狂的挖掘机7 小时前
记一次基于QT的图片操作处理优化思路(包括在图上放大缩小,截图,画线,取值等)
开发语言·数据库·qt
cnxy1887 小时前
围棋对弈Python程序开发完整指南:步骤4 - 提子逻辑和劫争规则实现
开发语言·python·机器学习
意趣新7 小时前
C 语言源文件从编写完成到最终生成可执行文件的完整、详细过程
c语言·开发语言
李艺为8 小时前
根据apk包名动态修改Android品牌与型号
android·开发语言
Yyyyy123jsjs8 小时前
外汇Tick数据交易时段详解与Python实战分析
人工智能·python·区块链
黄河滴滴8 小时前
java系统变卡变慢的原因是什么?从oom的角度分析
java·开发语言
老华带你飞9 小时前
农产品销售管理|基于java + vue农产品销售管理系统(源码+数据库+文档)
java·开发语言·前端·数据库·vue.js·spring boot·后端
superman超哥9 小时前
Rust Workspace 多项目管理:单体仓库的优雅组织
开发语言·rust·多项目管理·rust workspace·单体仓库
kylezhao20199 小时前
C#通过HSLCommunication库操作PLC用法
开发语言·c#