@@ -155,12 +155,44 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
155155 }
156156
157157 // Fetch all account records
158- recordsByType , err := common .GetLatestTokenAccountRecordsForOwner (ctx , s .data , owner )
158+ allRecordsByType , err := common .GetLatestTokenAccountRecordsForOwner (ctx , s .data , owner )
159159 if err != nil {
160160 log .WithError (err ).Warn ("failure getting latest account records" )
161161 return nil , status .Error (codes .Internal , "" )
162162 }
163163
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+
164196 var nextPoolIndex uint64
165197 latestPoolAccountInfoRecord , err := s .data .GetLatestAccountInfoByOwnerAddressAndType (ctx , owner .PublicKey ().ToBase58 (), commonpb .AccountType_POOL )
166198 switch err {
@@ -174,7 +206,7 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
174206 }
175207
176208 // 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 {
178210 if ! primaryRecords [0 ].General .RequiresDepositSync {
179211 primaryRecords [0 ].General .RequiresDepositSync = true
180212 err = s .data .UpdateAccountInfo (ctx , primaryRecords [0 ].General )
@@ -185,15 +217,15 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
185217 }
186218
187219 // Fetch balances
188- balanceMetadataByTokenAccount , err := s .fetchBalances (ctx , recordsByType )
220+ balanceMetadataByTokenAccount , err := s .fetchBalances (ctx , filteredRecordsByType )
189221 if err != nil {
190222 log .WithError (err ).Warn ("failure fetching balances" )
191223 return nil , status .Error (codes .Internal , "" )
192224 }
193225
194226 // Construct token account info
195227 tokenAccountInfos := make (map [string ]* accountpb.TokenAccountInfo )
196- for _ , batchRecords := range recordsByType {
228+ for _ , batchRecords := range filteredRecordsByType {
197229 for _ , records := range batchRecords {
198230 log := log .WithField ("token_account" , records .General .TokenAccount )
199231
@@ -220,8 +252,8 @@ func (s *server) GetTokenAccountInfos(ctx context.Context, req *accountpb.GetTok
220252 }
221253
222254 // 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 ]
225257
226258 switch tokenAccountInfo .ClaimState {
227259 case accountpb .TokenAccountInfo_CLAIM_STATE_CLAIMED , accountpb .TokenAccountInfo_CLAIM_STATE_EXPIRED :
0 commit comments