Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(mercury-rpc): Adjusting the get block info interface in v0.2.0 #67

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ func (cli *DefaultCkbApi) RegisterAddresses(normalAddresses []string) ([]string,
return cli.mercury.RegisterAddresses(normalAddresses)
}

func (cli *DefaultCkbApi) GetTransactionInfo(txHash string) (*resp.TransactionInfoWithStatusResponse, error) {
func (cli *DefaultCkbApi) GetTransactionInfo(txHash string) (*resp.GetTransactionInfoResponse, error) {
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
41 changes: 10 additions & 31 deletions mercury/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ type Client interface {
BuildAdjustAccountTransaction(payload *model.AdjustAccountPayload) (*resp.TransferCompletionResponse, error)
BuildAssetCollectionTransaction(payload *model.CollectAssetPayload) (*resp.TransferCompletionResponse, error)
RegisterAddresses(normalAddresses []string) ([]string, error)
GetTransactionInfo(txHash string) (*resp.TransactionInfoWithStatusResponse, error)
GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfoResponse, error)
GetTransactionInfo(txHash string) (*resp.GetTransactionInfoResponse, error)
GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfo, error)
QueryGenericTransactions(payload *model.QueryGenericTransactionsPayload) (*resp.QueryGenericTransactionsResponse, error)
}

