Skip to content

fix(nexchange): normalize swap native amounts to integers#452

Merged
samholmes merged 1 commit intomasterfrom
sam/nexchange-nativeAmount-bug
Apr 10, 2026
Merged

fix(nexchange): normalize swap native amounts to integers#452
samholmes merged 1 commit intomasterfrom
sam/nexchange-nativeAmount-bug

Conversation

@samholmes
Copy link
Copy Markdown
Contributor

@samholmes samholmes commented Apr 8, 2026

N.exchange can return decimal amounts with more precision than the target asset supports, which leaks fractional values into native amount fields.

Rounding to integer atomic units in this plugin keeps spend targets and saved swap metadata valid for Edge amount expectations.

CHANGELOG

Does this branch warrant an entry to the CHANGELOG?

  • Yes
  • No

Dependencies

none

Description

none

Note

Medium Risk
Touches swap amount calculation used to build spendTargets and saved swap metadata; incorrect rounding could cause under/over-payment or mismatched expectations. Change is small and uses conservative floor-to-integer behavior to avoid fractional native amounts.

Overview
Normalizes N.exchange order deposit_amount/withdraw_amount conversions to integer native (atomic) units by applying floor(..., 0) after denominationToNative.

This prevents fractional native amounts (from provider over-precision) from leaking into spendTargets and persisted swap fromAsset/toAsset amounts.

Reviewed by Cursor Bugbot for commit 6acb8fd. Bugbot is set up for automated code reviews on this repo. Configure here.


@samholmes samholmes force-pushed the sam/nexchange-nativeAmount-bug branch from 874bc8b to 0c99910 Compare April 9, 2026 00:08
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix prepared a fix for the issue found in the latest run.

  • ✅ Fixed: Using round instead of floor can overstate amounts
    • Replaced round with floor for native amount conversion to prevent overstating amounts, aligning with the established convention in godex.ts and swapuz.ts.

Create PR

Or push these changes by commenting:

@cursor push 5f71b513f5
Preview (5f71b513f5)
diff --git a/src/swap/central/nexchange.ts b/src/swap/central/nexchange.ts
--- a/src/swap/central/nexchange.ts
+++ b/src/swap/central/nexchange.ts
@@ -1,4 +1,4 @@
-import { gt, lt, round } from 'biggystring'
+import { floor, gt, lt } from 'biggystring'
 import {
   asArray,
   asDate,
@@ -416,8 +416,8 @@
     )
     // This provider may return more decimal places than the token supports.
     // Native amounts must be integer atomic units.
-    const amountExpectedFromNative = round(amountExpectedFromNativeRaw, 0)
-    const amountExpectedToNative = round(amountExpectedToNativeRaw, 0)
+    const amountExpectedFromNative = floor(amountExpectedFromNativeRaw, 0)
+    const amountExpectedToNative = floor(amountExpectedToNativeRaw, 0)
 
     const memos: EdgeMemo[] =
       order.deposit_address_extra_id == null ||

This Bugbot Autofix run was free. To enable autofix for future PRs, go to the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 0c99910. Configure here.

Comment thread src/swap/central/nexchange.ts Outdated
N.exchange can return decimal amounts with more precision than the target
asset supports, which leaks fractional values into native amount fields.

Rounding to integer atomic units in this plugin keeps spend targets and
saved swap metadata valid for Edge amount expectations.
@samholmes samholmes force-pushed the sam/nexchange-nativeAmount-bug branch from 0c99910 to 6acb8fd Compare April 10, 2026 17:53
@j0ntz j0ntz self-assigned this Apr 10, 2026
@samholmes samholmes merged commit fba60bf into master Apr 10, 2026
3 checks passed
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.

2 participants