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

相关推荐
侃侃_天下2 天前
最终的信号类
开发语言·c++·算法
echoarts2 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
zhang_xiaoyu582 天前
第十届99全球链商节重点项目“全球纸基生态战略联盟”正式签约
区块链
文火冰糖的硅基工坊2 天前
《投资-54》数字资产的形式有哪些?
人工智能·区块链
Aomnitrix2 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题2 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说2 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔2 天前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号2 天前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_2 天前
QT(4)
开发语言·汇编·c++·qt·算法