Skip to content

Commit ed310b4

Browse files
committed
Implement filters in GetTokenAccountInfos
1 parent fef2cc6 commit ed310b4

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ require (
66
filippo.io/edwards25519 v1.1.0
77
github.com/aws/aws-sdk-go-v2 v0.17.0
88
github.com/bits-and-blooms/bloom/v3 v3.1.0
9-
github.com/code-payments/code-protobuf-api v1.19.1-0.20250618155621-c66659ab4ff5
9+
github.com/code-payments/code-protobuf-api v1.19.1-0.20250704193024-5320c0a4c394
1010
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba
1111
github.com/emirpasic/gods v1.12.0
1212
github.com/envoyproxy/protoc-gen-validate v1.2.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
8080
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
8181
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
8282
github.com/cockroachdb/apd v1.1.0/go.mod h1:8Sl8LxpKi29FqWXR16WEFZRNSz3SoPzUzeMeY4+DwBQ=
83-
github.com/code-payments/code-protobuf-api v1.19.1-0.20250618155621-c66659ab4ff5 h1:fsZBRUCPGSZQngq/1rsJcEwhiYrZokr0wketRRTgGmI=
84-
github.com/code-payments/code-protobuf-api v1.19.1-0.20250618155621-c66659ab4ff5/go.mod h1:ee6TzKbgMS42ZJgaFEMG3c4R3dGOiffHSu6MrY7WQvs=
83+
github.com/code-payments/code-protobuf-api v1.19.1-0.20250704193024-5320c0a4c394 h1:N7jAqU3A0w4o55xT63ZW5xe14/4jpVb/59OzcW1xvj8=
84+
github.com/code-payments/code-protobuf-api v1.19.1-0.20250704193024-5320c0a4c394/go.mod h1:ee6TzKbgMS42ZJgaFEMG3c4R3dGOiffHSu6MrY7WQvs=
8585
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba h1:Bkp+gmeb6Y2PWXfkSCTMBGWkb2P1BujRDSjWeI+0j5I=
8686
github.com/code-payments/code-vm-indexer v0.1.11-0.20241028132209-23031e814fba/go.mod h1:jSiifpiBpyBQ8q2R0MGEbkSgWC6sbdRTyDBntmW+j1E=
8787
github.com/containerd/continuity v0.0.0-20190827140505-75bee3e2ccb6 h1:NmTXa/uVnDyp0TY5MKi197+3HWcnYWfnHGyaFthlnGw=

pkg/code/server/account/server.go

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)