forked from CovenantSQL/CovenantSQL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.go
96 lines (83 loc) · 2.68 KB
/
transactions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package api
import (
"context"
"errors"
"fmt"
"github.com/sourcegraph/jsonrpc2"
"github.com/CovenantSQL/CovenantSQL/api/models"
)
func init() {
rpc.RegisterMethod("bp_getTransactionList", bpGetTransactionList, bpGetTransactionListParams{})
rpc.RegisterMethod("bp_getTransactionByHash", bpGetTransactionByHash, bpGetTransactionByHashParams{})
rpc.RegisterMethod("bp_getTransactionListOfBlock", bpGetTransactionListOfBlock, bpGetTransactionListOfBlockParams{})
}
type bpGetTransactionListParams struct {
Since string `json:"since"`
Page int `json:"page"`
Size int `json:"size"`
}
func (params *bpGetTransactionListParams) Validate() error {
if params.Size > 1000 {
return errors.New("max size is 1000")
}
return nil
}
// BPGetTransactionListResponse is the response for method bp_getTransactionList.
type BPGetTransactionListResponse struct {
Transactions []*models.Transaction `json:"transactions"`
Pagination *models.Pagination `json:"pagination"`
}
func bpGetTransactionList(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
result interface{}, err error,
) {
params := ctx.Value("_params").(*bpGetTransactionListParams)
model := models.TransactionsModel{}
transactions, pagination, err := model.GetTransactionList(params.Since, params.Page, params.Size)
if err != nil {
return nil, err
}
result = &BPGetTransactionListResponse{
Transactions: transactions,
Pagination: pagination,
}
return result, nil
}
type bpGetTransactionListOfBlockParams struct {
BlockHeight int `json:"height"`
Page int `json:"page"`
Size int `json:"size"`
}
func (params *bpGetTransactionListOfBlockParams) Validate() error {
if params.BlockHeight < 1 {
return fmt.Errorf("invalid block height %d", params.BlockHeight)
}
if params.Size > 1000 {
return errors.New("max size is 1000")
}
return nil
}
func bpGetTransactionListOfBlock(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
result interface{}, err error,
) {
params := ctx.Value("_params").(*bpGetTransactionListOfBlockParams)
model := models.TransactionsModel{}
transactions, pagination, err := model.GetTransactionListOfBlock(params.BlockHeight, params.Page, params.Size)
if err != nil {
return nil, err
}
result = &BPGetTransactionListResponse{
Transactions: transactions,
Pagination: pagination,
}
return result, nil
}
type bpGetTransactionByHashParams struct {
Hash string `json:"hash"`
}
func bpGetTransactionByHash(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (
result interface{}, err error,
) {
params := ctx.Value("_params").(*bpGetTransactionByHashParams)
model := models.TransactionsModel{}
return model.GetTransactionByHash(params.Hash)
}