---
# System prepended metadata

title: Uniswap

---

# Uniswap
Uniswap runs on two smart contracts; an "Exchange" contract and a "Factory" contract

https://github.com/Rengo-Labs/uniswap-casper-core
https://github.com/Rengo-Labs/uniswap-casper-router
https://docs.uniswap.org/protocol/V2/introduction


## Core
### Factory
Create and keep track of pairs
Pair:ETH/DAI, ETH/USDT

### Pairs
Which are created from Factory, serving as an AMM and keeping track of pool token balances
Enbles trading between 2 ERC20 tokens

### ERC20

## Periphery
### Library
Convenience functions for fetching data and pricing
### Router
Which uses the library, supports basic req. of UI

## Functions
- Manage pools
- Liquidity providers (deposit/widthdraw)
- Swapper (end-users, swap tokens)
+ Price oracle

## Notes
- Wrapped token (WCSPR, WETH, WBTC): ratio 1:1, be able to use native tokens in DApps, without making the application unncessary complicated
  -> Swap ETH direct??
- Liquidity pools instead of order-book
  - requires additional layers (controls) complexity
- Tokes must be transfered to pair before executing any other actions?


## Workflow
### Token swap
A user choose an input token:amound to swap, and the expected output token 
=> The protocal calculates how much they will receive 

