Skip to content

Commit

Permalink
Merge pull request #16 from uniswapfoundation/feat/range-order-updated
Browse files Browse the repository at this point in the history
Update range order example for new sdk
  • Loading branch information
Florian-S-A-W authored Dec 7, 2023
2 parents 544984e + 3a24272 commit 033bda9
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 1,100 deletions.
6 changes: 3 additions & 3 deletions v3-sdk/range-order/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"@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",
"@uniswap/sdk-core": "npm:@koraykoska/uniswap-sdk-core@^6.0.9",
"@uniswap/v3-sdk": "npm:@florian-s-a-w/[email protected]",
"ethers": "^5.7.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand All @@ -19,7 +19,7 @@
"build": "react-scripts build",
"lint": "yarn eslint .",
"install:chain": "curl -L https://foundry.paradigm.xyz | bash && clear && echo $0 | exec && foundryup",
"start:chain": "anvil --fork-url https://mainnet.infura.io/v3/0ac57a06f2994538829c14745750d721 --block-time 6"
"start:chain": "anvil --fork-url https://mainnet.infura.io/v3/0ac57a06f2994538829c14745750d721"
},
"eslintConfig": {
"extends": [
Expand Down
8 changes: 2 additions & 6 deletions v3-sdk/range-order/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,13 @@ import { USDT_TOKEN, WETH_TOKEN } from './libs/constants'
// Sets if the example should run locally or on chain
export enum Environment {
LOCAL,
WALLET_EXTENSION,
MAINNET,
}

// Inputs that configure this example to run
export interface ExampleConfig {
env: Environment
rpc: {
local: string
mainnet: string
}
wallet: {
address: string
Expand Down Expand Up @@ -47,7 +44,6 @@ export const CurrentConfig: ExampleConfig = {
env: Environment.LOCAL,
rpc: {
local: 'http://localhost:8545',
mainnet: 'https://mainnet.infura.io/v3/0ac57a06f2994538829c14745750d721',
},
wallet: {
address: '0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266',
Expand All @@ -68,8 +64,8 @@ export const CurrentConfig: ExampleConfig = {
'0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d',
},
mockMarketMakerPool: {
buyAmount: 250,
sellAmount: 250,
buyAmount: 1,
sellAmount: 1,
token0: WETH_TOKEN,
token1: USDT_TOKEN,
poolFee: FeeAmount.LOW,
Expand Down
15 changes: 8 additions & 7 deletions v3-sdk/range-order/src/example/Example.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import React, { useCallback, useEffect, useState } from 'react'
import './Example.css'
import { Environment, CurrentConfig } from '../config'
import { getCurrencyBalance } from '../libs/balance'
import { getPositionInfo, PositionInfo } from '../libs/positions'
import { getPosition } from '../libs/positions'
import {
getProvider,
TransactionState,
getWalletAddress,
} from '../libs/providers'
import { WETH_TOKEN } from '../libs/constants'
import { wrapETH } from '../libs/wallet'
import { FeeAmount } from '@uniswap/v3-sdk'
import { FeeAmount, Position } from '@uniswap/v3-sdk'
import { Ether, Price, Token } from '@uniswap/sdk-core'
import { getPrice } from '../libs/pool'
import {
Expand Down Expand Up @@ -40,7 +40,7 @@ const Example = () => {
const [mmmBalance1, setMMMBalance1] = useState<string>()
const [mmmBalance0, setMMMBalance0] = useState<string>()
const [positionId, setPositionId] = useState<number>()
const [positionInfo, setPositionInfo] = useState<PositionInfo>()
const [position, setPosition] = useState<Position>()
const [rangeOrder, setRangeOrder] = useState<TakeProfitOrder>()
const [txState, setTxState] = useState<TransactionState>(TransactionState.New)
const [blockNumber, setBlockNumber] = useState<number>(0)
Expand Down Expand Up @@ -94,7 +94,7 @@ const Example = () => {
const refreshPosition = useCallback(async () => {
// Set Position Info
if (positionId !== undefined) {
setPositionInfo(await getPositionInfo(positionId))
setPosition(await getPosition(positionId))
}
}, [positionId])

Expand All @@ -114,6 +114,7 @@ const Example = () => {
const provider = getProvider()
const address = CurrentConfig.mockMarketMakerWallet.address
if (!provider || !address) {
console.log('Couldnt find provider or address')
return
}

Expand Down Expand Up @@ -152,7 +153,7 @@ const Example = () => {
} else {
setRangeOrder(order)
setPositionId(orderId)
setPositionInfo(await getPositionInfo(orderId))
setPosition(await getPosition(orderId))
setTxState(TransactionState.Sent)
}
}, [price, token0Balance])
Expand Down Expand Up @@ -235,11 +236,11 @@ const Example = () => {
<div className="positions">
<h2>Range Orders:</h2>
{positionId === undefined && <p>No active order</p>}
{positionId && positionInfo && rangeOrder && (
{positionId && position && rangeOrder && (
<div>
Active Order:
<p>
{positionId}: {positionInfo.liquidity.toString()} liquidity
{positionId}: {position._liquidity.toString()} liquidity
</p>
<p>
Target price: 1 {rangeOrder.position.amount0.currency.symbol}{' '}
Expand Down
12 changes: 4 additions & 8 deletions v3-sdk/range-order/src/libs/constants.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
// This file stores web3 related constants such as addresses, token definitions, ETH currency references and ABI's

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

// Addresses

export const POOL_FACTORY_CONTRACT_ADDRESS =
'0x1F98431c8aD98523631AE4a59f267346ea31F984'
export const NONFUNGIBLE_POSITION_MANAGER_CONTRACT_ADDRESS =
'0xC36442b4a4522E871399CD717aBDD847Ab11FE88'
export const V3_SWAP_ROUTER_ADDRESS =
'0xE592427A0AEce92De3Edee1F18E0157C05861564'
export const WETH_CONTRACT_ADDRESS =
'0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2'
export const QUOTER_CONTRACT_ADDRESS =
'0x61fFE014bA17989E743c5F6cB21bF9697530B21e'

// Currencies and Tokens
export const ETH = Ether.onChain(SupportedChainId.MAINNET)
export const ETH = Ether.onChain(ChainId.MAINNET)

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

export const USDT_TOKEN = new Token(
SupportedChainId.MAINNET,
ChainId.MAINNET,
'0xdAC17F958D2ee523a2206206994597C13D831ec7',
6,
'USDT',
Expand Down
17 changes: 4 additions & 13 deletions v3-sdk/range-order/src/libs/conversion.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,13 @@
import JSBI from 'jsbi'

export function fromReadableAmount(amount: number, decimals: number): JSBI {
export function fromReadableAmount(amount: number, decimals: number): bigint {
const extraDigits = Math.pow(10, countDecimals(amount))
const adjustedAmount = amount * extraDigits
return JSBI.divide(
JSBI.multiply(
JSBI.BigInt(adjustedAmount),
JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(decimals))
),
JSBI.BigInt(extraDigits)
return (
(BigInt(adjustedAmount) * 10n ** BigInt(decimals)) / BigInt(extraDigits)
)
}

export function toReadableAmount(rawAmount: number, decimals: number): string {
return JSBI.divide(
JSBI.BigInt(rawAmount),
JSBI.exponentiate(JSBI.BigInt(10), JSBI.BigInt(decimals))
).toString()
return (BigInt(rawAmount) / 10n ** BigInt(decimals)).toString()
}

function countDecimals(x: number) {
Expand Down
Loading

0 comments on commit 033bda9

Please sign in to comment.