Skip to content
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
55 changes: 55 additions & 0 deletions v3-sdk/quoting-v2/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"env": {
"browser": true,
"es2021": true
},
"root": true,
"extends": [
"plugin:react/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:react-hooks/recommended",
"plugin:prettier/recommended"
],
"overrides": [],
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": ["typescript-sort-keys"],
"rules": {
"prettier/prettier": [
"error",
{
"semi": false,
"singleQuote": true,
"printWidth": 80,
"bracketSameLine": true
}
],
"typescript-sort-keys/string-enum": [
"error",
"asc",
{
"caseSensitive": true
}
],
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/naming-convention": [
"warn",
{
"selector": "enum",
"format": ["StrictPascalCase"]
}
],
"curly": ["warn", "all"],
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": [
"error",
{
"argsIgnorePattern": "_",
"varsIgnorePattern": "_",
"caughtErrorsIgnorePattern": "_"
}
]
}
}
23 changes: 23 additions & 0 deletions v3-sdk/quoting-v2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
6 changes: 6 additions & 0 deletions v3-sdk/quoting-v2/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"semi": false,
"singleQuote": true,
"printWidth": 80,
"bracketSameLine": true
}
1 change: 1 addition & 0 deletions v3-sdk/quoting-v2/.yarnrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--frozen-lockfile true
34 changes: 34 additions & 0 deletions v3-sdk/quoting-v2/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Quoting V2

## Overview

This is an example of getting a quote using the Uniswap V3 **Quote V2** and includes running against mainnet or a locally deployed chain.

The core functionality of this example can be found in [`quote-v2.ts`](./src/libs/quote-v2.ts).

## Configuration

This application is a read only quoting application and can be configured to interact with:

1. A locally deployed mainnet fork
2. The mainnet

To configure local or mainnet, the input token/amount and output token, edit the [configuration](./src/config.ts) file. The code should need no further modification to function.

