Expand use of Asset::integral()

This commit is contained in:
Ed Hennis
2025-10-21 20:01:55 -04:00
parent 8757022312
commit f6098fd80b
6 changed files with 30 additions and 11 deletions

View File

@@ -174,6 +174,9 @@ public:
int
exponent() const noexcept;
bool
integral() const noexcept;
bool
native() const noexcept;
@@ -454,6 +457,12 @@ STAmount::exponent() const noexcept
return mOffset;
}
inline bool
STAmount::integral() const noexcept
{
return mAsset.integral();
}
inline bool
STAmount::native() const noexcept
{
@@ -735,7 +744,7 @@ roundToAsset(
{
NumberRoundModeGuard mg(rounding);
STAmount const ret{asset, value};
if (ret.asset().native() || !ret.asset().holds<Issue>())
if (ret.integral())
return ret;
// Not that the ctor will round integral types (XRP, MPT) via canonicalize,
// so no extra work is needed for those.

View File

@@ -321,7 +321,7 @@ STAmount::xrp() const
IOUAmount
STAmount::iou() const
{
if (native() || !holds<Issue>())
if (integral())
Throw<std::logic_error>("Cannot return non-IOU STAmount as IOUAmount");
auto mantissa = static_cast<std::int64_t>(mValue);
@@ -1513,7 +1513,7 @@ STAmount
roundToScale(STAmount value, std::int32_t scale, Number::rounding_mode rounding)
{
// Nothing to do for intgral types.
if (value.asset().native() || !value.asset().holds<Issue>())
if (value.integral())
return value;
// If the value's exponent is greater than or equal to the scale, then

View File

@@ -193,9 +193,7 @@ class Loan_test : public beast::unit_test::suite
auto const available = vaultSle->at(sfAssetsAvailable);
env.test.BEAST_EXPECT(
total == available ||
(!broker.asset.raw().native() &&
broker.asset.raw().holds<Issue>() &&
available != 0 &&
(!broker.asset.integral() && available != 0 &&
((total - available) / available <
Number(1, -6))));
env.test.BEAST_EXPECT(
@@ -1750,7 +1748,7 @@ class Loan_test : public beast::unit_test::suite
BEAST_EXPECT(
paymentComponents.final || diff == beast::zero ||
(diff > beast::zero &&
((broker.asset.raw().integral() &&
((broker.asset.integral() &&
(static_cast<Number>(diff) < 3)) ||
(totalDue.exponent() - diff.exponent() > 8))));

View File

@@ -84,14 +84,14 @@ class Vault_test : public beast::unit_test::suite
this]() -> std::tuple<PrettyAsset, Account> {
auto const vault = env.le(keylet);
BEAST_EXPECT(vault != nullptr);
if (asset.raw().holds<Issue>() && !asset.raw().native())
if (!asset.integral())
BEAST_EXPECT(vault->at(sfScale) == 6);
else
BEAST_EXPECT(vault->at(sfScale) == 0);
auto const shares =
env.le(keylet::mptIssuance(vault->at(sfShareMPTID)));
BEAST_EXPECT(shares != nullptr);
if (asset.raw().holds<Issue>() && !asset.raw().native())
if (!asset.integral())
BEAST_EXPECT(shares->at(sfAssetScale) == 6);
else
BEAST_EXPECT(shares->at(sfAssetScale) == 0);
@@ -446,7 +446,7 @@ class Vault_test : public beast::unit_test::suite
}
}
if (!asset.raw().native() && asset.raw().holds<Issue>())
if (!asset.integral())
{
testcase(prefix + " temporary authorization for 3rd party");
env(trust(erin, asset(1000)));

View File

@@ -233,6 +233,18 @@ public:
return {asset_};
}
bool
integral() const
{
return asset_.integral();
}
bool
native() const
{
return asset_.native();
}
template <ValidIssueType TIss>
bool
holds() const

View File

@@ -203,7 +203,7 @@ LoanManage::defaultLoan(
// Capital and any unclaimed funds amount:
vaultAssetsAvailableProxy += defaultCovered;
if (*vaultAssetsAvailableProxy > *vaultAssetsTotalProxy &&
!vaultAsset.native() && vaultAsset.holds<Issue>())
!vaultAsset.integral())
{
auto const difference =
vaultAssetsAvailableProxy - vaultAssetsTotalProxy;