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

相关推荐
IvorySQL4 分钟前
PostgreSQL 日报| PostgreSQL JDBC 安全更新(10 月 10 日)
数据库·postgresql·区块链
知识分享小能手13 分钟前
C学习教程,从入门到精通,C语言输入和输出(4)
c语言·开发语言·学习
道尔柯南14 分钟前
C++ 入门基础详解:从第一个程序到核心语法特性
开发语言·c++
盛世宏博智慧档案15 分钟前
Modbus TCP+PoE以太网温湿度传感器项目部署全流程,附网络调试实操
开发语言·tcp/ip·php·以太网·传感器·温湿度
ACP广源盛1392462567318 分钟前
Type-C 扩展坞方案选型笔记|国产 DP1.2 转 HDMI2.0 桥接芯片 GSV2201D@ACP 评估
开发语言·笔记·硬件架构·硬件工程·hdmi·国产芯片
yuniko-n40 分钟前
【JUC】线程间通信
java·开发语言
yuniko-n42 分钟前
【JUC】关于 Java 线程介绍
java·开发语言
geovindu1 小时前
rust: Borg Pattern(续)
开发语言·后端·设计模式·rust·博格模式
aqiu1111113 小时前
【C++算法打怪专栏】8. 字符串转换整数 (atoi)
开发语言·c++·算法
爱吃香菜的初学者3 小时前
十四.Linux——消息队列
linux·运维·开发语言