|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +/* |
| 3 | + This file is part of rippled: https://github.com/ripple/rippled |
| 4 | + Copyright (c) 2022 Ripple Labs Inc. |
| 5 | +
|
| 6 | + Permission to use, copy, modify, and/or distribute this software for any |
| 7 | + purpose with or without fee is hereby granted, provided that the above |
| 8 | + copyright notice and this permission notice appear in all copies. |
| 9 | +
|
| 10 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | + ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | +*/ |
| 18 | +//============================================================================== |
| 19 | + |
| 20 | +#include <xrpld/app/tx/detail/LoanBroker.h> |
| 21 | +#include <xrpld/app/tx/detail/SignerEntries.h> |
| 22 | +#include <xrpld/app/tx/detail/Transactor.h> |
| 23 | +#include <xrpld/app/tx/detail/VaultCreate.h> |
| 24 | +#include <xrpld/ledger/ApplyView.h> |
| 25 | +#include <xrpld/ledger/View.h> |
| 26 | + |
| 27 | +#include <xrpl/basics/Log.h> |
| 28 | +#include <xrpl/basics/Number.h> |
| 29 | +#include <xrpl/basics/chrono.h> |
| 30 | +#include <xrpl/beast/utility/Journal.h> |
| 31 | +#include <xrpl/beast/utility/instrumentation.h> |
| 32 | +#include <xrpl/protocol/AccountID.h> |
| 33 | +#include <xrpl/protocol/Feature.h> |
| 34 | +#include <xrpl/protocol/Indexes.h> |
| 35 | +#include <xrpl/protocol/PublicKey.h> |
| 36 | +#include <xrpl/protocol/SField.h> |
| 37 | +#include <xrpl/protocol/STAmount.h> |
| 38 | +#include <xrpl/protocol/STObject.h> |
| 39 | +#include <xrpl/protocol/STXChainBridge.h> |
| 40 | +#include <xrpl/protocol/TER.h> |
| 41 | +#include <xrpl/protocol/TxFlags.h> |
| 42 | +#include <xrpl/protocol/XRPAmount.h> |
| 43 | + |
| 44 | +namespace ripple { |
| 45 | + |
| 46 | +bool |
| 47 | +lendingProtocolEnabled(PreflightContext const& ctx) |
| 48 | +{ |
| 49 | + return ctx.rules.enabled(featureLendingProtocol) && |
| 50 | + VaultCreate::isEnabled(ctx); |
| 51 | +} |
| 52 | + |
| 53 | +bool |
| 54 | +LoanBrokerSet::isEnabled(PreflightContext const& ctx) |
| 55 | +{ |
| 56 | + return lendingProtocolEnabled(ctx); |
| 57 | +} |
| 58 | + |
| 59 | +std::uint32_t |
| 60 | +LoanBrokerSet::getFlagsMask(PreflightContext const& ctx) |
| 61 | +{ |
| 62 | + return tfUniversalMask; |
| 63 | +} |
| 64 | + |
| 65 | +NotTEC |
| 66 | +LoanBrokerSet::doPreflight(PreflightContext const& ctx) |
| 67 | +{ |
| 68 | + auto const& tx = ctx.tx; |
| 69 | + if (!validDataLength(tx[~sfData], maxDataPayloadLength)) |
| 70 | + return temINVALID; |
| 71 | + if (!validNumericRange(tx[~sfManagementFeeRate], 0, maxFeeRate)) |
| 72 | + return temINVALID; |
| 73 | + if (!validNumericRange(tx[~sfCoverRateMinimum], 0, maxCoverRate)) |
| 74 | + return temINVALID; |
| 75 | + if (!validNumericRange(tx[~sfCoverRateLiquidation], 0, maxCoverRate)) |
| 76 | + return temINVALID; |
| 77 | + |
| 78 | + if (tx.isFieldPresent(sfLoanBrokerID)) |
| 79 | + { |
| 80 | + // Fixed fields can not be specified if we're modifying an existing |
| 81 | + // LoanBroker Object |
| 82 | + if (tx.isFieldPresent(sfManagementFeeRate) || |
| 83 | + tx.isFieldPresent(sfCoverRateMinimum) || |
| 84 | + tx.isFieldPresent(sfCoverRateLiquidation)) |
| 85 | + return temINVALID; |
| 86 | + } |
| 87 | + |
| 88 | + return tesSUCCESS; |
| 89 | +} |
| 90 | + |
| 91 | +TER |
| 92 | +LoanBrokerSet::preclaim(PreclaimContext const& ctx) |
| 93 | +{ |
| 94 | + auto const& tx = ctx.tx; |
| 95 | + |
| 96 | + auto const account = tx[sfAccount]; |
| 97 | + if (auto const brokerID = tx[~sfLoanBrokerID]) |
| 98 | + { |
| 99 | + auto const sleBroker = ctx.view.read(keylet::loanbroker(*brokerID)); |
| 100 | + if (!sleBroker) |
| 101 | + { |
| 102 | + JLOG(ctx.j.warn()) << "LoanBroker does not exist."; |
| 103 | + return tecNO_ENTRY; |
| 104 | + } |
| 105 | + if (tx[sfVaultID] != sleBroker->at(sfVaultID)) |
| 106 | + { |
| 107 | + JLOG(ctx.j.warn()) |
| 108 | + << "Can not change VaultID on an existing LoanBroker."; |
| 109 | + return tecNO_PERMISSION; |
| 110 | + } |
| 111 | + if (account != sleBroker->at(sfOwner)) |
| 112 | + { |
| 113 | + JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker."; |
| 114 | + return tecNO_PERMISSION; |
| 115 | + } |
| 116 | + } |
| 117 | + else |
| 118 | + { |
| 119 | + auto const vaultID = tx[sfVaultID]; |
| 120 | + auto const sleVault = ctx.view.read(keylet::vault(vaultID)); |
| 121 | + if (!sleVault) |
| 122 | + { |
| 123 | + JLOG(ctx.j.warn()) << "Vault does not exist."; |
| 124 | + return tecNO_ENTRY; |
| 125 | + } |
| 126 | + if (account != sleVault->at(sfOwner)) |
| 127 | + { |
| 128 | + JLOG(ctx.j.warn()) << "Account is not the owner of the Vault."; |
| 129 | + return tecNO_PERMISSION; |
| 130 | + } |
| 131 | + } |
| 132 | + return tesSUCCESS; |
| 133 | +} |
| 134 | + |
| 135 | +TER |
| 136 | +LoanBrokerSet::doApply() |
| 137 | +{ |
| 138 | + auto const& tx = ctx_.tx; |
| 139 | + auto& view = ctx_.view(); |
| 140 | + |
| 141 | + if (auto const brokerID = tx[~sfLoanBrokerID]) |
| 142 | + { |
| 143 | + // Modify an existing LoanBroker |
| 144 | + auto const sleBroker = view.read(keylet::loanbroker(*brokerID)); |
| 145 | + |
| 146 | + assert(0); |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + // Create a new LoanBroker pointing back to the given Vault |
| 151 | + auto const vaultID = tx[sfVaultID]; |
| 152 | + auto const sleVault = view.read(keylet::vault(vaultID)); |
| 153 | + auto const vaultPseudoID = sleVault->at(sfAccount); |
| 154 | + auto const sequence = tx.getSeqValue(); |
| 155 | + |
| 156 | + auto owner = view.peek(keylet::account(account_)); |
| 157 | + auto broker = |
| 158 | + std::make_shared<SLE>(keylet::loanbroker(account_, sequence)); |
| 159 | + |
| 160 | + if (auto const ter = dirLink(view, account_, broker)) |
| 161 | + return ter; |
| 162 | + if (auto const ter = dirLink(view, vaultPseudoID, broker)) |
| 163 | + return ter; |
| 164 | + |
| 165 | + adjustOwnerCount(view, owner, 1, j_); |
| 166 | + auto ownerCount = owner->at(sfOwnerCount); |
| 167 | + if (mPriorBalance < view.fees().accountReserve(ownerCount)) |
| 168 | + return tecINSUFFICIENT_RESERVE; |
| 169 | + |
| 170 | + auto maybePseudo = createPseudoAccount( |
| 171 | + view, broker->key(), PseudoAccountOwnerType::LoanBroker); |
| 172 | + if (!maybePseudo) |
| 173 | + return maybePseudo.error(); |
| 174 | + auto& pseudo = *maybePseudo; |
| 175 | + auto pseudoId = pseudo->at(sfAccount); |
| 176 | + |
| 177 | + if (auto ter = addEmptyHolding( |
| 178 | + view, pseudoId, mPriorBalance, sleVault->at(sfAsset), j_)) |
| 179 | + return ter; |
| 180 | + |
| 181 | + // Initialize data fields: |
| 182 | + broker->at(sfSequence) = sequence; |
| 183 | + broker->at(sfVaultID) = vaultID; |
| 184 | + broker->at(sfOwner) = account_; |
| 185 | + broker->at(sfAccount) = pseudoId; |
| 186 | + if (auto const data = tx[~sfData]) |
| 187 | + broker->at(sfData) = *data; |
| 188 | + if (auto const rate = tx[~sfManagementFeeRate]) |
| 189 | + broker->at(sfManagementFeeRate) = *rate; |
| 190 | + if (auto const debtMax = tx[~sfDebtMaximum]; debtMax && *debtMax) |
| 191 | + broker->at(sfDebtMaximum) = *debtMax; |
| 192 | + if (auto const coverMin = tx[~sfCoverRateMinimum]) |
| 193 | + broker->at(sfCoverRateMinimum) = *coverMin; |
| 194 | + if (auto const coverLiq = tx[~sfCoverRateLiquidation]) |
| 195 | + broker->at(sfCoverRateLiquidation) = *coverLiq; |
| 196 | + |
| 197 | + view.insert(broker); |
| 198 | + } |
| 199 | + |
| 200 | + return temDISABLED; |
| 201 | +} |
| 202 | + |
| 203 | +//------------------------------------------------------------------------------ |
| 204 | + |
| 205 | +} // namespace ripple |
0 commit comments