Uniswap

With our SDK's Uniswap interface, you can interact directly with the protocol and perform swaps, provide liquidity, collect fees, and more.

The first step will be to instantiate the SDK as usual:

const sdk = new CryptumSdk({
  environment: 'testnet',
  apiKey: 'YOUR-API-KEY',
})

Get Pools

This function retrieves the pool addresses for a specific token pair. Because multiple pools may exist for the same pair due to varying fee tiers, the function returns an array that includes each pool address and its corresponding fee. If a pool doesn't exist, the address will be null.

const pools = await sdk.uniswap.getPools({
  protocol,
  tokenA: AtokenAddress,
  tokenB: BtokenAddress,
})


/*
[
      {
        poolAddress: '0x50…72df',
        poolFee: 100,
      },
      {
        poolAddress: '0xC5…3e0C',
        poolFee: 500,
      },
      {
        poolAddress: '0x63…Ea80',
        poolFee: 3000,
      },
      {
        poolAddress: null,
        poolFee: 10000,
      },
]
*/

Get Pool Data

This function retrieves pool data such as liquidity, current tick, fee tier, and more.

const poolData = await sdk.uniswap.getPoolData({
  protocol,
  poolAddress: '0x45…2df',
})

{
    poolAddress: '0x50...72df',
    fee: '100',
    token0: '0xE3...39f9',
    token1: '0xFf4...979',
    liquidity: '4274664239629845711',
    tickSpacing: '1',
    slot0: {
        sqrtPriceX96: '56022262241300288188759753413',
        tick: '-6932',
        observationIndex: '1',
        observationCardinality: '10',
    observationCardinalityNext: '10',
    feeProtocol: '0',
    unlocked: true,
  },
}

Get Token IDs

Since Uniswap positions are NFTs, they each have a unique ID that can be used to access further information regarding that position. This function retrieves all token IDs owned by a given address.

const tokenIds = await sdk.uniswap.getTokenIds({
    protocol,
    ownerAddress: '0xb5…23Af',
})

// ['6122', '6124', '6467', '6780', '6781', '6993', '6994', '6996', '6997']

Get Position

This function fetches position data by its ID.

const position = await sdk.uniswap.getPosition({
    protocol,
    tokenId: '7051',
})

/*
{
          nonce: '0',
          operator: '0x0000000000000000000000000000000000000000',
          token0: '0x9c…2889',
          token1: '0xa4…4411',
          fee: '10000',
          tickLower: '75000',
          tickUpper: '76600',
          liquidity: '0',
          feeGrowthInside0LastX128: '0',
          feeGrowthInside1LastX128: '0',
          tokensOwed0: '0',
          tokensOwed1: '0',
}
*/

Get Positions

This function retrieves all positions held by a given address. It can be further filtered by specifying a pool, although it’s optional.

const positions = await sdk.uniswap.getPositions({
  protocol,
  ownerAddress: '0xb6…23Af',
  // poolAddress: '0xC9…de0C'
})

/*
[
  {
    tokenId: '6780',
    token0: '0xE3…39f9',
    token1: '0xFf…6979',
    poolAddress: '0xC9…de0C',
    fee: '500',
    tickLower: '-7990',
    tickUpper: '-5110',
    liquidity: '0',
    feeGrowthInside0LastX128: '0',
    feeGrowthInside1LastX128: '0',
    tokensOwed0: '0',
    tokensOwed1: '0',
    operator: '0x0000000000000000000000000000000000000000',
  },
  {
    tokenId: '6781',
    token0: '0xE3…39f9',
    token1: '0xFf4…e979',
    poolAddress: '0xC9d335CB5D4859090f1F8f67643441B6d330de0C',
    fee: '500',
    tickLower: '-7990',
    tickUpper: '-5110',
    liquidity: '0',
    feeGrowthInside0LastX128: '8519556846084454442949710601260011',
    feeGrowthInside1LastX128: '0',
    tokensOwed0: '16398984956330600875554198579416617',
    tokensOwed1: '0',
    operator: '0x0000000000000000000000000000000000000000',
  }
]
*/

Observe Pool

This function retrieves historical price data for a specified pool. The secondsAgoToCheck parameter should be an array of numbers representing the number of seconds before the current time to retrieve data.

const observedPrices = await sdk.uniswap.observePool({
    protocol,
    pool: '0xC9...de0C',
    secondsAgoToCheck: [3600000, 0],
})

Get Swap Quotation