Expand Down Expand Up @@ -92,44 +92,23 @@ 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.TransactionInfoWithStatusResponse, error) {
var tx *rpcTransactionInfoWithStatusResponse
err := cli.c.Call(&tx, "get_generic_transaction", txHash)
func (cli *client) GetTransactionInfo(txHash string) (*resp.GetTransactionInfoResponse, error) {
var tx *resp.GetTransactionInfoResponse
err := cli.c.Call(&tx, "get_transaction_info", txHash)
if err != nil {
return nil, err
}

result, err := toTransactionInfoWithStatusResponse(tx)
if err != nil {
return nil, err
}

return result, err
return tx, err
}

func (cli *client) QueryGenericTransactions(payload *model.QueryGenericTransactionsPayload) (*resp.QueryGenericTransactionsResponse, error) {
Expand Down
47 changes: 46 additions & 1 deletion mercury/example/get_transaction_info_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"encoding/json"
"fmt"
"github.com/nervosnetwork/ckb-sdk-go/mercury/example/constant"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model/resp"
"testing"
)

func TestGetGenericTransaction(t *testing.T) {
func TestGetTransaction(t *testing.T) {
transaction, err := constant.GetMercuryApiInstance().GetTransactionInfo("0x83b849ef0c2fc02eab20faad0357026d0f94b98444a4fe947a11bcbafa01b4e8")
if err != nil {
t.Error(err)
Expand All @@ -19,3 +20,47 @@ func TestGetGenericTransaction(t *testing.T) {
}
fmt.Println(string(json))
}

func TestGetCellBaseTransactions(t *testing.T) {
transaction, err := constant.GetMercuryApiInstance().GetTransactionInfo("0x9d3aff02b84c0d624d9eed265997d83c067cde554e9c4128806517fccc523e2f")
if err != nil {
t.Error(err)
}

json, err := json.Marshal(transaction)
if err != nil {
t.Error(err)
}
fmt.Println(string(json))
}

func TestGetDaoTransactions(t *testing.T) {
transaction, err := constant.GetMercuryApiInstance().GetTransactionInfo("0x94f30978b3d23b6d94dc559e8dc3e1b7a185712be26bba42f31543a08470035e")
if err != nil {
t.Error(err)
}

json, err := json.Marshal(transaction)
if err != nil {
t.Error(err)
}
fmt.Println(string(json))
}

func TestDaoInfo1(t *testing.T) {
jsonStr := "{\"Dao\":{\"state\":{\"Deposit\":100},\"reward\":100}}"
d := &resp.ExtraFilter{}
json.Unmarshal([]byte(jsonStr), &d)

fmt.Println(d)

}

func TestDaoInfo2(t *testing.T) {
jsonStr := "{\"Dao\":{\"state\":{\"Withdraw\":[100,1000]},\"reward\":1000}}"
d := &resp.ExtraFilter{}
json.Unmarshal([]byte(jsonStr), &d)

fmt.Println(d)

}
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 []*TransactionInfoResponse `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"`
}
141 changes: 124 additions & 17 deletions mercury/model/resp/get_transaction_info_resp.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,142 @@
package resp

import (
"encoding/json"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model/common"
"github.com/nervosnetwork/ckb-sdk-go/types"
"strings"
)

type TransactionInfoWithStatusResponse struct {
Transaction *TransactionInfoResponse
Status types.TransactionStatus
BlockHash string
BlockNumber uint64
ConfirmedNumber uint64
type GetTransactionInfoResponse struct {
Transaction *TransactionInfo `json:"transaction"`
Status types.TransactionStatus `json:"status"`
RejectReason uint8 `json:"reject_reason"`
}

type TransactionInfoResponse struct {
TxHash string
Operations []*RecordResponse
type TransactionInfo struct {
TxHash string `json:"tx_hash"`
Records []Record `json:"records"`
Fee int64 `json:"fee"`
Burn []*BurnInfo `json:"burn"`
}

type RecordResponse struct {
Id uint
Address string
Amount string
AssetInfo *common.AssetInfo
type BurnInfo struct {
UdtHash string `json:"udt_hash"`
Amount *model.U128 `json:"amount"`
}

type Record struct {
Id string `json:"id"`
AddressOrLockHash *common.AddressOrLockHash `json:"address_or_lock_hash"`
Amount *model.U128 `json:"amount"`
Occupied *model.U128 `json:"occupied"`
AssetInfo *common.AssetInfo `json:"asset_info"`
Status RecordStatus `json:"status"`
Extra ExtraFilter `json:"extra"`
BlockNumber uint64 `json:"block_number"`
EpochNumber []byte `json:"epoch_number"`
}

type RecordStatus struct {
Status AssetStatus
BlockNumber uint
BlockNumber uint64
}

func (r *RecordStatus) UnmarshalJSON(bytes []byte) error {
recordData := make(map[string]interface{})
json.Unmarshal(bytes, &recordData)

if _, ok := recordData["Claimable"]; ok {
blockNumber := recordData["Claimable"].(float64)
r.BlockNumber = uint64(blockNumber)
r.Status = Claimable
} else {
blockNumber := recordData["Fixed"].(float64)
r.BlockNumber = uint64(blockNumber)
r.Status = Fixed

}

return nil
}

type ExtraFilter struct {
DaoInfo *DaoInfo
ExtraType ExtraType
}

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

if strings.Contains(string(bytes), "CellBase") {
e.ExtraType = CellBase
} else {
ExtraFilterData := make(map[string]interface{})
json.Unmarshal(bytes, &ExtraFilterData)

DaoData := ExtraFilterData["Dao"].(map[string]interface{})
stateData := DaoData["state"].(map[string]interface{})
var depositBlockNumber uint64
var withdrawBlockNumber uint64
var state DaoState

if _, ok := stateData["Deposit"]; ok {
depositNumber := stateData["Deposit"].(float64)
depositBlockNumber = uint64(depositNumber)
state = Deposit

} else {
withdraw := stateData["Withdraw"].([]interface{})
depositNumber := withdraw[0].(float64)
withdrawNumber := withdraw[1].(float64)
depositBlockNumber = uint64(depositNumber)
withdrawBlockNumber = uint64(withdrawNumber)
state = Withdraw
}

reward := DaoData["reward"].(float64)

e.DaoInfo = &DaoInfo{
DepositBlockNumber: depositBlockNumber,
WithdrawBlockNumber: withdrawBlockNumber,
DaoState: state,
Reward: uint64(reward),
}

e.ExtraType = Dao

}

return nil
}

type DaoInfo struct {
DepositBlockNumber uint64
WithdrawBlockNumber uint64
DaoState DaoState
Reward uint64
}

type DaoState = string

const (
Deposit DaoState = "Deposit"
Withdraw DaoState = "Withdraw"
)

type ExtraType string

const (
Dao ExtraType = "Dao"
CellBase ExtraType = "CellBase"
)

type AssetStatus string

const (
CLAIMABLE AssetStatus = "claimable"
FIXED AssetStatus = "fixed"
Claimable AssetStatus = "Claimable"
Fixed AssetStatus = "Fixed"
)
6 changes: 3 additions & 3 deletions mercury/model/resp/query_generic_transactions_resp.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package resp

type QueryGenericTransactionsResponse struct {
Txs []*TransactionInfoResponse `json:"txs"`
TotalCount uint64 `json:"total_count"`
NextOffset uint64 `json:"next_offset"`
Txs []*TransactionInfo `json:"txs"`
TotalCount uint64 `json:"total_count"`
NextOffset uint64 `json:"next_offset"`
}
Loading