mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-27 15:15:50 +00:00
auction slot tutorial
This commit is contained in:
331
_code-samples/quickstart/js/13.trade-with-auction-slot.html
Normal file
331
_code-samples/quickstart/js/13.trade-with-auction-slot.html
Normal file
@@ -0,0 +1,331 @@
|
||||
<html>
|
||||
<head>
|
||||
<title>Add to AMM Test Harness</title>
|
||||
<link href='https://fonts.googleapis.com/css?family=Work Sans' rel='stylesheet'>
|
||||
<style>
|
||||
body{font-family: "Work Sans", sans-serif;padding: 20px;background: #fafafa;}
|
||||
h1{font-weight: bold;}
|
||||
input, button {padding: 6px;margin-bottom: 8px;}
|
||||
button{font-weight: bold;font-family: "Work Sans", sans-serif;}
|
||||
td{vertical-align: middle;}
|
||||
</style>
|
||||
<script src='https://unpkg.com/xrpl@4.0.0/build/xrpl-latest-min.js'></script>
|
||||
<script src='https://unpkg.com/bignumber.js@9.1.2/bignumber.js'></script>
|
||||
<script src='ripplex1-send-xrp.js'></script>
|
||||
<script src='ripplex2-send-currency.js'></script>
|
||||
<script src='ripplex11-create-amm.js'></script>
|
||||
<script src='ripplex12-add-to-amm.js'></script>
|
||||
<script src='ripplex13-trade-with-auction-slot.js'></script>
|
||||
<script>
|
||||
if (typeof module !== "undefined") {
|
||||
const xrpl = require('xrpl')
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<!-- ************************************************************** -->
|
||||
<!-- ********************** The Form ****************************** -->
|
||||
<!-- ************************************************************** -->
|
||||
|
||||
<body>
|
||||
<h1>Add to AMM Test Harness</h1>
|
||||
<form id="theForm">
|
||||
Choose your ledger instance:
|
||||
|
||||
<input type="radio" id="tn" name="server"
|
||||
value="wss://s.altnet.rippletest.net:51233" checked>
|
||||
<label for="testnet">Testnet</label>
|
||||
|
||||
<input type="radio" id="dn" name="server"
|
||||
value="wss://s.devnet.rippletest.net:51233">
|
||||
<label for="devnet">Devnet</label>
|
||||
<br/><br/>
|
||||
<button type="button" onClick="getAccountsFromSeeds()">Get Accounts From Seeds</button>
|
||||
<br/>
|
||||
<textarea id="seeds" cols="40" rows= "2"></textarea>
|
||||
<br/><br/>
|
||||
<table>
|
||||
<tr valign="top">
|
||||
<td>
|
||||
<table style="padding-bottom: 400px;">
|
||||
<tr valign="top">
|
||||
<td>
|
||||
<td>
|
||||
<button type="button" onClick="getAccount('standby')">Get New Standby Account</button>
|
||||
<table>
|
||||
<tr valign="top">
|
||||
<td align="right">
|
||||
Standby Account
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbyAccountField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Seed
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbySeedField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
XRP Balance
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbyBalanceField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Amount
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbyAmountField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Destination
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbyDestinationField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr valign="top">
|
||||
<td><button type="button" onClick="configureAccount('standby',document.querySelector('#standbyDefault').checked)">Configure Account</button></td>
|
||||
<td>
|
||||
<input type="checkbox" id="standbyDefault" checked="true"/>
|
||||
<label for="standbyDefault">Allow Rippling</label>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Currency
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbyCurrencyField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p align="left">
|
||||
<textarea id="standbyResultField" cols="60" rows="20" style="resize: none;"></textarea>
|
||||
</p>
|
||||
<table>
|
||||
<tr valign="top">
|
||||
<td align="right">
|
||||
Asset 1 Currency
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="asset1CurrencyField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Asset 1 Issuer
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="asset1IssuerField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Asset 1 Amount
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="asset1AmountField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Asset 2 Currency
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="asset2CurrencyField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Asset 2 Issuer
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="asset2IssuerField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Asset 2 Amount
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="asset2AmountField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Trading Fee
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbyFeeField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
LP Tokens
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="standbyLPField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</td>
|
||||
<td>
|
||||
<table>
|
||||
<tr valign="top">
|
||||
<td align="center" valign="top" style="padding-top: 240px;">
|
||||
<br>
|
||||
<button type="button" onClick="sendXRP()">Send XRP></button>
|
||||
<br/><br/>
|
||||
<button type="button" onClick="createTrustline()">Create TrustLine</button>
|
||||
<br/>
|
||||
<button type="button" onClick="sendCurrency()">Send Currency</button>
|
||||
<br/>
|
||||
<button type="button" onClick="getBalances()" style="margin-bottom: 160px;">Get Balances</button>
|
||||
<br/>
|
||||
<button type="button" onClick="checkAMM()">Check AMM</button>
|
||||
<br/>
|
||||
<button type="button" onClick="createAMM()">Create AMM</button>
|
||||
<br/>
|
||||
<button type="button" onClick="addAssets()">Add to AMM</button>
|
||||
<br/>
|
||||
<button type="button" onClick="voteFees()">Vote on Fee</button>
|
||||
<br/>
|
||||
<button type="button" onClick="calculateLP()">Get LP Value</button>
|
||||
<br/>
|
||||
<button type="button" onClick="redeemLP()">Redeem LP</button>
|
||||
</td>
|
||||
</tr>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
<td>
|
||||
<table style="padding-bottom: 430px;">
|
||||
<tr>
|
||||
<td>
|
||||
<td>
|
||||
<table>
|
||||
<tr>
|
||||
<td align="center" valign="top" style="padding-bottom: 100px;">
|
||||
<button type="button" onClick="oPsendXRP()">< Send XRP</button>
|
||||
<br/><br/>
|
||||
<button type="button" onClick="oPcreateTrustline()">Create TrustLine</button>
|
||||
<br/>
|
||||
<button type="button" onClick="oPsendCurrency()">Send Currency</button>
|
||||
<br/>
|
||||
<button type="button" onClick="getBalances()">Get Balances</button>
|
||||
</td>
|
||||
<td valign="top" align="right">
|
||||
<button type="button" onClick="getAccount('operational')">Get New Operational Account</button>
|
||||
<table>
|
||||
<tr valign="top">
|
||||
<td align="right">
|
||||
Operational Account
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="operationalAccountField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Seed
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="operationalSeedField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
XRP Balance
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="operationalBalanceField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Amount
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="operationalAmountField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Destination
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="operationalDestinationField" size="40"></input>
|
||||
<br>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>
|
||||
</td>
|
||||
<td align="right">
|
||||
<input type="checkbox" id="operationalDefault" checked="true"/>
|
||||
<label for="operationalDefault">Allow Rippling</label>
|
||||
<button type="button" onClick="configureAccount('operational',document.querySelector('#operationalDefault').checked)">Configure Account</button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td align="right">
|
||||
Currency
|
||||
</td>
|
||||
<td>
|
||||
<input type="text" id="operationalCurrencyField" size="40"></input>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p align="right">
|
||||
<textarea id="operationalResultField" cols="60" rows="20" style="resize: none;"></textarea>
|
||||
<br><br>
|
||||
<textarea id="ammInfoField" cols="60" rows="20" style="resize: none;"></textarea>
|
||||
</p>
|
||||
</td>
|
||||
</td>
|
||||
</tr>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</form>
|
||||
</body>
|
||||
</html>
|
||||
159
_code-samples/quickstart/js/ripplex13-trade-with-auction-slot.js
Normal file
159
_code-samples/quickstart/js/ripplex13-trade-with-auction-slot.js
Normal file
@@ -0,0 +1,159 @@
|
||||
// AMM Formulas
|
||||
|
||||
/* Convert a trading fee to a value that can be multiplied
|
||||
* by a total to "subtract" the fee from the total.
|
||||
* @param tFee int {0, 1000}
|
||||
* such that 1 = 1/100,000 and 1000 = 1% fee
|
||||
* @returns BigNumber (1 - fee) as a decimal
|
||||
*/
|
||||
function feeMult(tFee) {
|
||||
return BigNumber(1).minus( feeDecimal(tFee) )
|
||||
}
|
||||
|
||||
/* Same as feeMult, but with half the trading fee. Single-asset deposits and
|
||||
* withdrawals use this because half of the deposit is treated as being
|
||||
* "swapped" for the other asset in the AMM's pool.
|
||||
* @param tFee int {0, 1000}
|
||||
* such that 1 = 1/100,000 and 1000 = 1% fee
|
||||
* @returns BigNumber (1 - (fee/2)) as a decimal
|
||||
*/
|
||||
function feeMultHalf(tFee) {
|
||||
return BigNumber(1).minus( feeDecimal(tFee).dividedBy(2) )
|
||||
}
|
||||
|
||||
/* Convert a trading fee to a decimal BigNumber value,
|
||||
* for example 1000 becomes 0.01
|
||||
* @param tFee int {0, 1000}
|
||||
* such that 1 = 1/100,000 and 1000 = 1% fee
|
||||
* @returns BigNumber(fee) as a decimal
|
||||
*/
|
||||
function feeDecimal(tFee) {
|
||||
const AUCTION_SLOT_FEE_SCALE_FACTOR = 100000
|
||||
return BigNumber(tFee).dividedBy(AUCTION_SLOT_FEE_SCALE_FACTOR)
|
||||
}
|
||||
|
||||
/* Implement the AMM SwapOut formula, as defined in XLS-30 section 2.4 AMM
|
||||
* Swap, formula 10. The asset weights WA/WB are currently always 1/1 so
|
||||
* they're canceled out.
|
||||
* C++ source: https://github.com/XRPLF/rippled/blob/2d1854f354ff8bb2b5671fd51252c5acd837c433/src/ripple/app/misc/AMMHelpers.h#L253-L258
|
||||
* @param asset_out_bn BigNumber - The target amount to receive from the AMM.
|
||||
* @param pool_in_bn BigNumber - The amount of the input asset in the AMM's
|
||||
* pool before the swap.
|
||||
* @param pool_out_bn BigNumber - The amount of the output asset in the AMM's
|
||||
* pool before the swap.
|
||||
* @param trading_fee int - The trading fee as an integer {0, 1000} where 1000
|
||||
* represents a 1% fee.
|
||||
* @returns BigNumber - The amount of the input asset that must be swapped in
|
||||
* to receive the target output amount. Unrounded, because
|
||||
* the number of decimals depends on if this is drops of
|
||||
* XRP or a decimal amount of a token; since this is a
|
||||
* theoretical input to the pool, it should be rounded
|
||||
* up (ceiling) to preserve the pool's constant product.
|
||||
*/
|
||||
function swapOut(asset_out_bn, pool_in_bn, pool_out_bn, trading_fee) {
|
||||
return ( ( pool_in_bn.multipliedBy(pool_out_bn) ).dividedBy(
|
||||
pool_out_bn.minus(asset_out_bn)
|
||||
).minus(pool_in_bn)
|
||||
).dividedBy(feeMult(trading_fee))
|
||||
}
|
||||
|
||||
/* Compute the quadratic formula. Helper function for ammAssetIn.
|
||||
* Params and return value are BigNumber instances.
|
||||
*/
|
||||
function solveQuadraticEq(a,b,c) {
|
||||
const b2minus4ac = b.multipliedBy(b).minus(
|
||||
a.multipliedBy(c).multipliedBy(4)
|
||||
)
|
||||
return ( b.negated().plus(b2minus4ac.sqrt()) ).dividedBy(a.multipliedBy(2))
|
||||
}
|
||||
|
||||
/* Implement the AMM single-asset deposit formula to calculate how much to
|
||||
* put in so that you receive a specific number of LP Tokens back.
|
||||
* C++ source: https://github.com/XRPLF/rippled/blob/2d1854f354ff8bb2b5671fd51252c5acd837c433/src/ripple/app/misc/impl/AMMHelpers.cpp#L55-L83
|
||||
* @param pool_in string - Quantity of input asset the pool already has
|
||||
* @param lpt_balance string - Quantity of LP Tokens already issued by the AMM
|
||||
* @param desired_lpt string - Quantity of new LP Tokens you want to receive
|
||||
* @param trading_fee int - The trading fee as an integer {0,1000} where 1000
|
||||
* represents a 1% fee.
|
||||
*/
|
||||
function ammAssetIn(pool_in, lpt_balance, desired_lpt, trading_fee) {
|
||||
// convert inputs to BigNumber
|
||||
const lpTokens = BigNumber(desired_lpt)
|
||||
const lptAMMBalance = BigNumber(lpt_balance)
|
||||
const asset1Balance = BigNumber(pool_in)
|
||||
|
||||
const f1 = feeMult(trading_fee)
|
||||
const f2 = feeMultHalf(trading_fee).dividedBy(f1)
|
||||
const t1 = lpTokens.dividedBy(lptAMMBalance)
|
||||
const t2 = t1.plus(1)
|
||||
const d = f2.minus( t1.dividedBy(t2) )
|
||||
const a = BigNumber(1).dividedBy( t2.multipliedBy(t2))
|
||||
const b = BigNumber(2).multipliedBy(d).dividedBy(t2).minus(
|
||||
BigNumber(1).dividedBy(f1)
|
||||
)
|
||||
const c = d.multipliedBy(d).minus( f2.multipliedBy(f2) )
|
||||
return asset1Balance.multipliedBy(solveQuadraticEq(a,b,c))
|
||||
}
|
||||
|
||||
/* Calculate how much to deposit, in terms of LP Tokens out, to be able to win
|
||||
* the auction slot. This is based on the slot pricing algorithm defined in
|
||||
* XLS-30 section 4.1.1, but factors in the increase in the minimum bid as a
|
||||
* result of having new LP Tokens issued to you from your deposit.
|
||||
*/
|
||||
function auctionDeposit(old_bid, time_interval, trading_fee, lpt_balance) {
|
||||
const tfee_decimal = feeDecimal(trading_fee)
|
||||
const lptokens = BigNumber(lpt_balance)
|
||||
const b = BigNumber(old_bid)
|
||||
let outbidAmount = BigNumber(0) // This is the case if time_interval >= 20
|
||||
if (time_interval == 0) {
|
||||
outbidAmount = b.multipliedBy("1.05")
|
||||
} else if (time_interval <= 19) {
|
||||
const t60 = BigNumber(time_interval).multipliedBy("0.05").exponentiatedBy(60)
|
||||
outbidAmount = b.multipliedBy("1.05").multipliedBy(BigNumber(1).minus(t60))
|
||||
}
|
||||
|
||||
const new_bid = lptokens.plus(outbidAmount).dividedBy(
|
||||
BigNumber(25).dividedBy(tfee_decimal).minus(1)
|
||||
).plus(outbidAmount)
|
||||
|
||||
// Significant digits for the deposit are limited by total LPTokens issued
|
||||
// so we calculate lptokens + deposit - lptokens to determine where the
|
||||
// rounding occurs. We use ceiling/floor to make sure the amount we receive
|
||||
// after rounding is still enough to win the auction slot.
|
||||
const rounded_bid = new_bid.plus(lptokens).precision(15, BigNumber.CEILING
|
||||
).minus(lptokens).precision(15, BigNumber.FLOOR)
|
||||
return rounded_bid
|
||||
}
|
||||
|
||||
/* Calculate the necessary bid to win the AMM Auction slot, per the pricing
|
||||
* algorithm defined in XLS-30 section 4.1.1, if you already hold LP Tokens.
|
||||
*
|
||||
* NOT USED in the Auction Slot tutorial, which assumes the user does not hold
|
||||
* any LP Tokens.
|
||||
*
|
||||
* @returns BigNumber - the minimum amount of LP tokens to win the auction slot
|
||||
*/
|
||||
function auctionPrice(old_bid, time_interval, trading_fee, lpt_balance) {
|
||||
const tfee_decimal = feeDecimal(trading_fee)
|
||||
const lptokens = BigNumber(lpt_balance)
|
||||
const min_bid = lptokens.multipliedBy(tfee_decimal).dividedBy(25)
|
||||
const b = BigNumber(old_bid)
|
||||
let new_bid = min_bid
|
||||
|
||||
if (time_interval == 0) {
|
||||
new_bid = b.multipliedBy("1.05").plus(min_bid)
|
||||
} else if (time_interval <= 19) {
|
||||
const t60 = BigNumber(time_interval).multipliedBy("0.05"
|
||||
).exponentiatedBy(60)
|
||||
new_bid = b.multipliedBy("1.05").multipliedBy(
|
||||
BigNumber(1).minus(t60)
|
||||
).plus(min_bid)
|
||||
}
|
||||
|
||||
const rounded_bid = new_bid.plus(lptokens).precision(15, BigNumber.CEILING
|
||||
).minus(lptokens).precision(15, BigNumber.FLOOR)
|
||||
return rounded_bid
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
Reference in New Issue
Block a user