Factor out static Number getShareTotal() from View.cpp

This commit is contained in:
Bronek Kozicki
2025-03-10 15:01:14 +00:00
parent 7b4e901dd7
commit 90fef02164
5 changed files with 33 additions and 31 deletions

View File

@@ -103,6 +103,11 @@ VaultClawback::doApply()
if (!vault) if (!vault)
return tecOBJECT_NOT_FOUND; return tecOBJECT_NOT_FOUND;
auto const mptIssuanceID = (*vault)[sfMPTokenIssuanceID];
auto const sleIssuance = view().read(keylet::mptIssuance(mptIssuanceID));
if (!sleIssuance)
return tefINTERNAL;
Asset const asset = vault->at(sfAsset); Asset const asset = vault->at(sfAsset);
STAmount const amount = [&]() -> STAmount { STAmount const amount = [&]() -> STAmount {
auto const maybeAmount = tx[~sfAmount]; auto const maybeAmount = tx[~sfAmount];
@@ -126,12 +131,12 @@ VaultClawback::doApply()
FreezeHandling::fhIGNORE_FREEZE, FreezeHandling::fhIGNORE_FREEZE,
AuthHandling::ahIGNORE_AUTH, AuthHandling::ahIGNORE_AUTH,
j_); j_);
assets = sharesToAssetsWithdraw(view(), vault, shares); assets = sharesToAssetsWithdraw(vault, sleIssuance, shares);
} }
else else
{ {
assets = amount; assets = amount;
shares = assetsToSharesWithdraw(view(), vault, assets); shares = assetsToSharesWithdraw(vault, sleIssuance, assets);
} }
// Clamp to maximum. // Clamp to maximum.
@@ -139,7 +144,7 @@ VaultClawback::doApply()
if (assets > maxAssets) if (assets > maxAssets)
{ {
assets = maxAssets; assets = maxAssets;
shares = assetsToSharesWithdraw(view(), vault, assets); shares = assetsToSharesWithdraw(vault, sleIssuance, assets);
} }
if (shares == beast::zero) if (shares == beast::zero)

View File

@@ -146,7 +146,7 @@ VaultDeposit::doApply()
} }
// Compute exchange before transferring any amounts. // Compute exchange before transferring any amounts.
auto const shares = assetsToSharesDeposit(view(), vault, assets); auto const shares = assetsToSharesDeposit(vault, sleIssuance, assets);
XRPL_ASSERT( XRPL_ASSERT(
shares.asset() != assets.asset(), shares.asset() != assets.asset(),
"ripple::VaultDeposit::doApply : assets are not shares"); "ripple::VaultDeposit::doApply : assets are not shares");

View File

@@ -82,6 +82,11 @@ VaultWithdraw::doApply()
if (!vault) if (!vault)
return tecOBJECT_NOT_FOUND; return tecOBJECT_NOT_FOUND;
auto const mptIssuanceID = (*vault)[sfMPTokenIssuanceID];
auto const sleIssuance = view().read(keylet::mptIssuance(mptIssuanceID));
if (!sleIssuance)
return tefINTERNAL;
// Note, we intentionally do not check lsfVaultPrivate flag on the Vault. If // Note, we intentionally do not check lsfVaultPrivate flag on the Vault. If
// you have a share in the vault, it means you were at some point authorized // you have a share in the vault, it means you were at some point authorized
// to deposit into it, and this means you are also indefinitely authorized // to deposit into it, and this means you are also indefinitely authorized
@@ -89,19 +94,19 @@ VaultWithdraw::doApply()
auto amount = ctx_.tx[sfAmount]; auto amount = ctx_.tx[sfAmount];
auto const asset = vault->at(sfAsset); auto const asset = vault->at(sfAsset);
auto const share = MPTIssue(vault->at(sfMPTokenIssuanceID)); auto const share = MPTIssue(mptIssuanceID);
STAmount shares, assets; STAmount shares, assets;
if (amount.asset() == asset) if (amount.asset() == asset)
{ {
// Fixed assets, variable shares. // Fixed assets, variable shares.
assets = amount; assets = amount;
shares = assetsToSharesWithdraw(view(), vault, assets); shares = assetsToSharesWithdraw(vault, sleIssuance, assets);
} }
else if (amount.asset() == share) else if (amount.asset() == share)
{ {
// Fixed shares, variable assets. // Fixed shares, variable assets.
shares = amount; shares = amount;
assets = sharesToAssetsWithdraw(view(), vault, shares); assets = sharesToAssetsWithdraw(vault, sleIssuance, shares);
} }
else else
return tefINTERNAL; return tefINTERNAL;

View File