This function provides a quotation for a specified swap. The tokenIn parameter represents the token to be spent in the swap, while tokenOut is the token to be received in return. If either token is the native currency of the protocol, pass the currency symbol (e.g., "ETH", "MATIC", "CELO") instead of an address.

const swapQuote = await sdk.uniswap.getSwapQuotation({
    wallet,
    protocol,
    tokenIn: "ETH",
    tokenOut: "0x4B...5dE1",
    amountIn: '0.1',
})

The returned object contains two components: the calculated amounts from the quotation and the prepared transaction for executing the swap, if desired.

Get Mint Position Quotation (Providing Liquidity)

This function provides a quotation for minting a new position (i.e. providing liquidity). You should provide either “amountTokenA” or “amountTokenB”, this is the value that will be used as a basis to prepare the position data. The minPriceDelta parameter defines the minimum price the position will act on, expressed as a percentage of the current price. For example, if the current price is 300 and you set a minPriceDelta of 10, the minimum price for the position will be 270. Conversely, maxPriceDelta sets the maximum price; if the current price is 300 and you set a maxPriceDelta of 20, the maximum price for the position will be 360.

const mintQuote= await sdk.uniswap.getMintPositionQuotation({
    wallet,
    protocol,
    pool: '0x63…Ea80',
    amountTokenA: '0.1',
    minPriceDelta: '10',
    maxPriceDelta: '10',
})

Similar to other quotation functions, the response includes two parts: the calculated amounts from the quotation and the prepared transaction for executing the mint, if desired.

Get Increase Liquidity Quotation

This function may be used to increase the amount of liquidity provided in a position that already exists. The tokenId parameter is used to indicate which position you want to increase the liquidity of, and the amountTokenA or amountTokenB is the value that will be used as a basis to prepare the transaction.

const increaseQuote = await sdk.uniswap.getIncreaseLiquidityQuotation({
    protocol,
    wallet,
    tokenId: '7244',
    amountTokenA: '0.1',
})

Similar to other quotation functions, the response includes two parts: the calculated amounts from the quotation and the prepared transaction for executing the increase of liquidity, if desired.

Create Pool

This function creates a pool with the specified parameters and returns its address. If the pool already exists, no transaction will be made, but the address will still be returned. If one of the tokens for the pool is the protocol's native currency, use its symbol instead of an address (e.g., "ETH", "MATIC", "CELO"). The valid values for fee tiers are 100, 500, 3000, or 10000.

const createPool = await sdk.uniswap.createPool({
    wallet,
    protocol,
    fee: 3000, // 100 , 500 , 3000 , 10000
    tokenA: AtokenAddress,
    tokenB: BtokenAddress,
    price: '2', // Initial desired ratio of A/B tokens
})

Increase Cardinality

In order to use the observePool function, you must first increase the pool’s cardinality using this function. The cardinality represents the amount of timestamps the pool’s data structure can hold at any given time.

const increaseCardinality = await sdk.uniswap.increaseCardinality({
    protocol,
    wallet,
    pool: '0x50…72df',
    cardinality: 4,
})

Decrease Liquidity

This function may be used to withdraw liquidity either partially, by specifying a percentage lower than 100, or fully by passing 100. The tokenId parameter is used to indicate which position you want to decrease the liquidity of. The slippage refers to the percentage of price difference that is still acceptable.

const decreaseLiquidity = await sdk.uniswap.decreaseLiquidity({
    protocol,
    wallet,
    tokenId: '7053',
    percentageToDecrease: '10',
    slippage: '5',
})

Collect Fees

This function collects outstanding fees from a position.

const collectFees = await sdk.uniswap.collectFees({
    protocol,
    wallet,
    tokenId: '7244',
})

Swap

To execute a swap, simply pass the result from the getSwapQuotation function as the transaction parameter for this function.

const swap = await sdk.uniswap.swap({
    wallet,
    transaction: swapQuotation,
})

Mint (Provide Liquidity)

To mint a position and provide liquidity, pass the result from the getMintPositionQuotation function as the transaction parameter for this function.

const mintPosition = await sdk.uniswap.mintPosition({
    wallet,
    transaction: mintPositionQuotation,
})

Increase Liquidity

To increase liquidity for a given position, pass the result from the getIncreaseLiquidityQuotation function as the transaction parameter for this function.

const increaseLiquidity = await sdk.uniswap.increaseLiquidity({
    wallet,
    transaction: increaseLiquidityQuotation,
})

Last updated