Skip to content

Conversation

@joncinque
Copy link
Contributor

Problem

As pointed out at
anza-xyz/agave#4663 (comment), the current scaling behavior is problematic for money amounts, because it uses the default Rust formatting of "round to nearest, ties to even" when producing a UI amount. This behavior can "overvalue" what's in someone's account.

Summary of changes

Floor all conversions to 0 (AKA truncate).

#### Problem

As pointed out at
anza-xyz/agave#4663 (comment), the
current scaling behavior is problematic for money amounts, because it
uses the default Rust formatting of "round to nearest, ties to even"
when producing a UI amount. This behavior can "overvalue" what's in
someone's account.

#### Summary of changes

Floor all conversions to 0 (AKA truncate).
Copy link
Contributor

@steveluscher steveluscher left a comment

Choose a reason for hiding this comment

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

I get that this is just taking the ScaledUiConfig and manipulating it faithfully, but I thought that negative multipliers were prohibited by program policy. I didn't want to have to think if rounding negative numbers toward zero was the right thing to do (ie. don't overstate how much you owe) or if we should be rounding toward negative infinity (ie. don't understate how much you owe).

Comment on lines 189 to 197
// negative truncation
let config = ScaledUiAmountConfig {
multiplier: PodF64::from(-0.99),
new_multiplier_effective_timestamp: UnixTimestamp::from(1),
..Default::default()
};
// This is really -0.99999... but it gets truncated
let ui_amount = config.amount_to_ui_amount(101, 2, 0).unwrap();
assert_eq!(ui_amount, "-0.99");
Copy link
Contributor

Choose a reason for hiding this comment

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

Wait, I thought negative multipliers were illegal.

if float_multiplier.is_sign_positive() && float_multiplier.is_normal() {
Ok(())
} else {
Err(TokenError::InvalidScale.into())
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh yeah, you're right! I'll remove those tests

@joncinque joncinque merged commit f69cc98 into solana-program:main Feb 3, 2025
20 checks passed
@joncinque joncinque deleted the floor branch February 3, 2025 20:37
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