Skip to content

Fix/safe price sorting - #474

Open
otobongdev wants to merge 3 commits into
Stellar-Ecosystem:mainfrom
otobongdev:fix/safe-price-sorting
Open

Fix/safe price sorting#474
otobongdev wants to merge 3 commits into
Stellar-Ecosystem:mainfrom
otobongdev:fix/safe-price-sorting

Conversation

@otobongdev

@otobongdev otobongdev commented Jul 29, 2026

Copy link
Copy Markdown

Closes #375

Summary by CodeRabbit

  • Bug Fixes
    • Improved service sorting by accurately comparing fixed-point USDC prices.
    • Malformed or invalid prices now appear after valid prices.
    • Preserved consistent ordering when valid or invalid prices have equal values.

@drips-wave

drips-wave Bot commented Jul 29, 2026

Copy link
Copy Markdown

@otobongdev 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! 🚀

Learn more about application limits

@coderabbitai

coderabbitai Bot commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

The change adds fixed-point USDC price parsing and applies it to service sorting. Invalid prices sort after valid prices without producing NaN. Tests cover malformed prices and update tie-breaker comparisons.

Changes

Service price sorting

Layer / File(s) Summary
Fixed-point price parsing and comparison
frontend/lib/sort.ts
Adds parsePriceMicroUsdc with seven-decimal precision and safe-integer validation. Invalid prices compare after valid prices.
Sorting integration and test coverage
frontend/lib/sort.ts, frontend/lib/sort.test.ts
Uses fixed-point comparison for service sorting and tie-breakers. Tests cover malformed prices and updated price comparisons.

Estimated code review effort: 2 (Simple) | ~10 minutes

Possibly related PRs

Suggested reviewers: ritik4ever, naninu123

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 inconclusive)

Check name Status Explanation Resolution
Linked Issues check ❓ Inconclusive The changes support fixed-point parsing, safe ordering, and malformed-price tests, but visible flagging of invalid prices is not verifiable. Confirm that unparseable prices are visibly flagged in the relevant service display or data representation.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: safer price sorting.
Out of Scope Changes check ✅ Passed The changes are limited to price comparison logic and its tests, which directly support issue #375.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ritik4ever

Copy link
Copy Markdown
Collaborator

Hi @otobongdev,

This PR could not be merged because it has merge conflicts with the target branch.

Please resolve the merge conflicts, push the updated changes, and the PR can be reviewed and merged.

Thank you!

@otobongdev
otobongdev requested a review from ritik4ever as a code owner August 3, 2026 10:49
@otobongdev

Copy link
Copy Markdown
Author

done

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@frontend/lib/sort.test.ts`:
- Around line 4-13: Update the makeAgent fixture to include defaults for the
required AgentEntry fields name, description, owner, successful_payments,
failed_payments, total_volume_stroops, last_active, flagged, and flag_reason,
while preserving the existing overrides spread so callers can replace any
default.

In `@frontend/lib/sort.ts`:
- Around line 15-21: Update the price parsing logic around match[2] and the
fixed-point conversion so fractional parts longer than seven digits return null
instead of being truncated by slice(0, 7). Preserve zero-padding for fractions
with seven or fewer digits and keep the existing safe-integer validation.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 68d465dd-bbc8-41dd-8a31-d86678b469f8

📥 Commits

Reviewing files that changed from the base of the PR and between 0546692 and cf26975.

⛔ Files ignored due to path filters (1)
  • frontend/package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • frontend/lib/sort.test.ts
  • frontend/lib/sort.ts

Comment thread frontend/lib/sort.test.ts
Comment on lines +4 to +13
function makeAgent(overrides: Partial<AgentEntry> = {}): AgentEntry {
return {
address: 'GAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAWFL',
score: 0,
total_payments: '0',
registered_at: '100',
active: true,
...overrides,
};
}

Copy link
Copy Markdown
Contributor

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:

#!/bin/bash
set -euo pipefail

# Inspect the factory and its declared return contract.
ast-grep outline frontend/lib/sort.test.ts --items all --match makeAgent
sed -n '4,14p' frontend/lib/sort.test.ts
sed -n '87,102p' frontend/lib/types.ts

Repository: Stellar-Ecosystem/lodestar

Length of output: 951


🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "Package version and tsconfig(s):"
if [ -f package.json ]; then
  jq -r '.devDependencies.typescript // .dependencies.typescript // empty' package.json
fi
fd -a 'tsconfig\.json$' . | sed 's#^\./##' | sort

echo
echo "TS availability:"
if command -v npx >/dev/null 2>&1; then
  npx tsc --version 2>/dev/null || true
fi
if command -v tsc >/dev/null 2>&1; then
  tsc --version 2>/dev/null || true
fi

echo
echo "Check TS diagnostic for sort.test.ts (read-only):"
if command -v tsc >/dev/null 2>&1; then
  tsc --noEmit --strict frontend/lib/sort.test.ts frontend/lib/types.ts 2>&1 || true
fi

Repository: Stellar-Ecosystem/lodestar

Length of output: 7318


Make the fixture satisfy AgentEntry.

makeAgent returns AgentEntry, but the object omits required fields: name, description, owner, successful_payments, failed_payments, total_volume_stroops, last_active, flagged, and flag_reason. Add defaults so overrides: Partial<AgentEntry> = {} can still produce a complete AgentEntry.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/lib/sort.test.ts` around lines 4 - 13, Update the makeAgent fixture
to include defaults for the required AgentEntry fields name, description, owner,
successful_payments, failed_payments, total_volume_stroops, last_active,
flagged, and flag_reason, while preserving the existing overrides spread so
callers can replace any default.

Comment thread frontend/lib/sort.ts
Comment on lines +15 to +21
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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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, "1.00000009" becomes the valid value "1.0000000" instead of sorting as an invalid price. Reject fractions longer than seven digits before fixed-point conversion.

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

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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;
const intPart = match[1];
const fraction = match[2] ?? '';
if (fraction.length > 7) return null;
const fracPart = fraction.padEnd(7, '0');
const combined = `${intPart}${fracPart}`;
const normalized = combined.replace(/^0+/, '') || '0';
const result = Number(normalized);
return Number.isSafeInteger(result) && result >= 0 ? result : null;
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@frontend/lib/sort.ts` around lines 15 - 21, Update the price parsing logic
around match[2] and the fixed-point conversion so fractional parts longer than
seven digits return null instead of being truncated by slice(0, 7). Preserve
zero-padding for fractions with seven or fewer digits and keep the existing
safe-integer validation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Frontend: parseFloat used for price sorting on currency strings

2 participants