## Deploys
### Factory
(https://testnet.cspr.live/contract/13cc83616c3fb4e6ea22ead5e61eb6319d728783ed02eab51b1f442085e605a7)
fee_to_setter (changable): account hash of user
0.3% => 1/6

### Flash swapper
a ref to Factory
(https://testnet.cspr.live/contract/1c23f9e89033e5c2d2a21a6926411b2645c000cf43fc0db495821633da2aed6e)

### Create pairs
- Pool ownership token (ERC20) - Liquidity pool
- Token0:reserve0, token1:reserve1

## Swap (token0, token1, amount0, amount1)
    fee 0.3% divide portionally to LPs


## Test data
Testnet node: localhost

AC02
Deploy tokens, router
01d91f6fc88439c67fd40659c41a4156e639d4b9def94cb36d3d45ffaf7edc708a

AC03
Work with router
01d020a370a4bf7165949719821ecdf51ccd7117d17e8456e419c5010dc83784fb


Tokens:
XOY
https://testnet.cspr.live/contract/ef31acc65fb9521c1c559fa47550677b95d4adeeb9c4030f7f4db38de6b5a314
LAK
https://testnet.cspr.live/contract/49f88a64b6e34ff02550afa187f7fef85fa4e69a788917d44f8af1a2559b59f6
RECV
https://testnet.cspr.live/contract/a563a37bbc119960e4956a277e7b608943bfb97945eed0aca332f4839dc941da
Router
https://testnet.cspr.live/contract/059cf19350e440df7dba3870073619831273582f45fb96f54ce08fb9f75292e0
## Test

### ERC20
```
casper-client get-state-root-hash --node-address http://localhost:11101 | jq -r
-- "39d112da61a1b9bb4e4b9e6cbc0efb95ec435ee20092a5c7f46969ca497d5ff2"
casper-client query-global-state --node-address http://localhost:11101 -k 01d020a370a4bf7165949719821ecdf51ccd7117d17e8456e419c5010dc83784fb -s 39d112da61a1b9bb4e4b9e6cbc0efb95ec435ee20092a5c7f46969ca497d5ff2
```

```bash
casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --session-path ./uniswap-casper-core/erc20/target/wasm32-unknown-unknown/release/erc20-token.wasm \
    --payment-amount 180000000000 \
    --session-arg="public_key:public_key='01d91f6fc88439c67fd40659c41a4156e639d4b9def94cb36d3d45ffaf7edc708a'" \
    --session-arg="name:string='Dang Xoy'" \
    --session-arg="symbol:string='XOY'" \
    --session-arg="decimals:u8='18'" \
    --session-arg="initial_supply:u256='1000000000000000000000000000'" \
    --session-arg="contract_name:string='ERC20 XOY'"

    
casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --session-path ./uniswap-casper-core/erc20/target/wasm32-unknown-unknown/release/erc20-token.wasm \
    --payment-amount 180000000000 \
    --session-arg="public_key:public_key='01d91f6fc88439c67fd40659c41a4156e639d4b9def94cb36d3d45ffaf7edc708a'" \
    --session-arg="name:string='Dang Lak'" \
    --session-arg="symbol:string='LAK'" \
    --session-arg="decimals:u8='18'" \
    --session-arg="initial_supply:u256='1000000000000000000000000000'" \
    --session-arg="contract_name:string='ERC20_LAK'"
    
casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --session-path ./uniswap-casper-core/erc20/target/wasm32-unknown-unknown/release/erc20-token.wasm \
    --payment-amount 180000000000 \
    --session-arg="public_key:public_key='01d91f6fc88439c67fd40659c41a4156e639d4b9def94cb36d3d45ffaf7edc708a'" \
    --session-arg="name:string='Dang Recv'" \
    --session-arg="symbol:string='RECV'" \
    --session-arg="decimals:u8='18'" \
    --session-arg="initial_supply:u256='1000000000000000000000000000'" \
    --session-arg="contract_name:string='ERC20_RECV'"
```

### Deploy router
```
casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --session-path ./uniswap-casper-router/uniswap-v2-router/target/wasm32-unknown-unknown/release/uniswap-v2-router.wasm \
    --payment-amount 220000000000 \
    --session-arg="public_key:public_key='01d91f6fc88439c67fd40659c41a4156e639d4b9def94cb36d3d45ffaf7edc708a'" \
    --session-arg="factory:Key='hash-5028190b8a5b6addbf3d51ee2c6ae5b913f09223d65eff9bcf5985f74ae976ec'" \
    --session-arg="wcspr:Key='hash-083756dee38a7e3a8a7190a17623cfbc8bc107511de206f03c3dbd1af5463a45'" \
    --session-arg="library:Key='hash-fa073d1a95a606871983689633dab9464fb5fbe5f723b0855e025ea01b9bf308'" \
    --session-arg="pair:Key='hash-8e6fbaae9f5ff3bb3cca7cb15723b2a47917d074922575187cb136e8d4b169a7'" \
    --session-arg="contract_name:string='CasperDash_test_uniswap'"
```

# Approve tokens
```
casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --payment-amount 2500000000 \
    --session-hash hash-ef31acc65fb9521c1c559fa47550677b95d4adeeb9c4030f7f4db38de6b5a314 \
    --session-entry-point "approve" \
    --session-arg "spender:Key='hash-119b7c0cc3b427079b1f50178c6f880c666346c9343491c4dbc65b564edb1ed3'" \
    --session-arg "amount:U256='10000000000000000000000000'"

casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --payment-amount 2500000000 \
    --session-hash hash-ef31acc65fb9521c1c559fa47550677b95d4adeeb9c4030f7f4db38de6b5a314 \
    --session-entry-point "allowance" \
    --session-arg "owner:Key='hash-119b7c0cc3b427079b1f50178c6f880c666346c9343491c4dbc65b564edb1ed3'" \
    --session-arg "spender:Key='hash-119b7c0cc3b427079b1f50178c6f880c666346c9343491c4dbc65b564edb1ed3'"

casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --payment-amount 2500000000 \
    --session-hash hash-49f88a64b6e34ff02550afa187f7fef85fa4e69a788917d44f8af1a2559b59f6 \
    --session-entry-point "approve" \
    --session-arg "spender:Key='hash-119b7c0cc3b427079b1f50178c6f880c666346c9343491c4dbc65b564edb1ed3'" \
    --session-arg "amount:U256='10000000000000000000000000'"
    
    
casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --payment-amount 2500000000 \
    --session-hash hash-49f88a64b6e34ff02550afa187f7fef85fa4e69a788917d44f8af1a2559b59f6 \
    --session-entry-point "allowance" \
    --session-arg "owner:Key='hash-119b7c0cc3b427079b1f50178c6f880c666346c9343491c4dbc65b564edb1ed3'" \
    --session-arg "spender:Key='hash-119b7c0cc3b427079b1f50178c6f880c666346c9343491c4dbc65b564edb1ed3'"
```

## Add liquidity pool
```
casper-client put-deploy \
    --chain-name casper-test \
    --node-address http://localhost:11101/ \
    --secret-key ./TestDeploy-secret.pem \
    --payment-amount 2500000000 \
    --session-hash hash-059cf19350e440df7dba3870073619831273582f45fb96f54ce08fb9f75292e0 \
    --session-entry-point "add_liquidity" \
    --session-arg "token_a:Key='hash-ef31acc65fb9521c1c559fa47550677b95d4adeeb9c4030f7f4db38de6b5a314'" \
    --session-arg "token_b:Key='hash-49f88a64b6e34ff02550afa187f7fef85fa4e69a788917d44f8af1a2559b59f6'" \
    --session-arg "amount_a_desired:U256='10000000000'" \
    --session-arg "amount_b_desired:U256='10000000000'" \
    --session-arg "amount_a_min:U256='1000000'" \
    --session-arg "amount_b_min:U256='1000000'" \
    --session-arg "to:Key='hash-a563a37bbc119960e4956a277e7b608943bfb97945eed0aca332f4839dc941da'" \
    --session-arg "deadline:U256='1650964984'"
```