Skip to content

Commit

Permalink
Merge pull request #67 from zhengjianhui/feat/adjusting-the-get-block…
Browse files Browse the repository at this point in the history
…-info-interface-in-v0.2.0
  • Loading branch information
fjchen7 authored Sep 26, 2021
2 parents d2d0307 + 1f4df73 commit f274a67
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 79 deletions.
2 changes: 1 addition & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (cli *DefaultCkbApi) GetTransactionInfo(txHash string) (*resp.GetTransactio
return cli.mercury.GetTransactionInfo(txHash)
}

func (cli *DefaultCkbApi) GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfoResponse, error) {
func (cli *DefaultCkbApi) GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfo, error) {
return cli.mercury.GetBlockInfo(payload)
}

Expand Down
25 changes: 5 additions & 20 deletions mercury/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Client interface {
BuildAssetCollectionTransaction(payload *model.CollectAssetPayload) (*resp.TransferCompletionResponse, error)
RegisterAddresses(normalAddresses []string) ([]string, error)
GetTransactionInfo(txHash string) (*resp.GetTransactionInfoResponse, error)
GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfoResponse, error)
GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfo, error)
QueryGenericTransactions(payload *model.QueryGenericTransactionsPayload) (*resp.QueryGenericTransactionsResponse, error)
GetDbInfo() (*resp.DBInfo, error)
GetMercuryInfo() (*resp.MercuryInfo, error)
Expand Down Expand Up @@ -114,29 +114,14 @@ func (cli *client) RegisterAddresses(normalAddresses []string) ([]string, error)
return scriptHash, err
}

func (cli *client) GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfoResponse, error) {
var block *rpcBlockInfoResponse
err := cli.c.Call(&block, "get_generic_block", payload)
func (cli *client) GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfo, error) {
var block resp.BlockInfo
err := cli.c.Call(&block, "get_block_info", payload)
if err != nil {
return nil, err
}

result := resp.BlockInfoResponse{
BlockNumber: block.BlockNumber,
BlockHash: block.BlockHash,
ParentBlockHash: block.ParentBlockHash,
Timestamp: block.Timestamp,
}

//for _, transaction := range block.Transactions {
// tx, err := toTransactionInfoResponse(transaction.Operations, transaction.TxHash)
// if err != nil {
// return nil, err
// }
// result.Transactions = append(result.Transactions, tx)
//}

return &result, err
return &block, err
}

