5.0 KiB
seo, labels
| seo | labels | |||||
|---|---|---|---|---|---|---|
|
|
Add Assets to an AMM
This example shows how to:
- Deposit assets to an existing AMM and receive LP tokens.
- Vote on AMM trading fees.
- Check the value of your LP tokens.
- Redeem LP tokens for assets in the AMM pair.
You can download the Quickstart Samples archive to try each of the samples in your own browser.
{% admonition type="note" name="Note" %} Without the Quickstart Samples, you will not be able to try the examples that follow. {% /admonition %}
Usage
Get Accounts
- Open
12.add-to-amm.htmlin a browser. - Select Testnet or Devnet
- Get test accounts.
- If you have existing account seeds:
- Paste account seeds in the Seeds field.
- Click Get Accounts from Seeds.
- If you don't have account seeds:
- Click Get New Standby Account.
- Click Get New Operational Account.
- If you have existing account seeds:
Get the AMM
- Enter a currency code in the Asset 1 Currency field. For example,
TST. - (Optional) If you entered a currency code other than
XRP, also enter the token issuer in the Asset 1 Issuer field. - Enter a second currency code in the Asset 2 Currency field.
- (Optional) If you entered a second currency code other than
XRP, also enter the token issuer in the Asset 2 Issuer field. - Click Check AMM.
(Optional) Acquire More Assets
If you need more assets, you can:
- Fund a new wallet with
XRPby clicking Get New Standby Account. - Get more tokens from the token issuer in the AMM pair. See: 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.
- Click Get Balances to verify how many tokens you have.
- Enter a value in the Asset 1 Amount field.
- (Optional) Enter a value in the Asset 2 Amount field.
- Click Add to AMM.
Vote on trading fees
- Enter a value in the Trading Fee field. 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.
- Click Vote on Fee.
Redeem Your LP Tokens
- Click Get LP Value.
- Enter a value in the LP Tokens field.
- Click Redeem LP.
Code Walkthrough
You can open ripplex12-add-to-amm.js from the Quickstart Samples to view the source code.
Add Assets to an Existing AMM
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.
{% 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. 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" /%}
Redeem Your LP Tokens
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.
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" /%}




