mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-04 11:55:50 +00:00
Incomplete AMM calculations sample code
This commit is contained in:
@@ -3,14 +3,24 @@ const BigNumber = require('bignumber.js')
|
||||
|
||||
/*
|
||||
* Convert a trading fee to a value that can be multiplied
|
||||
* or divided for fee calculations.
|
||||
* by a total to "subtract" the fee from the total.
|
||||
* @param tFee int {0, 1000}
|
||||
* 1 = 1/100,000 or 1000 = 1% fee
|
||||
* 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) )
|
||||
}
|
||||
/*
|
||||
* 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(1).minus( (BigNumber(tFee).dividedBy(AUCTION_SLOT_FEE_SCALE_FACTOR)) )
|
||||
return BigNumber(tFee).dividedBy(AUCTION_SLOT_FEE_SCALE_FACTOR)
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -42,19 +52,22 @@ function swapOut(asset_out_bn, pool_in_bn, pool_out_bn, trading_fee) {
|
||||
/*
|
||||
* Calculates the necessary bid to win the AMM Auction slot, per the pricing
|
||||
* algorithm defined in XLS-30 section 4.1.1.
|
||||
* @returns BigNumber - the minimum amount of LP tokens to win the auction slot
|
||||
*/
|
||||
function auctionPrice(old_bid, time_interval) {
|
||||
const min_bid = 123456 // @@TODO: figure out real min bid
|
||||
function auctionPrice(old_bid, time_interval, trading_fee, lpt_balance) {
|
||||
const tfee_decimal = feeDecimal(trading_fee)
|
||||
const min_bid = BigNumber(lpt_balance).multipliedBy(tfee_decimal).dividedBy(25)
|
||||
const b = BigNumber(old_bid)
|
||||
if (time_interval >= 20) {
|
||||
return min_bid
|
||||
}
|
||||
|
||||
if (time_interval <= 1) {
|
||||
return (old_bid * 1.05) + min_bid
|
||||
return b.multipliedBy(BigNumber("1.05")).plus(min_bid)
|
||||
}
|
||||
|
||||
const t60 = BigNumber(0.05).multipliedBy(time_interval).exponentiatedBy(60)
|
||||
return B * 1.05 * (1 - t60) + min_bid
|
||||
const t60 = BigNumber("0.05").multipliedBy(time_interval).exponentiatedBy(60)
|
||||
return b.multipliedBy("1.05").multipliedBy(BigNumber(1).minus(t60)).plus(min_bid)
|
||||
}
|
||||
|
||||
async function main() {
|
||||
@@ -80,7 +93,7 @@ async function main() {
|
||||
const amm_info = (await client.request({"command": "amm_info", "asset": from_asset, "asset2": to_asset}))
|
||||
console.dir(amm_info, {depth: null})
|
||||
const amm_account = amm_info.result.amm.account
|
||||
const lpt_code = amm_info.result.amm.lp_token.currency
|
||||
const lpt = amm_info.result.amm.lp_token
|
||||
const pool_drops = amm_info.result.amm.amount // XRP is always first if the pool is token←→XRP
|
||||
const pool_tst = amm_info.result.amm.amount2
|
||||
const full_trading_fee = amm_info.result.amm.trading_fee
|
||||
@@ -119,8 +132,10 @@ async function main() {
|
||||
const potential_savings = from_amount - discounted_from_amount
|
||||
console.log(`Potential savings: ${xrpl.dropsToXrp(potential_savings)} XRP`)
|
||||
|
||||
// Calculate the cost of winning the auction slot, in LP Tokens.
|
||||
const auction_price = auctionPrice(old_bid, time_interval)
|
||||
// Calculate the cost of winning the auction slot, then convert it to XRP
|
||||
const auction_price = auctionPrice(old_bid, time_interval, full_trading_fee, lpt.value)
|
||||
console.log(`Auction price: ${auction_price} LP Tokens`)
|
||||
// @@TODO: figure out how to convert auction_price from LPT to input asset.
|
||||
|
||||
|
||||
// Done.
|
||||
|
||||
Reference in New Issue
Block a user