func (cli *client) GetTransactionInfo(txHash string) (*resp.GetTransactionInfoResponse, error) {
Expand Down
File renamed without changes.
12 changes: 6 additions & 6 deletions mercury/model/get_block_info_payload.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package model

type GetBlockInfoPayload struct {
BlockNum uint64 `json:"block_num,omitempty"`
BlockHash string `json:"block_hash,omitempty"`
BlockNumber uint64 `json:"block_number,omitempty"`
BlockHash string `json:"block_hash,omitempty"`
}

type getBlockInfoPayloadBuilder struct {
blockNum uint64
blockHash string
blockNumber uint64
blockHash string
}

func (builder *getBlockInfoPayloadBuilder) AddBlockNumber(blockNumber uint64) {
builder.blockNum = blockNumber
builder.blockNumber = blockNumber
}

func (builder *getBlockInfoPayloadBuilder) AddBlockHash(blockHash string) {
Expand All @@ -20,7 +20,7 @@ func (builder *getBlockInfoPayloadBuilder) AddBlockHash(blockHash string) {

func (builder *getBlockInfoPayloadBuilder) Build() (*GetBlockInfoPayload, error) {
return &GetBlockInfoPayload{
builder.blockNum,
builder.blockNumber,
builder.blockHash,
}, nil
}
Expand Down
12 changes: 6 additions & 6 deletions mercury/model/resp/get_block_info_resp.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package resp

type BlockInfoResponse struct {
BlockNumber uint64 `json:"block_number"`
BlockHash string `json:"block_hash"`
ParentBlockHash string `json:"parent_block_hash"`
Timestamp uint64 `json:"timestamp"`
Transactions []*TransactionInfo `json:"transactions"`
type BlockInfo struct {
BlockNumber uint64 `json:"block_number"`
BlockHash string `json:"block_hash"`
ParentHash string `json:"parent_hash"`
Timestamp uint64 `json:"timestamp"`
Transactions []*TransactionInfo `json:"transactions"`
}
6 changes: 3 additions & 3 deletions mercury/model/resp/get_transaction_info_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package resp

import (
"encoding/json"
"fmt"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model/common"
"github.com/nervosnetwork/ckb-sdk-go/types"
Expand Down Expand Up @@ -47,7 +46,6 @@ type RecordStatus struct {
func (r *RecordStatus) UnmarshalJSON(bytes []byte) error {
recordData := make(map[string]interface{})
json.Unmarshal(bytes, &recordData)
fmt.Println(string(bytes))

if _, ok := recordData["Claimable"]; ok {
blockNumber := recordData["Claimable"].(float64)
Expand All @@ -69,7 +67,9 @@ type ExtraFilter struct {
}

func (e *ExtraFilter) UnmarshalJSON(bytes []byte) error {
fmt.Println(string(bytes))
if strings.Contains(string(bytes), "null") {
return nil
}

if strings.Contains(string(bytes), "CellBase") {
e.ExtraType = CellBase
Expand Down
82 changes: 39 additions & 43 deletions mercury/types.go
Original file line number Diff line number Diff line change
@@ -1,48 +1,44 @@
package mercury

import (
"github.com/nervosnetwork/ckb-sdk-go/types"
)

type rpcBalanceResp struct {
KeyAddress string `json:"key_address"`
UdtHash string `json:"udt_hash"`
Unconstrained string `json:"unconstrained"`
Fleeting string `json:"fleeting"`
Locked string `json:"locked"`
}
//type rpcBalanceResp struct {
// KeyAddress string `json:"key_address"`
// UdtHash string `json:"udt_hash"`
// Unconstrained string `json:"unconstrained"`
// Fleeting string `json:"fleeting"`
// Locked string `json:"locked"`
//}

type rpcGetBalanceResponse struct {
Balances []*rpcBalanceResp `json:"balances"`
}
//type rpcGetBalanceResponse struct {
// Balances []*rpcBalanceResp `json:"balances"`
//}

// -----------------------------------------------------------------------------------------------------------------------------------------------

type rpcTransactionInfoWithStatusResponse struct {
Transaction rpcTransactionInfoResponse `json:"transaction"`
Status types.TransactionStatus `json:"status"`
BlockHash string `json:"block_hash"`
BlockNumber uint64 `json:"block_number"`
ConfirmedNumber uint64 `json:"confirmed_number"`
}
//type rpcTransactionInfoWithStatusResponse struct {
// Transaction rpcTransactionInfoResponse `json:"transaction"`
// Status types.TransactionStatus `json:"status"`
// BlockHash string `json:"block_hash"`
// BlockNumber uint64 `json:"block_number"`
// ConfirmedNumber uint64 `json:"confirmed_number"`
//}

type rpcTransactionInfoResponse struct {
TxHash string `json:"tx_hash"`
Operations []*rpcRecordResponse `json:"operations"`
}
//type rpcTransactionInfoResponse struct {
// TxHash string `json:"tx_hash"`
// Operations []*rpcRecordResponse `json:"operations"`
//}

type rpcRecordResponse struct {
Id uint `json:"id"`
KeyAddress string `json:"key_address"`
NormalAddress string `json:"normal_address"`
Amount rpcAmountResp `json:"amount"`
}
//type rpcRecordResponse struct {
// Id uint `json:"id"`
// KeyAddress string `json:"key_address"`
// NormalAddress string `json:"normal_address"`
// Amount rpcAmountResp `json:"amount"`
//}

type rpcAmountResp struct {
Value string `json:"value"`
UdtHash string `json:"udt_hash"`
Status interface{} `json:"status"`
}
//type rpcAmountResp struct {
// Value string `json:"value"`
// UdtHash string `json:"udt_hash"`
// Status interface{} `json:"status"`
//}

//func toTransactionInfoWithStatusResponse(tx *rpcTransactionInfoWithStatusResponse) (*resp.GetTransactionInfoResponse, error) {
// transactionInfoResponse, err := toTransactionInfoResponse(tx.Transaction.Operations, tx.Transaction.TxHash)
Expand Down Expand Up @@ -103,10 +99,10 @@ type rpcAmountResp struct {
//}

// -----------------------------------------------------------------------------------------------------------------------------------------------
type rpcBlockInfoResponse struct {
BlockNumber uint64 `json:"block_number"`
BlockHash string `json:"block_hash"`
ParentBlockHash string `json:"parent_block_hash"`
Timestamp uint64 `json:"timestamp"`
Transactions []*rpcTransactionInfoResponse `json:"transactions"`
}
//type rpcBlockInfoResponse struct {
// BlockNumber uint64 `json:"block_number"`
// BlockHash string `json:"block_hash"`
// ParentBlockHash string `json:"parent_block_hash"`
// Timestamp uint64 `json:"timestamp"`
// Transactions []*rpcTransactionInfoResponse `json:"transactions"`
//}

0 comments on commit f274a67

Please sign in to comment.