DEX

Screenshot from 2022-09-05 09-23-04

Screenshot from 2022-09-05 08-47-51

Screenshot from 2022-09-05 08-48-17

Screenshot from 2022-09-05 08-49-25

Screenshot from 2022-09-05 16-40-40

Screenshot from 2022-09-05 16-41-16

Price function determining amount of output conversion token for input token.

EQUATION

yOutput = (997 * xInput * (yReserves)) / (xReserves + xInput) / 1000;

Liquidity reserves of both tokens.

Price function derivation

returns yOutput, or yDelta for xInput (or xDelta)

 *  Price section for an understanding of the DEX's pricing model and for a price function to add to your contract.
 
 *  we’re paying a 0.3% fee for additional deduction to DEX
 
 *  let’s move on to the interesting part of the AMM: exchanging tokens. Say that a user wants to get token_b in exchange for token_a of some specified amount (token_a_amount). Let’s denote the amount of tokens they will get by token_b_amount. In the equations below we let:
 
 *  a and b denote the amounts of token_a and token_b the user swaps,
 
 *  x and y denote the current balances of token_a and token_b in the AMM (that is, AmmState.token_a_balance and AmmState.token_b_balance).
 
 *  The AMM formula states that the value of token_a_balance * token_b_balance (that is, x * y) should be preserved. So we have:
 
 *  (x+a).(y-b) = x.y
 
 *  Let’s isolate b (as the rest of the values are known):
 
 *  b = y.a/(x+a)
 
 *  Output token price is determined using above.

Screenshot from 2022-09-05 16-43-18

Screenshot from 2022-09-05 16-43-38

Screenshot from 2022-09-05 16-44-04

#Slippage algorithm - for same amount of axie consecutive ethereum outputted is slightly slipped/less, due to change in reserves.

Screenshot from 2022-09-05 16-44-21

Screenshot from 2022-09-05 16-44-35

Screenshot from 2022-09-05 16-44-42

Screenshot from 2022-09-05 16-44-51

Screenshot from 2022-09-05 16-45-01

Screenshot from 2022-09-05 18-30-58

133.065 Axies get converted to 136.9639 tapping diffference in exchange rates.

Algorithm

1)Swap Axies to Eth

2)Swap same amount of Eth to Axies from different exchange.

Repeat this couple of times to increase your current token.

Practically swapping is done across two different exchanges.

Screenshot from 2022-09-05 18-31-05

Screenshot from 2022-09-05 18-32-04

Screenshot from 2022-09-05 18-32-26

Screenshot from 2022-09-05 18-33-06

Screenshot from 2022-09-05 18-33-26

Screenshot_20220720-055125

A simple AMM in Cairo to reduce slippage -

Reference - https://starknet.io/docs/0.10.0/hello_cairo/amm.html#

let’s move on to the interesting part of the AMM: exchanging tokens. Say that a user wants to get token_b in exchange for token_a of some specified amount (token_a_amount). Let’s denote the amount of tokens they will get by token_b_amount. In the equations below we let:

a and b denote the amounts of token_a and token_b the user swaps,

x and y denote the current balances of token_a and token_b in the AMM (that is, AmmState.token_a_balance and AmmState.token_b_balance).

The AMM formula states that the value of token_a_balance * token_b_balance (that is, x * y) should be preserved. So we have:

(x+a).(y-b) = x.y

Let’s isolate b (as the rest of the values are known):

b = y.a/(x+a)