-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconverters.go
More file actions
116 lines (94 loc) · 2.81 KB
/
converters.go
File metadata and controls
116 lines (94 loc) · 2.81 KB
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
package services
import (
"encoding/hex"
"math/big"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/multiversx/mx-chain-core-go/data/api"
"github.com/multiversx/mx-chain-rosetta/server/resources"
)
func blockToIdentifier(block *api.Block) *types.BlockIdentifier {
return &types.BlockIdentifier{
Index: int64(block.Nonce),
Hash: block.Hash,
}
}
func accountBlockCoordinatesToIdentifier(block resources.BlockCoordinates) *types.BlockIdentifier {
return &types.BlockIdentifier{
Index: int64(block.Nonce),
Hash: block.Hash,
}
}
func blockSummaryToIdentifier(blockSummary *resources.BlockSummary) *types.BlockIdentifier {
return &types.BlockIdentifier{
Index: int64(blockSummary.Nonce),
Hash: blockSummary.Hash,
}
}
func blockIdentifierToAccountQueryOptions(identifier *types.PartialBlockIdentifier) (resources.AccountQueryOptions, error) {
if identifier == nil {
return resources.NewAccountQueryOptionsOnFinalBlock(), nil
}
if identifier.Index != nil {
blockNonce := uint64(*identifier.Index)
return resources.NewAccountQueryOptionsWithBlockNonce(blockNonce), nil
}
if identifier.Hash != nil {
decodedHash, err := hex.DecodeString(*identifier.Hash)
if err != nil {
return resources.AccountQueryOptions{}, err
}
return resources.NewAccountQueryOptionsWithBlockHash(decodedHash), nil
}
return resources.NewAccountQueryOptionsOnFinalBlock(), nil
}
func addressToAccountIdentifier(address string) *types.AccountIdentifier {
return &types.AccountIdentifier{
Address: address,
}
}
func hashToTransactionIdentifier(hash string) *types.TransactionIdentifier {
return &types.TransactionIdentifier{
Hash: hash,
}
}
func indexToOperationIdentifier(index int) *types.OperationIdentifier {
return &types.OperationIdentifier{Index: int64(index)}
}
func timestampInMilliseconds(timestamp int64) int64 {
return timestamp * 1000
}
func getTimestampInMS(timestampSec int64, timestampMS int64) int64 {
if timestampMS > 0 {
return timestampMS
}
return timestampInMilliseconds(timestampSec)
}
func stringToHex(value string) string {
encoded := hex.EncodeToString([]byte(value))
encoded = ensureEvenLengthOfHexString(encoded)
return encoded
}
func ensureEvenLengthOfHexString(hexString string) string {
if len(hexString)%2 == 1 {
return "0" + hexString
}
return hexString
}
func amountToHex(value string) string {
bigAmount, ok := big.NewInt(0).SetString(value, 10)
if !ok {
log.Warn("amountToHex(): amount is not a number", "amount", value)
return ""
}
encoded := hex.EncodeToString(bigAmount.Bytes())
encoded = ensureEvenLengthOfHexString(encoded)
return encoded
}
func hexToAmount(hexString string) (string, error) {
amountBytes, err := hex.DecodeString(hexString)
if err != nil {
return "", err
}
amountBig := big.NewInt(0).SetBytes(amountBytes)
return amountBig.String(), nil
}