Add unit tests for account trustline health endpoint#180
Open
Silver36-ship-it wants to merge 3 commits into
Open
Add unit tests for account trustline health endpoint#180Silver36-ship-it wants to merge 3 commits into
Silver36-ship-it wants to merge 3 commits into
Conversation
- Implement tests for GET /account/:id/trustline-health to validate trustline data. - Cover various scenarios including trustline usage calculations, warnings for high usage, and handling of unauthorized trustlines. - Ensure correct handling of edge cases such as zero limits and accounts with no assets. - Validate response structure and data integrity for multiple trustlines and warnings.
|
@Silver36-ship-it Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
Contributor
|
Resolve Conflict in src/routes/account.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #156
#156
Closed
Pull Request: Add Account Trustline Health Dashboard Endpoint
Overview
This PR adds a new
GET /account/:id/trustline-healthendpoint that provides a comprehensive health overview of all trustlines on a Stellar account, including authorization status, liability usage, available capacity, and warnings for trustlines approaching their limits.Related Issue
Changes
New Endpoint
Endpoint:
GET /account/:id/trustline-healthFile:
src/routes/account.js(lines 1720-1780)Purpose: Returns a full health overview of all trustlines on an account with real-time metrics for monitoring asset exposure and capacity.
Features
✅ Account ID Validation - Validates Stellar account IDs (G-prefixed public keys)
✅ Trustline Health Data - Returns comprehensive metrics for each trustline:
assetCode- The asset code (e.g., USD, EUR, USDC)assetIssuer- The issuer's account public keybalance- Current balance of the assetlimit- Trustline limitbuyingLiabilities- Amount of XLM locked in open buy offerssellingLiabilities- Amount locked in open sell offersusagePercent- Usage percentage calculated as(balance + buyingLiabilities) / limit * 100availableCapacity- Remaining capacity:limit - balance - buyingLiabilitiesisAuthorized- Whether the trustline is authorized by the issuerisClawbackEnabled- Whether the issuer has clawback enabled for this trustlinewarning-"near_limit"if usage exceeds 90%, otherwisenull✅ Warning Detection - Flags trustlines where usage > 90% with
warning: "near_limit"✅ Warning Count - Aggregates total count of trustlines with warnings
✅ Native XLM Exclusion - Filters out native XLM balance; returns only asset trustlines
✅ Zero Limit Handling - Gracefully handles trustlines with zero limits
✅ Comprehensive Error Handling - Returns appropriate status codes and error messages
Response Structure
Success Response (200 OK)
{ "success": true, "data": { "accountId": "GAAZI4TCR3TY5OJHCTJC2A4QSY6CJWJH5IAJTGKIN2ER7LBNVKOCCWN", "trustlineCount": 2, "warningCount": 1, "trustlines": [ { "assetCode": "USD", "assetIssuer": "GBBD67CHI7LWB6C67GR77S3E5K5SNCZ275W6G3XF2A6F2A6F2A6F", "balance": "50.0000000", "limit": "100.0000000", "buyingLiabilities": "0.0000000", "sellingLiabilities": "10.0000000", "usagePercent": 50, "availableCapacity": "50.0000000", "isAuthorized": true, "isClawbackEnabled": false, "warning": null }, { "assetCode": "EUR", "assetIssuer": "GAHH7A6X7K4J5G4W5G4W5G4W5G4W5G4W5G4W5G4W5G4W5G4W5G4W5G4W", "balance": "95.0000000", "limit": "100.0000000", "buyingLiabilities": "0.0000000", "sellingLiabilities": "0.0000000", "usagePercent": 95, "availableCapacity": "5.0000000", "isAuthorized": true, "isClawbackEnabled": false, "warning": "near_limit" } ] } }Error Responses
Implementation Details
Algorithm
validateAccountId()server.loadAccount(id)(balance + buyingLiabilities) / limit * 100limit - balance - buyingLiabilities(minimum 0)"near_limit"if usage > 90%, otherwisenullKey Calculations
Files Modified
Added
src/routes/account.js- New endpoint handler (61 lines added)tests/account.trustlineHealth.test.js- Comprehensive test suite (13 test cases)Testing
Test Coverage
All 13 tests passing ✅
is_authorized: falseis_clawback_enabled: trueRunning Tests
npm test -- tests/account.trustlineHealth.test.jsTest Results:
Code Quality
Linting
✅ Passes ESLint with no errors in new code
Standards Compliance
successresponse wrapper)validateAccountId)API Documentation
Endpoint Summary
/account/:id/trustline-healthParameters
idResponse Fields
accountIdtrustlineCountwarningCounttrustlinesTrustline Health Object Fields
assetCodeassetIssuerbalancelimitbuyingLiabilitiessellingLiabilitiesusagePercentavailableCapacityisAuthorizedisClawbackEnabledwarning"near_limit"if usage > 90%, otherwisenullUse Cases
1. Wallet Dashboard
Monitor account trustline health in real-time to display warnings when approaching limits.
2. Risk Assessment
Identify accounts with high trustline usage to flag potential concentration risk.
3. Transaction Planning
Before sending assets, check available capacity on destination trustlines.
4. Portfolio Monitoring
Track liability exposure across all trusted assets in a single call.
5. Compliance & Reporting
Generate reports on asset exposure and trustline health across managed accounts.
Breaking Changes
None - This is a new endpoint. No existing functionality is affected.
Backward Compatibility
✅ Fully backward compatible - no modifications to existing endpoints.
Performance Considerations
loadAccountcall to Horizon)Security Considerations
validateAccountIdAdditional Notes
Future Enhancements
?minUsagePercent=Xto filter trustlines by usage threshold?sortBy=usage|balance|limitfor response orderingRelated Endpoints
GET /account/:id- Full account details including all trustlinesGET /account/:id/balances- Asset balances onlyGET /account/:id/can-receive/:assetCode/:assetIssuer- Receive capacity for specific assetGET /account/:id/freeze-status/:assetCode/:assetIssuer- Authorization status for specific assetChecklist
Review Requests
Questions for Reviewers
Author: GitHub Copilot
Date: May 29, 2026
Type: Feature
Severity: Low
Priority: Medium