Skip to content

Commit

Permalink
audit: fix conversion
Browse files Browse the repository at this point in the history
Audit specification: errors might come from get_data_with_usd_hop if there is a difference in the base and quote decimals configuration. Solution, realize the normalization first.
  • Loading branch information
JordyRo1 committed Nov 1, 2023
1 parent 8e1ff20 commit 4f5ec88
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/operations/time_series/convert.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@ use alexandria_math::pow;
use traits::Into;
use debug::PrintTrait;


fn normalize_to_decimals(value: u128, original_decimals: u32, target_decimals: u32) -> u128 {
if target_decimals >= original_decimals {
value * pow(10,(target_decimals - original_decimals).into())
} else {
value / pow(10,(original_decimals-target_decimals).into())
}
}
const MAX_POWER: u128 = 10000000000000000000000000000000;
fn div_decimals(a_price: u128, b_price: u128, output_decimals: u128) -> u128 {
let power = pow(10_u128, output_decimals);
Expand Down Expand Up @@ -51,4 +59,20 @@ fn test_convert_via_usd() {
let output_decimals: u32 = 6;
let result: u128 = convert_via_usd(a_price, b_price, output_decimals);
assert(result == 20833333, 'div failed'); //10**6 output decimals

let a_price: u128 = 25000000;
let original_decimals: u32 = 6;
let target_decimals : u32 = 8;
let result: u128 = normalize_to_decimals(a_price, original_decimals, target_decimals);
assert(result == 2500000000, 'div failed'); //10**8 output decimals


let a_price : u128 = 25000000;
let original_decimals: u32 = 8;
let target_decimals : u32 = 6;
let result: u128 = normalize_to_decimals(a_price, original_decimals, target_decimals);
result.print();
assert(result== 250000, 'div failed') //10**6 output decimals
}


7 changes: 5 additions & 2 deletions src/oracle/oracle.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ mod Oracle {
};
use hash::LegacyHash;
use pragma::entry::entry::Entry;
use pragma::operations::time_series::convert::convert_via_usd;
use pragma::operations::time_series::convert::{convert_via_usd, normalize_to_decimals};
use pragma::publisher_registry::publisher_registry::{
IPublisherRegistryABIDispatcher, IPublisherRegistryABIDispatcherTrait
};
Expand Down Expand Up @@ -961,7 +961,10 @@ mod Oracle {
IOracleABI::get_decimals(self, base_data_type),
IOracleABI::get_decimals(self, quote_data_type)
);
let rebased_value = convert_via_usd(basePPR.price, quotePPR.price, decimals);

let normalised_basePPR_price = normalize_to_decimals(basePPR.price, IOracleABI::get_decimals(self, base_data_type),decimals);
let normalised_quotePPR_price = normalize_to_decimals(quotePPR.price, IOracleABI::get_decimals(self, quote_data_type),decimals);
let rebased_value = convert_via_usd(normalised_basePPR_price,normalised_quotePPR_price , decimals);
let last_updated_timestamp = max(
quotePPR.last_updated_timestamp, basePPR.last_updated_timestamp
);
Expand Down

0 comments on commit 4f5ec88

Please sign in to comment.