-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathaccount.go
267 lines (231 loc) · 7.71 KB
/
account.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2018 The Fractal Team Authors
// This file is part of the fractal project.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package rpcapi
import (
"context"
"math/big"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/fractalplatform/fractal/accountmanager"
"github.com/fractalplatform/fractal/asset"
"github.com/fractalplatform/fractal/common"
)
type RPCAccount struct {
AcctName common.Name `json:"accountName"`
Founder common.Name `json:"founder"`
AccountID uint64 `json:"accountID"`
Number uint64 `json:"number"`
Nonce uint64 `json:"nonce"`
Code hexutil.Bytes `json:"code"`
CodeHash common.Hash `json:"codeHash"`
CodeSize uint64 `json:"codeSize"`
Threshold uint64 `json:"threshold"`
UpdateAuthorThreshold uint64 `json:"updateAuthorThreshold"`
AuthorVersion common.Hash `json:"authorVersion"`
Balances []*accountmanager.AssetBalance `json:"balances"`
Authors []*common.Author `json:"authors"`
Suicide bool `json:"suicide"`
Destroy bool `json:"destroy"`
Description string `json:"description"`
}
func NewRPCAccount(account *accountmanager.Account) *RPCAccount {
acctObject := RPCAccount{
AcctName: account.AcctName,
Founder: account.Founder,
AccountID: account.AccountID,
Number: account.Number,
Nonce: account.Nonce,
Code: hexutil.Bytes(account.Code),
CodeHash: account.CodeHash,
CodeSize: account.CodeSize,
Threshold: account.Threshold,
UpdateAuthorThreshold: account.UpdateAuthorThreshold,
AuthorVersion: account.AuthorVersion,
Balances: account.Balances,
Authors: account.Authors,
Suicide: account.Suicide,
Destroy: account.Destroy,
Description: account.Description,
}
return &acctObject
}
type AccountAPI struct {
b Backend
}
func NewAccountAPI(b Backend) *AccountAPI {
return &AccountAPI{b}
}
//AccountIsExist
func (api *AccountAPI) AccountIsExist(acctName common.Name) (bool, error) {
acct, err := api.b.GetAccountManager()
if err != nil {
return false, err
}
return acct.AccountIsExist(acctName)
}
func (api *AccountAPI) GetAccountExByID(accountID uint64) (*RPCAccount, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
accountObj, err := am.GetAccountById(accountID)
if err != nil {
return nil, err
}
var rpcAccountObj *RPCAccount
if accountObj != nil {
rpcAccountObj = NewRPCAccount(accountObj)
}
return rpcAccountObj, nil
}
//GetAccountByID
func (api *AccountAPI) GetAccountByID(accountID uint64) (*RPCAccount, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
accountObj, err := am.GetAccountById(accountID)
if err != nil {
return nil, err
}
var rpcAccountObj *RPCAccount
if accountObj != nil {
rpcAccountObj = NewRPCAccount(accountObj)
balances := make([]*accountmanager.AssetBalance, 0, len(rpcAccountObj.Balances))
zero := big.NewInt(0)
for _, balance := range rpcAccountObj.Balances {
if balance.Balance.Cmp(zero) > 0 {
balances = append(balances, &accountmanager.AssetBalance{AssetID: balance.AssetID, Balance: balance.Balance})
}
}
rpcAccountObj.Balances = balances
}
return rpcAccountObj, nil
}
func (api *AccountAPI) GetAccountExByName(accountName common.Name) (*RPCAccount, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
accountObj, err := am.GetAccountByName(accountName)
if err != nil {
return nil, err
}
var rpcAccountObj *RPCAccount
if accountObj != nil {
rpcAccountObj = NewRPCAccount(accountObj)
}
return rpcAccountObj, nil
}
//GetAccountByName
func (api *AccountAPI) GetAccountByName(accountName common.Name) (*RPCAccount, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
accountObj, err := am.GetAccountByName(accountName)
if err != nil {
return nil, err
}
var rpcAccountObj *RPCAccount
if accountObj != nil {
rpcAccountObj = NewRPCAccount(accountObj)
balances := make([]*accountmanager.AssetBalance, 0, len(rpcAccountObj.Balances))
zero := big.NewInt(0)
for _, balance := range rpcAccountObj.Balances {
if balance.Balance.Cmp(zero) > 0 {
balances = append(balances, &accountmanager.AssetBalance{AssetID: balance.AssetID, Balance: balance.Balance})
}
}
rpcAccountObj.Balances = balances
}
return rpcAccountObj, nil
}
//GetAccountBalanceByID
func (api *AccountAPI) GetAccountBalanceByID(accountName common.Name, assetID uint64, typeID uint64) (*big.Int, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
return am.GetAccountBalanceByID(accountName, assetID, typeID)
}
//GetCode
func (api *AccountAPI) GetCode(accountName common.Name) (hexutil.Bytes, error) {
acct, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
result, err := acct.GetCode(accountName)
if err != nil {
return nil, err
}
return (hexutil.Bytes)(result), nil
}
//GetNonce
func (api *AccountAPI) GetNonce(accountName common.Name) (uint64, error) {
acct, err := api.b.GetAccountManager()
if err != nil {
return 0, err
}
return acct.GetNonce(accountName)
}
//GetAssetInfoByName
func (api *AccountAPI) GetAssetInfoByName(ctx context.Context, assetName string) (*asset.AssetObject, error) {
acct, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
return acct.GetAssetInfoByName(assetName)
}
//GetAssetInfoByID
func (api *AccountAPI) GetAssetInfoByID(assetID uint64) (*asset.AssetObject, error) {
acct, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
return acct.GetAssetInfoByID(assetID)
}
//GetAssetAmountByTime
func (api *AccountAPI) GetAssetAmountByTime(assetID uint64, time uint64) (*big.Int, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
return am.GetAssetAmountByTime(assetID, time)
}
//GetAccountBalanceByTime
func (api *AccountAPI) GetAccountBalanceByTime(accountName common.Name, assetID uint64, typeID uint64, time uint64) (*big.Int, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return nil, err
}
return am.GetBalanceByTime(accountName, assetID, typeID, time)
}
//GetSnapshotLast get last snapshot time
func (api *AccountAPI) GetSnapshotLast() (uint64, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return 0, err
}
return am.GetSnapshotTime(0, 0)
}
//GetSnapshotTime m: 1 preview time 2 next time
func (api *AccountAPI) GetSnapshotTime(ctx context.Context, m uint64, time uint64) (uint64, error) {
am, err := api.b.GetAccountManager()
if err != nil {
return 0, err
}
return am.GetSnapshotTime(m, time)
}