Skip to content

Commit

Permalink
📌 Simplify starting frame, independence
Browse files Browse the repository at this point in the history
  • Loading branch information
JFWooten4 committed Nov 24, 2024
1 parent b7f4a07 commit 5492eb3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 42 deletions.
2 changes: 1 addition & 1 deletion docs/learn/encyclopedia/sdex/README.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: The SDEX
title: Liquidity on Stellar
sidebar_position: 90
---

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
title: "Liquidity on Stellar: the SDEX and Liquidity Pools"
title: "The SDEX and Automated Market Makers"
---

import { CodeExample } from "@site/src/components/CodeExample";
Expand Down Expand Up @@ -391,8 +391,6 @@ streamling also works for trading-history data, among other ledger updates. mark

Since all oreders for a currency pari fall into the same SDEX order book, you cna know that you're getting the best exchange rate between two explicit assets. Accordingly, you can analyze the past valuation of a currency by reading its exchanged trades feed. We'll continue our exmaple and set up a recent trading price query:

EX for the price query (getting best ex impl, see L72 at 9bc3c637047c8d1b010228d29876f2b92783074b):

<CodeExample>

```python
Expand Down Expand Up @@ -608,7 +606,55 @@ However, users don’t need to participate in the pool to take advantage of what

Instead of relying on the buy and sell orders of the SDEX, AMMs keep assets liquid 24/7 using pooled capital and a mathematical equation. AMMs hold two different assets in a liquidity pool, and the quantities of those assets (or reserves) are inputs for that equation (Asset A \* Asset B = k). If an AMM holds more of the reserve assets, the asset prices move less in response to a trade.

When you submit an
When you submit an..

<CodeExample>

```python
def getSpotPrice():
resp = server.liquidity_pools().liquidity_pool(poolId).call()
amountA = resp["reserves"][0]["amount"]
amountB = resp["reserves"][1]["amount"]
spotPrice = Decimal(amountA) / Decimal(amountB)
print(f"Price: {amountA}/{amountB} = {spotPrice:.7f}") # Max network precision
```

```js
async function getSpotPrice() {
const pool = await server.liquidityPools().liquidityPoolId(poolId).call();
const [a, b] = pool.reserves.map((r) => r.amount);
const spotPrice = new BigNumber(a).div(b);
console.log(`Price: ${a}/${b} = ${spotPrice.toFormat(7)}`);
}
```

```java
public static void getSpotPrice(String[] args) throws Exception {
LiquidityPoolResponse poolInfo = server.liquidityPools().liquidityPool(poolId).execute();
double reserveA = Double.parseDouble(poolInfo.getReserves().get(0).getAmount());
double reserveB = Double.parseDouble(poolInfo.getReserves().get(1).getAmount());
double spotPrice = reserveA / reserveB;
System.out.printf("Price: %.7f/%7f = %.7f%n", reserveA, reserveB, spotPrice);
}
```

```go
func getSpotPrice(server *horizonclient.Client, poolID string) {
poolRequest := horizonclient.LiquidityPoolRequest{LiquidityPoolID: poolID}
pool, err := server.LiquidityPoolDetail(poolRequest)
check(err)

reserveA, err := strconv.ParseFloat(pool.Reserves[0].Amount, 64)
check(err)
reserveB, err := strconv.ParseFloat(pool.Reserves[1].Amount, 64)
check(err)

spotPrice := reserveA / reserveB
fmt.Printf("Price: %.7f/%.7f = %.7f\n", reserveA, reserveB, spotPrice)
}
```

</CodeExample>

#### AMM Calculations

Expand Down Expand Up @@ -1399,7 +1445,6 @@ Finally, we can combine these pieces together to simulate some participation in
<CodeExample>

```python

# Step 1: kp1 adds liquidity
establishPoolTrustline(kps[1], poolAsset)
addLiquidity(
Expand Down Expand Up @@ -1433,13 +1478,6 @@ if not balance:
raise Exception("No liquidity pool shares found for kp1")
removeLiquidity(kps[1], poolId, balance)
getSpotPrice()

def getSpotPrice():
resp = server.liquidity_pools().liquidity_pool(poolId).call()
amountA = resp["reserves"][0]["amount"]
amountB = resp["reserves"][1]["amount"]
spotPrice = Decimal(amountA) / Decimal(amountB)
print(f"Price: {amountA}/{amountB} = {spotPrice:.7f}") # Max network precision
```

```js
Expand Down Expand Up @@ -1477,13 +1515,6 @@ async function main() {
await getSpotPrice();
}

async function getSpotPrice() {
const pool = await server.liquidityPools().liquidityPoolId(poolId).call();
const [a, b] = pool.reserves.map((r) => r.amount);
const spotPrice = new BigNumber(a).div(b);
console.log(`Price: ${a}/${b} = ${spotPrice.toFormat(7)}`);
}

preamble().then(main);
```

Expand Down Expand Up @@ -1518,14 +1549,6 @@ public static void main(String[] args) throws Exception {
removeLiquidity(kp1, poolId, balance);
getSpotPrice();
}

public static void getSpotPrice(String[] args) throws Exception {
LiquidityPoolResponse poolInfo = server.liquidityPools().liquidityPool(poolId).execute();
double reserveA = Double.parseDouble(poolInfo.getReserves().get(0).getAmount());
double reserveB = Double.parseDouble(poolInfo.getReserves().get(1).getAmount());
double spotPrice = reserveA / reserveB;
System.out.printf("Price: %.7f/%7f = %.7f%n", reserveA, reserveB, spotPrice);
}
```

```go
Expand Down Expand Up @@ -1569,20 +1592,6 @@ func main() {
removeLiquidity(kp1, poolID, balance)
getSpotPrice(poolID)
}

func getSpotPrice(server *horizonclient.Client, poolID string) {
poolRequest := horizonclient.LiquidityPoolRequest{LiquidityPoolID: poolID}
pool, err := server.LiquidityPoolDetail(poolRequest)
check(err)

reserveA, err := strconv.ParseFloat(pool.Reserves[0].Amount, 64)
check(err)
reserveB, err := strconv.ParseFloat(pool.Reserves[1].Amount, 64)
check(err)

spotPrice := reserveA / reserveB
fmt.Printf("Price: %.7f/%.7f = %.7f\n", reserveA, reserveB, spotPrice)
}
```

</CodeExample>

0 comments on commit 5492eb3

Please sign in to comment.