@@ -723,8 +723,8 @@ deleteAMMTrustLine(
// when they deposit a fixed amount of assets. // when they deposit a fixed amount of assets.
[[nodiscard]] STAmount [[nodiscard]] STAmount
assetsToSharesDeposit( assetsToSharesDeposit(
ReadView const& view, std::shared_ptr<SLE const> const& vault,
std::shared_ptr<SLE> const& vault, std::shared_ptr<SLE const> const& issuance,
STAmount const& assets); STAmount const& assets);
// From the perspective of a vault, // From the perspective of a vault,
@@ -732,8 +732,8 @@ assetsToSharesDeposit(
// when they ask to withdraw a fixed amount of assets. // when they ask to withdraw a fixed amount of assets.
[[nodiscard]] STAmount [[nodiscard]] STAmount
assetsToSharesWithdraw( assetsToSharesWithdraw(
ReadView const& view, std::shared_ptr<SLE const> const& vault,
std::shared_ptr<SLE> const& vault, std::shared_ptr<SLE const> const& issuance,
STAmount const& assets); STAmount const& assets);
// From the perspective of a vault, // From the perspective of a vault,
@@ -741,8 +741,8 @@ assetsToSharesWithdraw(
// when they redeem a fixed amount of shares. // when they redeem a fixed amount of shares.
[[nodiscard]] STAmount [[nodiscard]] STAmount
sharesToAssetsWithdraw( sharesToAssetsWithdraw(
ReadView const& view, std::shared_ptr<SLE const> const& vault,
std::shared_ptr<SLE> const& vault, std::shared_ptr<SLE const> const& issuance,
STAmount const& shares); STAmount const& shares);
} // namespace ripple } // namespace ripple

View File

@@ -2504,37 +2504,29 @@ rippleCredit(
saAmount.asset().value()); saAmount.asset().value());
} }
static Number
getShareTotal(ReadView const& view, std::shared_ptr<SLE> const& vault)
{
auto issuance =
view.read(keylet::mptIssuance(vault->at(sfMPTokenIssuanceID)));
return issuance->at(sfOutstandingAmount);
}
[[nodiscard]] STAmount [[nodiscard]] STAmount
assetsToSharesDeposit( assetsToSharesDeposit(
ReadView const& view, std::shared_ptr<SLE const> const& vault,
std::shared_ptr<SLE> const& vault, std::shared_ptr<SLE const> const& issuance,
STAmount const& assets) STAmount const& assets)
{ {
XRPL_ASSERT( XRPL_ASSERT(
assets.asset() == vault->at(sfAsset), assets.asset() == vault->at(sfAsset),
"ripple::assetsToSharesDeposit : assets and vault match"); "ripple::assetsToSharesDeposit : assets and vault match");
Number assetTotal = *vault->at(sfAssetTotal); Number assetTotal = vault->at(sfAssetTotal);
STAmount shares{ STAmount shares{
vault->at(sfMPTokenIssuanceID), static_cast<Number>(assets)}; vault->at(sfMPTokenIssuanceID), static_cast<Number>(assets)};
if (assetTotal == 0) if (assetTotal == 0)
return shares; return shares;
Number shareTotal = getShareTotal(view, vault); Number shareTotal = issuance->at(sfOutstandingAmount);
shares = shareTotal * (assets / assetTotal); shares = shareTotal * (assets / assetTotal);
return shares; return shares;
} }
[[nodiscard]] STAmount [[nodiscard]] STAmount
assetsToSharesWithdraw( assetsToSharesWithdraw(
ReadView const& view, std::shared_ptr<SLE const> const& vault,
std::shared_ptr<SLE> const& vault, std::shared_ptr<SLE const> const& issuance,
STAmount const& assets) STAmount const& assets)
{ {
XRPL_ASSERT( XRPL_ASSERT(
@@ -2545,15 +2537,15 @@ assetsToSharesWithdraw(
STAmount shares{vault->at(sfMPTokenIssuanceID)}; STAmount shares{vault->at(sfMPTokenIssuanceID)};
if (assetTotal == 0) if (assetTotal == 0)
return shares; return shares;
Number shareTotal = getShareTotal(view, vault); Number shareTotal = issuance->at(sfOutstandingAmount);
shares = shareTotal * (assets / assetTotal); shares = shareTotal * (assets / assetTotal);
return shares; return shares;
} }
[[nodiscard]] STAmount [[nodiscard]] STAmount
sharesToAssetsWithdraw( sharesToAssetsWithdraw(
ReadView const& view, std::shared_ptr<SLE const> const& vault,
std::shared_ptr<SLE> const& vault, std::shared_ptr<SLE const> const& issuance,
STAmount const& shares) STAmount const& shares)
{ {
XRPL_ASSERT( XRPL_ASSERT(
@@ -2564,7 +2556,7 @@ sharesToAssetsWithdraw(
STAmount assets{vault->at(sfAsset)}; STAmount assets{vault->at(sfAsset)};
if (assetTotal == 0) if (assetTotal == 0)
return assets; return assets;
Number shareTotal = getShareTotal(view, vault); Number shareTotal = issuance->at(sfOutstandingAmount);
assets = assetTotal * (shares / shareTotal); assets = assetTotal * (shares / shareTotal);
return assets; return assets;
} }