-
Notifications
You must be signed in to change notification settings - Fork 116
Fix/safe price sorting #474
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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,5 +1,45 @@ | ||||||||||||||||||||||||||||||||||
| import type { ServiceEntry, AgentEntry, SortOption, AgentSortOption } from './types'; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Parse a USDC price string to a fixed-point integer (micro-USDC, 7 decimals). | ||||||||||||||||||||||||||||||||||
| * Stellar USDC has 7 decimal places, so "1.50" → 15_000_000 micro-USDC. | ||||||||||||||||||||||||||||||||||
| * Returns null for unparseable values (e.g. "abc", "0.001abc"). | ||||||||||||||||||||||||||||||||||
| * Exported for testing. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| export function parsePriceMicroUsdc(price: string): number | null { | ||||||||||||||||||||||||||||||||||
| if (typeof price !== 'string') return null; | ||||||||||||||||||||||||||||||||||
| const trimmed = price.trim(); | ||||||||||||||||||||||||||||||||||
| const match = trimmed.match(/^(\d+)(?:\.(\d+))?$/); | ||||||||||||||||||||||||||||||||||
| if (!match) return null; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| const intPart = match[1]; | ||||||||||||||||||||||||||||||||||
| const fracPart = (match[2] ?? '').padEnd(7, '0').slice(0, 7); | ||||||||||||||||||||||||||||||||||
| const combined = `${intPart}${fracPart}`; | ||||||||||||||||||||||||||||||||||
| const normalized = combined.replace(/^0+/, '') || '0'; | ||||||||||||||||||||||||||||||||||
| const result = Number(normalized); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return Number.isSafeInteger(result) && result >= 0 ? result : null; | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+15
to
+21
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win Reject prices with more than seven fractional digits. Line 16 silently truncates excess precision. For example, Proposed fix const intPart = match[1];
- const fracPart = (match[2] ?? '').padEnd(7, '0').slice(0, 7);
+ const fraction = match[2] ?? '';
+ if (fraction.length > 7) return null;
+ const fracPart = fraction.padEnd(7, '0');📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Compare two price strings as fixed-point integers. | ||||||||||||||||||||||||||||||||||
| * Unparseable prices sort last; never returns NaN. | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| function comparePrice(a: string, b: string): number { | ||||||||||||||||||||||||||||||||||
| const pa = parsePriceMicroUsdc(a); | ||||||||||||||||||||||||||||||||||
| const pb = parsePriceMicroUsdc(b); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (pa !== null && pb !== null) { | ||||||||||||||||||||||||||||||||||
| if (pa < pb) return -1; | ||||||||||||||||||||||||||||||||||
| if (pa > pb) return 1; | ||||||||||||||||||||||||||||||||||
| return 0; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // Unparseable sorts after parseable | ||||||||||||||||||||||||||||||||||
| if (pa !== null) return -1; | ||||||||||||||||||||||||||||||||||
| if (pb !== null) return 1; | ||||||||||||||||||||||||||||||||||
| return 0; // both unparseable — preserve original order | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * Sort services by the given option. | ||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||
|
|
@@ -16,7 +56,7 @@ export function sortServices( | |||||||||||||||||||||||||||||||||
| return b.reputation - a.reputation; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| if (sort === 'price') { | ||||||||||||||||||||||||||||||||||
| return parseFloat(a.price_usdc) - parseFloat(b.price_usdc); | ||||||||||||||||||||||||||||||||||
| return comparePrice(a.price_usdc, b.price_usdc); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| // 'newest' - highest registered_at first | ||||||||||||||||||||||||||||||||||
| return b.registered_at - a.registered_at; | ||||||||||||||||||||||||||||||||||
|
|
@@ -65,7 +105,7 @@ export function sortServicesWithTieBreaker( | |||||||||||||||||||||||||||||||||
| if (sort === 'reputation') { | ||||||||||||||||||||||||||||||||||
| result = b.reputation - a.reputation; | ||||||||||||||||||||||||||||||||||
| } else if (sort === 'price') { | ||||||||||||||||||||||||||||||||||
| result = parseFloat(a.price_usdc) - parseFloat(b.price_usdc); | ||||||||||||||||||||||||||||||||||
| result = comparePrice(a.price_usdc, b.price_usdc); | ||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||
| result = b.registered_at - a.registered_at; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
@@ -103,4 +143,4 @@ export function sortAgentsWithTieBreaker( | |||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
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.
🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
Repository: Stellar-Ecosystem/lodestar
Length of output: 951
🏁 Script executed:
Repository: Stellar-Ecosystem/lodestar
Length of output: 7318
Make the fixture satisfy
AgentEntry.makeAgentreturnsAgentEntry, but the object omits required fields:name,description,owner,successful_payments,failed_payments,total_volume_stroops,last_active,flagged, andflag_reason. Add defaults sooverrides: Partial<AgentEntry> = {}can still produce a completeAgentEntry.🤖 Prompt for AI Agents