mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-12-06 17:27:57 +00:00
add better error handling and double token amm functionality
This commit is contained in:
@@ -10,10 +10,10 @@ labels:
|
||||
|
||||
This example shows how to:
|
||||
|
||||
1. Deposit `XRP` and issued tokens to an AMM and receive LP tokens.
|
||||
1. Deposit assets to an existing AMM and receive LP tokens.
|
||||
2. Vote on AMM trading fees.
|
||||
3. Check the value of your LP tokens.
|
||||
4. Redeem LP Tokens for assets.
|
||||
4. Redeem LP tokens for assets in the AMM pair.
|
||||
|
||||
[](/docs/img/quickstart-add-to-amm1.png)
|
||||
|
||||
@@ -41,13 +41,31 @@ Without the Quickstart Samples, you will not be able to try the examples that fo
|
||||
[](/docs/img/quickstart-add-to-amm2.png)
|
||||
|
||||
|
||||
### Add Assets to an Existing AMM
|
||||
### Get the AMM
|
||||
|
||||
Choose the amount of assets to add to the AMM. You can deposit either one or both assets, but deposting only one asset reduces the amount of LP tokens you receive. In the standby account fields:
|
||||
1. Enter a [currency code](/docs/references/protocol/data-types/currency-formats.md#currency-codes) in the **Asset 1 Currency** field. For example, `TST`.
|
||||
2. (Optional) If you entered a currency code other than `XRP`, also enter the token issuer in the **Asset 1 Issuer** field.
|
||||
3. Enter a second currency code in the **Asset 2 Currency** field.
|
||||
4. (Optional) If you entered a second currency code other than `XRP`, also enter the token issuer in the **Asset 2 Issuer** field.
|
||||
5. Click **Check AMM**.
|
||||
|
||||
1. Enter how much XRP to add in the **XRP Balance** field.
|
||||
2. Enter how many issued tokens to add in the **Amount** field.
|
||||
3. Enter the token issuer address in the **Destination** field.
|
||||
[](/docs/img/quickstart-add-to-amm3.png)
|
||||
|
||||
|
||||
### (Optional) Acquire More Assets
|
||||
|
||||
If you need more assets, you can:
|
||||
|
||||
- Fund a new wallet with `XRP` by clicking **Get New Standby Account**.
|
||||
- Get more tokens from the token issuer in the AMM pair. See: [Create an AMM](/docs/tutorials/javascript/amm/create-an-amm)
|
||||
|
||||
### Add Assets to the AMM
|
||||
|
||||
Choose the amount of assets to add to the AMM. You can deposit either one or both assets, but deposting only one reduces the amount of LP tokens you receive.
|
||||
|
||||
1. Click **Get Balances** to verify how many tokens you have.
|
||||
2. Enter a value in the **Asset 1 Amount** field.
|
||||
3. (Optional) Enter a value in the **Asset 2 Amount** field.
|
||||
4. Click **Add to AMM**.
|
||||
|
||||
[](/docs/img/quickstart-add-to-amm3.png)
|
||||
@@ -61,20 +79,13 @@ Choose the amount of assets to add to the AMM. You can deposit either one or bot
|
||||
[](/docs/img/quickstart-add-to-amm4.png)
|
||||
|
||||
|
||||
### Check LP Token Value
|
||||
|
||||
1. Enter a value in the **LP Tokens** field.
|
||||
2. Click **Get LP Value**.
|
||||
|
||||
[](/docs/img/quickstart-add-to-amm5.png)
|
||||
|
||||
|
||||
### Redeem Your LP Tokens
|
||||
|
||||
1. Enter a value in the **LP Tokens** field.
|
||||
2. Click **Redeem LP**.
|
||||
1. Click **Get LP Value**.
|
||||
2. Enter a value in the **LP Tokens** field.
|
||||
3. Click **Redeem LP**.
|
||||
|
||||
[](/docs/img/quickstart-add-to-amm6.png)
|
||||
[](/docs/img/quickstart-add-to-amm5.png)
|
||||
|
||||
|
||||
## Code Walkthrough
|
||||
@@ -86,27 +97,20 @@ You can open `ripplex12-add-to-amm.js` from the [Quickstart Samples](https://git
|
||||
|
||||
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/quickstart/js/ripplex12-add-to-amm.js" from="// Deposit assets to existing AMM." before="// Vote on AMM trading fees" language="js" /%}
|
||||
|
||||
|
||||
### 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.
|
||||
Trading fees are applied to any transaction that interacts with the AMM. As with the `addAssets()` function, this one checks the combination of assets provided to modifty the `ammVote` transaction.
|
||||
|
||||
{% code-snippet file="/_code-samples/quickstart/js/ripplex12-add-to-amm.js" from="// Vote on AMM trading fees" before="// Calculate the value of your LP tokens." language="js" /%}
|
||||
|
||||
|
||||
### 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 in circulation that you own qualifies you for the same percentage of the total assets in the AMM.
|
||||
|
||||
{% code-snippet file="/_code-samples/quickstart/js/ripplex12-add-to-amm.js" from="// Calculate the value of your LP tokens." before="// Withdraw by redeeming LP tokens." language="js" /%}
|
||||
|
||||
|
||||
### 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.
|
||||
The `calculateLP()` function gets the AMM account, which acts as its own issuer of LP tokens. It then checks your wallet balance and gets your LP token balance by matching it with the AMM issuer. Although there isn't a dedicated method to calculate what you can redeem your LP tokens for, the math to do so is simple. The function checks the percentage of LP tokens in circulation that you own, and then applies that same percentage to the total assets in the AMM to give you their redemption value.
|
||||
|
||||
{% code-snippet file="/_code-samples/quickstart/js/ripplex12-add-to-amm.js" from="// Withdraw by redeeming LP tokens." language="js" /%}
|
||||
The code to redeem the LP tokens checks how many tokens you want to redeem, as well as the combination of assets to format `amm_info` and `AMMWithdraw`.
|
||||
|
||||
{% code-snippet file="/_code-samples/quickstart/js/ripplex12-add-to-amm.js" from="// Calculate the value of your LP tokens." language="js" /%}
|
||||
|
||||
@@ -34,11 +34,13 @@ Without the Quickstart Samples, you will not be able to try the examples that fo
|
||||
|
||||
### Check AMM
|
||||
|
||||
In the standby account fields:
|
||||
Check if an AMM pair already exists. An AMM holds two different assets: at most one of these can be XRP, and one or both of them can be [tokens](/docs/concepts/tokens).
|
||||
|
||||
1. Enter a [currency code](/docs/references/protocol/data-types/currency-formats.md#currency-codes) in the **Currency** field.
|
||||
2. Enter a token issuer in the **Destination** field.
|
||||
3. Click **Check AMM**.
|
||||
1. Enter a [currency code](/docs/references/protocol/data-types/currency-formats.md#currency-codes) in the **Asset 1 Currency** field. For example, `TST`.
|
||||
2. (Optional) If you entered a currency code other than `XRP`, also enter the token issuer in the **Asset 1 Issuer** field.
|
||||
3. Enter a second currency code in the **Asset 2 Currency** field.
|
||||
4. (Optional) If you entered a second currency code other than `XRP`, also enter the token issuer in the **Asset 2 Issuer** field.
|
||||
5. Click **Check AMM**.
|
||||
|
||||
[](/docs/img/quickstart-create-amm3.png)
|
||||
|
||||
@@ -47,9 +49,9 @@ In the standby account fields:
|
||||
|
||||
Create a trustline from the operational account to the standby account. In the standby account fields:
|
||||
|
||||
1. Enter a currency code in the **Currency** field.
|
||||
2. Enter the maximum transfer limit in the **Amount** field.
|
||||
3. Enter the operational account address in the **Destination** field.
|
||||
1. Enter the maximum transfer limit in the **Amount** field.
|
||||
2. Enter the operational account address in the **Destination** field.
|
||||
3. Enter a currency code in the **Currency** field.
|
||||
4. Click **Create Trustline**.
|
||||
|
||||
[](/docs/img/quickstart-create-amm4.png)
|
||||
@@ -67,17 +69,18 @@ Send issued tokens from the operational account to the standby account. In the o
|
||||
|
||||
[](/docs/img/quickstart-create-amm5.png)
|
||||
|
||||
{% admonition type="note" name="Note" %}
|
||||
If you want to create an AMM with two test tokens, repeat the _Create Trustline_ and _Issue Tokens_ steps, using a different currency code.
|
||||
{% /admonition %}
|
||||
|
||||
### Create an AMM
|
||||
|
||||
In the standby account fields:
|
||||
Create a new AMM pool.
|
||||
|
||||
1. Click **Get Balances** to verify how many tokens you have.
|
||||
2. Enter how much XRP to add in the **XRP Balance** field.
|
||||
3. Enter the operational account address in the **Destination** field.
|
||||
4. Enter the currency code in the **Currency** field.
|
||||
5. Enter how many issued tokens to add in the **Amount** field.
|
||||
6. Click **Create AMM**.
|
||||
2. Enter a value in the **Asset 1 Amount** field.
|
||||
3. Enter a value in the **Asset 2 Amount** field.
|
||||
4. Click **Create AMM**.
|
||||
|
||||
[](/docs/img/quickstart-create-amm6.png)
|
||||
|
||||
@@ -96,6 +99,6 @@ This checks if an AMM already exists. While multiple tokens can share the same c
|
||||
|
||||
### Create AMM
|
||||
|
||||
This sends the `AMMCreate` transaction and creates a new AMM, using the initial assets provided.
|
||||
This sends the `AMMCreate` transaction and creates a new AMM, using the initial assets provided. The code checks the token currency fields and formats the `AMMCreate` transaction based on the combination of `XRP` and custom tokens.
|
||||
|
||||
{% code-snippet file="/_code-samples/quickstart/js/ripplex11-create-amm.js" from="// Create AMM function" before="// Check AMM function" language="js" /%}
|
||||
|
||||
Reference in New Issue
Block a user