-
Notifications
You must be signed in to change notification settings - Fork 2
/
account.go
225 lines (195 loc) · 6.21 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
package services
import (
"context"
"fmt"
"math/big"
"github.com/PositionExchange/posichain/core/vm"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
ethCommon "github.com/ethereum/go-ethereum/common"
hmyTypes "github.com/PositionExchange/posichain/core/types"
"github.com/PositionExchange/posichain/eth/rpc"
"github.com/PositionExchange/posichain/hmy"
internalCommon "github.com/PositionExchange/posichain/internal/common"
"github.com/PositionExchange/posichain/rosetta/common"
)
// AccountAPI implements the server.AccountAPIServicer interface.
type AccountAPI struct {
hmy *hmy.Harmony
}
func (s *AccountAPI) AccountCoins(ctx context.Context, request *types.AccountCoinsRequest) (*types.AccountCoinsResponse, *types.Error) {
panic("implement me")
}
// NewAccountAPI creates a new instance of a BlockAPI.
func NewAccountAPI(hmy *hmy.Harmony) server.AccountAPIServicer {
return &AccountAPI{
hmy: hmy,
}
}
// AccountBalance implements the /account/balance endpoint
func (s *AccountAPI) AccountBalance(
ctx context.Context, request *types.AccountBalanceRequest,
) (*types.AccountBalanceResponse, *types.Error) {
if err := assertValidNetworkIdentifier(request.NetworkIdentifier, s.hmy.ShardID); err != nil {
return nil, err
}
var block *hmyTypes.Block
var rosettaError *types.Error
if request.BlockIdentifier == nil {
block = s.hmy.CurrentBlock()
} else {
block, rosettaError = getBlock(ctx, s.hmy, request.BlockIdentifier)
if rosettaError != nil {
return nil, rosettaError
}
}
addr, err := getAddress(request.AccountIdentifier)
if err != nil {
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": err.Error(),
})
}
blockNum := rpc.BlockNumber(block.Header().Header.Number().Int64())
var balance *big.Int
if request.AccountIdentifier.SubAccount != nil {
// indicate it may be a request for delegated balance
balance, rosettaError = s.getStakingBalance(request.AccountIdentifier.SubAccount, addr, block)
if rosettaError != nil {
return nil, rosettaError
}
} else {
balance, err = s.hmy.GetBalance(ctx, addr, rpc.BlockNumberOrHashWithNumber(blockNum))
if err != nil {
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid address",
})
}
}
amount := types.Amount{
Value: balance.String(),
Currency: &common.NativeCurrency,
}
respBlock := types.BlockIdentifier{
Index: blockNum.Int64(),
Hash: block.Header().Hash().String(),
}
return &types.AccountBalanceResponse{
BlockIdentifier: &respBlock,
Balances: []*types.Amount{&amount},
}, nil
}
// getStakingBalance used for get delegated balance with sub account identifier
func (s *AccountAPI) getStakingBalance(
subAccount *types.SubAccountIdentifier, addr ethCommon.Address, block *hmyTypes.Block,
) (*big.Int, *types.Error) {
balance := new(big.Int)
ty, exist := subAccount.Metadata["type"]
if !exist {
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid sub account",
})
}
validatorAddr := internalCommon.HexToAddress(subAccount.Address)
switch ty.(string) {
case Delegation:
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
if validatorAddr.Hex() == validator.Hex() {
balance = new(big.Int).Add(balance, delegations[index].Amount)
}
}
case UnDelegation:
validators, delegations := s.hmy.GetDelegationsByDelegatorByBlock(addr, block)
for index, validator := range validators {
if validatorAddr.Hex() == validator.Hex() {
undelegations := delegations[index].Undelegations
for _, undelegate := range undelegations {
balance = new(big.Int).Add(balance, undelegate.Amount)
}
}
}
default:
return nil, common.NewError(common.SanityCheckError, map[string]interface{}{
"message": "invalid sub account type",
})
}
return balance, nil
}
// AccountMetadata used for account identifiers
type AccountMetadata struct {
Address string `json:"hex_address"`
}
// newAccountIdentifier ..
func newAccountIdentifier(
address ethCommon.Address,
) (*types.AccountIdentifier, *types.Error) {
hexAddress := address.Hex()
metadata, err := types.MarshalMap(AccountMetadata{Address: hexAddress})
if err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": err.Error(),
})
}
return &types.AccountIdentifier{
Address: hexAddress,
Metadata: metadata,
}, nil
}
// newAccountIdentifier ..
func newRosettaAccountIdentifier(address *vm.RosettaLogAddressItem) (*types.AccountIdentifier, *types.Error) {
if address == nil || address.Account == nil {
return nil, nil
}
hexAddress := address.Account.Hex()
metadata, err := types.MarshalMap(AccountMetadata{Address: hexAddress})
if err != nil {
return nil, common.NewError(common.CatchAllError, map[string]interface{}{
"message": err.Error(),
})
}
ai := &types.AccountIdentifier{
Address: hexAddress,
Metadata: metadata,
}
if address.SubAccount != nil {
ai.SubAccount = &types.SubAccountIdentifier{
Address: address.SubAccount.Hex(),
Metadata: address.Metadata,
}
}
return ai, nil
}
func newSubAccountIdentifier(
address ethCommon.Address, metadata map[string]interface{},
) (*types.SubAccountIdentifier, *types.Error) {
return &types.SubAccountIdentifier{
Address: address.Hex(),
Metadata: metadata,
}, nil
}
func newAccountIdentifierWithSubAccount(
address, subAddress ethCommon.Address, metadata map[string]interface{},
) (*types.AccountIdentifier, *types.Error) {
accountIdentifier, err := newAccountIdentifier(address)
if err != nil {
return nil, err
}
subAccountIdentifier, err := newSubAccountIdentifier(subAddress, metadata)
if err != nil {
return nil, err
}
accountIdentifier.SubAccount = subAccountIdentifier
return accountIdentifier, nil
}
// getAddress ..
func getAddress(
identifier *types.AccountIdentifier,
) (ethCommon.Address, error) {
if identifier == nil {
return ethCommon.Address{}, fmt.Errorf("identifier cannot be nil")
}
if !ethCommon.IsHexAddress(identifier.Address) {
return ethCommon.Address{}, fmt.Errorf("identifier address is not hex format")
}
return ethCommon.HexToAddress(identifier.Address), nil
}