Skip to content

Commit 5d936ed

Browse files
nhussein11eshaben
andauthored
Add Uniswap V2 tutorial to SC tutorials (#541)
* wip * fix: draft uniswap tutorial v2 ready * wip * fix: adding uniswap tutorial ready * fix: TODO * fix: feeback * Apply suggestions from code review Co-authored-by: Erin Shaben <[email protected]> * Update tutorials/smart-contracts/demo-aplications/deploying-uniswap-v2.md Co-authored-by: Erin Shaben <[email protected]> * Update tutorials/smart-contracts/demo-aplications/.pages Co-authored-by: Erin Shaben <[email protected]> * fix: desc * fix: feedback * fix: feedback * fix: uppercase * fix: feedback --------- Co-authored-by: Erin Shaben <[email protected]>
1 parent 5e4badd commit 5d936ed

File tree

8 files changed

+532
-0
lines changed

8 files changed

+532
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<div id="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx hardhat compile</span>
3+
<span data-ty>Compiling 12 Solidity files</span>
4+
<span data-ty>Successfully compiled 12 Solidity files</span>
5+
</div>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<div id="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx hardhat run scripts/deploy.js --network localNode</span>
3+
<span data-ty>Successfully compiled 12 Solidity files</span>
4+
<span data-ty>Deploying contracts using 0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac</span>
5+
<span data-ty>Deploying UniswapV2ERC20...</span>
6+
<span data-ty>ETH deployed to : 0x7acc1aC65892CF3547b1b0590066FB93199b430D</span>
7+
<span data-ty>Deploying UniswapV2Factory...</span>
8+
<span data-ty>Factory deployed to : 0x85b108660f47caDfAB9e0503104C08C1c96e0DA9</span>
9+
<span data-ty>Deploying UniswapV2Pair with JsonRpcProvider workaround...</span>
10+
<span data-ty>Pair deployed to : 0xF0e46847c8bFD122C4b5EEE1D4494FF7C5FC5104</span>
11+
</div>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<div id="termynal" data-termynal>
2+
<span data-ty="input"><span class="file-path"></span>npx hardhat test --network localNode</span>
3+
<span data-ty>Compiling 12 Solidity files</span>
4+
<span data-ty>Successfully compiled 12 Solidity files</span>
5+
<span data-ty></span>
6+
<span data-ty>UniswapV2ERC20</span>
7+
<span data-ty> ✔ name, symbol, decimals, totalSupply, balanceOf, DOMAIN_SEPARATOR, PERMIT_TYPEHASH (44ms)</span>
8+
<span data-ty> ✔ approve (5128ms)</span>
9+
<span data-ty> ✔ transfer (5133ms)</span>
10+
<span data-ty> ✔ transfer:fail</span>
11+
<span data-ty> ✔ transferFrom (6270ms)</span>
12+
<span data-ty> ✔ transferFrom:max (6306ms)</span>
13+
<span data-ty></span>
14+
<span data-ty>UniswapV2Factory</span>
15+
<span data-ty> ✔ feeTo, feeToSetter, allPairsLength</span>
16+
<span data-ty> ✔ createPair (176ms)</span>
17+
<span data-ty> ✔ createPair:reverse (1224ms)</span>
18+
<span data-ty> ✔ setFeeTo (1138ms)</span>
19+
<span data-ty> ✔ setFeeToSetter (1125ms)</span>
20+
<span data-ty></span>
21+
<span data-ty>UniswapV2Pair</span>
22+
<span data-ty> ✔ mint (11425ms)</span>
23+
<span data-ty> ✔ getInputPrice:0 (12590ms)</span>
24+
<span data-ty> ✔ getInputPrice:1 (17600ms)</span>
25+
<span data-ty> ✔ getInputPrice:2 (17618ms)</span>
26+
<span data-ty> ✔ getInputPrice:3 (17704ms)</span>
27+
<span data-ty> ✔ getInputPrice:4 (17649ms)</span>
28+
<span data-ty> ✔ getInputPrice:5 (17594ms)</span>
29+
<span data-ty> ✔ getInputPrice:6 (13643ms)</span>
30+
<span data-ty> ✔ optimistic:0 (17647ms)</span>
31+
<span data-ty> ✔ optimistic:1 (17946ms)</span>
32+
<span data-ty> ✔ optimistic:2 (17657ms)</span>
33+
<span data-ty> ✔ optimistic:3 (21625ms)</span>
34+
<span data-ty> ✔ swap:token0 (12665ms)</span>
35+
<span data-ty> ✔ swap:token1 (17631ms)</span>
36+
<span data-ty> ✔ burn (17690ms)</span>
37+
<span data-ty> ✔ feeTo:off (23900ms)</span>
38+
<span data-ty> ✔ feeTo:on (24991ms)</span>
39+
<span data-ty></span>
40+
<span data-ty>28 passing (12m)</span>
41+
</div>

llms.txt

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/system-chains/
163163
Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/testing/fork-live-chains/
164164
Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/testing/
165165
Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/testing/spawn-basic-chain/
166+
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/deploying-uniswap-v2/
167+
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/
166168
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/deploy-erc20/
167169
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/deploy-nft/
168170
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/
@@ -32083,6 +32085,271 @@ tail -f /tmp/zombie-794af21178672e1ff32c612c3c7408dc_-2397036-6717MXDxcS55/alic
3208332085
After running this command, you will see the logs of the `alice` node in real-time, which can be useful for debugging purposes. The logs of the `bob` and `collator01` nodes can be checked similarly.
3208432086
--- END CONTENT ---
3208532087

32088+
Doc-Content: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/deploying-uniswap-v2/
32089+
--- BEGIN CONTENT ---
32090+
---
32091+
title: Deploying Uniswap V2 on Polkadot
32092+
description: Learn how to deploy and test Uniswap V2 on Polkadot Hub using Hardhat, bringing AMM-based token swaps to the Polkadot ecosystem.
32093+
---
32094+
32095+
# Deploy Uniswap V2
32096+
32097+
## Introduction
32098+
32099+
Decentralized exchanges (DEXs) are a cornerstone of the DeFi ecosystem, allowing for permissionless token swaps without intermediaries. [Uniswap V2](https://docs.uniswap.org/contracts/v2/overview){target=\_blank}, with its Automated Market Maker (AMM) model, revolutionized DEXs by enabling liquidity provision for any ERC-20 token pair.
32100+
32101+
This tutorial will guide you through how Uniswap V2 works so you can take advantage of it in your projects deployed to Polkadot Hub. By understanding these contracts, you'll gain hands-on experience with one of the most influential DeFi protocols and understand how it functions across blockchain ecosystems.
32102+
32103+
## Prerequisites
32104+
32105+
Before starting, make sure you have:
32106+
32107+
- Node.js (v16.0.0 or later) and npm installed
32108+
- Basic understanding of Solidity and JavaScript
32109+
- Familiarity with [`hardhat-polkadot`](/develop/smart-contracts/dev-environments/hardhat){target=\_blank} development environment
32110+
- Some WND test tokens to cover transaction fees (obtained from the [Polkadot faucet](https://faucet.polkadot.io/westend){target=\_blank})
32111+
- Basic understanding of how AMMs and liquidity pools work
32112+
32113+
## Set Up the Project
32114+
32115+
Let's start by cloning the Uniswap V2 project:
32116+
32117+
1. Clone the Uniswap V2 repository:
32118+
32119+
```
32120+
git clone https://github.com/papermoonio/uniswap-V2-polkadot.git
32121+
cd uniswap-V2-polkadot
32122+
```
32123+
32124+
2. Install the required dependencies:
32125+
32126+
```bash
32127+
npm install
32128+
```
32129+
32130+
3. Update the `hardhat.config.js` file so the paths for the Substrate node and the ETH-RPC adapter match with the paths on your machine. For more info, check the [Testing your Contract](/develop/smart-contracts/dev-environments/hardhat/#testing-your-contract){target=\_blank} section in the Hardhat guide
32131+
32132+
```js title="hardhat.config.js"
32133+
hardhat: {
32134+
polkavm: true,
32135+
nodeConfig: {
32136+
nodeBinaryPath: '../bin/substrate-node',
32137+
rpcPort: 8000,
32138+
dev: true,
32139+
},
32140+
adapterConfig: {
32141+
adapterBinaryPath: '../bin/eth-rpc',
32142+
dev: true,
32143+
},
32144+
},
32145+
32146+
4. Create a `.env` file in your project root to store your private keys (you can use as an example the `env.example` file):
32147+
32148+
```text title=".env"
32149+
LOCAL_PRIV_KEY="INSERT_LOCAL_PRIVATE_KEY"
32150+
AH_PRIV_KEY="INSERT_AH_PRIVATE_KEY"
32151+
```
32152+
32153+
Ensure to replace `"INSERT_LOCAL_PRIVATE_KEY"` with a private key available in the local environment (you can get them from this [file](https://github.com/paritytech/hardhat-polkadot/blob/main/packages/hardhat-polkadot-node/src/constants.ts#L22){target=\_blank}). And `"INSERT_AH_PRIVATE_KEY"` with the account's private key you want to use to deploy the contracts. You can get this by exporting the private key from your wallet (e.g., MetaMask).
32154+
32155+
!!!warning
32156+
Keep your private key safe, and never share it with anyone. If it is compromised, your funds can be stolen.
32157+
32158+
5. Compile the contracts:
32159+
32160+
```bash
32161+
npx hardhat compile
32162+
```
32163+
32164+
If the compilation is successful, you should see the following output:
32165+
32166+
<div id="termynal" data-termynal>
32167+
<span data-ty="input"><span class="file-path"></span>npx hardhat compile</span>
32168+
<span data-ty>Compiling 12 Solidity files</span>
32169+
<span data-ty>Successfully compiled 12 Solidity files</span>
32170+
</div>
32171+
32172+
After running the above command, you should see the compiled contracts in the `artifacts-pvm` directory. This directory contains the ABI and bytecode of your contracts.
32173+
32174+
## Understanding Uniswap V2 Architecture
32175+
32176+
Before interacting with the contracts, it's essential to understand the core architecture that powers Uniswap V2. This model forms the basis of nearly every modern DEX implementation and operates under automated market making, token pair liquidity pools, and deterministic pricing principles.
32177+
32178+
At the heart of Uniswap V2 lies a simple but powerful system composed of two major smart contracts:
32179+
32180+
- **Factory Contract** - the factory acts as a registry and creator of new trading pairs. When two ERC-20 tokens are to be traded, the Factory contract is responsible for generating a new Pair contract that will manage that specific token pair’s liquidity pool. It keeps track of all deployed pairs and ensures uniqueness—no duplicate pools can exist for the same token combination
32181+
- **Pair Contract** - each pair contract is a decentralized liquidity pool that holds reserves of two ERC-20 tokens. These contracts implement the core logic of the AMM, maintaining a constant product invariant (x \* y = k) to facilitate swaps and price determination. Users can contribute tokens to these pools in return for LP (liquidity provider) tokens, which represent their proportional share of the reserves
32182+
32183+
This minimal architecture enables Uniswap to be highly modular, trustless, and extensible. By distributing responsibilities across these components, developers, and users can engage with the protocol in a composable and predictable manner, making it an ideal foundation for DEX functionality across ecosystems, including Polkadot Hub.
32184+
32185+
The project scaffolding is as follows:
32186+
32187+
```bash
32188+
uniswap-V2-polkadot
32189+
├── bin/
32190+
├── contracts/
32191+
│ ├── interfaces/
32192+
│ │ ├── IERC20.sol
32193+
│ │ ├── IUniswapV2Callee.sol
32194+
│ │ ├── IUniswapV2ERC20.sol
32195+
│ │ ├── IUniswapV2Factory.sol
32196+
│ │ └── IUniswapV2Pair.sol
32197+
│ ├── libraries/
32198+
│ │ ├── Math.sol
32199+
│ │ ├── SafeMath.sol
32200+
│ │ └── UQ112x112.sol
32201+
│ ├── test/
32202+
│ │ └── ERC20.sol
32203+
│ ├── UniswapV2ERC20.sol
32204+
│ ├── UniswapV2Factory.sol
32205+
│ └── UniswapV2Pair.sol
32206+
├── ignition/
32207+
├── scripts/
32208+
│ └── deploy.js
32209+
├── node_modules/
32210+
├── test/
32211+
│ ├── shared/
32212+
│ │ ├── fixtures.js
32213+
│ │ └── utilities.js
32214+
│ ├── UniswapV2ERC20.js
32215+
│ ├── UniswapV2Factory.js
32216+
│ └── UniswapV2Pair.js
32217+
├── .env.example
32218+
├── .gitignore
32219+
├── hardhat.config.js
32220+
├── package.json
32221+
└── README.md
32222+
```
32223+
32224+
## Test the Contracts
32225+
32226+
You can run the provided test suite to ensure the contracts are working as expected. The tests cover various scenarios, including creating pairs, adding liquidity, and executing swaps.
32227+
32228+
To test it locally, you can run the following commands:
32229+
32230+
1. Spawn a local node for testing:
32231+
32232+
```bash
32233+
npx hardhat node
32234+
```
32235+
32236+
This command will spawn a local Substrate node along with the ETH-RPC adapter. The node will be available at `ws://127.0.0.1:8000` and the ETH-RPC adapter at `http://localhost:8545`.
32237+
32238+
2. In a new terminal, run the tests:
32239+
32240+
```bash
32241+
npx hardhat test --network localNode
32242+
```
32243+
32244+
The result should look like this:
32245+
32246+
<div id="termynal" data-termynal>
32247+
<span data-ty="input"><span class="file-path"></span>npx hardhat test --network localNode</span>
32248+
<span data-ty>Compiling 12 Solidity files</span>
32249+
<span data-ty>Successfully compiled 12 Solidity files</span>
32250+
<span data-ty></span>
32251+
<span data-ty>UniswapV2ERC20</span>
32252+
<span data-ty> ✔ name, symbol, decimals, totalSupply, balanceOf, DOMAIN_SEPARATOR, PERMIT_TYPEHASH (44ms)</span>
32253+
<span data-ty> ✔ approve (5128ms)</span>
32254+
<span data-ty> ✔ transfer (5133ms)</span>
32255+
<span data-ty> ✔ transfer:fail</span>
32256+
<span data-ty> ✔ transferFrom (6270ms)</span>
32257+
<span data-ty> ✔ transferFrom:max (6306ms)</span>
32258+
<span data-ty></span>
32259+
<span data-ty>UniswapV2Factory</span>
32260+
<span data-ty> ✔ feeTo, feeToSetter, allPairsLength</span>
32261+
<span data-ty> ✔ createPair (176ms)</span>
32262+
<span data-ty> ✔ createPair:reverse (1224ms)</span>
32263+
<span data-ty> ✔ setFeeTo (1138ms)</span>
32264+
<span data-ty> ✔ setFeeToSetter (1125ms)</span>
32265+
<span data-ty></span>
32266+
<span data-ty>UniswapV2Pair</span>
32267+
<span data-ty> ✔ mint (11425ms)</span>
32268+
<span data-ty> ✔ getInputPrice:0 (12590ms)</span>
32269+
<span data-ty> ✔ getInputPrice:1 (17600ms)</span>
32270+
<span data-ty> ✔ getInputPrice:2 (17618ms)</span>
32271+
<span data-ty> ✔ getInputPrice:3 (17704ms)</span>
32272+
<span data-ty> ✔ getInputPrice:4 (17649ms)</span>
32273+
<span data-ty> ✔ getInputPrice:5 (17594ms)</span>
32274+
<span data-ty> ✔ getInputPrice:6 (13643ms)</span>
32275+
<span data-ty> ✔ optimistic:0 (17647ms)</span>
32276+
<span data-ty> ✔ optimistic:1 (17946ms)</span>
32277+
<span data-ty> ✔ optimistic:2 (17657ms)</span>
32278+
<span data-ty> ✔ optimistic:3 (21625ms)</span>
32279+
<span data-ty> ✔ swap:token0 (12665ms)</span>
32280+
<span data-ty> ✔ swap:token1 (17631ms)</span>
32281+
<span data-ty> ✔ burn (17690ms)</span>
32282+
<span data-ty> ✔ feeTo:off (23900ms)</span>
32283+
<span data-ty> ✔ feeTo:on (24991ms)</span>
32284+
<span data-ty></span>
32285+
<span data-ty>28 passing (12m)</span>
32286+
</div>
32287+
32288+
## Deploy the Contracts
32289+
32290+
After successfully testing the contracts, you can deploy them to the local node or Polkadot Hub. The deployment script is located in the `scripts` directory and is named `deploy.js`. This script deploys the `Factory` and `Pair` contracts to the network.
32291+
32292+
To deploy the contracts, run the following command:
32293+
32294+
```bash
32295+
npx hardhat run scripts/deploy.js --network localNode
32296+
```
32297+
32298+
This command deploys the contracts to your local blockchain for development and testing. If you want to deploy to Polkadot Hub, you can use the following command:
32299+
32300+
```bash
32301+
npx hardhat run scripts/deploy.js --network westendHub
32302+
```
32303+
32304+
The command above deploys to the actual Polkadot TestNet. It requires WND test tokens, persists on the network, and operates under real network conditions.
32305+
32306+
The deployment script will output the addresses of the deployed contracts. Save these addresses, as you will need them to interact with the contracts. For example, the output should look like this:
32307+
32308+
<div id="termynal" data-termynal>
32309+
<span data-ty="input"><span class="file-path"></span>npx hardhat run scripts/deploy.js --network localNode</span>
32310+
<span data-ty>Successfully compiled 12 Solidity files</span>
32311+
<span data-ty>Deploying contracts using 0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac</span>
32312+
<span data-ty>Deploying UniswapV2ERC20...</span>
32313+
<span data-ty>ETH deployed to : 0x7acc1aC65892CF3547b1b0590066FB93199b430D</span>
32314+
<span data-ty>Deploying UniswapV2Factory...</span>
32315+
<span data-ty>Factory deployed to : 0x85b108660f47caDfAB9e0503104C08C1c96e0DA9</span>
32316+
<span data-ty>Deploying UniswapV2Pair with JsonRpcProvider workaround...</span>
32317+
<span data-ty>Pair deployed to : 0xF0e46847c8bFD122C4b5EEE1D4494FF7C5FC5104</span>
32318+
</div>
32319+
32320+
## Conclusion
32321+
32322+
This tutorial guided you through deploying Uniswap V2 contracts to Polkadot Hub. This implementation brings the powerful AMM architecture to the Polkadot ecosystem, laying the foundation for the decentralized trading of ERC-20 token pairs.
32323+
32324+
By following this guide, you've gained practical experience with:
32325+
32326+
- Setting up a Hardhat project for deploying to Polkadot Hub
32327+
- Understanding the Uniswap V2 architecture
32328+
- Testing Uniswap V2 contracts in a local environment
32329+
- Deploying contracts to both local and testnet environments
32330+
32331+
To build on this foundation, you could extend this project by implementing functionality to create liquidity pools, execute token swaps, and build a user interface for interacting with your deployment.
32332+
32333+
This knowledge can be leveraged to build more complex DeFi applications or to integrate Uniswap V2 functionality into your existing projects on Polkadot.
32334+
--- END CONTENT ---
32335+
32336+
Doc-Content: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/
32337+
--- BEGIN CONTENT ---
32338+
---
32339+
title: Demo Applications
32340+
description: Explore working demo applications that can be deployed to Polkadot Hub, showcasing common use cases and integration patterns.
32341+
template: index-page.html
32342+
---
32343+
32344+
# Demo Applications
32345+
32346+
This section highlights demo applications that can be deployed to Polkadot Hub. These examples illustrate practical use cases and provide guidance for developers looking to launch and test applications within the Polkadot ecosystem.
32347+
32348+
## In This Section
32349+
32350+
:::INSERT_IN_THIS_SECTION:::
32351+
--- END CONTENT ---
32352+
3208632353
Doc-Content: https://docs.polkadot.com/tutorials/smart-contracts/deploy-erc20/
3208732354
--- BEGIN CONTENT ---
3208832355
---

tutorials/smart-contracts/.pages

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ nav:
44
- 'Deploy an NFT': deploy-nft.md
55
- 'Deploy an ERC-20': deploy-erc20.md
66
- launch-your-first-project
7+
- demo-aplications
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
title: Demo Applications
2+
nav:
3+
- index.md
4+
- 'Deploy Uniswap V2': deploying-uniswap-v2.md

0 commit comments

Comments
 (0)