diff --git a/api/api.go b/api/api.go index 4fae8f8f..ec61664f 100644 --- a/api/api.go +++ b/api/api.go @@ -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) } diff --git a/mercury/client.go b/mercury/client.go index e88df7fb..1c075828 100644 --- a/mercury/client.go +++ b/mercury/client.go @@ -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) } @@ -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) { diff --git a/mercury/example/balance_example_test.go b/mercury/example/balance_example.go similarity index 100% rename from mercury/example/balance_example_test.go rename to mercury/example/balance_example.go diff --git a/mercury/example/get_transaction_info_example.go b/mercury/example/get_transaction_info_example.go index f3c53ee1..6737ebc1 100644 --- a/mercury/example/get_transaction_info_example.go +++ b/mercury/example/get_transaction_info_example.go @@ -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) @@ -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) + +} diff --git a/mercury/model/get_block_info_payload.go b/mercury/model/get_block_info_payload.go index 40e17a5f..4bc89486 100644 --- a/mercury/model/get_block_info_payload.go +++ b/mercury/model/get_block_info_payload.go @@ -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) { @@ -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 } diff --git a/mercury/model/resp/get_block_info_resp.go b/mercury/model/resp/get_block_info_resp.go index 9f7c4fc2..b763625a 100644 --- a/mercury/model/resp/get_block_info_resp.go +++ b/mercury/model/resp/get_block_info_resp.go @@ -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"` } diff --git a/mercury/model/resp/get_transaction_info_resp.go b/mercury/model/resp/get_transaction_info_resp.go index 09412c33..7d507d61 100644 --- a/mercury/model/resp/get_transaction_info_resp.go +++ b/mercury/model/resp/get_transaction_info_resp.go @@ -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" ) diff --git a/mercury/model/resp/query_generic_transactions_resp.go b/mercury/model/resp/query_generic_transactions_resp.go index e7836e08..4dbd1751 100644 --- a/mercury/model/resp/query_generic_transactions_resp.go +++ b/mercury/model/resp/query_generic_transactions_resp.go @@ -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"` } diff --git a/mercury/types.go b/mercury/types.go index 6530ce1c..879767e6 100644 --- a/mercury/types.go +++ b/mercury/types.go @@ -1,115 +1,108 @@ package mercury -import ( - "encoding/json" - "github.com/nervosnetwork/ckb-sdk-go/mercury/model/common" - "github.com/nervosnetwork/ckb-sdk-go/mercury/model/resp" - "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 rpcGetBalanceResponse struct { - Balances []*rpcBalanceResp `json:"balances"` -} +//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 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 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"` -} - -func toTransactionInfoWithStatusResponse(tx *rpcTransactionInfoWithStatusResponse) (*resp.TransactionInfoWithStatusResponse, error) { - transactionInfoResponse, err := toTransactionInfoResponse(tx.Transaction.Operations, tx.Transaction.TxHash) - if err != nil { - return nil, err - } - result := &resp.TransactionInfoWithStatusResponse{ - Status: tx.Status, - BlockHash: tx.BlockHash, - BlockNumber: tx.BlockNumber, - ConfirmedNumber: tx.ConfirmedNumber, - Transaction: transactionInfoResponse, - } - - return result, nil -} - -func toTransactionInfoResponse(txs []*rpcRecordResponse, txHash string) (*resp.TransactionInfoResponse, error) { - infoResponse := &resp.TransactionInfoResponse{TxHash: txHash} - for _, op := range txs { - - var asset *common.AssetInfo - if op.Amount.Status == common.CKB { - asset = common.NewCkbAsset() - } else { - asset = common.NewUdtAsset(op.Amount.UdtHash) - } - - var status map[resp.AssetStatus]uint - data, err := json.Marshal(op.Amount.Status) - if err != nil { - return nil, err - } - json.Unmarshal(data, status) - - var assetStatus resp.AssetStatus - var blockNumber uint - if _, ok := status[resp.FIXED]; ok { - assetStatus = resp.FIXED - blockNumber = status[resp.FIXED] - } else { - assetStatus = resp.CLAIMABLE - blockNumber = status[resp.FIXED] - } - - infoResponse.Operations = append(infoResponse.Operations, &resp.RecordResponse{ - op.Id, - op.KeyAddress, - op.Amount.Value, - asset, - assetStatus, - blockNumber, - }) - - } - - return infoResponse, nil -} +//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 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"` +//} + +//func toTransactionInfoWithStatusResponse(tx *rpcTransactionInfoWithStatusResponse) (*resp.GetTransactionInfoResponse, error) { +// transactionInfoResponse, err := toTransactionInfoResponse(tx.Transaction.Operations, tx.Transaction.TxHash) +// if err != nil { +// return nil, err +// } +// result := &resp.GetTransactionInfoResponse{ +// Status: tx.Status, +// BlockHash: tx.BlockHash, +// BlockNumber: tx.BlockNumber, +// ConfirmedNumber: tx.ConfirmedNumber, +// Transaction: transactionInfoResponse, +// } +// +// return result, nil +//} +// +//func toTransactionInfoResponse(txs []*rpcRecordResponse, txHash string) (*resp.TransactionInfo, error) { +// infoResponse := &resp.TransactionInfo{TxHash: txHash} +// for _, op := range txs { +// +// var asset *common.AssetInfo +// if op.Amount.Status == common.CKB { +// asset = common.NewCkbAsset() +// } else { +// asset = common.NewUdtAsset(op.Amount.UdtHash) +// } +// +// var status map[resp.AssetStatus]uint +// data, err := json.Marshal(op.Amount.Status) +// if err != nil { +// return nil, err +// } +// json.Unmarshal(data, status) +// +// var assetStatus resp.AssetStatus +// var blockNumber uint +// if _, ok := status[resp.FIXED]; ok { +// assetStatus = resp.FIXED +// blockNumber = status[resp.FIXED] +// } else { +// assetStatus = resp.CLAIMABLE +// blockNumber = status[resp.FIXED] +// } +// +// infoResponse.Operations = append(infoResponse.Operations, &resp.Record{ +// op.Id, +// op.KeyAddress, +// op.Amount.Value, +// asset, +// assetStatus, +// blockNumber, +// }) +// +// } +// +// return infoResponse, nil +//} // ----------------------------------------------------------------------------------------------------------------------------------------------- -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"` +//}