Skip to main content

scale_token_amount

Function scale_token_amount 

Source
pub fn scale_token_amount(
    amount: i64,
    from_decimals: OracleDecimals,
    to_decimals: OracleDecimals,
) -> Result<i64, ArithmeticError>
Expand description

Scale a token amount between different decimal precisions.

Useful for converting between tokens with different decimals (e.g., USDC with 6 decimals to DAI with 18 decimals).

ยงExample

use precision_core::oracle::{scale_token_amount, OracleDecimals};

// Convert 1000 USDC (6 decimals) representation to 8 decimals
let usdc_amount = 1_000_000_000i64;  // 1000 USDC (6 decimals)
let scaled = scale_token_amount(
    usdc_amount,
    OracleDecimals::Six,
    OracleDecimals::Eight
).unwrap();
assert_eq!(scaled, 100_000_000_000);  // 1000 * 10^8