Skip to content

Commit

Permalink
safe_ -> checked_ name changes
Browse files Browse the repository at this point in the history
  • Loading branch information
motosharpley committed Sep 7, 2023
1 parent 11aa50b commit e693087
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 29 deletions.
8 changes: 4 additions & 4 deletions core/payment-splitter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ mod payment_splitter {
non_fungible_id,
Vault::new(self.accepted_token_resource_address),
);
self.total_amount_of_shares.safe_add(amount_of_shares);
self.total_amount_of_shares.checked_add(amount_of_shares);

// Returning the shareholder back to the method caller
return shareholder_badge;
Expand Down Expand Up @@ -396,7 +396,7 @@ mod payment_splitter {
let shareholder: Shareholder =
shareholder_resource_manager.get_non_fungible_data(&non_fungible_id);
self.total_amount_of_shares
.safe_sub(shareholder.amount_of_shares);
.checked_sub(shareholder.amount_of_shares);

// Burning the shareholder NFT
shareholder_badge.burn();
Expand Down Expand Up @@ -444,8 +444,8 @@ mod payment_splitter {
shareholder_resource_manager.get_non_fungible_data(&non_fungible_id);
let amount_owed: Decimal = shareholder
.amount_of_shares
.safe_mul(bucket.amount())
.and_then(|d| d.safe_div(self.total_amount_of_shares))
.checked_mul(bucket.amount())
.and_then(|d| d.checked_div(self.total_amount_of_shares))
.unwrap();
// shareholder.amount_of_shares * bucket.amount() / self.total_amount_of_shares;
vault.put(bucket.take(amount_owed));
Expand Down
6 changes: 3 additions & 3 deletions core/radix-name-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ mod radix_name_service {
let hash = Self::hash_name(name);
let deposit_amount = self
.deposit_per_year
.safe_mul(Decimal::from(reserve_years))
.checked_mul(Decimal::from(reserve_years))
.unwrap();
let last_valid_epoch =
Runtime::current_epoch().number() + EPOCHS_PER_YEAR * u64::from(reserve_years);
Expand Down Expand Up @@ -177,7 +177,7 @@ mod radix_name_service {

let total_deposit_amount = Decimal::zero();
for nft in name_nft.as_non_fungible().non_fungibles::<DomainName>() {
total_deposit_amount.safe_add(nft.data().deposit_amount);
total_deposit_amount.checked_add(nft.data().deposit_amount);
}

name_nft.burn();
Expand Down Expand Up @@ -249,7 +249,7 @@ mod radix_name_service {

let name_nft = name_nft.check(self.name_resource.address());

let fee_amount = self.fee_renewal_per_year.safe_mul(renew_years).unwrap();
let fee_amount = self.fee_renewal_per_year.checked_mul(renew_years).unwrap();
assert!(
fee.amount() >= fee_amount,
"Insufficient fee amount. You need to send a fee of {} XRD",
Expand Down
12 changes: 6 additions & 6 deletions core/vesting/src/beneficiary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl BeneficiaryVestingSchedule {
end_epoch,
total_vesting_amount,
amount_available_on_cliff: total_vesting_amount
.safe_mul(percentage_available_on_cliff)
.checked_mul(percentage_available_on_cliff)
.unwrap(),
};
}
Expand All @@ -106,9 +106,9 @@ impl BeneficiaryVestingSchedule {
pub fn vesting_gradient(&self) -> Decimal {
return (self
.total_vesting_amount
.safe_sub(self.amount_available_on_cliff))
.checked_sub(self.amount_available_on_cliff))
.unwrap()
.safe_div(self.end_epoch.number() - self.cliff_epoch.number())
.checked_div(self.end_epoch.number() - self.cliff_epoch.number())
.unwrap();
}

Expand All @@ -129,9 +129,9 @@ impl BeneficiaryVestingSchedule {
} else {
cmp::min(
self.vesting_gradient()
.safe_mul(epoch - self.cliff_epoch.number())
.checked_mul(epoch - self.cliff_epoch.number())
.unwrap()
.safe_add(self.amount_available_on_cliff)
.checked_add(self.amount_available_on_cliff)
.unwrap(),
self.total_vesting_amount,
)
Expand All @@ -150,7 +150,7 @@ impl BeneficiaryVestingSchedule {
pub fn get_unvested_amount(&self, epoch: u64) -> Decimal {
return self
.total_vesting_amount
.safe_sub(self.get_vested_amount(epoch))
.checked_sub(self.get_vested_amount(epoch))
.unwrap();
}
}
4 changes: 2 additions & 2 deletions core/vesting/src/vesting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ mod vesting {
if admin_resource_manager.total_supply().unwrap() <= dec!("2") {
admin_resource_manager.total_supply().unwrap()
} else {
(admin_resource_manager.total_supply().unwrap().safe_div(dec!("2")).unwrap().safe_ceiling()).unwrap()
(admin_resource_manager.total_supply().unwrap().checked_div(dec!("2")).unwrap().checked_ceiling()).unwrap()
};
info!(
"[Add Admin]: Minimum required admins is: {}",
Expand Down Expand Up @@ -349,7 +349,7 @@ mod vesting {
// now and the amount that should have not have vested yet.
let beneficiary_vault: &mut Vault = self.funds.get_mut(&beneficiary_id).unwrap();
let claim_amount: Decimal = beneficiary_vault.amount()
.safe_sub(beneficiary_vesting_schedule.get_unvested_amount(Runtime::current_epoch().number())).unwrap();
.checked_sub(beneficiary_vesting_schedule.get_unvested_amount(Runtime::current_epoch().number())).unwrap();
info!(
"[Withdraw Funds]: Withdraw successful. Withdrawing {} tokens",
claim_amount
Expand Down
2 changes: 1 addition & 1 deletion defi/basic-flash-loan/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ mod basic_flash_loan {
);

// Calculate how much we must be repaid
let amount_due = loan_amount.safe_mul(dec!("1.001"));
let amount_due = loan_amount.checked_mul(dec!("1.001"));

// Mint an NFT with the loan terms. Remember that this resource previously had rules defined which
// forbid it from ever being deposited in any vault. Thus, once it is present in the transaction
Expand Down
6 changes: 3 additions & 3 deletions defi/radiswap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ mod radiswap {
let (output_resource_address, output_reserves) = reserves.into_iter().next().unwrap();

let output_amount = input_amount
.safe_mul(output_reserves)
.checked_mul(output_reserves)
.unwrap()
.safe_div(input_reserves.safe_add(input_amount).unwrap())
.checked_div(input_reserves.checked_add(input_amount).unwrap())
.unwrap();

// NOTE: It's the responsibility of the user of the pool to do the appropriate rounding
Expand All @@ -76,7 +76,7 @@ mod radiswap {
self.withdraw(output_resource_address, output_amount)
}

fn vault_reserves(&self) -> BTreeMap<ResourceAddress, Decimal> {
fn vault_reserves(&self) -> IndexMap<ResourceAddress, Decimal> {
self.pool_component.get_vault_amounts()
}

Expand Down
4 changes: 2 additions & 2 deletions defi/regulated-token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,10 @@ mod regulated_token {

// Take what we're owed
self.collected_xrd
.put(payment.take(price.safe_mul(quantity).unwrap()));
.put(payment.take(price.checked_mul(quantity).unwrap()));

// Can we fill the desired quantity from current supply?
let extra_demand = quantity.safe_sub(self.token_supply.amount()).unwrap();
let extra_demand = quantity.checked_sub(self.token_supply.amount()).unwrap();
if extra_demand <= dec!("0") {
// Take the required quantity, and return it along with any change
// The token may currently be under restricted transfer, so we will authorize our withdrawal
Expand Down
6 changes: 3 additions & 3 deletions full-stack/rcnet-radiswap/scrypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ mod radiswap {

let (output_resource_address, output_reserves) = reserves.into_iter().next().unwrap();

let output_amount = (input_amount.safe_mul(output_reserves))
.and_then(|d| d.safe_div(input_reserves.safe_add(input_amount).unwrap()));
let output_amount = (input_amount.checked_mul(output_reserves))
.and_then(|d| d.checked_div(input_reserves.checked_add(input_amount).unwrap()));

// NOTE: It's the responsibility of the user of the pool to do the appropriate rounding
// before calling the withdraw method.
Expand All @@ -81,7 +81,7 @@ mod radiswap {
self.withdraw(output_resource_address, output_amount.unwrap())
}

pub fn vault_reserves(&self) -> BTreeMap<ResourceAddress, Decimal> {
pub fn vault_reserves(&self) -> IndexMap<ResourceAddress, Decimal> {
self.liquidity_pool_component.get_vault_amounts()
}

Expand Down
10 changes: 6 additions & 4 deletions nft/nft-marketplace/src/dutch_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,18 +307,20 @@ mod dutch_auction {
/// * `ResourceAddress` - The resource address of the accepted payment token.
/// * `Decimal` - A decimal value of the price of the NFT(s) in terms of the `accepted_payment_token`.
pub fn price(&self) -> (ResourceAddress, Decimal) {
let gradient: Decimal = (self.ending_price.safe_sub(self.starting_price))
let gradient: Decimal = (self.ending_price.checked_sub(self.starting_price))
.unwrap()
.safe_div(self.ending_epoch.number() - self.starting_epoch.number())
.checked_div(self.ending_epoch.number() - self.starting_epoch.number())
.unwrap();
return (
self.accepted_payment_token,
std::cmp::max(
self.ending_price,
gradient
.safe_mul(Runtime::current_epoch().number() - self.starting_epoch.number())
.checked_mul(
Runtime::current_epoch().number() - self.starting_epoch.number(),
)
.unwrap()
.safe_add(self.starting_price)
.checked_add(self.starting_price)
.unwrap(),
),
);
Expand Down
2 changes: 1 addition & 1 deletion nft/nft-marketplace/src/english_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ mod english_auction {
resource_manager.update_non_fungible_data(
&non_fungible_local_id,
"bid_amount",
bidders_badge_data.bid_amount.safe_add(funds.amount()),
bidders_badge_data.bid_amount.checked_add(funds.amount()),
);

// Adding the funds to the vault of the bidder
Expand Down

0 comments on commit e693087

Please sign in to comment.