Skip to content

Commit

Permalink
Name parameters and import packages from uniswapfoundation in v3 sdk …
Browse files Browse the repository at this point in the history
…guides
  • Loading branch information
Florian-S-A-W committed Dec 27, 2023
1 parent fe47579 commit 0e889ba
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 159 deletions.
4 changes: 2 additions & 2 deletions docs/sdk/v3/guides/02-local-development.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
30 changes: 14 additions & 16 deletions docs/sdk/v3/guides/advanced/03-price-oracle.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
```
Expand Down
20 changes: 10 additions & 10 deletions docs/sdk/v3/guides/advanced/04-range-orders.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down Expand Up @@ -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)

Expand All @@ -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))

Expand All @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
```
Expand All @@ -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<CollectOptions, 'tokenId'> = {
expectedCurrencyOwed0: CurrencyAmount.fromRawAmount(
Expand Down
72 changes: 34 additions & 38 deletions docs/sdk/v3/guides/liquidity/02-minting-position.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:

Expand All @@ -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)

Expand All @@ -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.
Expand All @@ -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 = ...
Expand Down Expand Up @@ -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()

Expand All @@ -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:
Expand All @@ -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.

Expand Down
6 changes: 3 additions & 3 deletions docs/sdk/v3/guides/liquidity/03-fetching-positions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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')

Expand Down
12 changes: 6 additions & 6 deletions docs/sdk/v3/guides/liquidity/04-modifying-position.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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"
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions docs/sdk/v3/guides/liquidity/05-collecting-fees.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand All @@ -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)
```
Expand Down
Loading

0 comments on commit 0e889ba

Please sign in to comment.