From 0e889ba6492ca1901a73f1f607f2174331afa685 Mon Sep 17 00:00:00 2001 From: Florian Winkler Date: Wed, 27 Dec 2023 17:18:39 +0100 Subject: [PATCH] Name parameters and import packages from uniswapfoundation in v3 sdk guides --- docs/sdk/v3/guides/02-local-development.md | 4 +- .../sdk/v3/guides/advanced/03-price-oracle.md | 30 ++++---- .../sdk/v3/guides/advanced/04-range-orders.md | 20 +++--- .../guides/liquidity/02-minting-position.md | 72 +++++++++---------- .../guides/liquidity/03-fetching-positions.md | 6 +- .../guides/liquidity/04-modifying-position.md | 12 ++-- .../v3/guides/liquidity/05-collecting-fees.md | 4 +- .../liquidity/06-swap-and-add-liquidity.md | 50 ++++++------- docs/sdk/v3/guides/swaps/01-quoting.md | 22 +++--- docs/sdk/v3/guides/swaps/02-trading.md | 40 +++++------ .../v3/guides/swaps/03-simulate-offchain.md | 31 ++++---- docs/sdk/v3/guides/swaps/04-routing.md | 20 +++--- 12 files changed, 152 insertions(+), 159 deletions(-) diff --git a/docs/sdk/v3/guides/02-local-development.md b/docs/sdk/v3/guides/02-local-development.md index fd1808604..84958bff2 100644 --- a/docs/sdk/v3/guides/02-local-development.md +++ b/docs/sdk/v3/guides/02-local-development.md @@ -33,8 +33,8 @@ one of the network you want to use. For this guide, the following packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) - [`ethers@5`](https://www.npmjs.com/package/ethers) Please note that we use ethers version 5, as this is still the most commonly used version of ethers.js. diff --git a/docs/sdk/v3/guides/advanced/03-price-oracle.md b/docs/sdk/v3/guides/advanced/03-price-oracle.md index b7c41201d..fe839eb36 100644 --- a/docs/sdk/v3/guides/advanced/03-price-oracle.md +++ b/docs/sdk/v3/guides/advanced/03-price-oracle.md @@ -26,7 +26,7 @@ Before diving into this guide, consider reading the theory behind using Uniswap For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) The core code of this guide can be found in [`oracle.ts`](https://github.com/Uniswap/examples/tree/main/v3-sdk/price-oracle/src/libs/oracle.ts) @@ -40,12 +40,12 @@ const provider = new ethers.providers.JsonRpcProvider( '...rpcUrl' ) -const pool = Pool.initFromChain( +const pool = await Pool.initFromChain({ provider, - CurrentConfig.pool.token0, - CurrentConfig.pool.token1, - CurrentConfig.pool.fee -) + tokenA: CurrentConfig.pool.token0, + tokenB: CurrentConfig.pool.token1, + fee: CurrentConfig.pool.fee, + }) ``` All V3 pools store observations of the current tick and the block timestamp. @@ -86,10 +86,10 @@ import { ethers } from 'ethers' let wallet = new ethers.Wallet('private_key', provider) const observationCardinalityNext = 10 -const txRes = await pool.increaseObservationCardinalityNext( - wallet, - observationCardinalityNext -) +const txRes = await pool.rpcIncreaseObservationCardinalityNext({ + signer: wallet, + observationCardinalityNext, + }) ``` The Pool will now fill the open Observation Slots. @@ -121,7 +121,7 @@ function observe( The sdk wraps this function in the `rpcObserve` function on our Pool object. ```typescript - const observeResponse = await pool.rpcObserve(timestamps) + const observeResponse = await pool.rpcObserve({ secondsAgo: timestamps }) ``` Let's create an interface to map our data to: @@ -168,15 +168,13 @@ We cannot directly use the value of a single Observation for anything meaningful const diffTickCumulative = observations[0].tickCumulative - observations[1].tickCumulative const secondsBetween = 108 -const averageTick = diffTickCumulative / secondsBetween +const averageTick = Number(diffTickCumulative / BigInt(secondsBetween)) ``` -Now that we know the average active Tick over the last 10 blocks, we can calculate the price with the `tickToPrice` function, which returns a [`Price`](../../../core/reference/classes/Price.md) Object. Check out the [Pool data](./02-pool-data.md) guide to understand how to construct a Pool Object and access its properties. We don't need the full Tick Data for this guide. +Now that we know the average active Tick over the last 10 blocks, we can calculate the price with the `tickToPrice` function, which returns a [`Price`](../../../core/reference/classes/Price.md) Object. ```typescript -import { tickToPrice, Pool } from '@uniswap/v3-sdk' - -const pool = new Pool(...) +import { tickToPrice } from '@uniswapfoundation/v3-sdk' const TWAP = tickToPrice(pool.token0, pool.token1, averageTick) ``` diff --git a/docs/sdk/v3/guides/advanced/04-range-orders.md b/docs/sdk/v3/guides/advanced/04-range-orders.md index fc61b44c4..c12ce4fb7 100644 --- a/docs/sdk/v3/guides/advanced/04-range-orders.md +++ b/docs/sdk/v3/guides/advanced/04-range-orders.md @@ -29,8 +29,8 @@ Before working through this guide, consider checking out the Range Orders [conce For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) The core code of this guide can be found in [`range-order.ts`](https://github.com/Uniswap/examples/tree/main/v3-sdk/range-order/src/libs/range-order.ts). @@ -85,7 +85,7 @@ We create a Pool that represents the V3 Pool we are interacting with and get the We won't need full tick data in this example. ```typescript -import { Pool } from '@uniswap/v3-sdk' +import { Pool } from '@uniswapfoundation/v3-sdk' const pool = new Pool(token0, token1, fee, sqrtPriceX96, liquidity, tickCurrent) @@ -95,7 +95,7 @@ const currentPrice = pool.token0Price Next we increase the `Price` by 5%. We create a new Price with a numerator 5% higher than our current Price: ```typescript -import { Price, Fraction } from '@uniswap/sdk-core' +import { Price, Fraction } from '@uniswapfoundation/sdk-core' const targetFraction = Price.asFraction.multiply(new Fraction(100 + 5, 100)) @@ -119,7 +119,7 @@ We use the `priceToClosestTick` function to find the closest tick to our targetP We then use the `nearestUsableTick` function to find the closest initializable Tick for the `tickSpacing` of the `Pool`. ```typescript -import {priceToClosestTick, nearestUsableTick} from '@uniswap/v3-sdk' +import {priceToClosestTick, nearestUsableTick} from '@uniswapfoundation/v3-sdk' let targetTick = nearestUsableTick( priceToClosestTick(targetPrice), @@ -163,7 +163,7 @@ If you are not familiar with liquidity Positions, check out the [liquidity posit We create a `Position` object with our ticks and the amount of tokens we want to deposit: ```typescript -import { Position } from '@uniswap/v3-sdk' +import { Position } from '@uniswapfoundation/v3-sdk' const position = Position.fromAmount0({ pool: pool, @@ -201,7 +201,7 @@ Once we have our approval, we create the calldata for the **Mint** call using th ```typescript import {MintOptions, NonfungiblePositionManager} -import { Percent } from '@uniswap/sdk-core' +import { Percent } from '@uniswapfoundation/sdk-core' const mintOptions: MintOptions = { recipient: wallet.address, @@ -331,7 +331,7 @@ We check if the tick has crossed our position, and if so we withdraw the Positio We use the NonfungiblePositionManager together with the `tokenId` to get all info of our position as we may have gotten fees from trades on the Pool: ```typescript -import { NonfungiblePositionManager } from '@uniswap/v3-sdk' +import { NonfungiblePositionManager } from '@uniswapfoundation/v3-sdk' const currentPosition = await NonfungiblePositionManager.fetchWithPositionId(getProvider(), tokenId) ``` @@ -341,8 +341,8 @@ We use the `NonfungiblePositionManager`, the `pool`, `positionInfo` and `tokenId We start with creating `CollectOptions`: ```typescript -import { Percent, CurrencyAmount } from '@uniswap/sdk-core' -import { CollectOptions, RemoveLiquidityOptions } from '@uniswap/v3-sdk' +import { Percent, CurrencyAmount } from '@uniswapfoundation/sdk-core' +import { CollectOptions, RemoveLiquidityOptions } from '@uniswapfoundation/v3-sdk' const collectOptions: Omit = { expectedCurrencyOwed0: CurrencyAmount.fromRawAmount( diff --git a/docs/sdk/v3/guides/liquidity/02-minting-position.md b/docs/sdk/v3/guides/liquidity/02-minting-position.md index 77a6e871e..fc5d50be1 100644 --- a/docs/sdk/v3/guides/liquidity/02-minting-position.md +++ b/docs/sdk/v3/guides/liquidity/02-minting-position.md @@ -13,7 +13,7 @@ To run this example, check out the examples's [README](https://github.com/Uniswa If you need a briefer on the SDK and to learn more about how these guides connect to the examples repository, please visit our [background](../01-background.md) page! ::: -In the Uniswap V3 protocol, liquidity positions are represented using non-fungible tokens. In this guide we will use the `NonfungiblePositionManager` class to help us mint a liquidity position for the **USDC - DAI** pair. The inputs to our guide are the **two tokens** that we are pooling for, the **amount** of each token we are pooling for and the Pool **fee**. +In the Uniswap V3 protocol, liquidity positions are represented using non-fungible tokens. In this guide we will mint a liquidity position for the **USDC - DAI** pair. The inputs to our guide are the **two tokens** that we are pooling for, the **amount** of each token we are pooling for and the Pool **fee**. The guide will **cover**: @@ -25,9 +25,8 @@ At the end of the guide, given the inputs above, we should be able to mint a liq For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) -- [`@uniswap/smart-order-router`](https://www.npmjs.com/package/@uniswap/smart-order-router) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) The core code of this guide can be found in [`mintPosition()`](https://github.com/Uniswap/examples/blob/main/v3-sdk/minting-position/src/libs/positions.ts#L37) @@ -39,27 +38,25 @@ In situations where a smart contract is transfering tokens on our behalf, we nee We can use the `approveTokenTransfer()` function from the sdk for that: ```typescript -import { approveTokenTransfer } from '@uniswap/v3-sdk' -import { NONFUNGIBLE_POSITION_MANAGER_ADDRESSES } from '@uniswap/sdk-core' - -const signer = getWallet() +import { approveTokenTransfer } from '@uniswapfoundation/v3-sdk' +import { NONFUNGIBLE_POSITION_MANAGER_ADDRESSES } from '@uniswapfoundation/sdk-core' const positionManagerAddress = NONFUNGIBLE_POSITION_MANAGER_ADDRESSES[ CurrentConfig.tokens.token0.chainId ] -const token0Approval = await approveTokenTransfer( - positionManagerAddress, - token0Address, - amount0, - signer -) -const token1Approval = await approveTokenTransfer( - positionManagerAddress, - token1Address, - amount1, - signer -) +const token0Approval = await approveTokenTransfer({ + contractAddress: positionManagerAddress, + tokenAddress: CurrentConfig.tokens.token0.address, + amount: amount0, + signer: getWallet(), + }) + const token1Approval = await approveTokenTransfer({ + contractAddress: positionManagerAddress, + tokenAddress: CurrentConfig.tokens.token1.address, + amount: amount1, + signer: getWallet(), + }) ``` We can get the Contract address for the NonfungiblePositionManager from the `NONFUNGIBLE_POSITION_MANAGER_ADDRESSES` in the sdk-core. @@ -69,23 +66,23 @@ We can get the Contract address for the NonfungiblePositionManager from the `NON To create our Position, we first need to instantiate a `Pool` object: ```typescript - import {Pool} from '@uniswap/v3-sdk' + import {Pool} from '@uniswapfoundation/v3-sdk' const provider = getProvider() - const pool = await Pool.initFromChain( + const pool = await Pool.initFromChain({ provider, - CurrentConfig.tokens.in, - CurrentConfig.tokens.out, - CurrentConfig.tokens.poolFee - ) + tokenA: CurrentConfig.tokens.token0, + tokenB: CurrentConfig.tokens.token1, + fee: CurrentConfig.tokens.poolFee, + }) ``` Next, we can use the pool to create an instance of a `Position` object, which represents a Liquidity Position offchain: ```typescript -import { Position } from '@uniswap/v3-sdk' -import { BigIntish } from '@uniswap/sdk-core' +import { Position } from '@uniswapfoundation/v3-sdk' +import { BigIntish } from '@uniswapfoundation/sdk-core' // The maximum token amounts we want to provide. BigIntish accepts number, string or bigint const amount0: BigIntish = ... @@ -114,11 +111,11 @@ Given those parameters, `fromAmounts` will attempt to calculate the maximum amou ## Configuring and executing our minting transaction -We can now use the `NonfungiblePositionManager` class to mint our Position: +We can now mint our Position: ```typescript -import { MintOptions, NonfungiblePositionManager } from '@uniswap/v3-sdk' -import { Percent } from '@uniswap/sdk-core' +import { MintOptions } from '@uniswapfoundation/v3-sdk' +import { Percent } from '@uniswapfoundation/sdk-core' const signer = getWallet() @@ -129,12 +126,11 @@ const mintOptions: MintOptions = { } // get calldata for minting a position -const txResponse = NonfungiblePositionManager.mintOnChain( - signer, - provider, - position, - mintOptions -) +const txResponse = await positionToMint.mint({ + signer: getWallet(), + provider, + options: mintOptions, + }) ``` The `MintOptions` interface requires three keys: @@ -143,7 +139,7 @@ The `MintOptions` interface requires three keys: - `deadline` defines the latest point in time at which we want our transaction to be included in the blockchain. - `slippageTolerance` defines the maximum amount of **change of the ratio** of the Tokens we provide. The ratio can change if for example **trades** that change the price of the Pool are included before our transaction. -The `mitOnChain()` function directly signs and executes a transaction that will create our Position. +The `mint()` function directly signs and executes a transaction that will create our Position. The effect of the transaction is to mint a new Position NFT. We should see a new position with liquidity in our list of positions. diff --git a/docs/sdk/v3/guides/liquidity/03-fetching-positions.md b/docs/sdk/v3/guides/liquidity/03-fetching-positions.md index 41998917a..c1da8334f 100644 --- a/docs/sdk/v3/guides/liquidity/03-fetching-positions.md +++ b/docs/sdk/v3/guides/liquidity/03-fetching-positions.md @@ -22,8 +22,8 @@ The guide will **cover**: For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) ## Fetching the number of Positions @@ -32,7 +32,7 @@ We first fetch the number of positions an address owns using the `getPositionCou ```typescript import ethers from 'ethers' -import { Position } from '@uniswap/v3-sdk' +import { Position } from '@uniswapfoundation/v3-sdk' const provider = new ethers.providers.JsonRpcProvider('...rpcUrl') diff --git a/docs/sdk/v3/guides/liquidity/04-modifying-position.md b/docs/sdk/v3/guides/liquidity/04-modifying-position.md index ae1ec0945..235c983ff 100644 --- a/docs/sdk/v3/guides/liquidity/04-modifying-position.md +++ b/docs/sdk/v3/guides/liquidity/04-modifying-position.md @@ -22,8 +22,8 @@ At the end of the guide, given the inputs above, we should be able to add or rem For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) The core code of this guide can be found in [`addLiquidity()`](https://github.com/Uniswap/examples/blob/d34a53412dbf905802da2249391788a225719bb8/v3-sdk/modifying-position/src/example/Example.tsx#L33) and [`removeLiquidity()`](https://github.com/Uniswap/examples/blob/733d586070afe2c8cceb35d557a77eac7a19a656/v3-sdk/modifying-position/src/example/Example.tsx#L83) @@ -77,7 +77,7 @@ Use the following snippet to fetch a position using the position id: ```typescript import { ethers } from 'ethers' -import { Position } from '@uniswap/v3-sdk' +import { Position } from '@uniswapfoundation/v3-sdk' // You need to know this, or fetch the position differently const myPositionId = "0xabcdef" @@ -98,7 +98,7 @@ The snippet to increase liquidity by 10% can be seen below: ```typescript import { ethers } from 'ethers' -import { Fraction } from '@uniswap/sdk-core' +import { Fraction } from '@uniswapfoundation/sdk-core' // Use your private key or another way to initialize a Wallet. // A different way would be to use the Metamask signer in the browser. @@ -179,7 +179,7 @@ Use the following snippet to fetch a position using the position id: ```typescript import { ethers } from 'ethers' -import { Position } from '@uniswap/v3-sdk' +import { Position } from '@uniswapfoundation/v3-sdk' // You need to know this, or fetch the position differently const myPositionId = "0xabcdef" @@ -201,7 +201,7 @@ The snippet to decrease liquidity by 10% can be seen below: ```typescript import { ethers } from 'ethers' -import { Fraction } from '@uniswap/sdk-core' +import { Fraction } from '@uniswapfoundation/sdk-core' // Use your private key or another way to initialize a Wallet. // A different way would be to use the Metamask signer in the browser. diff --git a/docs/sdk/v3/guides/liquidity/05-collecting-fees.md b/docs/sdk/v3/guides/liquidity/05-collecting-fees.md index 2ccb59d2e..a217165d2 100644 --- a/docs/sdk/v3/guides/liquidity/05-collecting-fees.md +++ b/docs/sdk/v3/guides/liquidity/05-collecting-fees.md @@ -21,7 +21,7 @@ At the end of the guide, given the inputs above, we should be able to collect th For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) - [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) The core code of this guide can be found in [`collectFees()`](https://github.com/Uniswap/examples/blob/main/v3-sdk/collecting-fees/src/libs/liquidity.ts#L35). @@ -39,7 +39,7 @@ All of the fee collecting logic can be found in the [`collectFees`](https://gith To start, we fetch the position from the NonfungiblePositionManager Contract to get the fees we are owed: ```typescript -import { Position } from '@uniswap/v3-sdk' +import { Position } from '@uniswapfoundation/v3-sdk' const position = await Position.fetchWithPositionId(provider, positionId) ``` diff --git a/docs/sdk/v3/guides/liquidity/06-swap-and-add-liquidity.md b/docs/sdk/v3/guides/liquidity/06-swap-and-add-liquidity.md index 96b12e6d2..15daa6c5a 100644 --- a/docs/sdk/v3/guides/liquidity/06-swap-and-add-liquidity.md +++ b/docs/sdk/v3/guides/liquidity/06-swap-and-add-liquidity.md @@ -26,9 +26,9 @@ At the end of the guide, given the inputs above, we should be able swap-and-add For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) -- [`@uniswap/smart-order-router`](https://www.npmjs.com/package/@uniswap/smart-order-router) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) +- [`@uniswapfoundation/smart-order-router`](https://www.npmjs.com/package/@uniswapfoundation/smart-order-router) The core code of this guide can be found in [`swapAndAddLiquidity()`](https://github.com/Uniswap/examples/blob/main/v3-sdk/swap-and-add-liquidity/src/libs/liquidity.ts#L48). @@ -44,27 +44,29 @@ The first step is to approve the `SwapRouter` smart contract to spend our tokens ```typescript // Give approval to the router contract to transfer tokens - const tokenInApproval = await getTokenTransferApproval( - V3_SWAP_ROUTER_ADDRESS, - CurrentConfig.tokens.token0, - TOKEN_AMOUNT_TO_APPROVE_FOR_TRANSFER - ) - - const tokenOutApproval = await getTokenTransferApproval( - V3_SWAP_ROUTER_ADDRESS, - CurrentConfig.tokens.token1, - TOKEN_AMOUNT_TO_APPROVE_FOR_TRANSFER - ) + const tokenInApproval = await approveTokenTransfer({ + contractAddress: V3_SWAP_ROUTER_ADDRESS, + tokenAddress: CurrentConfig.tokens.token0.address, + amount: TOKEN_AMOUNT_TO_APPROVE_FOR_TRANSFER, + signer: getWallet(), + }) + + const tokenOutApproval = await approveTokenTransfer({ + contractAddress: V3_SWAP_ROUTER_ADDRESS, + tokenAddress: CurrentConfig.tokens.token1.address, + amount: TOKEN_AMOUNT_TO_APPROVE_FOR_TRANSFER, + signer: getWallet(), + }) ``` We described the `getTokenTransferApproval` function [here](./02-minting-position.md#giving-approval-to-transfer-our-tokens). We defined the address for the swaprouter in our `constants.ts` file. -Then we can setup our router, the [`AlphaRouter`](https://github.com/Uniswap/smart-order-router/blob/97c1bb7cb64b22ebf3509acda8de60c0445cf250/src/routers/alpha-router/alpha-router.ts#L333), which is part of the [smart-order-router package](https://www.npmjs.com/package/@uniswap/smart-order-router). The router requires a `chainId` and a `provider` to be initialized. Note that routing is not supported for local forks, so we will use a mainnet provider even when swapping on a local fork: +Then we can setup our router, the [`AlphaRouter`](https://github.com/Uniswap/smart-order-router/blob/97c1bb7cb64b22ebf3509acda8de60c0445cf250/src/routers/alpha-router/alpha-router.ts#L333), which is part of the [smart-order-router package](https://www.npmjs.com/package/@uniswapfoundation/smart-order-router). The router requires a `chainId` and a `provider` to be initialized. Note that routing is not supported for local forks, so we will use a mainnet provider even when swapping on a local fork: ```typescript import { ethers } from 'ethers' -import { AlphaRouter } from '@uniswap/smart-order-router' +import { AlphaRouter } from '@uniswapfoundation/smart-order-router' const provider = new ethers.providers.JsonRpcProvider(rpcUrl) @@ -80,7 +82,7 @@ Having created the router, we now need to construct the parameters required to m The first two parameters are the currency amounts we use as input to the `routeToRatio` algorithm: ```typescript -import { CurrencyAmount } from '@uniswap/sdk-core' +import { CurrencyAmount } from '@uniswapfoundation/sdk-core' const token0CurrencyAmount = CurrencyAmount.fromRawAmount( token0, @@ -102,7 +104,7 @@ const token1CurrencyAmount = CurrencyAmount.fromRawAmount( Next, we will create a placeholder position with a liquidity of `1` since liquidity is still unknown and will be set inside the call to `routeToRatio`: ```typescript -import { Pool, Position, nearestUsableTick } from '@uniswap/v3-sdk' +import { Pool, Position, nearestUsableTick } from '@uniswapfoundation/v3-sdk' const placeholderPosition = new Position{ pool, @@ -122,8 +124,8 @@ We then need to create an instance of `SwapAndAddConfig` which will set addition - `maxIterations` determines the maximum times the algorithm will iterate to find a ratio within error tolerance. If max iterations is exceeded, an error is returned. The benefit of running the algorithm more times is that we have more chances to find a route, but more iterations will longer to execute. We've used a default of 6 in our example. ```typescript -import { Fraction } from '@uniswap/sdk-core' -import { SwapAndAddConfig } from '@uniswap/smart-order-router' +import { Fraction } from '@uniswapfoundation/sdk-core' +import { SwapAndAddConfig } from '@uniswapfoundation/smart-order-router' const swapAndAddConfig: SwapAndAddConfig = { ratioErrorTolerance: new Fraction(1, 100), @@ -137,7 +139,7 @@ Finally, we will create an instance of `SwapAndAddOptions` to configure which po - **`addLiquidityOptions`** must contain a `tokenId` to add to an existing position ```typescript -import { SwapAndAddOptions } from '@uniswap/smart-order-router' +import { SwapAndAddOptions } from '@uniswapfoundation/smart-order-router' const swapAndAddOptions: SwapAndAddOptions = { swapOptions: { @@ -157,7 +159,7 @@ const swapAndAddOptions: SwapAndAddOptions = { Having constructed all the parameters we need to call `routeToRatio`, we can now make the call to the function: ```typescript -import { SwapToRatioResponse } from '@uniswap/smart-order-router' +import { SwapToRatioResponse } from '@uniswapfoundation/smart-order-router' const routeToRatioResponse: SwapToRatioResponse = await router.routeToRatio( token0CurrencyAmount, @@ -171,7 +173,7 @@ const routeToRatioResponse: SwapToRatioResponse = await router.routeToRatio( The return type of the function call is [SwapToRatioResponse](https://github.com/Uniswap/smart-order-router/blob/97c1bb7cb64b22ebf3509acda8de60c0445cf250/src/routers/router.ts#L121). If a route was found successfully, this object will have two fields: the status (success) and the `SwapToRatioRoute` object. We check to make sure that both of those conditions hold true before we construct and submit the transaction: ```typescript -import { SwapToRatioStatus } from '@uniswap/smart-order-router' +import { SwapToRatioStatus } from '@uniswapfoundation/smart-order-router' if ( !routeToRatioResponse || @@ -188,7 +190,7 @@ In case a route was not found, we return from the function a `Failed` state for After making sure that a route was successfully found, we can now construct and send the transaction. The response (`SwapToRatioRoute`) will have the properties we need to construct our transaction object: ```typescript -import { SwapToRatioRoute } from '@uniswap/smart-order-router' +import { SwapToRatioRoute } from '@uniswapfoundation/smart-order-router' const route: SwapToRatioRoute = routeToRatioResponse.result const transaction = { diff --git a/docs/sdk/v3/guides/swaps/01-quoting.md b/docs/sdk/v3/guides/swaps/01-quoting.md index 2b6a814b5..7aa8f6df1 100644 --- a/docs/sdk/v3/guides/swaps/01-quoting.md +++ b/docs/sdk/v3/guides/swaps/01-quoting.md @@ -24,8 +24,8 @@ At the end of the guide, we should be able to fetch a quote for the given input For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) The core code of this guide can be found in [`quote.ts`](https://github.com/Uniswap/examples/blob/main/v3-sdk/quoting/src/libs/quote.ts) @@ -34,8 +34,8 @@ The core code of this guide can be found in [`quote.ts`](https://github.com/Unis We will use the example configuration `CurrentConfig` in most code snippets of this guide. It has the format: ```typescript -import { Token } from '@uniswap/sdk-core' -import { FeeAmount } from '@uniswap/v3-sdk' +import { Token } from '@uniswapfoundation/sdk-core' +import { FeeAmount } from '@uniswapfoundation/v3-sdk' interface ExampleConfig { rpc: { @@ -114,16 +114,16 @@ const currencyAmountIn = CurrencyAmount.fromRawAmount( We can now use the SwapQuoter class to fetch a quote for our swap. We need a provider to connect to the blockchain: ```typescript -import { SwapQuoter } from '@uniswap/v3-sdk' +import { SwapQuoter } from '@uniswapfoundation/v3-sdk' const provider = new ethers.providers.JsonRpcProvider(CurrentConfig.rpc.mainnet) -const currencyAmountOut = await SwapQuoter.quoteExactInputSingle( - currencyAmountInt, - CurrentConfig.tokens.out, - CurrentConfig.tokens.poolFee, - provider -) +const currencyAmountOut = await SwapQuoter.quoteExactInputSingle({ + amountIn: currencyAmountIn, + tokenOut: CurrentConfig.tokens.out, + poolFee: CurrentConfig.tokens.poolFee, + provider, + }) ``` The function calls the Quoter contract and parses the response value. It only works on chains where Uniswap has officially deployed the QuoterV2 contract. diff --git a/docs/sdk/v3/guides/swaps/02-trading.md b/docs/sdk/v3/guides/swaps/02-trading.md index 1020d6fa4..47b0868ec 100644 --- a/docs/sdk/v3/guides/swaps/02-trading.md +++ b/docs/sdk/v3/guides/swaps/02-trading.md @@ -29,8 +29,8 @@ Included in the example application is functionality to wrap/unwrap ETH as neede For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) The core code of this guide can be found in [`trading.ts`](https://github.com/Uniswap/examples/blob/main/v3-sdk/trading/src/libs/trading.ts) @@ -112,7 +112,7 @@ To construct our trade, we will first create a model instance of a `Pool`. We cr The sdk has a utility function to create a Pool from onchain data: ```typescript - import {Pool} from '@uniswap/v3-sdk' + import {Pool} from '@uniswapfoundation/v3-sdk' import ethers from 'ethers' const provider = new ethers.providers.JsonRpcProvider(CurrentConfig.rpc.mainnet) @@ -153,7 +153,7 @@ The `Route` object can find this route from an array of given pools and an input To keep it simple for this guide, we only swap over one Pool: ```typescript -import { Route } from '@uniswap/v3-sdk' +import { Route } from '@uniswapfoundation/v3-sdk' const swapRoute = new Route( [pool], @@ -176,8 +176,8 @@ As shown below, the quote is obtained using the `v3-sdk`'s `SwapQuoter`, for thi In contrast to the `quoteExactInputSingle()` function we used in the previous guide, this function works for a Route with any number of Uniswap V3 Pools, not just a swap over a single Pool: ```typescript -import { SwapQuoter } from '@uniswap/v3-sdk' -import { CurrencyAmount, TradeType } from '@uniswap/sdk-core' +import { SwapQuoter } from '@uniswapfoundation/v3-sdk' +import { CurrencyAmount, TradeType } from '@uniswapfoundation/sdk-core' const rawInputAmount = ethers.utils.parseUnits( CurrentConfig.tokens.amountIn, @@ -189,12 +189,12 @@ const currencyAmountIn = CurrencyAmount.fromRawAmount( rawInputAmount ) -const expectedOutput = await SwapQuoter.callQuoter( - swapRoute, - currencyAmountIn, - TradeType.EXACT_INPUT, +const expectedOutput = await SwapQuoter.callQuoter({ + route: swapRoute, + amount: currencyAmountIn, + tradeType: TradeType.EXACT_INPUT, provider -) +}) ``` We construct the input the same way we did in the previous guide. @@ -204,7 +204,7 @@ With the quote and the route, we can now construct a trade using the route in ad Because we already know the expected output of our Trade, we do not have to check it again. We can use the `uncheckedTrade` function to create our Trade: ```typescript -import { Trade } from 'uniswap/v3-sdk' +import { Trade } from '@uniswapfoundation/v3-sdk' const uncheckedTrade = Trade.createUncheckedTrade({ route: swapRoute, @@ -223,8 +223,8 @@ We will use the `executeTrade()` function of the `SwapRouter` class. First we specify the deadline and the slippage tolerance we are willing to accept for our trade: ```typescript -import { SwapOptions } frpm '@uniswap/v3-sdk' -import { Percent } from '@uniswap/sdk-core' +import { SwapOptions } frpm '@uniswapfoundation/v3-sdk' +import { Percent } from '@uniswapfoundation/sdk-core' const swapOptions: SwapOptions = { slippageTolerance: new Percent(50, 10_000), @@ -250,13 +250,13 @@ wallet.connect(provider) We are now ready to execute our trade: ```typescript -import { SwapRouter } from '@uniswap/v3-sdk' +import { SwapRouter } from '@uniswapfoundation/v3-sdk' -const txResponse = await SwapRouter.executeTrade( - [uncheckedTrade], - swapOptions, - wallet -) +const txResponse = await SwapRouter.executeTrade({ + trades: [uncheckedTrade], + options: swapOptions, + signer: wallet +}) ``` The function automatically checks if the necessary token transfer approvals exist and creates them if not. diff --git a/docs/sdk/v3/guides/swaps/03-simulate-offchain.md b/docs/sdk/v3/guides/swaps/03-simulate-offchain.md index c7458cf82..003bbcf38 100644 --- a/docs/sdk/v3/guides/swaps/03-simulate-offchain.md +++ b/docs/sdk/v3/guides/swaps/03-simulate-offchain.md @@ -25,8 +25,8 @@ At the end of the guide, we should be able to initialize Pools with full tickdat For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) The core code of this guide can be found in [`simulations.ts`](https://github.com/Uniswap/examples/blob/main/v3-sdk/offchain-simulation/src/libs/simulations.ts) @@ -35,8 +35,8 @@ The core code of this guide can be found in [`simulations.ts`](https://github.co We will use the example configuration `CurrentConfig` in most code snippets of this guide. It has the format: ```typescript -import { Token } from '@uniswap/sdk-core' -import { FeeAmount, Pool } from '@uniswap/v3-sdk' +import { Token } from '@uniswapfoundation/sdk-core' +import { FeeAmount, Pool } from '@uniswapfoundation/v3-sdk' interface ExampleConfig { rpc: { @@ -114,8 +114,8 @@ In this example, we want to make an exact Input trade from **USDC** to **DAI**. We use the `Trade.bestTradeExactIn` function to find the best route given our Pools: ```typescript -import { Trade, BestTradeOptions } from '@uniswap/v3-sdk' -import { CurrencyAmount } from '@uniswap/sdk-core' +import { Trade, BestTradeOptions } from '@uniswapfoundation/v3-sdk' +import { CurrencyAmount } from '@uniswapfoundation/sdk-core' const currencyAmountIn = CurrencyAmount.fromRawAmount(...) @@ -145,13 +145,12 @@ We can access them with the `swaps` getter if we want to access this information Like in the previous guide, we execute the trade with `executeTrade()` on the `SwapRouter` class: ```typescript -import { SwapRouter } from '@uniswap/v3-sdk' +import { SwapRouter } from '@uniswapfoundation/v3-sdk' wallet.connect(provider) const txResponse = SwapRouter.executeTrade( - [bestTrade[0]], - , - wallet + trades: [bestTrade[0]], + signer: wallet ) ``` @@ -164,13 +163,11 @@ wallet.connect(provider) const txResponse = SwapRouter.executeBestSimulatedSwapOnPools( pools, - currencyAmountIn, - CurrentConfig.tokens.in, - CurrentConfig.tokens.out, - TradeType.EXACT_INPUT, - undefined, - undefined, - wallet + amount: currencyAmountIn, + currencyIn: CurrentConfig.tokens.in, + currencyOut: CurrentConfig.tokens.out, + tradeType: TradeType.EXACT_INPUT, + signer: wallet ) ``` diff --git a/docs/sdk/v3/guides/swaps/04-routing.md b/docs/sdk/v3/guides/swaps/04-routing.md index 0ae265bd9..3c3c77508 100644 --- a/docs/sdk/v3/guides/swaps/04-routing.md +++ b/docs/sdk/v3/guides/swaps/04-routing.md @@ -23,16 +23,16 @@ At the end of the guide, we should be able to create a route and and execute a s For this guide, the following Uniswap packages are used: -- [`@uniswap/v3-sdk`](https://www.npmjs.com/package/@uniswap/v3-sdk) -- [`@uniswap/sdk-core`](https://www.npmjs.com/package/@uniswap/sdk-core) -- [`@uniswap/smart-order-router`](https://www.npmjs.com/package/@uniswap/smart-order-router) +- [`@uniswapfoundation/v3-sdk`](https://www.npmjs.com/package/@uniswapfoundation/v3-sdk) +- [`@uniswapfoundation/sdk-core`](https://www.npmjs.com/package/@uniswapfoundation/sdk-core) +- [`@uniswapfoundation/smart-order-router`](https://www.npmjs.com/package/@uniswapfoundation/smart-order-router) The core code of this guide can be found in [`routing.ts`](https://github.com/Uniswap/examples/blob/main/v3-sdk/routing/src/libs/routing.ts) The config, which we will use in some code snippets in this guides has this structure: ```typescript -import { Token } from '@uniswap/sdk-core' +import { Token } from '@uniswapfoundation/sdk-core' interface ExampleConfig { env: Environment @@ -56,10 +56,10 @@ export const CurrentConfig: ExampleConfig = {...} ## Creating a router instance -To compute our route, we will use the `@uniswap/smart-order-router` package, specifically the `AlphaRouter` class which requires a `chainId` and a `provider`. Note that routing is not supported for local forks, so we will use a mainnet provider even when swapping on a local fork: +To compute our route, we will use the `@uniswapfoundation/smart-order-router` package, specifically the `AlphaRouter` class which requires a `chainId` and a `provider`. Note that routing is not supported for local forks, so we will use a mainnet provider even when swapping on a local fork: ```typescript -import { AlphaRouter, ChainId } from '@uniswap/smart-order-router' +import { AlphaRouter, ChainId } from '@uniswapfoundation/smart-order-router' const provider = new ethers.providers.JsonRpcProvider(rpcUrl) @@ -77,8 +77,8 @@ In contrast to the contract we used previously, this on can execute swaps on bot The `smart-order-router` package provides us with a `SwapOptionsSwapRouter02` interface, defining the wallet to use, slippage tolerance, and deadline for the transaction that we need to interact with the contract: ```typescript -import { SwapOptionsSwapRouter02, SwapType } from '@uniswap/smart-order-router' -import { Percent } from '@uniswap/sdk-core' +import { SwapOptionsSwapRouter02, SwapType } from '@uniswapfoundation/smart-order-router' +import { Percent } from '@uniswapfoundation/sdk-core' const options: SwapOptionsSwapRouter02 = { recipient: CurrentConfig.wallet.address, @@ -93,9 +93,9 @@ Like explained in the [trading guide](./02-trading.md#executing-a-trade), it is Using these options, we can now create a trade (`TradeType.EXACT_INPUT` or `TradeType.EXACT_OUTPUT`) with the currency and the input amount to use to get a quote. For this example, we'll use an `EXACT_INPUT` trade to get a quote outputted in the quote currency. ```typescript -import { CurrencyAmount, TradeType } from '@uniswap/sdk-core' +import { CurrencyAmount, TradeType } from '@uniswapfoundation/sdk-core' -const rawTokenAmountIn: JSBI = fromReadableAmount( +const rawTokenAmountIn = fromReadableAmount( CurrentConfig.currencies.amountIn, CurrentConfig.currencies.in.decimals )