rippled
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | Static Private Member Functions | List of all members
ripple::AMMWithdraw Class Reference

AMMWithdraw implements AMM withdraw Transactor. More...

Inheritance diagram for ripple::AMMWithdraw:
Inheritance graph
[legend]
Collaboration diagram for ripple::AMMWithdraw:
Collaboration graph
[legend]

Public Types

enum  ConsequencesFactoryType { Normal, Blocker, Custom }
 

Public Member Functions

 AMMWithdraw (ApplyContext &ctx)
 
TER doApply () override
 
std::pair< TER, bool > operator() ()
 Process the transaction. More...
 
ApplyViewview ()
 
ApplyView const & view () const
 

Static Public Member Functions

static NotTEC preflight (PreflightContext const &ctx)
 
static TER preclaim (PreclaimContext const &ctx)
 
static NotTEC checkSeqProxy (ReadView const &view, STTx const &tx, beast::Journal j)
 
static NotTEC checkPriorTxAndLastLedger (PreclaimContext const &ctx)
 
static TER checkFee (PreclaimContext const &ctx, XRPAmount baseFee)
 
static NotTEC checkSign (PreclaimContext const &ctx)
 
static XRPAmount calculateBaseFee (ReadView const &view, STTx const &tx)
 
static TER ticketDelete (ApplyView &view, AccountID const &account, uint256 const &ticketIndex, beast::Journal j)
 

Static Public Attributes

static constexpr ConsequencesFactoryType ConsequencesFactory {Normal}
 

Protected Member Functions

TER apply ()
 
virtual void preCompute ()
 

Static Protected Member Functions

static XRPAmount minimumFee (Application &app, XRPAmount baseFee, Fees const &fees, ApplyFlags flags)
 Compute the minimum fee required to process a transaction with a given baseFee based on the current server load. More...
 

Protected Attributes

ApplyContextctx_
 
const beast::Journal j_
 
const AccountID account_
 
XRPAmount mPriorBalance
 
XRPAmount mSourceBalance
 

Private Member Functions

std::pair< TER, bool > applyGuts (Sandbox &view)
 
std::pair< TER, STAmountwithdraw (Sandbox &view, AccountID const &ammAccount, STAmount const &amountWithdraw, STAmount const &amountBalance, std::optional< STAmount > const &amount2Withdraw, STAmount const &lpTokensAMMBalance, STAmount const &lpTokensWithdraw, std::uint16_t tfee)
 Withdraw requested assets and token from AMM into LP account. More...
 
std::pair< TER, STAmountequalWithdrawTokens (Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount2Balance, STAmount const &lptAMMBalance, STAmount const &lpTokens, STAmount const &lpTokensWithdraw, std::uint16_t tfee)
 Equal-asset withdrawal (LPTokens) of some AMM instance pools shares represented by the number of LPTokens . More...
 
std::pair< TER, STAmountequalWithdrawLimit (Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &amount2Balance, STAmount const &lptAMMBalance, STAmount const &amount, STAmount const &amount2, std::uint16_t tfee)
 Withdraw both assets (Asset1Out, Asset2Out) with the constraints on the maximum amount of each asset that the trader is willing to withdraw. More...
 
std::pair< TER, STAmountsingleWithdraw (Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &lptAMMBalance, STAmount const &amount, std::uint16_t tfee)
 Single asset withdrawal (Asset1Out) equivalent to the amount specified in Asset1Out. More...
 
std::pair< TER, STAmountsingleWithdrawTokens (Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &lptAMMBalance, STAmount const &amount, STAmount const &lpTokensWithdraw, std::uint16_t tfee)
 Single asset withdrawal (Asset1Out, LPTokens) proportional to the share specified by tokens. More...
 
std::pair< TER, STAmountsingleWithdrawEPrice (Sandbox &view, AccountID const &ammAccount, STAmount const &amountBalance, STAmount const &lptAMMBalance, STAmount const &amount, STAmount const &ePrice, std::uint16_t tfee)
 Withdraw single asset (Asset1Out, EPrice) with two constraints. More...
 
std::pair< TER, XRPAmountreset (XRPAmount fee)
 Reset the context, discarding any changes made and adjust the fee. More...
 
TER consumeSeqProxy (SLE::pointer const &sleAccount)
 
TER payFee ()
 

Static Private Member Functions

static NotTEC checkSingleSign (PreclaimContext const &ctx)
 
static NotTEC checkMultiSign (PreclaimContext const &ctx)
 

Detailed Description

AMMWithdraw implements AMM withdraw Transactor.

The withdraw transaction is used to remove liquidity from the AMM instance pool, thus redeeming some share of the pools that one owns in the form of LPTokens. If the trader withdraws proportional values of both assets without changing their relative pricing, no trading fee is charged on the transaction. The trader can specify different combination of the fields in the withdrawal. LPTokens - transaction assumes proportional withdrawal of pool assets for the amount of LPTokens. Asset1Out - transaction assumes withdrawal of single asset equivalent to the amount specified in Asset1Out. Asset1Out and Asset2Out - transaction assumes all assets withdrawal with the constraints on the maximum amount of each asset that the trader is willing to withdraw. Asset1Out and LPTokens - transaction assumes withdrawal of single asset specified in Asset1Out proportional to the share represented by the amount of LPTokens. Asset1Out and EPrice - transaction assumes withdrawal of single asset with the following constraints: a. Amount of asset1 if specified (not 0) in Asset1Out specifies the minimum amount of asset1 that the trader is willing to withdraw. b. The effective price of asset traded out does not exceed the amount specified in EPrice. Following updates after a successful transaction: The withdrawn asset, if XRP, is transferred from AMM instance account to the account that initiated the transaction, thus changing the Balance field of each account. The withdrawn asset, if token, is balanced between the AMM instance account and the issuer account. The LPTokens ~ are balanced between the AMM instance account and the account that initiated the transaction. The pool composition is updated.

See also
XLS30d:AMMWithdraw transaction

Definition at line 65 of file AMMWithdraw.h.

Member Enumeration Documentation

◆ ConsequencesFactoryType

Enumerator
Normal 
Blocker 
Custom 

Definition at line 101 of file Transactor.h.

Constructor & Destructor Documentation

◆ AMMWithdraw()

ripple::AMMWithdraw::AMMWithdraw ( ApplyContext ctx)
explicit

Definition at line 70 of file AMMWithdraw.h.

Member Function Documentation

◆ preflight()

NotTEC ripple::AMMWithdraw::preflight ( PreflightContext const &  ctx)
static

Definition at line 36 of file AMMWithdraw.cpp.

◆ preclaim()

TER ripple::AMMWithdraw::preclaim ( PreclaimContext const &  ctx)
static

Definition at line 172 of file AMMWithdraw.cpp.

◆ doApply()

TER ripple::AMMWithdraw::doApply ( )
overridevirtual

Implements ripple::Transactor.

Definition at line 407 of file AMMWithdraw.cpp.

◆ applyGuts()

std::pair< TER, bool > ripple::AMMWithdraw::applyGuts ( Sandbox view)
private

Definition at line 296 of file AMMWithdraw.cpp.

◆ withdraw()

std::pair< TER, STAmount > ripple::AMMWithdraw::withdraw ( Sandbox view,
AccountID const &  ammAccount,
STAmount const &  amountWithdraw,
STAmount const &  amountBalance,
std::optional< STAmount > const &  amount2Withdraw,
STAmount const &  lpTokensAMMBalance,
STAmount const &  lpTokensWithdraw,
std::uint16_t  tfee 
)
private

Withdraw requested assets and token from AMM into LP account.

Return new total LPToken balance.

Parameters
view
ammAccount
amountBalance
amountWithdraw
amount2Withdraw
lpTokensAMMBalancecurrent AMM LPT balance
lpTokensWithdraw
tfee
Returns

Definition at line 421 of file AMMWithdraw.cpp.

◆ equalWithdrawTokens()

std::pair< TER, STAmount > ripple::AMMWithdraw::equalWithdrawTokens ( Sandbox view,
AccountID const &  ammAccount,
STAmount const &  amountBalance,
STAmount const &  amount2Balance,
STAmount const &  lptAMMBalance,
STAmount const &  lpTokens,
STAmount const &  lpTokensWithdraw,
std::uint16_t  tfee 
)
private

Equal-asset withdrawal (LPTokens) of some AMM instance pools shares represented by the number of LPTokens .

Proportional withdrawal of pool assets for the amount of LPTokens.

The trading fee is not charged.

Parameters
view
ammAccount
amountBalancecurrent LP asset1 balance
amount2Balancecurrent LP asset2 balance
lptAMMBalancecurrent AMM LPT balance
lpTokenscurrent LPT balance
lpTokensWithdrawamount of tokens to withdraw
tfeetrading fee in basis points
Returns

Definition at line 569 of file AMMWithdraw.cpp.

◆ equalWithdrawLimit()

std::pair< TER, STAmount > ripple::AMMWithdraw::equalWithdrawLimit ( Sandbox view,
AccountID const &  ammAccount,
STAmount const &  amountBalance,
STAmount const &  amount2Balance,
STAmount const &  lptAMMBalance,
STAmount const &  amount,
STAmount const &  amount2,
std::uint16_t  tfee 
)
private

Withdraw both assets (Asset1Out, Asset2Out) with the constraints on the maximum amount of each asset that the trader is willing to withdraw.

All assets withdrawal with the constraints on the maximum amount of each asset that the trader is willing to withdraw.

The trading fee is not charged.

Parameters
view
ammAccount
amountBalancecurrent AMM asset1 balance
amount2Balancecurrent AMM asset2 balance
lptAMMBalancecurrent AMM LPT balance
amountasset1 withdraw amount
amount2max asset2 withdraw amount
tfeetrading fee in basis points
Returns

a = (t/T) * A (5) b = (t/T) * B (6) where A,B: current pool composition T: current balance of outstanding LPTokens a: balance of asset A being withdrawn b: balance of asset B being withdrawn t: balance of LPTokens issued to LP after a successful transaction Use equation 5 to compute t, given the amount in Asset1Out. Let this be Z Use equation 6 to compute the amount of asset2, given Z. Let the computed amount of asset2 be X If X <= amount in Asset2Out: The amount of asset1 to be withdrawn is the one specified in Asset1Out The amount of asset2 to be withdrawn is X The amount of LPTokens redeemed is Z If X> amount in Asset2Out: Use equation 5 to compute t, given the amount in Asset2Out. Let this be Q Use equation 6 to compute the amount of asset1, given Q. Let the computed amount of asset1 be W The amount of asset2 to be withdrawn is the one specified in Asset2Out The amount of asset1 to be withdrawn is W The amount of LPTokens redeemed is Q

Definition at line 649 of file AMMWithdraw.cpp.

◆ singleWithdraw()

std::pair< TER, STAmount > ripple::AMMWithdraw::singleWithdraw ( Sandbox view,
AccountID const &  ammAccount,
STAmount const &  amountBalance,
STAmount const &  lptAMMBalance,
STAmount const &  amount,
std::uint16_t  tfee 
)
private

Single asset withdrawal (Asset1Out) equivalent to the amount specified in Asset1Out.

Withdraw single asset equivalent to the amount specified in Asset1Out.

The trading fee is charged.

Parameters
view
ammAccount
amountBalancecurrent AMM asset1 balance
lptAMMBalancecurrent AMM LPT balance
amountasset1 withdraw amount
tfeetrading fee in basis points
Returns

t = T * (c - sqrt(c**2 - 4*R))/2 where R = b/B, c = R*fee + 2 - fee Use equation 7 to compute the t, given the amount in Asset1Out.

Definition at line 691 of file AMMWithdraw.cpp.

◆ singleWithdrawTokens()

std::pair< TER, STAmount > ripple::AMMWithdraw::singleWithdrawTokens ( Sandbox view,
AccountID const &  ammAccount,
STAmount const &  amountBalance,
STAmount const &  lptAMMBalance,
STAmount const &  amount,
STAmount const &  lpTokensWithdraw,
std::uint16_t  tfee 
)
private

Single asset withdrawal (Asset1Out, LPTokens) proportional to the share specified by tokens.

withdrawal of single asset specified in Asset1Out proportional to the share represented by the amount of LPTokens.

The trading fee is charged.

Parameters
view
ammAccount
amountBalancecurrent AMM asset1 balance
lptAMMBalancecurrent AMM LPT balance
amountasset1 withdraw amount
lpTokensWithdrawamount of tokens to withdraw
tfeetrading fee in basis points
Returns

Use equation 8 to compute the amount of asset1, given the redeemed t represented by LPTokens. Let this be Y. If (amount exists for Asset1Out & Y >= amount in Asset1Out) || (amount field does not exist for Asset1Out): The amount of asset out is Y The amount of LPTokens redeemed is LPTokens Equation 8 solves equation 7

See also
singleWithdraw for b.

Definition at line 724 of file AMMWithdraw.cpp.

◆ singleWithdrawEPrice()

std::pair< TER, STAmount > ripple::AMMWithdraw::singleWithdrawEPrice ( Sandbox view,
AccountID const &  ammAccount,
STAmount const &  amountBalance,
STAmount const &  lptAMMBalance,
STAmount const &  amount,
STAmount const &  ePrice,
std::uint16_t  tfee 
)
private

Withdraw single asset (Asset1Out, EPrice) with two constraints.

Withdraw single asset with two constraints.

The trading fee is charged.

Parameters
view
ammAccount
amountBalancecurrent AMM asset1 balance
lptAMMBalancecurrent AMM LPT balance
amountasset1 withdraw amount
ePricemaximum asset1 effective price
tfeetrading fee in basis points
Returns

a. amount of asset1 if specified (not 0) in Asset1Out specifies the minimum amount of asset1 that the trader is willing to withdraw. b. The effective price of asset traded out does not exceed the amount specified in EPrice The effective price (EP) of a trade is defined as the ratio of the tokens the trader sold or swapped in (Token B) and the token they got in return or swapped out (Token A). EP(B/A) = b/a (III) b = B * (t1**2 + t1*(f - 2))/(t1*f - 1) (8) where t1 = t/T Use equations 8 & III and amount in EPrice to compute the two variables: asset in as LPTokens. Let this be X asset out as that in Asset1Out. Let this be Y If (amount exists for Asset1Out & Y >= amount in Asset1Out) || (amount field does not exist for Asset1Out): The amount of assetOut is given by Y The amount of LPTokens is given by X

Definition at line 768 of file AMMWithdraw.cpp.

◆ operator()()

std::pair< TER, bool > ripple::Transactor::operator() ( )
inherited

Process the transaction.

Definition at line 827 of file Transactor.cpp.

◆ view() [1/2]

ApplyView& ripple::Transactor::view ( )
inherited

Definition at line 107 of file Transactor.h.

◆ view() [2/2]

ApplyView const& ripple::Transactor::view ( ) const
inherited

Definition at line 113 of file Transactor.h.

◆ checkSeqProxy()

NotTEC ripple::Transactor::checkSeqProxy ( ReadView const &  view,
STTx const &  tx,
beast::Journal  j 
)
staticinherited

Definition at line 263 of file Transactor.cpp.

◆ checkPriorTxAndLastLedger()

NotTEC ripple::Transactor::checkPriorTxAndLastLedger ( PreclaimContext const &  ctx)
staticinherited

Definition at line 335 of file Transactor.cpp.

◆ checkFee()

TER ripple::Transactor::checkFee ( PreclaimContext const &  ctx,
XRPAmount  baseFee 
)
staticinherited

Definition at line 190 of file Transactor.cpp.

◆ checkSign()

NotTEC ripple::Transactor::checkSign ( PreclaimContext const &  ctx)
staticinherited

Definition at line 478 of file Transactor.cpp.

◆ calculateBaseFee()

XRPAmount ripple::Transactor::calculateBaseFee ( ReadView const &  view,
STTx const &  tx 
)
staticinherited

Definition at line 162 of file Transactor.cpp.

◆ ticketDelete()

TER ripple::Transactor::ticketDelete ( ApplyView view,
AccountID const &  account,
uint256 const &  ticketIndex,
beast::Journal  j 
)
staticinherited

Definition at line 383 of file Transactor.cpp.

◆ apply()

TER ripple::Transactor::apply ( )
protectedinherited

Definition at line 443 of file Transactor.cpp.

◆ preCompute()

void ripple::Transactor::preCompute ( )
protectedvirtualinherited

Reimplemented in ripple::SetSignerList, ripple::CreateOffer, and ripple::Change.

Definition at line 437 of file Transactor.cpp.

◆ minimumFee()

XRPAmount ripple::Transactor::minimumFee ( Application app,
XRPAmount  baseFee,
Fees const &  fees,
ApplyFlags  flags 
)
staticprotectedinherited

Compute the minimum fee required to process a transaction with a given baseFee based on the current server load.

Parameters
appThe application hosting the server
baseFeeThe base fee of a candidate transaction
See also
ripple::calculateBaseFee
Parameters
feesFee settings from the current ledger
flagsTransaction processing fees

Definition at line 180 of file Transactor.cpp.

◆ reset()

std::pair< TER, XRPAmount > ripple::Transactor::reset ( XRPAmount  fee)
privateinherited

Reset the context, discarding any changes made and adjust the fee.

Definition at line 788 of file Transactor.cpp.

◆ consumeSeqProxy()

TER ripple::Transactor::consumeSeqProxy ( SLE::pointer const &  sleAccount)
privateinherited

Definition at line 365 of file Transactor.cpp.

◆ payFee()

TER ripple::Transactor::payFee ( )
privateinherited

Definition at line 243 of file Transactor.cpp.

◆ checkSingleSign()

NotTEC ripple::Transactor::checkSingleSign ( PreclaimContext const &  ctx)
staticprivateinherited

Definition at line 488 of file Transactor.cpp.

◆ checkMultiSign()

NotTEC ripple::Transactor::checkMultiSign ( PreclaimContext const &  ctx)
staticprivateinherited

Definition at line 562 of file Transactor.cpp.

Member Data Documentation

◆ ConsequencesFactory

constexpr ConsequencesFactoryType ripple::AMMWithdraw::ConsequencesFactory {Normal}
staticconstexpr

Definition at line 68 of file AMMWithdraw.h.

◆ ctx_

ApplyContext& ripple::Transactor::ctx_
protectedinherited

Definition at line 88 of file Transactor.h.

◆ j_

const beast::Journal ripple::Transactor::j_
protectedinherited

Definition at line 89 of file Transactor.h.

◆ account_

const AccountID ripple::Transactor::account_
protectedinherited

Definition at line 91 of file Transactor.h.

◆ mPriorBalance

XRPAmount ripple::Transactor::mPriorBalance
protectedinherited

Definition at line 92 of file Transactor.h.

◆ mSourceBalance

XRPAmount ripple::Transactor::mSourceBalance
protectedinherited

Definition at line 93 of file Transactor.h.