Skip to content

Token Decimal Normalization for Split Calculations #555

Description

@Kingsman-99

Description

Stellar assets can have different decimal precisions (XLM uses 7 decimal places; custom tokens may use 2, 6, or 18). When computing each recipient's share from a raw token amount, integer arithmetic on differing scales produces silently wrong results — either overpaying or underpaying recipients. The contract must normalize amounts to a canonical scale before any split calculation and denormalize before token transfer.

Technical Context

contracts/split/src/lib.rs#[contractimpl] pay() and release_funds() entry points; contracts/split/src/types.rsInvoice and RecipientShare structs (add token_decimals: u32 field); contracts/split/src/math.rs (new helper module) — normalize_amount(raw: i128, decimals: u32) -> i128 and denormalize_amount(normalized: i128, decimals: u32) -> i128 functions; soroban_sdk::token::Client::decimals() called against the payment token during initialize() to populate the stored decimal count.

Acceptance Criteria

  • Invoice stores the payment token's decimal count, populated from soroban_sdk::token::Client::decimals() during initialize()
  • normalize_amount scales all raw amounts to a fixed 7-decimal internal representation before split arithmetic
  • denormalize_amount converts back to the token's native scale before each token::Client::transfer() call
  • A unit test in contracts/split/src/tests/decimal_tests.rs verifies correct per-recipient amounts for tokens with 2, 6, and 7 decimal places across at least three split configurations
  • Checked arithmetic (checked_mul, checked_div) is used throughout and propagates Error::ArithmeticOverflow on out-of-range inputs
  • All CI checks (cargo build --target wasm32-unknown-unknown, cargo test, cargo clippy) pass and the branch has no merge conflicts

Metadata

Metadata

Labels

Stellar WaveIssues in the Stellar wave programcomplexity: highComplex feature requiring deep knowledge - 200 ptsperformance

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions