Skip to content

Normalize approximation native token decimals#4125

Merged
squadgazzz merged 5 commits intomainfrom
normalize-approx-tokens
Feb 6, 2026
Merged

Normalize approximation native token decimals#4125
squadgazzz merged 5 commits intomainfrom
normalize-approx-tokens

Conversation

@squadgazzz
Copy link
Contributor

@squadgazzz squadgazzz commented Feb 4, 2026

Description

Instead of validating that native price approximation token pairs have matching decimals at startup, this PR normalizes prices based on the decimal difference between tokens, as was suggested in another PR comment[link].

Changes

  • Added ApproximationToken type that stores both the approximation address and a normalization factor (10^(to_decimals - from_decimals))
  • Factory fetches decimals for all approximation token pairs at startup and computes normalization factors
  • Price approximation now multiplies the fetched price by the normalization factor, correctly handling tokens with different decimals

How to test

Added unit tests for normalization in both directions (source > target decimals and target > source decimals)

@squadgazzz squadgazzz changed the base branch from main to shared-native-price-cache February 4, 2026 10:32
@squadgazzz squadgazzz changed the title Separate autopilot API native price estimator Normalize approximation native token decimals Feb 4, 2026
@squadgazzz squadgazzz marked this pull request as ready for review February 4, 2026 11:02
@squadgazzz squadgazzz requested a review from a team as a code owner February 4, 2026 11:02
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request normalizes price approximations for tokens with different decimals. It also includes an unrelated logic change to preserve the KeepPriceUpdated flag in the cache. This unrelated change should be extracted into a separate pull-request, in line with the guideline to avoid mixing logic changes with refactoring.

@squadgazzz
Copy link
Contributor Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces decimal normalization for native price approximations and includes an unrelated bug fix for preserving the KeepPriceUpdated cache flag. The decimal normalization logic appears correct and is well-tested. However, the unrelated bug fix should be moved to a separate pull request to adhere to the single responsibility principle for commits, as per the rule: 'Avoid mixing logic changes with refactoring in the same pull request. Defer logic changes to a separate PR.'

Copy link
Contributor

@MartinquaXD MartinquaXD left a comment

Choose a reason for hiding this comment

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

Math checks out.
Only minor nits.

Comment on lines 1161 to 703
// 5e-22 * 10^12 = 5e-10
assert!((price - 5e-10).abs() < 1e-18);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why don't we have a strict equality assertion here? Is there any loss of precision in the relevant code paths?

Copy link
Contributor Author

@squadgazzz squadgazzz Feb 6, 2026

Choose a reason for hiding this comment

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

There is precision loss after all: 10f64.powi(-12) isn't exactly representable. Updated the test.

@squadgazzz squadgazzz force-pushed the normalize-approx-tokens branch from 7da2820 to 941cb88 Compare February 6, 2026 09:06
@squadgazzz squadgazzz changed the base branch from shared-native-price-cache to main February 6, 2026 09:06
@squadgazzz squadgazzz enabled auto-merge February 6, 2026 15:03
@squadgazzz squadgazzz added this pull request to the merge queue Feb 6, 2026
Merged via the queue into main with commit a0b65c1 Feb 6, 2026
19 checks passed
@squadgazzz squadgazzz deleted the normalize-approx-tokens branch February 6, 2026 15:26
@github-actions github-actions bot locked and limited conversation to collaborators Feb 6, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants