4.1 KiB
seo, labels
| seo | labels | |||||
|---|---|---|---|---|---|---|
|
|
Add Assets to an AMM
This example shows how to:
- Purchase
FOOtokens from an AMM. - Deposit
XRPandFOOto the AMM and receive LP tokens. - Vote on AMM trading fees.
- Redeem LP Tokens for assets.
Usage
Download the AMM Samples to use the AMM test harness in your browser and follow along in this tutorial.
Set up an AMM
- Click Get New Devnet Account.
- Click Issue FOO Tokens.
- Click Create AMM. You can view the AMM pool info in the
AMM Infobox.
Buy FOO tokens
Buy FOO tokens from the established AMM in order to interact with the pool.
- Click Get Second Devnet Account.
- Click Buy FOO Tokens. To simplify this tutorial, this purchases 250
FOO. The AMM starts with a ratio of 10:1FOO:XRP. This doesn't translate to a direct 25XRPspend, since the AMM will adjust the cost ofFOOto be more expensive as you buy more from it.
Add Assets to the AMM
Add a value in the XRP and FOO fields, then click Add Assets. You can deposit either one or both assets, but depositing only one asset will reduce the amount of LP tokens you receive.
Vote on trading fees
Add a value in the Fee field, then click Add Vote. You can view the updated AMM info after doing so and see your submission under trading fees.
The proposed fee is in units of 1/100,000; a value of 1 is equivalent to 0.001%. The maximum value is 1000, indicating a 1% fee.
Redeem your LP tokens
- Click Calculate LP Value.
- Click Withdraw LP. This tutorial redeems all your LP tokens to withdraw both assets, but AMM withdrawals can be either both or one, depending on the fields specified in the transaction.
Code Walkthrough
Buy FOO tokens
Purchases 250 FOO tokens by creating an offer on the XRPL DEX. Since the AMM created in the test harness is the only available source of FOO, it takes the offer.
{% code-snippet file="/_code-samples/amm/js/2.add-assets-to-amm.js" language="js" from="// Buy FOO tokens from the XRP/FOO AMM." before="// Deposit assets to AMM." /%}
Deposit assets
This code checks if you're trying to add one or both assets, and then modifies the AMMDeposit transaction to be either a single or double-asset deposit.
The function to update LP balance checks the AMM to get the unique AMM account, which acts as its own issuer of LP tokens. It then checks your wallet balance and gets the LP token value by matching it with the AMM issuer.
{% code-snippet file="/_code-samples/amm/js/2.add-assets-to-amm.js" language="js" from="// Deposit assets to AMM." before="// Vote on fees." /%}
Vote on trading fees
Trading fees are applied to any transaction that interacts with the AMM. The act of voting is straightforward and only requires you to hold the AMM LP tokens before submitting a vote.
{% code-snippet file="/_code-samples/amm/js/2.add-assets-to-amm.js" language="js" from="// Vote on fees." before="// Calculate the value of LP tokens." /%}
Calculate value of LP tokens
There isn't a dedicated method to calculate how much you can redeem your LP tokens for, but the math isn't too complicated. The percentage of the total LP tokens you own qualifies you for the same percentage of the total assets in the AMM.
{% code-snippet file="/_code-samples/amm/js/2.add-assets-to-amm.js" language="js" from="// Calculate the value of LP tokens." before="// Withdraw by redeeming LP tokens." /%}
Redeem your LP tokens
Redeeming your LP tokens requires you to get the LP token issuer and currency code, both of which you can check using the amm_info method. This code redeems assets using all your LP tokens, but you can also specify withdrawals by the amount of an asset you want to take out.
{% code-snippet file="/_code-samples/amm/js/2.add-assets-to-amm.js" language="js" from="// Withdraw by redeeming LP tokens." /%}
