/
ledger.go
153 lines (133 loc) · 4.67 KB
/
ledger.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package api
import (
"math/big"
"github.com/zenon-network/go-zenon/chain/nom"
"github.com/zenon-network/go-zenon/common/types"
"github.com/zenon-network/go-zenon/rpc/api"
"github.com/zenon-network/go-zenon/rpc/server"
)
type LedgerApi struct {
client *server.Client
}
func NewLedgerApi(client *server.Client) *LedgerApi {
return &LedgerApi{
client: client,
}
}
func (la *LedgerApi) PublishRawTransaction(transaction *nom.AccountBlock) error {
var ans error
// check that proto works
if err := la.client.Call(ans, "ledger.publishRawTransaction", transaction); err != nil {
return err
}
return ans
}
// Unconfirmed AccountBlocks
func (la *LedgerApi) GetUnconfirmedBlocksByAddress(address types.Address, pageIndex, pageSize uint32) (*api.AccountBlockList, error) {
ans := new(api.AccountBlockList)
if err := la.client.Call(ans, "ledger.getUnconfirmedBlocksByAddress", address.String(), pageIndex, pageSize); err != nil {
return nil, err
}
return ans, nil
}
// AccountBlocks
func (la *LedgerApi) GetFrontierAccountBlock(address types.Address) (*api.AccountBlock, error) {
ans := new(api.AccountBlock)
if err := la.client.Call(ans, "ledger.getFrontierAccountBlock", address.String()); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetAccountBlockByHash(blockHash types.Hash) (*api.AccountBlock, error) {
ans := new(api.AccountBlock)
if err := la.client.Call(ans, "ledger.getAccountBlockByHash", blockHash.String()); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetAccountBlocksByHeight(address types.Address, height, count uint64) (*api.AccountBlockList, error) {
ans := new(api.AccountBlockList)
if err := la.client.Call(ans, "ledger.getAccountBlocksByHeight", address.String(), height, count); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetAccountBlocksByPage(address types.Address, pageIndex, pageSize uint32) (*api.AccountBlockList, error) {
ans := new(api.AccountBlockList)
if err := la.client.Call(ans, "ledger.getAccountBlocksByPage", address.String(), pageIndex, pageSize); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetAccountInfoByAddress(address types.Address) (*api.AccountInfo, error) {
ans := new(api.AccountInfo)
if err := la.client.Call(ans, "ledger.getAccountBlocksByPage", address.String()); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetUnreceivedBlocksByAddress(address types.Address, pageIndex, pageSize uint32) (*api.AccountBlockList, error) {
ans := new(api.AccountBlockList)
if err := la.client.Call(ans, "ledger.getUnreceivedBlocksByAddress", address.String(), pageIndex, pageSize); err != nil {
return nil, err
}
return ans, nil
}
// Momentum
func (la *LedgerApi) GetFrontierMomentum() (*api.Momentum, error) {
ans := new(api.Momentum)
if err := la.client.Call(ans, "ledger.getFrontierMomentum"); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetMomentumBeforeTime(timestamp int64) (*api.Momentum, error) {
ans := new(api.Momentum)
if err := la.client.Call(ans, "ledger.getMomentumBeforeTime", timestamp); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetMomentumByHash(hash types.Hash) (*api.Momentum, error) {
ans := new(api.Momentum)
if err := la.client.Call(ans, "ledger.getMomentumByHash", hash.String()); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetMomentumsByHeight(height, count uint64) (*api.MomentumList, error) {
ans := new(api.MomentumList)
if err := la.client.Call(ans, "ledger.getMomentumsByHeight", height, count); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetMomentumsByPage(pageIndex, pageSize uint32) (*api.MomentumList, error) {
ans := new(api.MomentumList)
if err := la.client.Call(ans, "ledger.getMomentumsByPage", pageIndex, pageSize); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) GetDetailedMomentumsByHeight(height, count uint64) (*api.DetailedMomentumList, error) {
ans := new(api.DetailedMomentumList)
if err := la.client.Call(ans, "ledger.getDetailedMomentumsByHeight", height, count); err != nil {
return nil, err
}
return ans, nil
}
func (la *LedgerApi) SendTemplate(toAddress types.Address, tokenStandard types.ZenonTokenStandard, amount *big.Int, data []byte) *nom.AccountBlock {
return &nom.AccountBlock{
BlockType: nom.BlockTypeUserSend,
ToAddress: toAddress,
TokenStandard: tokenStandard,
Amount: amount,
Data: data,
}
}
func (la *LedgerApi) ReceiveTemplate(fromBlockHash types.Hash) *nom.AccountBlock {
return &nom.AccountBlock{
BlockType: nom.BlockTypeUserReceive,
FromBlockHash: fromBlockHash,
}
}