Skip to content
19 changes: 17 additions & 2 deletions include/xrpl/protocol/STAmount.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class STAmount final : public STBase, public CountedObject<STAmount>
public:
using value_type = STAmount;

static int const cMinOffset = -96;
static int const cMaxOffset = 80;
static int constexpr cMinOffset = -96;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't just add static TYPE constexpr when you see constexpr static TYPE just below :rage4:
On a serious note though i see we are all over the place with this in the codebase and this should probably be addressed by clang-tidy/format instead.

static int constexpr cMaxOffset = 80;

// Maximum native value supported by the code
constexpr static std::uint64_t cMinValue = 1'000'000'000'000'000ull;
Expand Down Expand Up @@ -739,6 +739,21 @@ canAdd(STAmount const& amt1, STAmount const& amt2);
bool
canSubtract(STAmount const& amt1, STAmount const& amt2);

/** Get the scale of a Number for a given asset.
*
* "scale" is similar to "exponent", but from the perspective of STAmount, which has different rules
* and mantissa ranges for determining the exponent than Number.
*
* @param number The Number to get the scale of.
* @param asset The asset to use for determining the scale.
* @return The scale of this Number for the given asset.
*/
inline int
scale(Number const& number, Asset const& asset)
{
return STAmount{asset, number}.exponent();
}

} // namespace xrpl

//------------------------------------------------------------------------------
Expand Down
19 changes: 18 additions & 1 deletion include/xrpl/tx/invariants/VaultInvariant.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <xrpl/protocol/STTx.h>
#include <xrpl/protocol/TER.h>

#include <optional>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -60,18 +61,34 @@ class ValidVault
Shares static make(SLE const&);
};

public:
struct DeltaInfo final
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's worth implementing arithmetic operations for this class so that we can simplify the code a lot.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't see what you did here?

{
Number delta = numZero;
std::optional<int> scale;
Comment thread
Tapanito marked this conversation as resolved.

// Compute the delta between two Numbers, taking the coarsest scale
[[nodiscard]] static DeltaInfo
makeDelta(Number const& after, Number const& before, Asset const& asset);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the parameters ordered "after" then "before" is a little bit unintuitive. For a function, I'd expect more "before, after". I'll concede that changing it now is going to be a little bit of a pain in the neck...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's bad either way.. strong types would be the ideal solution. Right now you can pass two Numbers in any order and it will compile just fine. I also don't think we need to fix that right now though.

};

private:
std::vector<Vault> afterVault_ = {};
std::vector<Shares> afterMPTs_ = {};
std::vector<Vault> beforeVault_ = {};
std::vector<Shares> beforeMPTs_ = {};
std::unordered_map<uint256, Number> deltas_ = {};
std::unordered_map<uint256, DeltaInfo> deltas_ = {};

public:
void
visitEntry(bool, std::shared_ptr<SLE const> const&, std::shared_ptr<SLE const> const&);

bool
finalize(STTx const&, TER const, XRPAmount const, ReadView const&, beast::Journal const&);

// Compute the coarsest scale required to represent all numbers
[[nodiscard]] static std::int32_t
Comment thread
Tapanito marked this conversation as resolved.
computeCoarsestScale(std::vector<DeltaInfo> const& numbers);
};

} // namespace xrpl
2 changes: 1 addition & 1 deletion include/xrpl/tx/transactors/lending/LendingHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ getAssetsTotalScale(SLE::const_ref vaultSle)
{
if (!vaultSle)
return Number::minExponent - 1; // LCOV_EXCL_LINE
return STAmount{vaultSle->at(sfAsset), vaultSle->at(sfAssetsTotal)}.exponent();
return scale(vaultSle->at(sfAssetsTotal), vaultSle->at(sfAsset));
}

TER
Expand Down
Loading
Loading