-
Notifications
You must be signed in to change notification settings - Fork 168
[ENG-1188] Move parent subaccount transfer filtering to SQL to respect limit parameter #3234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughA new DB query function, findAllToOrFromParentSubaccount, fetches transfers to/from child subaccounts of a parent while excluding transfers between same-parent children. The transfers controller now calls this DB function and tests were added/updated to cover filtering, pagination, and time/height constraints. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant Controller as Transfers Controller
participant DB as Transfer Table
participant Subaccount as Subaccount Table
Client->>Controller: GET /transfers?parentSubaccountNumber=X
Controller->>DB: findAllToOrFromParentSubaccount({ subaccountId, limit, page, ... })
rect rgb(235, 245, 255)
note right of DB: Server-side parent-subaccount flow
DB->>Subaccount: Join to map parent -> child subaccount IDs
Subaccount-->>DB: child subaccount IDs
DB->>DB: WHERE (to_id IN child_ids OR from_id IN child_ids)\nAND NOT (from_id IN child_ids AND to_id IN child_ids)
DB->>DB: Apply createdBeforeOrAt / createdBeforeOrAtHeight, order, limit/page
end
DB-->>Controller: Filtered transfers (cross-parent only)
Controller-->>Client: JSON response
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
indexer/packages/postgres/__tests__/stores/transfer-table.test.ts(3 hunks)indexer/packages/postgres/src/stores/transfer-table.ts(2 hunks)indexer/packages/postgres/src/types/query-types.ts(2 hunks)indexer/services/comlink/src/controllers/api/v4/transfers-controller.ts(5 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: hwray
PR: dydxprotocol/v4-chain#2551
File: protocol/x/subaccounts/keeper/subaccount.go:852-865
Timestamp: 2024-11-15T16:00:11.304Z
Learning: The function `GetCrossInsuranceFundBalance` in `protocol/x/subaccounts/keeper/subaccount.go` already existed and was just moved in this PR; changes to its error handling may be out of scope.
Learnt from: hwray
PR: dydxprotocol/v4-chain#2551
File: protocol/x/subaccounts/keeper/subaccount.go:833-850
Timestamp: 2024-11-15T15:59:28.095Z
Learning: The function `GetInsuranceFundBalance` in `protocol/x/subaccounts/keeper/subaccount.go` already existed and was just moved in this PR; changes to its error handling may be out of scope.
📚 Learning: 2024-11-15T16:00:11.304Z
Learnt from: hwray
PR: dydxprotocol/v4-chain#2551
File: protocol/x/subaccounts/keeper/subaccount.go:852-865
Timestamp: 2024-11-15T16:00:11.304Z
Learning: The function `GetCrossInsuranceFundBalance` in `protocol/x/subaccounts/keeper/subaccount.go` already existed and was just moved in this PR; changes to its error handling may be out of scope.
Applied to files:
indexer/packages/postgres/src/stores/transfer-table.ts
🧬 Code graph analysis (3)
indexer/services/comlink/src/controllers/api/v4/transfers-controller.ts (3)
indexer/packages/redis/__tests__/caches/constants.ts (1)
address(15-15)indexer/services/comlink/src/lib/errors.ts (1)
NotFoundError(15-20)indexer/services/comlink/src/types.ts (1)
ParentSubaccountTransferResponseObject(223-245)
indexer/packages/postgres/__tests__/stores/transfer-table.test.ts (3)
indexer/packages/postgres/__tests__/helpers/mock-generators.ts (1)
seedData(58-107)indexer/packages/postgres/src/helpers/db-helpers.ts (3)
migrate(176-178)clearData(143-159)teardown(180-186)indexer/packages/postgres/__tests__/helpers/constants.ts (10)
defaultSubaccountId(156-159)isolatedSubaccountId(176-179)defaultAsset(217-222)defaultTendermintEventId(495-499)createdDateTime(70-70)createdHeight(71-71)defaultSubaccountId2(160-163)defaultTendermintEventId2(500-504)defaultAddress(74-74)defaultWalletAddress(83-83)
indexer/packages/postgres/src/stores/transfer-table.ts (7)
indexer/packages/postgres/src/types/query-types.ts (2)
ParentSubaccountTransferQueryConfig(258-266)QueryConfig(111-114)indexer/packages/postgres/src/types/utility-types.ts (1)
Options(15-25)indexer/packages/postgres/src/constants.ts (1)
DEFAULT_POSTGRES_OPTIONS(124-127)indexer/packages/postgres/src/types/pagination-types.ts (1)
PaginationFromDatabase(1-6)indexer/packages/postgres/src/types/db-model-types.ts (1)
TransferFromDatabase(167-178)indexer/packages/postgres/src/helpers/stores-helpers.ts (2)
verifyAllRequiredFields(14-24)setupBaseQuery(47-60)indexer/packages/postgres/src/models/transfer-model.ts (1)
TransferModel(8-150)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (28)
- GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
- GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-vulcan / (vulcan) Build and Push
- GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-ecs-service-roundtable / (roundtable) Build and Push
- GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-ecs-service-socks / (socks) Build and Push
- GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-auxo-lambda / (auxo) Build and Push Lambda
- GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-ecs-service-ender / (ender) Build and Push
- GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-auxo-lambda / (auxo) Build and Push Lambda
- GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-bazooka-lambda / (bazooka) Build and Push Lambda
- GitHub Check: (Public Testnet) Build and Push ECS Services / call-build-and-push-ecs-service-comlink / (comlink) Build and Push
- GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-vulcan / (vulcan) Build and Push
- GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-ecs-service-comlink / (comlink) Build and Push
- GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-ecs-service-roundtable / (roundtable) Build and Push
- GitHub Check: call-build-ecs-service-vulcan / (vulcan) Check docker image build
- GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-ecs-service-ender / (ender) Build and Push
- GitHub Check: call-build-ecs-service-ender / (ender) Check docker image build
- GitHub Check: call-build-ecs-service-roundtable / (roundtable) Check docker image build
- GitHub Check: call-build-ecs-service-socks / (socks) Check docker image build
- GitHub Check: call-build-ecs-service-comlink / (comlink) Check docker image build
- GitHub Check: (Mainnet) Build and Push ECS Services / call-build-and-push-ecs-service-socks / (socks) Build and Push
- GitHub Check: check-build-auxo
- GitHub Check: check-build-bazooka
- GitHub Check: test / run_command
- GitHub Check: build-and-push-testnet
- GitHub Check: build-and-push-mainnet
- GitHub Check: run_command
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: Analyze (go)
- GitHub Check: Summary
Changelist
Bug Fix:
/transfers/parentSubaccountNumberreturning only ~10 results instead of respecting limitProblem:
The
/transfers/parentSubaccountNumberendpoint was incorrectly returning only 10-11 transfers even when no limit was specified (default should be 1000).Root Cause:
The endpoint had a two-step process:
When a parent subaccount had many internal transfers between its children, the database would return 1000 transfers (mostly same-parent), filter them out, and leave only ~10 cross-parent transfers.
Solution:
findAllToOrFromParentSubaccount()that filters same-parent transfers at the SQL levelsubaccountstable to filter transfers where both sender and recipient have the same address AND same parent subaccount number (subaccountNumber % 128)Changes:
New Database Function (
packages/postgres/src/stores/transfer-table.ts)findAllToOrFromParentSubaccount()with SQL-level filteringParentSubaccountTransferQueryConfiginterface(sender_sa.subaccountNumber % 128) = (recipient_sa.subaccountNumber % 128)Updated Controller (
indexer/services/comlink/src/controllers/api/v4/transfers-controller.ts)getTransfersForParentSubaccount()to use new database functionTest Plan
Key Test Cases
Manul Testing on Staging
curl "https://indexer.v4staging.dydx.exchange/v4/transfers/parentSubaccountNumber?address=dydx1nzuttarf5k2j0nug5yzhr6p74t9avehn9hlh8m&parentSubaccountNumber=0" | jq '{transferCount: (.transfers | length), totalResults: .totalResults, pageSize: .pageSize, offset: .offset}'
Now it retrieves up the API limit - 1,000
Author/Reviewer Checklist
state-breakinglabel.indexer-postgres-breakinglabel.PrepareProposalorProcessProposal, manually add the labelproposal-breaking.feature:[feature-name].backport/[branch-name].refactor,chore,bug.Summary by CodeRabbit
New Features
Behavior Changes
Tests