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/TER.h>
10#include <xrpl/protocol/TxFlags.h>
11
12namespace xrpl {
13
16{
17 if (ctx.tx[sfVaultID] == beast::zero)
18 {
19 JLOG(ctx.j.debug()) << "VaultWithdraw: zero/empty vault ID.";
20 return temMALFORMED;
21 }
22
23 if (ctx.tx[sfAmount] <= beast::zero)
24 return temBAD_AMOUNT;
25
26 if (auto const destination = ctx.tx[~sfDestination])
27 {
28 if (*destination == beast::zero)
29 {
30 return temMALFORMED;
31 }
32 }
33
34 return tesSUCCESS;
35}
36
37TER
39{
40 auto const vault = ctx.view.read(keylet::vault(ctx.tx[sfVaultID]));
41 if (!vault)
42 return tecNO_ENTRY;
43
44 auto const assets = ctx.tx[sfAmount];
45 auto const vaultAsset = vault->at(sfAsset);
46 auto const vaultShare = vault->at(sfShareMPTID);
47 if (assets.asset() != vaultAsset && assets.asset() != vaultShare)
48 return tecWRONG_ASSET;
49
50 auto const& vaultAccount = vault->at(sfAccount);
51 auto const& account = ctx.tx[sfAccount];
52 auto const& dstAcct = ctx.tx[~sfDestination].value_or(account);
53 if (auto ter = canTransfer(ctx.view, vaultAsset, vaultAccount, dstAcct);
54 !isTesSuccess(ter))
55 {
56 JLOG(ctx.j.debug())
57 << "VaultWithdraw: vault assets are non-transferable.";
58 return ter;
59 }
60
61 // Enforce valid withdrawal policy
62 if (vault->at(sfWithdrawalPolicy) != vaultStrategyFirstComeFirstServe)
63 {
64 // LCOV_EXCL_START
65 JLOG(ctx.j.error()) << "VaultWithdraw: invalid withdrawal policy.";
66 return tefINTERNAL;
67 // LCOV_EXCL_STOP
68 }
69
70 if (auto const ret = canWithdraw(ctx.view, ctx.tx))
71 return ret;
72
73 // If sending to Account (i.e. not a transfer), we will also create (only
74 // if authorized) a trust line or MPToken as needed, in doApply().
75 // Destination MPToken or trust line must exist if _not_ sending to Account.
76 AuthType const authType =
77 account == dstAcct ? AuthType::WeakAuth : AuthType::StrongAuth;
78 if (auto const ter = requireAuth(ctx.view, vaultAsset, dstAcct, authType);
79 !isTesSuccess(ter))
80 return ter;
81
82 // Cannot withdraw from a Vault an Asset frozen for the destination account
83 if (auto const ret = checkFrozen(ctx.view, dstAcct, vaultAsset))
84 return ret;
85
86 // Cannot return shares to the vault, if the underlying asset was frozen for
87 // the submitter
88 if (auto const ret = checkFrozen(ctx.view, account, vaultShare))
89 return ret;
90
91 return tesSUCCESS;
92}
93
94TER
96{
97 auto const vault = view().peek(keylet::vault(ctx_.tx[sfVaultID]));
98 if (!vault)
99 return tefINTERNAL; // LCOV_EXCL_LINE
100
101 auto const mptIssuanceID = *((*vault)[sfShareMPTID]);
102 auto const sleIssuance = view().read(keylet::mptIssuance(mptIssuanceID));
103 if (!sleIssuance)
104 {
105 // LCOV_EXCL_START
106 JLOG(j_.error()) << "VaultWithdraw: missing issuance of vault shares.";
107 return tefINTERNAL;
108 // LCOV_EXCL_STOP
109 }
110
111 // Note, we intentionally do not check lsfVaultPrivate flag on the Vault. If
112 // you have a share in the vault, it means you were at some point authorized
113 // to deposit into it, and this means you are also indefinitely authorized
114 // to withdraw from it.
115
116 auto const amount = ctx_.tx[sfAmount];
117 Asset const vaultAsset = vault->at(sfAsset);
118 MPTIssue const share{mptIssuanceID};
119 STAmount sharesRedeemed = {share};
120 STAmount assetsWithdrawn;
121 try
122 {
123 if (amount.asset() == vaultAsset)
124 {
125 // Fixed assets, variable shares.
126 {
127 auto const maybeShares =
128 assetsToSharesWithdraw(vault, sleIssuance, amount);
129 if (!maybeShares)
130 return tecINTERNAL; // LCOV_EXCL_LINE
131 sharesRedeemed = *maybeShares;
132 }
133
134 if (sharesRedeemed == beast::zero)
135 return tecPRECISION_LOSS;
136 auto const maybeAssets =
137 sharesToAssetsWithdraw(vault, sleIssuance, sharesRedeemed);
138 if (!maybeAssets)
139 return tecINTERNAL; // LCOV_EXCL_LINE
140 assetsWithdrawn = *maybeAssets;
141 }
142 else if (amount.asset() == share)
143 {
144 // Fixed shares, variable assets.
145 sharesRedeemed = amount;
146 auto const maybeAssets =
147 sharesToAssetsWithdraw(vault, sleIssuance, sharesRedeemed);
148 if (!maybeAssets)
149 return tecINTERNAL; // LCOV_EXCL_LINE
150 assetsWithdrawn = *maybeAssets;
151 }
152 else
153 return tefINTERNAL; // LCOV_EXCL_LINE
154 }
155 catch (std::overflow_error const&)
156 {
157 // It's easy to hit this exception from Number with large enough Scale
158 // so we avoid spamming the log and only use debug here.
159 JLOG(j_.debug()) //
160 << "VaultWithdraw: overflow error with"
161 << " scale=" << (int)vault->at(sfScale).value() //
162 << ", assetsTotal=" << vault->at(sfAssetsTotal).value()
163 << ", sharesTotal=" << sleIssuance->at(sfOutstandingAmount)
164 << ", amount=" << amount.value();
165 return tecPATH_DRY;
166 }
167
168 if (accountHolds(
169 view(),
170 account_,
171 share,
174 j_) < sharesRedeemed)
175 {
176 JLOG(j_.debug()) << "VaultWithdraw: account doesn't hold enough shares";
178 }
179
180 auto assetsAvailable = vault->at(sfAssetsAvailable);
181 auto assetsTotal = vault->at(sfAssetsTotal);
182 [[maybe_unused]] auto const lossUnrealized = vault->at(sfLossUnrealized);
183 XRPL_ASSERT(
184 lossUnrealized <= (assetsTotal - assetsAvailable),
185 "xrpl::VaultWithdraw::doApply : loss and assets do balance");
186
187 // The vault must have enough assets on hand. The vault may hold assets
188 // that it has already pledged. That is why we look at AssetAvailable
189 // instead of the pseudo-account balance.
190 if (*assetsAvailable < assetsWithdrawn)
191 {
192 JLOG(j_.debug()) << "VaultWithdraw: vault doesn't hold enough assets";
194 }
195
196 assetsTotal -= assetsWithdrawn;
197 assetsAvailable -= assetsWithdrawn;
198 view().update(vault);
199
200 auto const& vaultAccount = vault->at(sfAccount);
201 // Transfer shares from depositor to vault.
202 if (auto const ter = accountSend(
203 view(),
204 account_,
205 vaultAccount,
206 sharesRedeemed,
207 j_,
209 !isTesSuccess(ter))
210 return ter;
211
212 // Try to remove MPToken for shares, if the account balance is zero. Vault
213 // pseudo-account will never set lsfMPTAuthorized, so we ignore flags.
214 // Keep MPToken if holder is the vault owner.
215 if (account_ != vault->at(sfOwner))
216 {
217 if (auto const ter = removeEmptyHolding(
218 view(), account_, sharesRedeemed.asset(), j_);
219 isTesSuccess(ter))
220 {
221 JLOG(j_.debug()) //
222 << "VaultWithdraw: removed empty MPToken for vault shares"
223 << " MPTID=" << to_string(mptIssuanceID) //
224 << " account=" << toBase58(account_);
225 }
226 else if (ter != tecHAS_OBLIGATIONS)
227 {
228 // LCOV_EXCL_START
229 JLOG(j_.error()) //
230 << "VaultWithdraw: failed to remove MPToken for vault shares"
231 << " MPTID=" << to_string(mptIssuanceID) //
232 << " account=" << toBase58(account_) //
233 << " with result: " << transToken(ter);
234 return ter;
235 // LCOV_EXCL_STOP
236 }
237 // else quietly ignore, account balance is not zero
238 }
239
240 auto const dstAcct = ctx_.tx[~sfDestination].value_or(account_);
241
242 return doWithdraw(
243 view(),
244 ctx_.tx,
245 account_,
246 dstAcct,
247 vaultAccount,
249 assetsWithdrawn,
250 j_);
251}
252
253} // namespace xrpl
Stream error() const
Definition Journal.h:327
Stream debug() const
Definition Journal.h:309
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:475
AccountID const account_
Definition Transactor.h:128
beast::Journal const j_
Definition Transactor.h:126
ApplyView & view()
Definition Transactor.h:144
XRPAmount mPriorBalance
Definition Transactor.h:129
ApplyContext & ctx_
Definition Transactor.h:124
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:508
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:546
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
@ fhZERO_IF_FROZEN
Definition View.h:59
std::uint8_t constexpr vaultStrategyFirstComeFirstServe
Vault withdrawal policies.
Definition Protocol.h:241
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, Issue const &issue, beast::Journal journal)
Definition View.cpp:1762
std::string to_string(base_uint< Bits, Tag > const &a)
Definition base_uint.h:611
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:1390
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
Definition View.h:160
@ tefINTERNAL
Definition TER.h:154
std::string toBase58(AccountID const &v)
Convert AccountID to base58 checked string.
Definition AccountID.cpp:95
std::string transToken(TER code)
Definition TER.cpp:245
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
Definition View.cpp:461
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:2777
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:3597
@ ahIGNORE_AUTH
Definition View.h:62
AuthType
Definition View.h:985
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:3096
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:3325
TER canWithdraw(AccountID const &from, ReadView const &view, AccountID const &to, SLE::const_ref toSle, bool hasDestinationTag)
Checks that can withdraw funds from an object to itself or a destination.
Definition View.cpp:1346
@ temMALFORMED
Definition TER.h:68
@ temBAD_AMOUNT
Definition TER.h:70
bool isTesSuccess(TER x) noexcept
Definition TER.h:659
std::optional< STAmount > sharesToAssetsWithdraw(std::shared_ptr< SLE const > const &vault, std::shared_ptr< SLE const > const &issuance, STAmount const &shares)
Definition View.cpp:3626
@ tecWRONG_ASSET
Definition TER.h:342
@ tecNO_ENTRY
Definition TER.h:288
@ tecPATH_DRY
Definition TER.h:276
@ tecINTERNAL
Definition TER.h:292
@ tecINSUFFICIENT_FUNDS
Definition TER.h:307
@ tecPRECISION_LOSS
Definition TER.h:345
@ tecHAS_OBLIGATIONS
Definition TER.h:299
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:590
@ tesSUCCESS
Definition TER.h:226
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
beast::Journal const j
Definition Transactor.h:69
State information when preflighting a tx.
Definition Transactor.h:16
beast::Journal const j
Definition Transactor.h:23