Skip to content

Commit

Permalink
Decimal::to_amount → Decimal::to_integer (ordinals#3382)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored and vuviettai committed Mar 28, 2024
1 parent 6cce187 commit 0f0ad09
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub struct Decimal {
}

impl Decimal {
pub fn to_amount(self, divisibility: u8) -> Result<u128> {
pub fn to_integer(self, divisibility: u8) -> Result<u128> {
match divisibility.checked_sub(self.scale) {
Some(difference) => Ok(
self
Expand Down Expand Up @@ -128,15 +128,15 @@ mod tests {
assert_eq!(
s.parse::<Decimal>()
.unwrap()
.to_amount(divisibility)
.to_integer(divisibility)
.unwrap(),
amount,
);
}

assert_eq!(
Decimal { value: 0, scale: 0 }
.to_amount(255)
.to_integer(255)
.unwrap_err()
.to_string(),
"divisibility out of range"
Expand All @@ -147,15 +147,15 @@ mod tests {
value: u128::MAX,
scale: 0,
}
.to_amount(1)
.to_integer(1)
.unwrap_err()
.to_string(),
"amount out of range",
);

assert_eq!(
Decimal { value: 1, scale: 1 }
.to_amount(0)
.to_integer(0)
.unwrap_err()
.to_string(),
"excessive precision",
Expand Down
10 changes: 5 additions & 5 deletions src/subcommand/wallet/inscribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,16 +235,16 @@ impl Inscribe {
"rune `{rune}` has already been etched",
);

let premine = etching.premine.to_amount(etching.divisibility)?;
let premine = etching.premine.to_integer(etching.divisibility)?;

let supply = etching.supply.to_amount(etching.divisibility)?;
let supply = etching.supply.to_integer(etching.divisibility)?;

let mintable = etching
.terms
.map(|terms| -> Result<u128> {
terms
.cap
.checked_mul(terms.amount.to_amount(etching.divisibility)?)
.checked_mul(terms.amount.to_integer(etching.divisibility)?)
.ok_or_else(|| anyhow!("`terms.count` * `terms.amount` over maximum"))
})
.transpose()?
Expand Down Expand Up @@ -295,10 +295,10 @@ impl Inscribe {
);
}

ensure!(terms.cap > 0, "`terms.cap` must be greater than zero",);
ensure!(terms.cap > 0, "`terms.cap` must be greater than zero");

ensure!(
terms.amount.to_amount(etching.divisibility)? > 0,
terms.amount.to_integer(etching.divisibility)? > 0,
"`terms.amount` must be greater than zero",
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/wallet/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl Send {
.get_rune(spaced_rune.rune)?
.with_context(|| format!("rune `{}` has not been etched", spaced_rune.rune))?;

let amount = decimal.to_amount(entry.divisibility)?;
let amount = decimal.to_integer(entry.divisibility)?;

let inscribed_outputs = inscriptions
.keys()
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/batch/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ impl Plan {

let vout;
let destination;
premine = etching.premine.to_amount(etching.divisibility)?;
premine = etching.premine.to_integer(etching.divisibility)?;

if premine > 0 {
let output = u32::try_from(reveal_outputs.len()).unwrap();
Expand Down Expand Up @@ -470,7 +470,7 @@ impl Plan {
terms.height.and_then(|range| (range.start)),
terms.height.and_then(|range| (range.end)),
),
amount: Some(terms.amount.to_amount(etching.divisibility)?),
amount: Some(terms.amount.to_integer(etching.divisibility)?),
offset: (
terms.offset.and_then(|range| (range.start)),
terms.offset.and_then(|range| (range.end)),
Expand Down
10 changes: 5 additions & 5 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ fn batch(
} = batchfile.etching.unwrap();

{
let supply = supply.to_amount(divisibility).unwrap();
let premine = premine.to_amount(divisibility).unwrap();
let supply = supply.to_integer(divisibility).unwrap();
let premine = premine.to_integer(divisibility).unwrap();

let mintable = terms
.map(|terms| terms.cap * terms.amount.to_amount(divisibility).unwrap())
.map(|terms| terms.cap * terms.amount.to_integer(divisibility).unwrap())
.unwrap_or_default();

assert_eq!(supply, premine + mintable);
Expand Down Expand Up @@ -324,7 +324,7 @@ fn batch(
mint_definition.push(format!(
"<dd>{}</dd>",
Pile {
amount: terms.amount.to_amount(divisibility).unwrap(),
amount: terms.amount.to_integer(divisibility).unwrap(),
divisibility,
symbol: Some(symbol),
}
Expand Down Expand Up @@ -384,7 +384,7 @@ fn batch(
rune,
} = inscribe.rune.clone().unwrap();

if premine.to_amount(divisibility).unwrap() > 0 {
if premine.to_integer(divisibility).unwrap() > 0 {
let destination = destination
.unwrap()
.clone()
Expand Down

0 comments on commit 0f0ad09

Please sign in to comment.