Skip to content

Commit cf5f65b

Browse files
Bronekximinez
andauthored
Add Scale to SingleAssetVault (#5652)
* Add and Scale to VaultCreate * Add round-trip calculation to VaultDeposit VaultWithdraw and VaultClawback * Implement Number::truncate() for VaultClawback * Add rounding to DepositWithdraw * Disallow zero shares withdraw or deposit with tecPRECISION_LOSS * Return tecPATH_DRY on overflow when converting shares/assets * Remove empty shares MPToken in clawback or withdraw (except for vault owner) * Implicitly create shares MPToken for vault owner in VaultCreate * Review feedback: defensive checks in shares/assets calculations --------- Co-authored-by: Ed Hennis <[email protected]>
1 parent c38f2a3 commit cf5f65b

File tree

17 files changed

+1862
-214
lines changed

17 files changed

+1862
-214
lines changed

include/xrpl/basics/Number.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,24 @@ class Number
150150
return (mantissa_ < 0) ? -1 : (mantissa_ ? 1 : 0);
151151
}
152152

153+
Number
154+
truncate() const noexcept
155+
{
156+
if (exponent_ >= 0 || mantissa_ == 0)
157+
return *this;
158+
159+
Number ret = *this;
160+
while (ret.exponent_ < 0 && ret.mantissa_ != 0)
161+
{
162+
ret.exponent_ += 1;
163+
ret.mantissa_ /= rep(10);
164+
}
165+
// We are guaranteed that normalize() will never throw an exception
166+
// because exponent is either negative or zero at this point.
167+
ret.normalize();
168+
return ret;
169+
}
170+
153171
friend constexpr bool
154172
operator>(Number const& x, Number const& y) noexcept
155173
{

include/xrpl/protocol/Protocol.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,13 @@ std::size_t constexpr maxDataPayloadLength = 256;
122122
/** Vault withdrawal policies */
123123
std::uint8_t constexpr vaultStrategyFirstComeFirstServe = 1;
124124

125+
/** Default IOU scale factor for a Vault */
126+
std::uint8_t constexpr vaultDefaultIOUScale = 6;
127+
/** Maximum scale factor for a Vault. The number is chosen to ensure that
128+
1 IOU can be always converted to shares.
129+
10^19 > maxMPTokenAmount (2^64-1) > 10^18 */
130+
std::uint8_t constexpr vaultMaximumIOUScale = 18;
131+
125132
/** Maximum recursion depth for vault shares being put as an asset inside
126133
* another vault; counted from 0 */
127134
std::uint8_t constexpr maxAssetCheckDepth = 5;

include/xrpl/protocol/detail/ledger_entries.macro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,7 @@ LEDGER_ENTRY(ltVAULT, 0x0084, Vault, vault, ({
499499
{sfLossUnrealized, soeREQUIRED},
500500
{sfShareMPTID, soeREQUIRED},
501501
{sfWithdrawalPolicy, soeREQUIRED},
502+
{sfScale, soeDEFAULT},
502503
// no SharesTotal ever (use MPTIssuance.sfOutstandingAmount)
503504
// no PermissionedDomainID ever (use MPTIssuance.sfDomainID)
504505
}))

include/xrpl/protocol/detail/transactions.macro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,7 @@ TRANSACTION(ttVAULT_CREATE, 65, VaultCreate, Delegation::delegatable, ({
483483
{sfDomainID, soeOPTIONAL},
484484
{sfWithdrawalPolicy, soeOPTIONAL},
485485
{sfData, soeOPTIONAL},
486+
{sfScale, soeOPTIONAL},
486487
}))
487488

488489
/** This transaction updates a single asset vault. */

0 commit comments

Comments
 (0)