mirror of
https://github.com/XRPLF/rippled.git
synced 2026-03-04 11:52:25 +00:00
Compare commits
23 Commits
develop
...
tapanito/v
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
07a6f77ed2 | ||
|
|
e4a716f260 | ||
|
|
ed4330a7d6 | ||
|
|
feba605998 | ||
|
|
b322097529 | ||
|
|
e159d27373 | ||
|
|
ba53026006 | ||
|
|
34773080df | ||
|
|
3029d10102 | ||
|
|
3c3bd75991 | ||
|
|
c89dd9f0a3 | ||
|
|
7459fe454d | ||
|
|
3a0cd45f51 | ||
|
|
106bf48725 | ||
|
|
79be4717f5 | ||
|
|
74c968d4e3 | ||
|
|
167147281c | ||
|
|
ba60306610 | ||
|
|
6674500896 | ||
|
|
c5d7ebe93d | ||
|
|
d0b5ca9dab | ||
|
|
5e51893e9b | ||
|
|
3422c11d02 |
@@ -291,6 +291,8 @@ constexpr std::uint32_t const tfLoanImpair = 0x00020000;
|
||||
constexpr std::uint32_t const tfLoanUnimpair = 0x00040000;
|
||||
constexpr std::uint32_t const tfLoanManageMask = ~(tfUniversal | tfLoanDefault | tfLoanImpair | tfLoanUnimpair);
|
||||
|
||||
constexpr std::uint32_t const tfVaultDonate = 0x00010000;
|
||||
constexpr std::uint32_t const tfVaultDepositMask = ~(tfUniversal | tfVaultDonate);
|
||||
// clang-format on
|
||||
|
||||
} // namespace xrpl
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// Add new amendments to the top of this list.
|
||||
// Keep it sorted in reverse chronological order.
|
||||
|
||||
XRPL_FIX (LendingProtocolV1_1, Supported::yes, VoteBehavior::DefaultNo)
|
||||
XRPL_FIX (PermissionedDomainInvariant, Supported::yes, VoteBehavior::DefaultNo)
|
||||
XRPL_FIX (ExpiredNFTokenOfferRemoval, Supported::yes, VoteBehavior::DefaultNo)
|
||||
XRPL_FIX (BatchInnerSigs, Supported::no, VoteBehavior::DefaultNo)
|
||||
|
||||
@@ -868,6 +868,7 @@ TRANSACTION(ttVAULT_DELETE, 67, VaultDelete,
|
||||
mustDeleteAcct | destroyMPTIssuance | mustModifyVault,
|
||||
({
|
||||
{sfVaultID, soeREQUIRED},
|
||||
{sfMemoData, soeOPTIONAL},
|
||||
}))
|
||||
|
||||
/** This transaction trades assets for shares with a vault. */
|
||||
|
||||
@@ -13,6 +13,9 @@ public:
|
||||
{
|
||||
}
|
||||
|
||||
static std::uint32_t
|
||||
getFlagsMask(PreflightContext const& ctx);
|
||||
|
||||
static NotTEC
|
||||
preflight(PreflightContext const& ctx);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <xrpl/protocol/Protocol.h>
|
||||
#include <xrpl/protocol/SField.h>
|
||||
#include <xrpl/protocol/STNumber.h>
|
||||
#include <xrpl/protocol/TxFlags.h>
|
||||
#include <xrpl/protocol/TxFormats.h>
|
||||
#include <xrpl/tx/invariants/InvariantCheckPrivilege.h>
|
||||
|
||||
@@ -378,10 +379,14 @@ ValidVault::finalize(
|
||||
return std::nullopt;
|
||||
}();
|
||||
|
||||
if (!beforeShares &&
|
||||
(tx.getTxnType() == ttVAULT_DEPOSIT || //
|
||||
tx.getTxnType() == ttVAULT_WITHDRAW || //
|
||||
tx.getTxnType() == ttVAULT_CLAWBACK))
|
||||
bool const isDonate = !view.rules().enabled(fixLendingProtocolV1_1) || tx.isFlag(tfVaultDonate);
|
||||
bool const shouldUpdateShares =
|
||||
// Vault Asset donation is the only operation that can succeed without updating shares
|
||||
((tx.getTxnType() == ttVAULT_DEPOSIT && !isDonate) || //
|
||||
tx.getTxnType() == ttVAULT_WITHDRAW || //
|
||||
tx.getTxnType() == ttVAULT_CLAWBACK);
|
||||
|
||||
if (!beforeShares && shouldUpdateShares)
|
||||
{
|
||||
JLOG(j.fatal()) << "Invariant failed: vault operation succeeded "
|
||||
"without updating shares";
|
||||
@@ -635,37 +640,57 @@ ValidVault::finalize(
|
||||
result = false;
|
||||
}
|
||||
|
||||
auto const accountDeltaShares = deltaShares(tx[sfAccount]);
|
||||
if (!accountDeltaShares)
|
||||
// If assets are not donated, check share invariants
|
||||
if (view.rules().enabled(fixLendingProtocolV1_1) && tx.isFlag(tfVaultDonate))
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must change depositor "
|
||||
"shares";
|
||||
return false; // That's all we can do
|
||||
}
|
||||
auto const accountDeltaShares = deltaShares(tx[sfAccount]);
|
||||
if (accountDeltaShares)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: donation must not change depositor shares";
|
||||
return false; // That's all we can do
|
||||
}
|
||||
|
||||
if (*accountDeltaShares <= zero)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must increase depositor "
|
||||
"shares";
|
||||
result = false;
|
||||
auto const vaultDeltaShares = deltaShares(afterVault.pseudoId);
|
||||
if (vaultDeltaShares)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: donation must not change vault shares";
|
||||
return false; // That's all we can do
|
||||
}
|
||||
}
|
||||
|
||||
auto const vaultDeltaShares = deltaShares(afterVault.pseudoId);
|
||||
if (!vaultDeltaShares || *vaultDeltaShares == zero)
|
||||
else
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must change vault shares";
|
||||
return false; // That's all we can do
|
||||
}
|
||||
auto const accountDeltaShares = deltaShares(tx[sfAccount]);
|
||||
if (!accountDeltaShares)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must change depositor shares";
|
||||
return false; // That's all we can do
|
||||
}
|
||||
|
||||
if (*vaultDeltaShares * -1 != *accountDeltaShares)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must change depositor and "
|
||||
"vault shares by equal amount";
|
||||
result = false;
|
||||
if (*accountDeltaShares <= zero)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must increase depositor shares";
|
||||
result = false;
|
||||
}
|
||||
|
||||
auto const vaultDeltaShares = deltaShares(afterVault.pseudoId);
|
||||
if (!vaultDeltaShares || *vaultDeltaShares == zero)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must change vault shares";
|
||||
return false; // That's all we can do
|
||||
}
|
||||
|
||||
if (*vaultDeltaShares * -1 != *accountDeltaShares)
|
||||
{
|
||||
JLOG(j.fatal()) << //
|
||||
"Invariant failed: deposit must change depositor and vault shares by "
|
||||
"equal amount";
|
||||
result = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (beforeVault.assetsTotal + *vaultDeltaAssets != afterVault.assetsTotal)
|
||||
|
||||
@@ -18,6 +18,13 @@ VaultDelete::preflight(PreflightContext const& ctx)
|
||||
return temMALFORMED;
|
||||
}
|
||||
|
||||
if (ctx.tx.isFieldPresent(sfMemoData) && !ctx.rules.enabled(fixLendingProtocolV1_1))
|
||||
return temDISABLED;
|
||||
|
||||
// The sfMemoData field is an optional field used to record the deletion reason.
|
||||
if (auto const data = ctx.tx[~sfMemoData]; data && !validDataLength(data, maxDataPayloadLength))
|
||||
return temMALFORMED;
|
||||
|
||||
return tesSUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,15 @@
|
||||
|
||||
namespace xrpl {
|
||||
|
||||
std::uint32_t
|
||||
VaultDeposit::getFlagsMask(PreflightContext const& ctx)
|
||||
{
|
||||
if (ctx.rules.enabled(fixLendingProtocolV1_1))
|
||||
return tfVaultDepositMask;
|
||||
|
||||
return tfVaultDepositMask | tfVaultDonate;
|
||||
}
|
||||
|
||||
NotTEC
|
||||
VaultDeposit::preflight(PreflightContext const& ctx)
|
||||
{
|
||||
@@ -68,6 +77,22 @@ VaultDeposit::preclaim(PreclaimContext const& ctx)
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
if (ctx.view.rules().enabled(fixLendingProtocolV1_1) && ctx.tx.isFlag(tfVaultDonate))
|
||||
{
|
||||
if (account != vault->at(sfOwner))
|
||||
{
|
||||
JLOG(ctx.j.debug()) << "VaultDeposit: only owner can donate to vault.";
|
||||
return tecNO_PERMISSION;
|
||||
}
|
||||
|
||||
// Cannot donate to a vault with no shares
|
||||
if (sleIssuance->at(sfOutstandingAmount) == 0)
|
||||
{
|
||||
JLOG(ctx.j.debug()) << "VaultDeposit: empty vault cannot receive donations.";
|
||||
return tecNO_PERMISSION;
|
||||
}
|
||||
}
|
||||
|
||||
if (sleIssuance->isFlag(lsfMPTLocked))
|
||||
{
|
||||
// LCOV_EXCL_START
|
||||
@@ -180,42 +205,51 @@ VaultDeposit::doApply()
|
||||
return err;
|
||||
}
|
||||
}
|
||||
|
||||
STAmount sharesCreated = {vault->at(sfShareMPTID)}, assetsDeposited;
|
||||
try
|
||||
if (view().rules().enabled(fixLendingProtocolV1_1) && ctx_.tx.isFlag(tfVaultDonate))
|
||||
{
|
||||
// Compute exchange before transferring any amounts.
|
||||
{
|
||||
auto const maybeShares = assetsToSharesDeposit(vault, sleIssuance, amount);
|
||||
if (!maybeShares)
|
||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||
sharesCreated = *maybeShares;
|
||||
}
|
||||
if (sharesCreated == beast::zero)
|
||||
return tecPRECISION_LOSS;
|
||||
|
||||
auto const maybeAssets = sharesToAssetsDeposit(vault, sleIssuance, sharesCreated);
|
||||
if (!maybeAssets)
|
||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||
else if (*maybeAssets > amount)
|
||||
{
|
||||
// LCOV_EXCL_START
|
||||
JLOG(j_.error()) << "VaultDeposit: would take more than offered.";
|
||||
return tecINTERNAL;
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
assetsDeposited = *maybeAssets;
|
||||
XRPL_ASSERT(
|
||||
account_ == vault->at(sfOwner), "xrpl::VaultDeposit::doApply : account is owner");
|
||||
assetsDeposited = amount;
|
||||
}
|
||||
catch (std::overflow_error const&)
|
||||
else
|
||||
{
|
||||
// It's easy to hit this exception from Number with large enough Scale
|
||||
// so we avoid spamming the log and only use debug here.
|
||||
JLOG(j_.debug()) //
|
||||
<< "VaultDeposit: overflow error with"
|
||||
<< " scale=" << (int)vault->at(sfScale).value() //
|
||||
<< ", assetsTotal=" << vault->at(sfAssetsTotal).value()
|
||||
<< ", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) << ", amount=" << amount;
|
||||
return tecPATH_DRY;
|
||||
try
|
||||
{
|
||||
// Compute exchange before transferring any amounts.
|
||||
{
|
||||
auto const maybeShares = assetsToSharesDeposit(vault, sleIssuance, amount);
|
||||
if (!maybeShares)
|
||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||
sharesCreated = *maybeShares;
|
||||
}
|
||||
if (sharesCreated == beast::zero)
|
||||
return tecPRECISION_LOSS;
|
||||
|
||||
auto const maybeAssets = sharesToAssetsDeposit(vault, sleIssuance, sharesCreated);
|
||||
if (!maybeAssets)
|
||||
return tecINTERNAL; // LCOV_EXCL_LINE
|
||||
else if (*maybeAssets > amount)
|
||||
{
|
||||
// LCOV_EXCL_START
|
||||
JLOG(j_.error()) << "VaultDeposit: would take more than offered.";
|
||||
return tecINTERNAL;
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
assetsDeposited = *maybeAssets;
|
||||
}
|
||||
catch (std::overflow_error const&)
|
||||
{
|
||||
// It's easy to hit this exception from Number with large enough Scale
|
||||
// so we avoid spamming the log and only use debug here.
|
||||
JLOG(j_.debug()) //
|
||||
<< "VaultDeposit: overflow error with"
|
||||
<< " scale=" << (int)vault->at(sfScale).value() //
|
||||
<< ", assetsTotal=" << vault->at(sfAssetsTotal).value()
|
||||
<< ", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
|
||||
<< ", amount=" << amount;
|
||||
return tecPATH_DRY;
|
||||
}
|
||||
}
|
||||
|
||||
XRPL_ASSERT(
|
||||
@@ -252,11 +286,19 @@ VaultDeposit::doApply()
|
||||
// LCOV_EXCL_STOP
|
||||
}
|
||||
|
||||
// Transfer shares from vault to depositor.
|
||||
if (auto const ter =
|
||||
accountSend(view(), vaultAccount, account_, sharesCreated, j_, WaiveTransferFee::Yes);
|
||||
!isTesSuccess(ter))
|
||||
return ter;
|
||||
if (view().rules().enabled(fixLendingProtocolV1_1) && ctx_.tx.isFlag(tfVaultDonate))
|
||||
{
|
||||
XRPL_ASSERT(
|
||||
sharesCreated == beast::zero, "xrpl::VaultDeposit::doApply: donation issued shares");
|
||||
}
|
||||
else
|
||||
{
|
||||
// Transfer shares from vault to depositor.
|
||||
if (auto const ter = accountSend(
|
||||
view(), vaultAccount, account_, sharesCreated, j_, WaiveTransferFee::Yes);
|
||||
!isTesSuccess(ter))
|
||||
return ter;
|
||||
}
|
||||
|
||||
associateAsset(*vault, vaultAsset);
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
#include <xrpl/protocol/STLedgerEntry.h>
|
||||
#include <xrpl/protocol/STNumber.h>
|
||||
#include <xrpl/protocol/TER.h>
|
||||
#include <xrpl/protocol/TxFlags.h>
|
||||
#include <xrpl/protocol/TxFormats.h>
|
||||
#include <xrpl/protocol/XRPAmount.h>
|
||||
#include <xrpl/tx/ApplyContext.h>
|
||||
@@ -3452,6 +3453,45 @@ class Invariants_test : public beast::unit_test::suite
|
||||
precloseXrp,
|
||||
TxAccount::A2);
|
||||
|
||||
doInvariantCheck(
|
||||
{"donation must not change depositor shares"},
|
||||
[&](Account const& A1, Account const& A2, ApplyContext& ac) {
|
||||
auto const keylet = keylet::vault(A1.id(), ac.view().seq());
|
||||
return adjust(ac.view(), keylet, args(A2.id(), 10, [&](Adjustments& sample) {
|
||||
sample.accountShares->amount = 10;
|
||||
}));
|
||||
},
|
||||
XRPAmount{},
|
||||
STTx{
|
||||
ttVAULT_DEPOSIT,
|
||||
[](STObject& tx) {
|
||||
tx[sfAmount] = XRPAmount(10);
|
||||
tx[sfFlags] = tfVaultDonate;
|
||||
}},
|
||||
{tecINVARIANT_FAILED, tecINVARIANT_FAILED},
|
||||
precloseXrp,
|
||||
TxAccount::A2);
|
||||
|
||||
doInvariantCheck(
|
||||
{"donation must not change vault shares"},
|
||||
[&](Account const& A1, Account const& A2, ApplyContext& ac) {
|
||||
auto const keylet = keylet::vault(A1.id(), ac.view().seq());
|
||||
return adjust(ac.view(), keylet, args(A2.id(), 10, [&](Adjustments& sample) {
|
||||
sample.sharesTotal = 10;
|
||||
sample.accountShares = std::nullopt;
|
||||
}));
|
||||
},
|
||||
XRPAmount{},
|
||||
STTx{
|
||||
ttVAULT_DEPOSIT,
|
||||
[](STObject& tx) {
|
||||
tx[sfAmount] = XRPAmount(10);
|
||||
tx[sfFlags] = tfVaultDonate;
|
||||
}},
|
||||
{tecINVARIANT_FAILED, tecINVARIANT_FAILED},
|
||||
precloseXrp,
|
||||
TxAccount::A2);
|
||||
|
||||
testcase << "Vault withdrawal";
|
||||
doInvariantCheck(
|
||||
{"withdrawal must change vault balance"},
|
||||
|
||||
@@ -1787,10 +1787,21 @@ class LoanBroker_test : public beast::unit_test::suite
|
||||
testRIPD4274MPT();
|
||||
}
|
||||
|
||||
void
|
||||
testFixAmendmentEnabled()
|
||||
{
|
||||
using namespace jtx;
|
||||
testcase("testFixAmendmentEnabled");
|
||||
Env env{*this};
|
||||
|
||||
BEAST_EXPECT(env.enabled(fixLendingProtocolV1_1));
|
||||
}
|
||||
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testFixAmendmentEnabled();
|
||||
testLoanBrokerSetDebtMaximum();
|
||||
testLoanBrokerCoverDepositNullVault();
|
||||
|
||||
|
||||
@@ -1073,6 +1073,7 @@ class Vault_test : public beast::unit_test::suite
|
||||
Asset const& asset,
|
||||
Vault& vault)> test) {
|
||||
Env env{*this, testable_amendments() | featureSingleAssetVault};
|
||||
|
||||
Account issuer{"issuer"};
|
||||
Account owner{"owner"};
|
||||
Account depositor{"depositor"};
|
||||
@@ -5357,6 +5358,213 @@ class Vault_test : public beast::unit_test::suite
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testVaultDeleteData()
|
||||
{
|
||||
using namespace test::jtx;
|
||||
|
||||
Env env{*this};
|
||||
|
||||
Account const owner{"owner"};
|
||||
env.fund(XRP(1'000'000), owner);
|
||||
env.close();
|
||||
|
||||
Vault vault{env};
|
||||
|
||||
auto const keylet = keylet::vault(owner.id(), 1);
|
||||
auto delTx = vault.del({.owner = owner, .id = keylet.key});
|
||||
|
||||
// Test VaultDelete with fixLendingProtocolV1_1 disabled
|
||||
// Transaction fails if the data field is provided
|
||||
{
|
||||
testcase("VaultDelete data fixLendingProtocolV1_1 disabled");
|
||||
env.disableFeature(fixLendingProtocolV1_1);
|
||||
delTx[sfMemoData] = strHex(std::string(maxDataPayloadLength, 'A'));
|
||||
env(delTx, ter(temDISABLED), THISLINE);
|
||||
env.close();
|
||||
env.enableFeature(fixLendingProtocolV1_1);
|
||||
}
|
||||
|
||||
// Transaction fails if the data field is too large
|
||||
{
|
||||
testcase("VaultDelete data fixLendingProtocolV1_1 enabled data too large");
|
||||
delTx[sfMemoData] = strHex(std::string(maxDataPayloadLength + 1, 'A'));
|
||||
env(delTx, ter(temMALFORMED), THISLINE);
|
||||
env.close();
|
||||
}
|
||||
|
||||
// Transaction fails if the data field is set, but is empty
|
||||
{
|
||||
testcase("VaultDelete data fixLendingProtocolV1_1 enabled data empty");
|
||||
delTx[sfMemoData] = strHex(std::string(0, 'A'));
|
||||
env(delTx, ter(temMALFORMED), THISLINE);
|
||||
env.close();
|
||||
}
|
||||
|
||||
{
|
||||
testcase("VaultDelete data fixLendingProtocolV1_1 enabled data valid");
|
||||
PrettyAsset const xrpAsset = xrpIssue();
|
||||
auto [tx, keylet] = vault.create({.owner = owner, .asset = xrpAsset});
|
||||
env(tx, ter(tesSUCCESS), THISLINE);
|
||||
env.close();
|
||||
// Recreate the transaction as the vault keylet changed
|
||||
auto delTx = vault.del({.owner = owner, .id = keylet.key});
|
||||
delTx[sfMemoData] = strHex(std::string(maxDataPayloadLength, 'A'));
|
||||
env(delTx, ter(tesSUCCESS), THISLINE);
|
||||
env.close();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testVaultDepositDonate()
|
||||
{
|
||||
using namespace test::jtx;
|
||||
std::string const prefix = "VaultDeposit donate";
|
||||
|
||||
Env env{*this};
|
||||
Vault vault{env};
|
||||
|
||||
auto const vaultShareBalance = [&](Keylet const& vaultKeylet) {
|
||||
auto const sleVault = env.le(vaultKeylet);
|
||||
BEAST_EXPECT(sleVault != nullptr);
|
||||
|
||||
auto const sleIssuance = env.le(keylet::mptIssuance(sleVault->at(sfShareMPTID)));
|
||||
BEAST_EXPECT(sleIssuance != nullptr);
|
||||
|
||||
return sleIssuance->at(sfOutstandingAmount);
|
||||
};
|
||||
|
||||
auto const vaultAssetBalance = [&](Keylet const& vaultKeylet) {
|
||||
auto const sleVault = env.le(vaultKeylet);
|
||||
BEAST_EXPECT(sleVault != nullptr);
|
||||
|
||||
return std::make_pair(sleVault->at(sfAssetsAvailable), sleVault->at(sfAssetsTotal));
|
||||
};
|
||||
|
||||
Account const owner{"owner"};
|
||||
Account const depositor{"depositor"};
|
||||
env.fund(XRP(1'000'000), owner, depositor);
|
||||
env.close();
|
||||
|
||||
auto const depositAmount = XRP(10);
|
||||
|
||||
auto const [tx, keylet] = vault.create({.owner = owner, .asset = xrpIssue()});
|
||||
env(tx, ter(tesSUCCESS), THISLINE);
|
||||
env.close();
|
||||
|
||||
// With fixLendingProtocolV1_1 disabled, donations fail
|
||||
{
|
||||
testcase(prefix + " fails with fixLendingProtocolV1_1 disabled");
|
||||
env.disableFeature(fixLendingProtocolV1_1);
|
||||
auto const tx = vault.deposit({
|
||||
.depositor = owner,
|
||||
.id = keylet.key,
|
||||
.amount = depositAmount,
|
||||
.flags = tfVaultDonate,
|
||||
});
|
||||
env(tx, ter{temINVALID_FLAG}, THISLINE);
|
||||
env.enableFeature(fixLendingProtocolV1_1);
|
||||
env.close();
|
||||
}
|
||||
|
||||
// Donation is not allowed to an empty vault
|
||||
{
|
||||
testcase(prefix + " fails to an empty vault");
|
||||
auto const tx = vault.deposit({
|
||||
.depositor = owner,
|
||||
.id = keylet.key,
|
||||
.amount = depositAmount,
|
||||
.flags = tfVaultDonate,
|
||||
});
|
||||
env(tx, ter{tecNO_PERMISSION}, THISLINE);
|
||||
env.close();
|
||||
}
|
||||
|
||||
// Further unit tests require assets in the Vault
|
||||
env(vault.deposit({
|
||||
.depositor = depositor,
|
||||
.id = keylet.key,
|
||||
.amount = depositAmount,
|
||||
}),
|
||||
ter{tesSUCCESS},
|
||||
THISLINE);
|
||||
env.close();
|
||||
|
||||
// Donation is not allowed by a non-owner
|
||||
{
|
||||
testcase(prefix + " fails by a non-owner");
|
||||
auto const tx = vault.deposit({
|
||||
.depositor = depositor,
|
||||
.id = keylet.key,
|
||||
.amount = depositAmount,
|
||||
.flags = tfVaultDonate,
|
||||
});
|
||||
env(tx, ter{tecNO_PERMISSION}, THISLINE);
|
||||
env.close();
|
||||
}
|
||||
|
||||
// Donation cannot exceed assets maximum
|
||||
{
|
||||
testcase(prefix + " cannot exceed assets maximum");
|
||||
auto tx = vault.set({
|
||||
.owner = owner,
|
||||
.id = keylet.key,
|
||||
});
|
||||
tx[sfAssetsMaximum] = XRP(30).number();
|
||||
env(tx, ter{tesSUCCESS}, THISLINE);
|
||||
|
||||
tx = vault.deposit({
|
||||
.depositor = owner,
|
||||
.id = keylet.key,
|
||||
.amount = depositAmount + XRP(30),
|
||||
.flags = tfVaultDonate,
|
||||
});
|
||||
|
||||
env(tx, ter{tecLIMIT_EXCEEDED}, THISLINE);
|
||||
env.close();
|
||||
}
|
||||
|
||||
{
|
||||
testcase(prefix + " succeeds");
|
||||
auto const shareBalance = vaultShareBalance(keylet);
|
||||
auto const [assetsAvailable, assetsTotal] = vaultAssetBalance(keylet);
|
||||
|
||||
auto tx = vault.deposit({
|
||||
.depositor = owner,
|
||||
.id = keylet.key,
|
||||
.amount = depositAmount,
|
||||
.flags = tfVaultDonate,
|
||||
});
|
||||
env(tx, ter{tesSUCCESS}, THISLINE);
|
||||
env.close();
|
||||
|
||||
auto const shareBalanceAfterDeposit = vaultShareBalance(keylet);
|
||||
auto const [assetsAvailableAfterDeposit, assetsTotalAfterDeposit] =
|
||||
vaultAssetBalance(keylet);
|
||||
|
||||
BEAST_EXPECT(shareBalance == shareBalanceAfterDeposit);
|
||||
BEAST_EXPECT(assetsAvailable + depositAmount.number() == assetsAvailableAfterDeposit);
|
||||
BEAST_EXPECT(assetsTotal + depositAmount.number() == assetsTotalAfterDeposit);
|
||||
|
||||
auto const sleVault = env.le(keylet);
|
||||
if (!BEAST_EXPECT(sleVault))
|
||||
return;
|
||||
|
||||
// The depositor can withdraw their assets and the donated amount
|
||||
Asset shareAsset(sleVault->at(sfShareMPTID));
|
||||
tx = vault.withdraw(
|
||||
{.depositor = depositor, .id = keylet.key, .amount = shareAsset(shareBalance)});
|
||||
env(tx, ter{tesSUCCESS}, THISLINE);
|
||||
|
||||
auto const shareBalanceAfterWithdraw = vaultShareBalance(keylet);
|
||||
auto const [assetsAvailableAfterWithdraw, assetsTotalAfterWithdraw] =
|
||||
vaultAssetBalance(keylet);
|
||||
BEAST_EXPECT(shareBalanceAfterWithdraw == 0);
|
||||
BEAST_EXPECT(assetsAvailableAfterWithdraw == 0);
|
||||
BEAST_EXPECT(assetsTotalAfterWithdraw == 0);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
void
|
||||
run() override
|
||||
@@ -5378,6 +5586,8 @@ public:
|
||||
testVaultClawbackBurnShares();
|
||||
testVaultClawbackAssets();
|
||||
testAssetsMaximum();
|
||||
testVaultDepositDonate();
|
||||
testVaultDeleteData();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -52,6 +52,9 @@ Vault::deposit(DepositArgs const& args)
|
||||
jv[jss::Account] = args.depositor.human();
|
||||
jv[sfVaultID] = to_string(args.id);
|
||||
jv[jss::Amount] = to_json(args.amount);
|
||||
if (args.flags)
|
||||
jv[jss::Flags] = *args.flags;
|
||||
|
||||
return jv;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,7 @@ struct Vault
|
||||
Account depositor;
|
||||
uint256 id;
|
||||
STAmount amount;
|
||||
std::optional<std::uint32_t> flags{};
|
||||
};
|
||||
|
||||
Json::Value
|
||||
|
||||
Reference in New Issue
Block a user