-
Notifications
You must be signed in to change notification settings - Fork 2
/
call_service.go
562 lines (517 loc) · 18 KB
/
call_service.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package services
import (
"context"
"encoding/json"
"github.com/PositionExchange/posichain/eth/rpc"
"github.com/PositionExchange/posichain/hmy"
internal_common "github.com/PositionExchange/posichain/internal/common"
"github.com/PositionExchange/posichain/rosetta/common"
rpc2 "github.com/PositionExchange/posichain/rpc"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/pkg/errors"
)
var CallMethod = []string{
"hmyv2_call",
"hmyv2_getCode",
"hmyv2_getStorageAt",
"hmyv2_getDelegationsByDelegator",
"hmyv2_getDelegationsByDelegatorByBlockNumber",
"hmyv2_getDelegationsByValidator",
"hmyv2_getAllValidatorAddresses",
"hmyv2_getAllValidatorInformation",
"hmyv2_getAllValidatorInformationByBlockNumber",
"hmyv2_getElectedValidatorAddresses",
"hmyv2_getValidatorInformation",
"hmyv2_getCurrentUtilityMetrics",
"hmyv2_getMedianRawStakeSnapshot",
"hmyv2_getStakingNetworkInfo",
"hmyv2_getSuperCommittees",
}
type CallAPIService struct {
hmy *hmy.Harmony
publicContractAPI rpc.API
publicStakingAPI rpc.API
publicBlockChainAPI rpc.API
}
// Call implements the /call endpoint.
func (c *CallAPIService) Call(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
switch request.Method {
case "hmyv2_call":
return c.call(ctx, request)
case "hmyv2_getCode":
return c.getCode(ctx, request)
case "hmyv2_getStorageAt":
return c.getStorageAt(ctx, request)
case "hmyv2_getDelegationsByDelegator":
return c.getDelegationsByDelegator(ctx, request)
case "hmyv2_getDelegationsByDelegatorByBlockNumber":
return c.getDelegationsByDelegatorByBlockNumber(ctx, request)
case "hmyv2_getDelegationsByValidator":
return c.getDelegationsByValidator(ctx, request)
case "hmyv2_getAllValidatorAddresses":
return c.getAllValidatorAddresses(ctx)
case "hmyv2_getAllValidatorInformation":
return c.getAllValidatorInformation(ctx, request)
case "hmyv2_getAllValidatorInformationByBlockNumber":
return c.getAllValidatorInformationByBlockNumber(ctx, request)
case "hmyv2_getElectedValidatorAddresses":
return c.getElectedValidatorAddresses(ctx)
case "hmyv2_getValidatorInformation":
return c.getValidatorInformation(ctx, request)
case "hmyv2_getCurrentUtilityMetrics":
return c.getCurrentUtilityMetrics()
case "hmyv2_getMedianRawStakeSnapshot":
return c.getMedianRawStakeSnapshot()
case "hmyv2_getStakingNetworkInfo":
return c.getStakingNetworkInfo(ctx)
case "hmyv2_getSuperCommittees":
return c.getSuperCommittees()
}
return nil, common.NewError(common.ErrCallMethodInvalid, map[string]interface{}{
"message": "method not supported",
})
}
func NewCallAPIService(hmy *hmy.Harmony, limiterEnable bool, rateLimit int) server.CallAPIServicer {
return &CallAPIService{
hmy: hmy,
publicContractAPI: rpc2.NewPublicContractAPI(hmy, rpc2.V2),
publicStakingAPI: rpc2.NewPublicStakingAPI(hmy, rpc2.V2),
publicBlockChainAPI: rpc2.NewPublicBlockchainAPI(hmy, rpc2.V2, limiterEnable, rateLimit),
}
}
func (c *CallAPIService) getSuperCommittees() (*types.CallResponse, *types.Error) {
committees, err := c.hmy.GetSuperCommittees()
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get super committees error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": committees,
},
}, nil
}
func (c *CallAPIService) getStakingNetworkInfo(
ctx context.Context,
) (*types.CallResponse, *types.Error) {
publicBlockChainAPI := c.publicBlockChainAPI.Service.(*rpc2.PublicBlockchainService)
resp, err := publicBlockChainAPI.GetStakingNetworkInfo(ctx)
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get staking network info error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": resp,
},
}, nil
}
func (c *CallAPIService) getMedianRawStakeSnapshot() (*types.CallResponse, *types.Error) {
snapshot, err := c.hmy.GetMedianRawStakeSnapshot()
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get median raw stake snapshot error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": snapshot,
},
}, nil
}
func (c *CallAPIService) getCurrentUtilityMetrics() (*types.CallResponse, *types.Error) {
metric, err := c.hmy.GetCurrentUtilityMetrics()
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get current utility metrics error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": metric,
},
}, nil
}
func (c *CallAPIService) getValidatorInformation(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
args := GetValidatorInformationRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
resp, err := stakingAPI.GetValidatorInformation(ctx, args.ValidatorAddr)
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get validator information error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": resp,
},
}, nil
}
func (c *CallAPIService) getElectedValidatorAddresses(
ctx context.Context,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
addresses, err := stakingAPI.GetElectedValidatorAddresses(ctx)
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get elected validator addresses error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": addresses,
},
}, nil
}
func (c *CallAPIService) getAllValidatorInformationByBlockNumber(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
args := GetAllValidatorInformationByBlockNumberRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
resp, err := stakingAPI.GetAllValidatorInformationByBlockNumber(ctx, args.PageNumber, rpc2.BlockNumber(args.BlockNumber))
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get all validator information by block number error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": resp,
},
}, nil
}
func (c *CallAPIService) getAllValidatorInformation(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
args := GetAllValidatorInformationRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
resp, err := stakingAPI.GetAllValidatorInformation(ctx, args.PageNumber)
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get all validator information error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": resp,
},
}, nil
}
func (c *CallAPIService) getAllValidatorAddresses(
ctx context.Context,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
addresses, err := stakingAPI.GetAllValidatorAddresses(ctx)
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get all validator addresses error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": addresses,
},
}, nil
}
func (c *CallAPIService) getDelegationsByValidator(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
args := GetDelegationsByValidatorRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
resp, err := stakingAPI.GetDelegationsByValidator(ctx, args.ValidatorAddr)
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get delegations by validator error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": resp,
},
}, nil
}
func (c *CallAPIService) getDelegationsByDelegatorByBlockNumber(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
args := GetDelegationByDelegatorAddrAndBlockNumRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
addr, err := internal_common.Bech32ToAddress(args.DelegatorAddr)
if err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "delegator address error").Error(),
})
}
addOrList := rpc2.AddressOrList{Address: &addr}
resp, err := stakingAPI.GetDelegationsByDelegatorByBlockNumber(ctx, addOrList, rpc2.BlockNumber(args.BlockNum))
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get delegations by delegator and number error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": resp,
},
}, nil
}
func (c *CallAPIService) getDelegationsByDelegator(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
stakingAPI := c.publicStakingAPI.Service.(*rpc2.PublicStakingService)
args := GetDelegationsByDelegatorAddrRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
resp, err := stakingAPI.GetDelegationsByDelegator(ctx, args.DelegatorAddr)
if err != nil {
return nil, common.NewError(common.ErrGetStakingInfo, map[string]interface{}{
"message": errors.WithMessage(err, "get delegations by delegator error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": resp,
},
}, nil
}
func (c *CallAPIService) getStorageAt(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
contractAPI := c.publicContractAPI.Service.(*rpc2.PublicContractService)
args := GetStorageAtRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
res, err := contractAPI.GetStorageAt(ctx, args.Addr, args.Key, rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(args.BlockNum)))
if err != nil {
return nil, common.NewError(common.ErrCallExecute, map[string]interface{}{
"message": errors.WithMessage(err, "get storage at error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": res.String(),
},
}, nil
}
func (c *CallAPIService) getCode(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
contractAPI := c.publicContractAPI.Service.(*rpc2.PublicContractService)
args := GetCodeRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
code, err := contractAPI.GetCode(ctx, args.Addr, rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(args.BlockNum)))
if err != nil {
return nil, common.NewError(common.ErrCallExecute, map[string]interface{}{
"message": errors.WithMessage(err, "get code error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": code.String(),
},
}, nil
}
func (c *CallAPIService) call(
ctx context.Context, request *types.CallRequest,
) (*types.CallResponse, *types.Error) {
contractAPI := c.publicContractAPI.Service.(*rpc2.PublicContractService)
args := CallRequest{}
if err := args.UnmarshalFromInterface(request.Parameters); err != nil {
return nil, common.NewError(common.ErrCallParametersInvalid, map[string]interface{}{
"message": errors.WithMessage(err, "invalid parameters").Error(),
})
}
data, err := contractAPI.Call(ctx, args.CallArgs, rpc.BlockNumberOrHashWithNumber(rpc.BlockNumber(args.BlockNum)))
if err != nil {
return nil, common.NewError(common.ErrCallExecute, map[string]interface{}{
"message": errors.WithMessage(err, "call smart contract error").Error(),
})
}
return &types.CallResponse{
Result: map[string]interface{}{
"result": data.String(),
},
}, nil
}
type CallRequest struct {
rpc2.CallArgs
BlockNum int64 `json:"block_num"`
}
func (cr *CallRequest) UnmarshalFromInterface(args interface{}) error {
var callRequest CallRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &callRequest); err != nil {
return err
}
*cr = callRequest
return nil
}
type GetCodeRequest struct {
Addr string `json:"addr"`
BlockNum int64 `json:"block_num"`
}
func (cr *GetCodeRequest) UnmarshalFromInterface(args interface{}) error {
var getCodeRequest GetCodeRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getCodeRequest); err != nil {
return err
}
*cr = getCodeRequest
return nil
}
type GetStorageAtRequest struct {
Addr string `json:"addr"`
Key string `json:"key"`
BlockNum int64 `json:"block_num"`
}
func (sr *GetStorageAtRequest) UnmarshalFromInterface(args interface{}) error {
var getStorageAt GetStorageAtRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getStorageAt); err != nil {
return err
}
*sr = getStorageAt
return nil
}
type GetDelegationsByDelegatorAddrRequest struct {
DelegatorAddr string `json:"delegator_addr"`
}
func (r *GetDelegationsByDelegatorAddrRequest) UnmarshalFromInterface(args interface{}) error {
var getDelegationsByDelegatorAddrRequest GetDelegationsByDelegatorAddrRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getDelegationsByDelegatorAddrRequest); err != nil {
return err
}
*r = getDelegationsByDelegatorAddrRequest
return nil
}
type GetDelegationByDelegatorAddrAndBlockNumRequest struct {
DelegatorAddr string `json:"delegator_addr"`
BlockNum int64 `json:"block_num"`
}
func (r *GetDelegationByDelegatorAddrAndBlockNumRequest) UnmarshalFromInterface(args interface{}) error {
var getDelegationByDelegatorAddrAndBlockNumRequest GetDelegationByDelegatorAddrAndBlockNumRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getDelegationByDelegatorAddrAndBlockNumRequest); err != nil {
return err
}
*r = getDelegationByDelegatorAddrAndBlockNumRequest
return nil
}
type GetDelegationsByValidatorRequest struct {
ValidatorAddr string `json:"validator_addr"`
}
func (r *GetDelegationsByValidatorRequest) UnmarshalFromInterface(args interface{}) error {
var getDelegationsByValidatorRequest GetDelegationsByValidatorRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getDelegationsByValidatorRequest); err != nil {
return err
}
*r = getDelegationsByValidatorRequest
return nil
}
type GetAllValidatorInformationRequest struct {
PageNumber int `json:"page_number"`
}
func (r *GetAllValidatorInformationRequest) UnmarshalFromInterface(args interface{}) error {
var getAllValidatorInformationRequest GetAllValidatorInformationRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getAllValidatorInformationRequest); err != nil {
return err
}
*r = getAllValidatorInformationRequest
return nil
}
type GetAllValidatorInformationByBlockNumberRequest struct {
PageNumber int `json:"page_number"`
BlockNumber int64 `json:"block_number"`
}
func (r *GetAllValidatorInformationByBlockNumberRequest) UnmarshalFromInterface(args interface{}) error {
var getAllValidatorInformationByBlockNumber GetAllValidatorInformationByBlockNumberRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getAllValidatorInformationByBlockNumber); err != nil {
return err
}
*r = getAllValidatorInformationByBlockNumber
return nil
}
type GetValidatorInformationRequest struct {
ValidatorAddr string `json:"validator_addr"`
}
func (r *GetValidatorInformationRequest) UnmarshalFromInterface(args interface{}) error {
var getValidatorInformationRequest GetValidatorInformationRequest
data, err := json.Marshal(args)
if err != nil {
return err
}
if err := json.Unmarshal(data, &getValidatorInformationRequest); err != nil {
return err
}
*r = getValidatorInformationRequest
return nil
}