上一章我们介绍了如何通过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"`
}
可以根据自己的需要去获取自己想要的数据