Skip to content

Commit

Permalink
Merge pull request #34 from cs-pub-ro/defi
Browse files Browse the repository at this point in the history
Add DeFi practical session
  • Loading branch information
CostinCarabas authored Dec 12, 2024
2 parents 6328d7f + 5099e1a commit 22c7270
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 38 deletions.
2 changes: 1 addition & 1 deletion chapters/money/lab/content/mint_tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Let's create a token new token:

![Issue Token](../media/issue_token.png)

and inspect the transaction
and inspect the transaction.

![Explorer Token Created](../media/explorer_token_created.png)

Expand Down
55 changes: 52 additions & 3 deletions chapters/money/lab/content/swap.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,57 @@ Keep in mind that Devnet and Testnet chains are different! First go to [Devnet W
![DEX](../media/xexchange.png)

Use **Connect** button and connect with your wallet via your preferred method.
After connection go to the **Swap** tab (it's located on the top of the page).

![DEX](../media/swap.png)
Let's perform a swap from EGLD to ASH.

Select **USDC** and click **Swap**. Check the transaction on the Explorer. Check your balance on your Wallet.
![SwapASH](../media/swap_ash1.png)
Notice that for 1 EGLD, we would get **212 ASH** tokens.

Let's try to swap more:
![SwapASH](../media/swap_ash2.png)

Notice that for 10 EGLD, we would get 1498 ASH tokens.
That's **149.8 ASH** tokens for 1 EGLD.

Any intuition of why the drop of exchange rate?

Let's try to swap more:
![SwapASH](../media/swap_ash3.png)

Notice that for 100 EGLD, we would get 3798 ASH tokens.
That's **37.98 ASH** tokens for 1 EGLD.

## Automated Market Maker (AMM)

An AMM relies on a mathematical formula to price assets.
Instead of using an order book like a traditional exchange, assets are priced according to a pricing algorithm.

This formula can vary with each protocol.
For example, xExchange DEX uses the industry standard "x*y=k" constant product AMM model, which has proven its reliability in existing implementations, and has been formally modelled and verified.
In this formula, k is a fixed constant, meaning the pool’s total liquidity always has to remain the same.

![AMM](../media/amm.png)

## Pair Smart Contract

The smart contract code can be found [here](https://github.com/multiversx/mx-exchange-sc/tree/main/dex/pair).

Let's inspect the deployed [contract](https://devnet-explorer.multiversx.com/accounts/erd1qqqqqqqqqqqqqpgq0e9pmlzr0nk5nkulzcmessttsjkzr4xf0n4sue4r8e/tokens).

![AshLPExplorer](../media/ash_lp_explorer.png)

Notice the contract's balance:
* WEGLD: 27.14
* ASH: 4,817.99
* ASHWEGLD: 0.000000000000001 - This is the token provided to liquidity providers when they add liquidity. Let's ignore this for now.

Let's try to exchange more ASH than the contract can provide:

![SwapASH](../media/swap_ash4.png)

In this example, we tried to exchange **100 millions EGLD**, but the contract would output **4,580.0391 ASH** tokens.
That's because of the curve of **x*y=k** formula (see image above).

## Task
Perform a swap. Go to the [Explorer](https://devnet-explorer.multiversx.com) and inspect your transaction.
Inspect if there are any evets emitted.
Binary file added chapters/money/lab/media/amm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapters/money/lab/media/ash_lp_explorer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed chapters/money/lab/media/swap.png
Binary file not shown.
Binary file added chapters/money/lab/media/swap_ash1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapters/money/lab/media/swap_ash2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapters/money/lab/media/swap_ash3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added chapters/money/lab/media/swap_ash4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified chapters/money/lab/media/xexchange.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions chapters/smart-contracts/lab/content/events.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Events

In this section you will learn what Smart Contract Events are, how to see what events were emitted in a transaction and how to emit one yourself.

Key Takeaways:
* Events in smart contracts serve as a communication mechanism between the contract and user interfaces;
* By emitting events, smart contracts can notify external applications or listeners about specific actions or changes;
Expand Down
47 changes: 13 additions & 34 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,26 @@ docusaurus:
- ../media/
subsections:
- Building your first dApp: dApp.md
- Smart Contract Events:
path: chapters/smart-contracts/lab/content/
subsections:
- SC Events: events.md
extra:
- ../media/
- DeFi:
path: chapters/money/lab/content/
extra:
- ../media/
subsections:
- ESDT Tokens: mint_tokens.md
- Swap Tokens: swap.md
- Observer:
path: chapters/introduction/lab/content/observer/
extra:
- ../../media/
# - ../../quiz/
subsections:
- Observers: observer.md
- Money (DeFi):
path: chapters/money/lab/content
extra:
- ../media/
subsections:
- ESDT Tokens: mint_tokens.md
- Swap Tokens: swap.md
- Basic Peer-to-Peer Blockchain:
path: chapters/peer-to-peer/lab/content
subsections:
Expand All @@ -150,33 +156,6 @@ docusaurus:
- Sources : sources.md
extra:
- ../media/
- Smart Contract Events:
path: chapters/smart-contracts/lab/content
subsections:
- SC Events: events.md
extra:
- ../media/

# - Fungible Tokens:
# path: chapters/template-chapter/yet-another-topic/
# subsections:
# - Drills/: drills/
# - Non Fungible Tokens (NFT):
# path: chapters/template-chapter/yet-another-topic/
# subsections:
# - Drills/: drills/
# - dApps:
# path: chapters/template-chapter/yet-another-topic/
# subsections:
# - Drills/: drills/
# - Security:
# path: chapters/template-chapter/yet-another-topic/
# subsections:
# - Drills/: drills/
# - DeFi:
# path: chapters/template-chapter/yet-another-topic/
# subsections:
# - Drills/: drills/
- Assignments:
- Assignment 1: assignments/assignment1.md
- Assignment 2: assignments/assignment2.md
Expand Down

0 comments on commit 22c7270

Please sign in to comment.