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

相关推荐
LyaJpunov12 分钟前
C++中move和forword的区别
开发语言·c++
程序猿练习生17 分钟前
C++速通LeetCode中等第9题-合并区间
开发语言·c++·leetcode
一名路过的小码农27 分钟前
C/C++动态库函数导出 windows
c语言·开发语言·c++
m0_6312704029 分钟前
标准c语言(一)
c语言·开发语言·算法
万河归海42829 分钟前
C语言——二分法搜索数组中特定元素并返回下标
c语言·开发语言·数据结构·经验分享·笔记·算法·visualstudio
Messiah___35 分钟前
【论文阅读】Slim Fly: A Cost Effective Low-Diameter Network Topology 一种经济高效的小直径网络拓扑
开发语言·php
农民小飞侠1 小时前
python AutoGen接入开源模型xLAM-7b-fc-r,测试function calling的功能
开发语言·python
指尖流烟1 小时前
C#调用图表的使用方法
开发语言·c#
敲代码不忘补水1 小时前
Python 项目实践:简单的计算器
开发语言·python·json·项目实践
蒟蒻的贤1 小时前
Web APIs 第二天
开发语言·前端·javascript