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/implement get spent transaction #68

Merged
merged 3 commits into from
Sep 26, 2021
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
9 changes: 8 additions & 1 deletion api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type DefaultCkbApi struct {
mercury mercury.Client
}

func (cli *DefaultCkbApi) GetSpentTransactionWithTransactionInfo(payload *model.GetSpentTransactionPayload) (*resp.TransactionInfoWrapper, error) {
return cli.mercury.GetSpentTransactionWithTransactionInfo(payload)
}

func (cli *DefaultCkbApi) GetSpentTransactionWithTransactionView(payload *model.GetSpentTransactionPayload) (*resp.TransactionViewWrapper, error) {
return cli.mercury.GetSpentTransactionWithTransactionView(payload)
}

func (cli *DefaultCkbApi) GetTransactionProof(ctx context.Context, txHashes []string, blockHash *types.Hash) (*types.TransactionProof, error) {
return cli.ckb.GetTransactionProof(ctx, txHashes, blockHash)
}
Expand Down Expand Up @@ -233,7 +241,6 @@ func (cli *DefaultCkbApi) GetCellsCapacity(ctx context.Context, searchKey *index
return cli.ckb.GetCellsCapacity(ctx, searchKey)
}


func (cli *DefaultCkbApi) Close() {
cli.ckb.Close()
}
Expand Down
22 changes: 22 additions & 0 deletions mercury/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ type Client interface {
BuildAssetCollectionTransaction(payload *model.CollectAssetPayload) (*resp.TransferCompletionResponse, error)
RegisterAddresses(normalAddresses []string) ([]string, error)
GetTransactionInfo(txHash string) (*resp.GetTransactionInfoResponse, error)
GetSpentTransactionWithTransactionInfo(*model.GetSpentTransactionPayload) (*resp.TransactionInfoWrapper, error)
GetSpentTransactionWithTransactionView(*model.GetSpentTransactionPayload) (*resp.TransactionViewWrapper, error)
GetBlockInfo(payload *model.GetBlockInfoPayload) (*resp.BlockInfo, error)
QueryGenericTransactions(payload *model.QueryGenericTransactionsPayload) (*resp.QueryGenericTransactionsResponse, error)
GetDbInfo() (*resp.DBInfo, error)
Expand Down Expand Up @@ -133,6 +135,26 @@ func (cli *client) GetTransactionInfo(txHash string) (*resp.GetTransactionInfoRe
return tx, err
}

func (cli *client) GetSpentTransactionWithTransactionInfo(payload *model.GetSpentTransactionPayload) (*resp.TransactionInfoWrapper, error) {
payload.StructureType = model.DoubleEntry
var tx *resp.TransactionInfoWrapper
err := cli.c.Call(&tx, "get_spent_transaction", payload)
if err != nil {
return nil, err
}
return tx, err
}

func (cli *client) GetSpentTransactionWithTransactionView(payload *model.GetSpentTransactionPayload) (*resp.TransactionViewWrapper, error) {
payload.StructureType = model.Native
var tx *resp.TransactionViewWrapper
err := cli.c.Call(&tx, "get_spent_transaction", payload)
if err != nil {
return nil, err
}
return tx, err
}

func (cli *client) QueryGenericTransactions(payload *model.QueryGenericTransactionsPayload) (*resp.QueryGenericTransactionsResponse, error) {
var queryGenericTransactionsResponse resp.QueryGenericTransactionsResponse
err := cli.c.Call(&queryGenericTransactionsResponse, "query_generic_transactions", payload)
Expand Down
51 changes: 51 additions & 0 deletions mercury/example/get_spent_transaction_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package test

import (
"encoding/json"
"fmt"
"github.com/nervosnetwork/ckb-sdk-go/mercury/example/constant"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model/common"
"github.com/nervosnetwork/ckb-sdk-go/types"
"testing"
)

func TestGetSpentTransactionView(t *testing.T) {
payload := &model.GetSpentTransactionPayload{
OutPoint: common.OutPoint{
types.HexToHash("0xb2e952a30656b68044e1d5eed69f1967347248967785449260e3942443cbeece"),
01,
},
}

transactionView, err := constant.GetMercuryApiInstance().GetSpentTransactionWithTransactionView(payload)
if err != nil {
t.Error(err)
}

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

func TestGetSpentTransactionInfo(t *testing.T) {
payload := &model.GetSpentTransactionPayload{
OutPoint: common.OutPoint{
types.HexToHash("0xb2e952a30656b68044e1d5eed69f1967347248967785449260e3942443cbeece"),
01,
},
}

transactionInfo, err := constant.GetMercuryApiInstance().GetSpentTransactionWithTransactionInfo(payload)
if err != nil {
t.Error(err)
}

json, err := json.Marshal(transactionInfo)
if err != nil {
t.Error(err)
}
fmt.Println(string(json))
}
52 changes: 52 additions & 0 deletions mercury/model/common/transaction.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package common

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

type CellDep struct {
OutPoint OutPoint `json:"out_point"`
DepType types.DepType `json:"dep_type"`
}

type OutPoint struct {
TxHash types.Hash `json:"tx_hash"`
Index hexutil.Uint `json:"index"`
}

type CellInput struct {
Since hexutil.Uint64 `json:"since"`
PreviousOutput OutPoint `json:"previous_output"`
}

type CellOutput struct {
Capacity hexutil.Uint64 `json:"capacity"`
Lock *Script `json:"lock"`
Type *Script `json:"type"`
}

type Script struct {
CodeHash types.Hash `json:"code_hash"`
HashType types.ScriptHashType `json:"hash_type"`
Args hexutil.Bytes `json:"args"`
}

type Transaction struct {
Version hexutil.Uint `json:"version"`
Hash types.Hash `json:"hash"`
CellDeps []CellDep `json:"cell_deps"`
HeaderDeps []types.Hash `json:"header_deps"`
Inputs []CellInput `json:"inputs"`
Outputs []CellOutput `json:"outputs"`
OutputsData []hexutil.Bytes `json:"outputs_data"`
Witnesses []hexutil.Bytes `json:"witnesses"`
}

type TransactionWithStatus struct {
Transaction Transaction `json:"transaction"`
TxStatus struct {
BlockHash *types.Hash `json:"block_hash"`
Status types.TransactionStatus `json:"status"`
} `json:"tx_status"`
}
15 changes: 15 additions & 0 deletions mercury/model/get_spent_transaction_payload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package model

import "github.com/nervosnetwork/ckb-sdk-go/mercury/model/common"

type GetSpentTransactionPayload struct {
OutPoint common.OutPoint `json:"outpoint"`
StructureType StructureType `json:"structure_type"`
}

type StructureType string

const (
Native StructureType = "Native"
DoubleEntry StructureType = "DoubleEntry"
)
13 changes: 13 additions & 0 deletions mercury/model/resp/get_spent_transaction_resp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package resp

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

type TransactionInfoWrapper struct {
TransactionInfo TransactionInfo `json:"TransactionInfo"`
}

type TransactionViewWrapper struct {
TransactionView common.TransactionWithStatus `json:"TransactionView"`
}
10 changes: 7 additions & 3 deletions mercury/model/resp/mercury_info_resp.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package resp

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

type MercuryInfo struct {
MercuryVersion string `json:"mercury_version"`
CkbNodeVersion string `json:"ckb_node_version"`
Expand All @@ -17,7 +21,7 @@ const (
)

type Extension struct {
Name string `json:"name"`
Scripts []Script `json:"scripts"`
CellDeps []CellDep `json:"cell_deps"`
Name string `json:"name"`
Scripts []types.Script `json:"scripts"`
CellDeps []types.CellDep `json:"cell_deps"`
}
7 changes: 4 additions & 3 deletions mercury/model/resp/transcation.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resp

import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model/common"
"github.com/nervosnetwork/ckb-sdk-go/transaction"
"github.com/nervosnetwork/ckb-sdk-go/types"
)
Expand Down Expand Up @@ -68,7 +69,7 @@ func toTransaction(tx transactionResp) *types.Transaction {
}
}

func toCellDeps(deps []CellDep) []*types.CellDep {
func toCellDeps(deps []common.CellDep) []*types.CellDep {
result := make([]*types.CellDep, len(deps))
for i := 0; i < len(deps); i++ {
dep := deps[i]
Expand All @@ -83,7 +84,7 @@ func toCellDeps(deps []CellDep) []*types.CellDep {
return result
}

func toInputs(inputs []cellInput) []*types.CellInput {
func toInputs(inputs []common.CellInput) []*types.CellInput {
result := make([]*types.CellInput, len(inputs))
for i := 0; i < len(inputs); i++ {
input := inputs[i]
Expand All @@ -98,7 +99,7 @@ func toInputs(inputs []cellInput) []*types.CellInput {
return result
}

func toOutputs(outputs []cellOutput) []*types.CellOutput {
func toOutputs(outputs []common.CellOutput) []*types.CellOutput {
result := make([]*types.CellOutput, len(outputs))
for i := 0; i < len(outputs); i++ {
output := outputs[i]
Expand Down
44 changes: 9 additions & 35 deletions mercury/model/resp/transcation_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,17 @@ package resp

import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/nervosnetwork/ckb-sdk-go/mercury/model/common"
"github.com/nervosnetwork/ckb-sdk-go/types"
)

type transactionResp struct {
Version hexutil.Uint `json:"version"`
Hash types.Hash `json:"hash"`
CellDeps []CellDep `json:"cell_deps"`
HeaderDeps []types.Hash `json:"header_deps"`
Inputs []cellInput `json:"inputs"`
Outputs []cellOutput `json:"outputs"`
OutputsData []hexutil.Bytes `json:"outputs_data"`
Witnesses []hexutil.Bytes `json:"witnesses"`
}

type CellDep struct {
OutPoint outPoint `json:"out_point"`
DepType types.DepType `json:"dep_type"`
}

type outPoint struct {
TxHash types.Hash `json:"tx_hash"`
Index hexutil.Uint `json:"index"`
}

type cellInput struct {
Since hexutil.Uint64 `json:"since"`
PreviousOutput outPoint `json:"previous_output"`
}

type cellOutput struct {
Capacity hexutil.Uint64 `json:"capacity"`
Lock *Script `json:"lock"`
Type *Script `json:"type"`
}

type Script struct {
CodeHash types.Hash `json:"code_hash"`
HashType types.ScriptHashType `json:"hash_type"`
Args hexutil.Bytes `json:"args"`
Version hexutil.Uint `json:"version"`
Hash types.Hash `json:"hash"`
CellDeps []common.CellDep `json:"cell_deps"`
HeaderDeps []types.Hash `json:"header_deps"`
Inputs []common.CellInput `json:"inputs"`
Outputs []common.CellOutput `json:"outputs"`
OutputsData []hexutil.Bytes `json:"outputs_data"`
Witnesses []hexutil.Bytes `json:"witnesses"`
}