rippled
Loading...
Searching...
No Matches
VaultWithdraw.cpp
1#include <xrpld/app/tx/detail/VaultWithdraw.h>
2
3#include <xrpl/ledger/CredentialHelpers.h>
4#include <xrpl/ledger/View.h>
5#include <xrpl/protocol/AccountID.h>
6#include <xrpl/protocol/Feature.h>
7#include <xrpl/protocol/SField.h>
8#include <xrpl/protocol/STNumber.h>
9#include <xrpl/protocol/STTakesAsset.h>
10#include <xrpl/protocol/TER.h>
11#include <xrpl/protocol/TxFlags.h>
12
13namespace xrpl {
14
17{
18 if (ctx.tx[sfVaultID] == beast::zero)
19 {
20 JLOG(ctx.j.debug()) << "VaultWithdraw: zero/empty vault ID.";
21 return temMALFORMED;
22 }
23
24 if (ctx.tx[sfAmount] <= beast::zero)
25 return temBAD_AMOUNT;
26
27 if (auto const destination = ctx.tx[~sfDestination])
28 {
29 if (*destination == beast::zero)
30 {
31 return temMALFORMED;
32 }
33 }
34
35 return tesSUCCESS;
36}
37
38TER
40{
41 auto const vault = ctx.view.read(keylet::vault(ctx.tx[sfVaultID]));
42 if (!vault)
43 return tecNO_ENTRY;
44
45 auto const assets = ctx.tx[sfAmount];
46 auto const vaultAsset = vault->at(sfAsset);
47 auto const vaultShare = vault->at(sfShareMPTID);
48 if (assets.asset() != vaultAsset && assets.asset() != vaultShare)
49 return tecWRONG_ASSET;
50
51 auto const& vaultAccount = vault->at(sfAccount);
52 auto const& account = ctx.tx[sfAccount];
53 auto const& dstAcct = ctx.tx[~sfDestination].value_or(account);
54 if (auto ter = canTransfer(ctx.view, vaultAsset, vaultAccount, dstAcct); !isTesSuccess(ter))
55 {
56 JLOG(ctx.j.debug()) << "VaultWithdraw: vault assets are non-transferable.";
57 return ter;
58 }
59
60 // Enforce valid withdrawal policy
61 if (vault->at(sfWithdrawalPolicy) != vaultStrategyFirstComeFirstServe)
62 {
63 // LCOV_EXCL_START
64 JLOG(ctx.j.error()) << "VaultWithdraw: invalid withdrawal policy.";
65 return tefINTERNAL;
66 // LCOV_EXCL_STOP
67 }
68
69 if (auto const ret = canWithdraw(ctx.view, ctx.tx))
70 return ret;
71
72 // If sending to Account (i.e. not a transfer), we will also create (only
73 // if authorized) a trust line or MPToken as needed, in doApply().
74 // Destination MPToken or trust line must exist if _not_ sending to Account.
75 AuthType const authType = account == dstAcct ? AuthType::WeakAuth : AuthType::StrongAuth;
76 if (auto const ter = requireAuth(ctx.view, vaultAsset, dstAcct, authType); !isTesSuccess(ter))
77 return ter;
78
79 // Cannot withdraw from a Vault an Asset frozen for the destination account
80 if (auto const ret = checkFrozen(ctx.view, dstAcct, vaultAsset))
81 return ret;
82
83 // Cannot return shares to the vault, if the underlying asset was frozen for
84 // the submitter
85 if (auto const ret = checkFrozen(ctx.view, account, vaultShare))
86 return ret;
87
88 return tesSUCCESS;
89}
90
91TER
93{
94 auto const vault = view().peek(keylet::vault(ctx_.tx[sfVaultID]));
95 if (!vault)
96 return tefINTERNAL; // LCOV_EXCL_LINE
97
98 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
99 auto const sleIssuance = view().read(keylet::mptIssuance(mptIssuanceID));
100 if (!sleIssuance)
101 {
102 // LCOV_EXCL_START
103 JLOG(j_.error()) << "VaultWithdraw: missing issuance of vault shares.";
104 return tefINTERNAL;
105 // LCOV_EXCL_STOP
106 }
107
108 // Note, we intentionally do not check lsfVaultPrivate flag on the Vault. If
109 // you have a share in the vault, it means you were at some point authorized
110 // to deposit into it, and this means you are also indefinitely authorized
111 // to withdraw from it.
112
113 auto const amount = ctx_.tx[sfAmount];
114 Asset const vaultAsset = vault->at(sfAsset);
115
116 MPTIssue const share{mptIssuanceID};
117 STAmount sharesRedeemed = {share};
118 STAmount assetsWithdrawn;
119 try
120 {
121 if (amount.asset() == vaultAsset)
122 {
123 // Fixed assets, variable shares.
124 {
125 auto const maybeShares = assetsToSharesWithdraw(vault, sleIssuance, amount);
126 if (!maybeShares)
127 return tecINTERNAL; // LCOV_EXCL_LINE
128 sharesRedeemed = *maybeShares;
129 }
130
131 if (sharesRedeemed == beast::zero)
132 return tecPRECISION_LOSS;
133 auto const maybeAssets = sharesToAssetsWithdraw(vault, sleIssuance, sharesRedeemed);
134 if (!maybeAssets)
135 return tecINTERNAL; // LCOV_EXCL_LINE
136 assetsWithdrawn = *maybeAssets;
137 }
138 else if (amount.asset() == share)
139 {
140 // Fixed shares, variable assets.
141 sharesRedeemed = amount;
142 auto const maybeAssets = sharesToAssetsWithdraw(vault, sleIssuance, sharesRedeemed);
143 if (!maybeAssets)
144 return tecINTERNAL; // LCOV_EXCL_LINE
145 assetsWithdrawn = *maybeAssets;
146 }
147 else
148 return tefINTERNAL; // LCOV_EXCL_LINE
149 }
150 catch (std::overflow_error const&)
151 {
152 // It's easy to hit this exception from Number with large enough Scale
153 // so we avoid spamming the log and only use debug here.
154 JLOG(j_.debug()) //
155 << "VaultWithdraw: overflow error with"
156 << " scale=" << (int)vault->at(sfScale).value() //
157 << ", assetsTotal=" << vault->at(sfAssetsTotal).value()
158 << ", sharesTotal=" << sleIssuance->at(sfOutstandingAmount) << ", amount=" << amount.value();
159 return tecPATH_DRY;
160 }
161
163 sharesRedeemed)
164 {
165 JLOG(j_.debug()) << "VaultWithdraw: account doesn't hold enough shares";
167 }
168
169 auto assetsAvailable = vault->at(sfAssetsAvailable);
170 auto assetsTotal = vault->at(sfAssetsTotal);
171 [[maybe_unused]] auto const lossUnrealized = vault->at(sfLossUnrealized);
172 XRPL_ASSERT(
173 lossUnrealized <= (assetsTotal - assetsAvailable), "xrpl::VaultWithdraw::doApply : loss and assets do balance");
174
175 // The vault must have enough assets on hand. The vault may hold assets
176 // that it has already pledged. That is why we look at AssetAvailable
177 // instead of the pseudo-account balance.
178 if (*assetsAvailable < assetsWithdrawn)
179 {
180 JLOG(j_.debug()) << "VaultWithdraw: vault doesn't hold enough assets";
182 }
183
184 assetsTotal -= assetsWithdrawn;
185 assetsAvailable -= assetsWithdrawn;
186 view().update(vault);
187
188 auto const& vaultAccount = vault->at(sfAccount);
189 // Transfer shares from depositor to vault.
190 if (auto const ter = accountSend(view(), account_, vaultAccount, sharesRedeemed, j_, WaiveTransferFee::Yes);
191 !isTesSuccess(ter))
192 return ter;
193
194 // Try to remove MPToken for shares, if the account balance is zero. Vault
195 // pseudo-account will never set lsfMPTAuthorized, so we ignore flags.
196 // Keep MPToken if holder is the vault owner.
197 if (account_ != vault->at(sfOwner))
198 {
199 if (auto const ter = removeEmptyHolding(view(), account_, sharesRedeemed.asset(), j_); isTesSuccess(ter))
200 {
201 JLOG(j_.debug()) //
202 << "VaultWithdraw: removed empty MPToken for vault shares"
203 << " MPTID=" << to_string(mptIssuanceID) //
204 << " account=" << toBase58(account_);
205 }
206 else if (ter != tecHAS_OBLIGATIONS)
207 {
208 // LCOV_EXCL_START
209 JLOG(j_.error()) //
210 << "VaultWithdraw: failed to remove MPToken for vault shares"
211 << " MPTID=" << to_string(mptIssuanceID) //
212 << " account=" << toBase58(account_) //
213 << " with result: " << transToken(ter);
214 return ter;
215 // LCOV_EXCL_STOP
216 }
217 // else quietly ignore, account balance is not zero
218 }
219
220 auto const dstAcct = ctx_.tx[~sfDestination].value_or(account_);
221
222 associateAsset(*vault, vaultAsset);
223
224 return doWithdraw(view(), ctx_.tx, account_, dstAcct, vaultAccount, mPriorBalance, assetsWithdrawn, j_);
225}
226
227} // namespace xrpl
Stream error() const
Definition Journal.h:318
Stream debug() const
Definition Journal.h:300
STTx const & tx
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Asset const & asset() const
Definition STAmount.h:441
AccountID const account_
Definition Transactor.h:112
beast::Journal const j_
Definition Transactor.h:110
ApplyView & view()
Definition Transactor.h:128
XRPAmount mPriorBalance
Definition Transactor.h:113
ApplyContext & ctx_
Definition Transactor.h:108
TER doApply() override
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
Keylet mptIssuance(std::uint32_t seq, AccountID const &issuer) noexcept
Definition Indexes.cpp:462
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:492
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
@ fhZERO_IF_FROZEN
Definition View.h:58
std::uint8_t constexpr vaultStrategyFirstComeFirstServe
Vault withdrawal policies.
Definition Protocol.h:240
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, Issue const &issue, beast::Journal journal)
Definition View.cpp:1554
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:597
TER doWithdraw(ApplyView &view, STTx const &tx, AccountID const &senderAcct, AccountID const &dstAcct, AccountID const &sourceAcct, XRPAmount priorBalance, STAmount const &amount, beast::Journal j)
Definition View.cpp:1209
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
Definition View.h:121
@ tefINTERNAL
Definition TER.h:153
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:92
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
Definition View.cpp:392
std::string transToken(TER code)
Definition TER.cpp:243
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
Definition View.cpp:2446
std::optional< STAmount > assetsToSharesWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &assets, TruncateShares truncate=TruncateShares::no)
Definition View.cpp:3149
@ ahIGNORE_AUTH
Definition View.h:61
AuthType
Definition View.h:801
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
Definition View.cpp:2710
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
Definition View.cpp:2914
@ temMALFORMED
Definition TER.h:67
@ temBAD_AMOUNT
Definition TER.h:69
bool isTesSuccess(TER x) noexcept
Definition TER.h:649
std::optional< STAmount > sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
Definition View.cpp:3174
@ tecWRONG_ASSET
Definition TER.h:341
@ tecNO_ENTRY
Definition TER.h:287
@ tecPATH_DRY
Definition TER.h:275
@ tecINTERNAL
Definition TER.h:291
@ tecINSUFFICIENT_FUNDS
Definition TER.h:306
@ tecPRECISION_LOSS
Definition TER.h:344
@ tecHAS_OBLIGATIONS
Definition TER.h:298
TER canWithdraw(ReadView const &view, AccountID const &from, AccountID const &to, SLE::const_ref toSle, STAmount const &amount, bool hasDestinationTag)
Checks that can withdraw funds from an object to itself or a destination.
Definition View.cpp:1163
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:580
@ tesSUCCESS
Definition TER.h:225
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:53
ReadView const & view
Definition Transactor.h:56
beast::Journal const j
Definition Transactor.h:61
State information when preflighting a tx.
Definition Transactor.h:15
beast::Journal const j
Definition Transactor.h:22