Skip to content

Add Uniswap V2 tutorial to SC tutorials #541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: staging-ah-smart-contracts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>npx hardhat compile</span>
<span data-ty>Compiling 12 Solidity files</span>
<span data-ty>Successfully compiled 12 Solidity files</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>npx hardhat run scripts/deploy.js --network localNode</span>
<span data-ty>Successfully compiled 12 Solidity files</span>
<span data-ty>Deploying contracts using 0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac</span>
<span data-ty>Deploying UniswapV2ERC20...</span>
<span data-ty>ETH deployed to : 0x7acc1aC65892CF3547b1b0590066FB93199b430D</span>
<span data-ty>Deploying UniswapV2Factory...</span>
<span data-ty>Factory deployed to : 0x85b108660f47caDfAB9e0503104C08C1c96e0DA9</span>
<span data-ty>Deploying UniswapV2Pair with JsonRpcProvider workaround...</span>
<span data-ty>Pair deployed to : 0xF0e46847c8bFD122C4b5EEE1D4494FF7C5FC5104</span>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>npx hardhat test --network localNode</span>
<span data-ty>Compiling 12 Solidity files</span>
<span data-ty>Successfully compiled 12 Solidity files</span>
<span data-ty></span>
<span data-ty>UniswapV2ERC20</span>
<span data-ty> ✔ name, symbol, decimals, totalSupply, balanceOf, DOMAIN_SEPARATOR, PERMIT_TYPEHASH (44ms)</span>
<span data-ty> ✔ approve (5128ms)</span>
<span data-ty> ✔ transfer (5133ms)</span>
<span data-ty> ✔ transfer:fail</span>
<span data-ty> ✔ transferFrom (6270ms)</span>
<span data-ty> ✔ transferFrom:max (6306ms)</span>
<span data-ty></span>
<span data-ty>UniswapV2Factory</span>
<span data-ty> ✔ feeTo, feeToSetter, allPairsLength</span>
<span data-ty> ✔ createPair (176ms)</span>
<span data-ty> ✔ createPair:reverse (1224ms)</span>
<span data-ty> ✔ setFeeTo (1138ms)</span>
<span data-ty> ✔ setFeeToSetter (1125ms)</span>
<span data-ty></span>
<span data-ty>UniswapV2Pair</span>
<span data-ty> ✔ mint (11425ms)</span>
<span data-ty> ✔ getInputPrice:0 (12590ms)</span>
<span data-ty> ✔ getInputPrice:1 (17600ms)</span>
<span data-ty> ✔ getInputPrice:2 (17618ms)</span>
<span data-ty> ✔ getInputPrice:3 (17704ms)</span>
<span data-ty> ✔ getInputPrice:4 (17649ms)</span>
<span data-ty> ✔ getInputPrice:5 (17594ms)</span>
<span data-ty> ✔ getInputPrice:6 (13643ms)</span>
<span data-ty> ✔ optimistic:0 (17647ms)</span>
<span data-ty> ✔ optimistic:1 (17946ms)</span>
<span data-ty> ✔ optimistic:2 (17657ms)</span>
<span data-ty> ✔ optimistic:3 (21625ms)</span>
<span data-ty> ✔ swap:token0 (12665ms)</span>
<span data-ty> ✔ swap:token1 (17631ms)</span>
<span data-ty> ✔ burn (17690ms)</span>
<span data-ty> ✔ feeTo:off (23900ms)</span>
<span data-ty> ✔ feeTo:on (24991ms)</span>
<span data-ty></span>
<span data-ty>28 passing (12m)</span>
</div>
274 changes: 271 additions & 3 deletions llms.txt
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/system-chains/
Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/testing/fork-live-chains/
Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/testing/
Doc-Page: https://docs.polkadot.com/tutorials/polkadot-sdk/testing/spawn-basic-chain/
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/deploying-uniswap-v2/
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/deploy-erc20/
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/deploy-nft/
Doc-Page: https://docs.polkadot.com/tutorials/smart-contracts/
Expand Down Expand Up @@ -6492,8 +6494,8 @@ module.exports = {

Since you compiled these from source using Rust's Cargo build system, you can find them at:

- Substrate node path - `polkadot-sdk/target/release/substrate-node`
- ETH-RPC adapter path - `polkadot-sdk/target/release/eth-rpc`
- **Substrate node path** - `polkadot-sdk/target/release/substrate-node`
- **ETH-RPC adapter path** - `polkadot-sdk/target/release/eth-rpc`

For example, if you cloned the polkadot-sdk repository to your home directory, the paths might look like:

Expand Down Expand Up @@ -16251,7 +16253,7 @@ In this unit file, you will write the commands that you want to run on server bo
[Unit]
Description=Polkadot Node
After=network.target
Documentation=https://github.com/paritytech/polkadot
Documentation=https://github.com/paritytech/polkadot-sdk

[Service]
EnvironmentFile=-/etc/default/polkadot
Expand Down Expand Up @@ -31957,6 +31959,272 @@ tail -f /tmp/zombie-794af21178672e1ff32c612c3c7408dc_-2397036-6717MXDxcS55/alic
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.
--- END CONTENT ---

Doc-Content: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/deploying-uniswap-v2/
--- BEGIN CONTENT ---
---
title: Deploying Uniswap V2
description: Learn how to write a basic smart contract using just a text editor. This guide covers creating and preparing a contract for deployment on Polkadot Hub.
---

# Deploying Uniswap V2

## Introduction

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. This tutorial will guide you through how Uniswap V2 works, so you can take advantage of it in your projects deployed to the 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.

## Prerequisites

Before starting, make sure you have:

- Node.js (v16.0.0 or later) and npm installed
- Basic understanding of Solidity and JavaScript
- Familiarity with [`hardhat-polkadot`](/develop/smart-contracts/dev-environments/hardhat){target=\_blank} development environment
- Some WND test tokens to cover transaction fees (obtained from the [Polkadot faucet](https://faucet.polkadot.io/westend){target=\_blank})
- Basic understanding of how AMMs and liquidity pools work

## Setting Up the Project

Let's start by cloning the Uniswap V2 project:

1. Clone the Uniswap V2 repository:

```
git clone https://github.com/papermoonio/uniswap-V2-polkadot.git
cd uniswap-V2-polkadot
```

2. Install the required dependencies:

```bash
npm install
```

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

```js title="hardhat.config.js"
hardhat: {
polkavm: true,
nodeConfig: {
nodeBinaryPath: "../bin/substrate-node",
rpcPort: 8000,
dev: true,
},
adapterConfig: {
adapterBinaryPath: "../bin/eth-rpc",
dev: true,
},
},
```

Ensure that the paths for `nodeBinaryPath` and `adapterBinaryPath` point to the correct locations of your substrate node and eth-rpc adapter binaries.

4. Create a `.env` file in your project root to store your private keys (you can use as an example the `env.example` file):

```text title=".env"
LOCAL_PRIV_KEY="INSERT_LOCAL_PRIVATE_KEY"
AH_PRIV_KEY="INSERT_AH_PRIVATE_KEY"
```

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 private key of the account you want to use for deploying the contracts. You can get this by exporting the private key from your wallet (e.g., MetaMask).

!!!warning
Keep your private key safe, and never share it with anyone. If it is compromised, your funds can be stolen.

5. Compile the contracts:

```bash
npx hardhat compile
```

If the compilation is successful, you should see the following output:

<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>npx hardhat compile</span>
<span data-ty>Compiling 12 Solidity files</span>
<span data-ty>Successfully compiled 12 Solidity files</span>
</div>

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.

## Understanding Uniswap V2 Architecture

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 the principles of automated market making, token pair liquidity pools, and deterministic pricing.

At the heart of Uniswap V2 lies a simple but powerful system composed of three major smart contracts:

- **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
- **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

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.

The project scaffolding is as follows:

```bash
uniswap-V2-polkadot
├── bin/
├── contracts/
│ ├── interfaces/
│ │ ├── IERC20.sol
│ │ ├── IUniswapV2Callee.sol
│ │ ├── IUniswapV2ERC20.sol
│ │ ├── IUniswapV2Factory.sol
│ │ └── IUniswapV2Pair.sol
│ ├── libraries/
│ │ ├── Math.sol
│ │ ├── SafeMath.sol
│ │ └── UQ112x112.sol
│ ├── test/
│ │ └── ERC20.sol
│ ├── UniswapV2ERC20.sol
│ ├── UniswapV2Factory.sol
│ └── UniswapV2Pair.sol
├── ignition/
├── scripts/
│ └── deploy.js
├── node_modules/
├── test/
│ ├── shared/
│ │ ├── fixtures.js
│ │ └── utilities.js
│ ├── UniswapV2ERC20.js
│ ├── UniswapV2Factory.js
│ └── UniswapV2Pair.js
├── .env.example
├── .gitignore
├── hardhat.config.js
├── package.json
└── README.md
```

## Testing the Contracts

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.

To test it locally, you can run the following commands:

1. Spawn a local node for testing:

```bash
npx hardhat node
```

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`.

2. In a new terminal, run the tests:

```bash
npx hardhat test --network localNode
```

The result should look like this:

<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>npx hardhat test --network localNode</span>
<span data-ty>Compiling 12 Solidity files</span>
<span data-ty>Successfully compiled 12 Solidity files</span>
<span data-ty></span>
<span data-ty>UniswapV2ERC20</span>
<span data-ty> ✔ name, symbol, decimals, totalSupply, balanceOf, DOMAIN_SEPARATOR, PERMIT_TYPEHASH (44ms)</span>
<span data-ty> ✔ approve (5128ms)</span>
<span data-ty> ✔ transfer (5133ms)</span>
<span data-ty> ✔ transfer:fail</span>
<span data-ty> ✔ transferFrom (6270ms)</span>
<span data-ty> ✔ transferFrom:max (6306ms)</span>
<span data-ty></span>
<span data-ty>UniswapV2Factory</span>
<span data-ty> ✔ feeTo, feeToSetter, allPairsLength</span>
<span data-ty> ✔ createPair (176ms)</span>
<span data-ty> ✔ createPair:reverse (1224ms)</span>
<span data-ty> ✔ setFeeTo (1138ms)</span>
<span data-ty> ✔ setFeeToSetter (1125ms)</span>
<span data-ty></span>
<span data-ty>UniswapV2Pair</span>
<span data-ty> ✔ mint (11425ms)</span>
<span data-ty> ✔ getInputPrice:0 (12590ms)</span>
<span data-ty> ✔ getInputPrice:1 (17600ms)</span>
<span data-ty> ✔ getInputPrice:2 (17618ms)</span>
<span data-ty> ✔ getInputPrice:3 (17704ms)</span>
<span data-ty> ✔ getInputPrice:4 (17649ms)</span>
<span data-ty> ✔ getInputPrice:5 (17594ms)</span>
<span data-ty> ✔ getInputPrice:6 (13643ms)</span>
<span data-ty> ✔ optimistic:0 (17647ms)</span>
<span data-ty> ✔ optimistic:1 (17946ms)</span>
<span data-ty> ✔ optimistic:2 (17657ms)</span>
<span data-ty> ✔ optimistic:3 (21625ms)</span>
<span data-ty> ✔ swap:token0 (12665ms)</span>
<span data-ty> ✔ swap:token1 (17631ms)</span>
<span data-ty> ✔ burn (17690ms)</span>
<span data-ty> ✔ feeTo:off (23900ms)</span>
<span data-ty> ✔ feeTo:on (24991ms)</span>
<span data-ty></span>
<span data-ty>28 passing (12m)</span>
</div>

## Deploying the Contracts

After successfully testing the contracts, you can deploy them to the local node or the 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.

To deploy the contracts, run the following command:

```bash
npx hardhat run scripts/deploy.js --network localNode
```

This command deploys to your local blockchain for development and testing. If you want to deploy to the Polkadot Hub, you can use the following command:

```bash
npx hardhat run scripts/deploy.js --network westendHub
```

Commnad above deploys to the actual Polkadot TestNet. Requires WND test tokens, persists on the network, and operates under real network conditions.

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:

<div id="termynal" data-termynal>
<span data-ty="input"><span class="file-path"></span>npx hardhat run scripts/deploy.js --network localNode</span>
<span data-ty>Successfully compiled 12 Solidity files</span>
<span data-ty>Deploying contracts using 0xf24FF3a9CF04c71Dbc94D0b566f7A27B94566cac</span>
<span data-ty>Deploying UniswapV2ERC20...</span>
<span data-ty>ETH deployed to : 0x7acc1aC65892CF3547b1b0590066FB93199b430D</span>
<span data-ty>Deploying UniswapV2Factory...</span>
<span data-ty>Factory deployed to : 0x85b108660f47caDfAB9e0503104C08C1c96e0DA9</span>
<span data-ty>Deploying UniswapV2Pair with JsonRpcProvider workaround...</span>
<span data-ty>Pair deployed to : 0xF0e46847c8bFD122C4b5EEE1D4494FF7C5FC5104</span>
</div>

## Conclusion

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 decentralized trading of ERC-20 token pairs.

By following this guide, you've gained practical experience with:

- Setting up a Hardhat project for deploying to Polkadot Hub
- Understanding the Uniswap V2 architecture
- Testing Uniswap V2 contracts in a local environment
- Deploying contracts to both local and testnet environments

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.

This knowledge can be leveraged to build more complex DeFi applications or to integrate Uniswap V2 functionality into your existing projects on Polkadot.
--- END CONTENT ---

Doc-Content: https://docs.polkadot.com/tutorials/smart-contracts/demo-aplications/
--- BEGIN CONTENT ---
---
title: Demo Applications
description: Explore working demo applications that can be deployed to the Polkadot Hub, showcasing common use cases and integration patterns.
template: index-page.html
---

# Demo Applications

This section highlights demo applications that can be deployed to the Polkadot Hub. These examples illustrate practical use cases and provide guidance for developers looking to launch and test applications within the Polkadot ecosystem.

## In This Section

:::INSERT_IN_THIS_SECTION:::
--- END CONTENT ---

Doc-Content: https://docs.polkadot.com/tutorials/smart-contracts/deploy-erc20/
--- BEGIN CONTENT ---
---
Expand Down
1 change: 1 addition & 0 deletions tutorials/smart-contracts/.pages
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ nav:
- 'Deploy an NFT': deploy-nft.md
- 'Deploy an ERC-20': deploy-erc20.md
- launch-your-first-project
- demo-aplications
4 changes: 4 additions & 0 deletions tutorials/smart-contracts/demo-aplications/.pages
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
title: Demo Applications
nav:
- index.md
- deploying-uniswap-v2.md
Loading