@@ -155,12 +155,44 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
155
155
}
156
156
157
157
// Fetch all account records
158
- recordsByType , err := common .GetLatestTokenAccountRecordsForOwner (ctx , s .data , owner )
158
+ allRecordsByType , err := common .GetLatestTokenAccountRecordsForOwner (ctx , s .data , owner )
159
159
if err != nil {
160
160
log .WithError (err ).Warn ("failure getting latest account records" )
161
161
return nil , status .Error (codes .Internal , "" )
162
162
}
163
163
164
+ switch req .Filter .(type ) {
165
+ case * accountpb.GetTokenAccountInfosRequest_FilterByTokenAddress ,
166
+ * accountpb.GetTokenAccountInfosRequest_FilterByAccountType :
167
+ if _ , ok := allRecordsByType [commonpb .AccountType_REMOTE_SEND_GIFT_CARD ]; ok {
168
+ return nil , status .Error (codes .InvalidArgument , "filter must be nil for gift card owner accounts" )
169
+ }
170
+ }
171
+
172
+ // Filter account records based on client request
173
+ //
174
+ // todo: this needs tests
175
+ filteredRecordsByType := make (map [commonpb.AccountType ][]* common.AccountRecords )
176
+ switch typed := req .Filter .(type ) {
177
+ case * accountpb.GetTokenAccountInfosRequest_FilterByTokenAddress :
178
+ filterAccount , err := common .NewAccountFromProto (typed .FilterByTokenAddress )
179
+ if err != nil {
180
+ log .WithError (err ).Warn ("invalid token address filter" )
181
+ return nil , status .Error (codes .Internal , "" )
182
+ }
183
+ for accountType , batchRecords := range allRecordsByType {
184
+ for _ , records := range batchRecords {
185
+ if records .General .TokenAccount == filterAccount .PublicKey ().ToBase58 () {
186
+ filteredRecordsByType [accountType ] = []* common.AccountRecords {records }
187
+ }
188
+ }
189
+ }
190
+ case * accountpb.GetTokenAccountInfosRequest_FilterByAccountType :
191
+ filteredRecordsByType [typed .FilterByAccountType ] = allRecordsByType [typed .FilterByAccountType ]
192
+ default :
193
+ filteredRecordsByType = allRecordsByType
194
+ }
195
+
164
196
var nextPoolIndex uint64
165
197
latestPoolAccountInfoRecord , err := s .data .GetLatestAccountInfoByOwnerAddressAndType (ctx , owner .PublicKey ().ToBase58 (), commonpb .AccountType_POOL )
166
198
switch err {
@@ -174,7 +206,7 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
174
206
}
175
207
176
208
// Trigger a deposit sync with the blockchain for the primary account, if it exists
177
- if primaryRecords , ok := recordsByType [commonpb .AccountType_PRIMARY ]; ok {
209
+ if primaryRecords , ok := filteredRecordsByType [commonpb .AccountType_PRIMARY ]; ok {
178
210
if ! primaryRecords [0 ].General .RequiresDepositSync {
179
211
primaryRecords [0 ].General .RequiresDepositSync = true
180
212
err = s .data .UpdateAccountInfo (ctx , primaryRecords [0 ].General )
@@ -185,15 +217,15 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
185
217
}
186
218
187
219
// Fetch balances
188
- balanceMetadataByTokenAccount , err := s .fetchBalances (ctx , recordsByType )
220
+ balanceMetadataByTokenAccount , err := s .fetchBalances (ctx , filteredRecordsByType )
189
221
if err != nil {
190
222
log .WithError (err ).Warn ("failure fetching balances" )
191
223
return nil , status .Error (codes .Internal , "" )
192
224
}
193
225
194
226
// Construct token account info
195
227
tokenAccountInfos := make (map [string ]* accountpb.TokenAccountInfo )
196
- for _ , batchRecords := range recordsByType {
228
+ for _ , batchRecords := range filteredRecordsByType {
197
229
for _ , records := range batchRecords {
198
230
log := log .WithField ("token_account" , records .General .TokenAccount )
199
231
@@ -220,8 +252,8 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
220
252
}
221
253
222
254
// Is this a gift card in a terminal state that we can cache?
223
- if _ , ok := recordsByType [commonpb .AccountType_REMOTE_SEND_GIFT_CARD ]; len (tokenAccountInfos ) == 1 && ok {
224
- tokenAccountInfo := tokenAccountInfos [recordsByType [commonpb .AccountType_REMOTE_SEND_GIFT_CARD ][0 ].General .TokenAccount ]
255
+ if _ , ok := filteredRecordsByType [commonpb .AccountType_REMOTE_SEND_GIFT_CARD ]; len (tokenAccountInfos ) == 1 && ok {
256
+ tokenAccountInfo := tokenAccountInfos [filteredRecordsByType [commonpb .AccountType_REMOTE_SEND_GIFT_CARD ][0 ].General .TokenAccount ]
225
257
226
258
switch tokenAccountInfo .ClaimState {
227
259
case accountpb .TokenAccountInfo_CLAIM_STATE_CLAIMED , accountpb .TokenAccountInfo_CLAIM_STATE_EXPIRED :
0 commit comments