区块链-FISCO BCOS Go SDK使用(二)

上一章我们介绍了如何通过go-sdk去调用fisco-bcos上部署的合约,这一张主要介绍一下常用的一些go-sdk里的方法

  • GetBlockNumber:获取最新块高
  • GetBlockByNumber 根据块高获取区块的数据
    这两个方法如果在自己做区块链浏览器的时候,会经常用到,先获取块高,然后循环取得每个区块的数据
	privateKey, _ := hex.DecodeString("edab970c8c98fc5ecd3d6e8d46c96b30d1be924d4d88928d11fa5f29d1477fd9")
	config := &client.Config{IsSMCrypto: false, GroupID: "group0",
		PrivateKey: privateKey, Host: "192.168.1.135", Port: 20200, TLSCaFile: "conf/ca.crt", TLSKeyFile: "conf/sdk.key", TLSCertFile: "conf/sdk.crt"}
	client, err := client.DialContext(context.Background(), config)
	if err != nil {
		fmt.Println("DialContext err", err)
	}
	instance, err := facegateblock.NewFacegateblock(common.HexToAddress("0xa28ac30a792a59c3cd114a87a75193c6b8278d7e"), client)

	blocknum, err := client.GetBlockNumber(context.Background())
	if err != nil {
		fmt.Println("GetBlockNumber error", err)
	}
	for i := 1; i < int(blocknum); i++ {
		block, err := client.GetBlockByNumber(context.Background(), int64(i), true, true)
		if err != nil {
			fmt.Println("GetBlockByNumber err", err)
		}
		fmt.Println(block.Hash)
		fmt.Println(block.Number)
	}

  • GetTransactionByHash
  • GetTransactionReceipt
    这2个方法分别通过交易hash获取交易信息和回执。
txHash := common.HexToHash("0xb5414ac537853141906432d96e9c2e78b7cb2bcc56e0d4afdf9f592bb0ee9717")
	trans_detail, err := client.GetTransactionByHash(context.Background(), txHash, true)
	trans_detail, err := client.GetTransactionReceipt(context.Background(), txHash, true)

其中交易信息的结构如下:

type TransactionDetail struct {
	Abi              string   `json:"abi"`
	BlockLimit       int64    `json:"blockLimit"`
	ChainID          string   `json:"chainID"`
	From             string   `json:"from"`
	GroupID          string   `json:"groupID"`
	Hash             string   `json:"hash"`
	ImportTime       int64    `json:"importTime"`
	Input            string   `json:"input"`
	Nonce            string   `json:"nonce"`
	Signature        string   `json:"signature"`
	To               string   `json:"to"`
	TransactionProof []string `json:"txProof"`
	Version          uint64   `json:"version"`
}

交易回执的结构体如下:

type Receipt struct {
	BlockNumber     int       `json:"blockNumber"`
	ContractAddress string    `json:"contractAddress"`
	From            string    `json:"from"`
	GasUsed         string    `json:"gasUsed"`
	Hash            string    `json:"hash"`
	Input           string    `json:"input"`
	Logs            []*NewLog `json:"logEntries"`
	Message         string    `json:"message"`
	Output          string    `json:"output"`
	Status          int       `json:"status"`
	To              string    `json:"to"`
	TransactionHash string    `json:"transactionHash"`
	ReceiptProof    []string  `json:"txReceiptProof"`
	Version         uint64    `json:"version"`
}

可以根据自己的需要去获取自己想要的数据

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值