/
account.go
208 lines (181 loc) · 6.6 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
package services
import (
"context"
rosettaFilecoinLib "github.com/zondax/rosetta-filecoin-lib"
"github.com/zondax/rosetta-filecoin-lib/actors"
"strconv"
"github.com/coinbase/rosetta-sdk-go/server"
"github.com/coinbase/rosetta-sdk-go/types"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api"
filTypes "github.com/filecoin-project/lotus/chain/types"
)
// AccountAPIService implements the server.BlockAPIServicer interface.
type AccountAPIService struct {
network *types.NetworkIdentifier
node api.FullNode
rosettaLib *rosettaFilecoinLib.RosettaConstructionFilecoin
}
// NewBlockAPIService creates a new instance of a BlockAPIService.
func NewAccountAPIService(network *types.NetworkIdentifier, node *api.FullNode, r *rosettaFilecoinLib.RosettaConstructionFilecoin) server.AccountAPIServicer {
return &AccountAPIService{
network: network,
node: *node,
rosettaLib: r,
}
}
// AccountBalance implements the /account/balance endpoint.
func (a AccountAPIService) AccountBalance(ctx context.Context,
request *types.AccountBalanceRequest) (*types.AccountBalanceResponse, *types.Error) {
errNet := ValidateNetworkId(ctx, &a.node, request.NetworkIdentifier)
if errNet != nil {
return nil, errNet
}
addr, filErr := address.NewFromString(request.AccountIdentifier.Address)
if filErr != nil {
return nil, BuildError(ErrInvalidAccountAddress, nil, true)
}
// Check sync status
status, syncErr := CheckSyncStatus(ctx, &a.node)
if syncErr != nil {
return nil, syncErr
}
if !status.IsSynced() {
return nil, BuildError(ErrNodeNotSynced, nil, true)
}
useHeadTipSet := false
var queryTipSet *filTypes.TipSet // TipSet to use on StateGetActor
var responseTipSet *filTypes.TipSet // TipSet to get queryTipSetHeight and queryTipSetHash values for response
var headTipSet *filTypes.TipSet // Chain's head TipSet
var fixedQueryHeight int64
var originalQueryHeight int64
// To return in response
var queryTipSetHeight int64
var queryTipSetHash *string
headTipSet, filErr = a.node.ChainHead(ctx)
if filErr != nil {
return nil, BuildError(ErrUnableToGetLatestBlk, filErr, true)
}
if request.BlockIdentifier != nil {
if request.BlockIdentifier.Index == nil {
return nil, BuildError(ErrInsufficientQueryInputs, nil, true)
}
originalQueryHeight = *request.BlockIdentifier.Index
// From lotus v1.5 and on, StateGetActor computes the state at parent's tipSet.
// To get the state on the requested height, we need to query the block at (height + 1).
// First, check if we're querying the head tipSet, if not, query the +1 tipSet
if originalQueryHeight == int64(headTipSet.Height()) {
useHeadTipSet = true
} else {
fixedQueryHeight = originalQueryHeight + 1
}
} else {
// If BlockIdentifier is not set, query chain's head tipSet
useHeadTipSet = true
}
if useHeadTipSet {
queryTipSet = headTipSet
responseTipSet, filErr = a.node.ChainGetTipSet(ctx, headTipSet.Parents())
if filErr != nil {
return nil, BuildError(ErrUnableToGetParentBlk, filErr, true)
}
} else {
queryTipSet, filErr = a.node.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(fixedQueryHeight), filTypes.EmptyTSK)
if filErr != nil {
return nil, BuildError(ErrUnableToGetBlk, filErr, true)
}
if queryTipSet.Height() == abi.ChainEpoch(originalQueryHeight) {
// Means that the tipset at originalQueryHeight + 1 has no blocks, so we need to skip it
fixedQueryHeight = originalQueryHeight + 2
// Repeat the call with the updated height
queryTipSet, filErr = a.node.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(fixedQueryHeight), filTypes.EmptyTSK)
if filErr != nil {
return nil, BuildError(ErrUnableToGetBlk, filErr, true)
}
}
responseTipSet, filErr = a.node.ChainGetTipSetByHeight(ctx, abi.ChainEpoch(originalQueryHeight), filTypes.EmptyTSK)
if filErr != nil {
return nil, BuildError(ErrUnableToGetBlk, filErr, true)
}
}
var balanceStr = "0"
queryTipSetHeight = int64(responseTipSet.Height())
queryTipSetHash, filErr = BuildTipSetKeyHash(responseTipSet.Key())
if filErr != nil {
return nil, BuildError(ErrUnableToBuildTipSetHash, filErr, true)
}
actor, err := a.node.StateGetActor(ctx, addr, queryTipSet.Key())
if err != nil {
// If actor is not found on chain, return 0 balance
return &types.AccountBalanceResponse{
BlockIdentifier: &types.BlockIdentifier{
Index: queryTipSetHeight,
Hash: *queryTipSetHash,
},
Balances: []*types.Amount{
{
Value: "0",
Currency: GetCurrencyData(),
},
},
}, nil
}
md := make(map[string]interface{})
if request.AccountIdentifier.SubAccount != nil {
// First, check if account is a multisig
if !a.rosettaLib.BuiltinActors.IsActor(actor.Code, actors.ActorMultisigName) {
return nil, BuildError(ErrAddNotMSig, nil, true)
}
switch request.AccountIdentifier.SubAccount.Address {
case LockedBalanceStr:
lockedBalance := actor.Balance
spendableBalance, err := a.node.MsigGetAvailableBalance(ctx, addr, queryTipSet.Key())
if err != nil {
return nil, BuildError(ErrUnableToGetBalance, err, true)
}
lockedBalance.Sub(lockedBalance.Int, spendableBalance.Int)
balanceStr = lockedBalance.String()
case SpendableBalanceStr:
spendableBalance, err := a.node.MsigGetAvailableBalance(ctx, addr, queryTipSet.Key())
if err != nil {
return nil, BuildError(ErrUnableToGetBalance, err, true)
}
balanceStr = spendableBalance.String()
case VestingScheduleStr:
vestingSch, err := a.node.MsigGetVestingSchedule(ctx, addr, queryTipSet.Key())
if err != nil {
return nil, BuildError(ErrUnableToGetVesting, err, true)
}
vestingMap := map[string]string{}
vestingMap[VestingStartEpochKey] = vestingSch.StartEpoch.String()
vestingMap[VestingUnlockDurationKey] = vestingSch.UnlockDuration.String()
vestingMap[VestingInitialBalanceKey] = vestingSch.InitialBalance.String()
md[VestingScheduleStr] = vestingMap
default:
return nil, BuildError(ErrMustSpecifySubAccount, nil, true)
}
} else {
// Get available balance (spendable + locked if multisig)
balanceStr = actor.Balance.String()
}
// Fill nonce
md[NonceKey] = strconv.FormatUint(actor.Nonce, 10)
resp := &types.AccountBalanceResponse{
BlockIdentifier: &types.BlockIdentifier{
Index: queryTipSetHeight,
Hash: *queryTipSetHash,
},
Balances: []*types.Amount{
{
Value: balanceStr,
Currency: GetCurrencyData(),
},
},
Metadata: md,
}
return resp, nil
}
func (a AccountAPIService) AccountCoins(ctx context.Context, request *types.AccountCoinsRequest) (*types.AccountCoinsResponse, *types.Error) {
return nil, ErrNotImplemented
}