Runs in Ethereum by defaul, you can use a diffent networks adapting the [PoolFactory and QuoterV2 addressess](./src/libs/constants.ts) as explained [here](https://docs.uniswap.org/contracts/v3/reference/deployments/).

### Get a mainnet RPC URL

1. Create aun API key using any of the [Ethereum API providers](https://docs.ethers.io/v5/api/providers/) and grab the respective RPC URL, eg `https://mainnet.infura.io/v3/0ac57a06f2994538829c14745750d721`
2. Set that as the value of the `mainnet` `rpc` vale inside the [config](./src/config.ts).

## Setup

### Install dependencies

1. Run `yarn install` to install the project dependencies
2. Run `yarn install:chain` to download and install Foundry

### Start the web interface

Run `yarn start` and navigate to [http://localhost:3000/](http://localhost:3000/)
59 changes: 59 additions & 0 deletions v3-sdk/quoting-v2/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"name": "quoting",
"version": "0.1.0",
"private": true,
"dependencies": {
"@types/node": "^16.7.13",
"@types/react": "^18.0.0",
"@types/react-dom": "^18.0.0",
"@uniswap/sdk-core": "^3.1.0",
"@uniswap/v3-sdk": "^3.9.0",
"ethers": "^5.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"typescript": "^4.4.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"lint": "yarn eslint .",
"install:chain": "curl -L https://foundry.paradigm.xyz | bash && clear && echo $0 | exec && foundryup",
"start:chain": "anvil --chain-id 1337 --fork-url"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"eslint": "^8.29.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.31.11",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-simple-import-sort": "^8.0.0",
"eslint-plugin-typescript-sort-keys": "^2.1.0",
"eslint-plugin-unused-imports": "^2.0.0",
"prettier": "^2.8.0"
},
"engines": {
"node": ">=16.0.0"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
42 changes: 42 additions & 0 deletions v3-sdk/quoting-v2/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.

Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.

You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.

To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>
32 changes: 32 additions & 0 deletions v3-sdk/quoting-v2/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Token } from '@uniswap/sdk-core'
import { FeeAmount } from '@uniswap/v3-sdk'
import { USDC_TOKEN, WETH_TOKEN } from './libs/constants'

// Inputs that configure this example to run
export interface ExampleConfig {
rpc: {
local: string
mainnet: string
}
tokens: {
in: Token
amountIn: number
out: Token
poolFee: number
}
}

// Example Configuration

export const CurrentConfig: ExampleConfig = {
rpc: {
local: 'http://localhost:8545',
mainnet: '',
},
tokens: {
in: USDC_TOKEN,
amountIn: 1000,
out: WETH_TOKEN,
poolFee: FeeAmount.MEDIUM,
},
}
15 changes: 15 additions & 0 deletions v3-sdk/quoting-v2/src/example/Example.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.App {
text-align: center;
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.error {
color: red;
}
29 changes: 29 additions & 0 deletions v3-sdk/quoting-v2/src/example/Example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import React, { useState, useCallback } from 'react'
import './Example.css'
import { CurrentConfig } from '../config'
import { quote } from '../libs/quote-v2'

const Example = () => {
const [outputAmount, setOutputAmount] = useState<string>()

const onQuote = useCallback(async () => {
setOutputAmount(await quote())
}, [])

return (
<div className="App">
{CurrentConfig.rpc.local === '' && CurrentConfig.rpc.mainnet === '' && (
<h2 className="error">
Please set your local or mainnet RPC URL in config.ts
</h2>
)}
<h3>{`Quote input amount: ${CurrentConfig.tokens.amountIn} ${CurrentConfig.tokens.in.symbol}`}</h3>
<h3>{`Quote output amount: ${outputAmount} ${CurrentConfig.tokens.out.symbol}`}</h3>
<button onClick={onQuote}>
<p>Quote</p>
</button>
</div>
)
}

export default Example
13 changes: 13 additions & 0 deletions v3-sdk/quoting-v2/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}

code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}
11 changes: 11 additions & 0 deletions v3-sdk/quoting-v2/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import './index.css'
import Example from './example/Example'

const root = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)
root.render(
<React.StrictMode>
<Example />
</React.StrictMode>
)
31 changes: 31 additions & 0 deletions v3-sdk/quoting-v2/src/libs/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// This file stores web3 related constants such as addresses, token definitions, ETH currency references and ABI's

import { SupportedChainId, Token } from '@uniswap/sdk-core'

// Addresses

export const POOL_FACTORY_CONTRACT_ADDRESS =
'0x1F98431c8aD98523631AE4a59f267346ea31F984'
export const QUOTERV2_CONTRACT_ADDRESS =
'0x61fFE014bA17989E743c5F6cB21bF9697530B21e'

export const QUOTER_CONTRACT_ADDRESS =
'0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6'

// Currencies and Tokens

export const WETH_TOKEN = new Token(
SupportedChainId.MAINNET,
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
18,
'WETH',
'Wrapped Ether'
)

export const USDC_TOKEN = new Token(
SupportedChainId.MAINNET,
'0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48',
6,
'USDC',
'USD//C'
)
16 changes: 16 additions & 0 deletions v3-sdk/quoting-v2/src/libs/conversion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { BigNumber, ethers } from 'ethers'

const READABLE_FORM_LEN = 4

export function fromReadableAmount(
amount: number,
decimals: number
): BigNumber {
return ethers.utils.parseUnits(amount.toString(), decimals)
}

export function toReadableAmount(rawAmount: number, decimals: number): string {
return ethers.utils
.formatUnits(rawAmount, decimals)
.slice(0, READABLE_FORM_LEN)
}
10 changes: 10 additions & 0 deletions v3-sdk/quoting-v2/src/libs/providers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ethers, providers } from 'ethers'
import { CurrentConfig } from '../config'

// Provider Functions

export function getProvider(): providers.Provider {
return new ethers.providers.JsonRpcProvider(
CurrentConfig.rpc.local || CurrentConfig.rpc.mainnet || ''
)
}
Loading