Skip to content

Commit

Permalink
feat(v4-sdk): add collect call parameters (#103)
Browse files Browse the repository at this point in the history
Co-authored-by: Siyu Jiang (See-You John) <[email protected]>
Co-authored-by: Sara Reynolds <[email protected]>
Co-authored-by: Sara Reynolds <[email protected]>
  • Loading branch information
4 people authored Sep 23, 2024
1 parent 9d3e8bc commit d8d910c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ import {
} from './internalConstants'
import { Pool } from './entities/pool'
import { Position } from './entities/position'
import { RemoveLiquidityOptions, V4PositionManager } from './PositionManager'
import { CollectOptions, RemoveLiquidityOptions, V4PositionManager } from './PositionManager'
import { Multicall } from './multicall'
import { Actions, toHex, V4Planner } from './utils'
import { PoolKey } from './entities/pool'
import { toAddress } from './utils/currencyMap'
import { MSG_SENDER } from './actionConstants'
import { V4PositionPlanner } from './utils'

describe('POSM', () => {
describe('PositionManager', () => {
const currency0 = new Token(1, '0x0000000000000000000000000000000000000001', 18, 't0', 'currency0')
const currency1 = new Token(1, '0x0000000000000000000000000000000000000002', 18, 't1', 'currency1')
const currency_native = Ether.onChain(1)
Expand Down Expand Up @@ -375,4 +375,35 @@ describe('POSM', () => {
expect(value).toEqual('0x00')
})
})

describe('#collectCallParameters', () => {
const position = new Position({
pool: pool_0_1,
tickLower: -TICK_SPACINGS[FeeAmount.MEDIUM],
tickUpper: TICK_SPACINGS[FeeAmount.MEDIUM],
liquidity: 100,
})

const collectLiqOptions: CollectOptions = {
tokenId,
slippageTolerance,
deadline,
recipient,
}

it('succeeds', () => {
const { calldata, value } = V4PositionManager.collectCallParameters(position, collectLiqOptions)

const planner = new V4Planner()

planner.addAction(Actions.DECREASE_LIQUIDITY, [tokenId.toString(), '0', '0', '0', EMPTY_BYTES])

planner.addAction(Actions.TAKE_PAIR, [toAddress(currency0), toAddress(currency1), recipient])

expect(calldata).toEqual(
V4PositionManager.encodeModifyLiquidities(planner.finalize(), collectLiqOptions.deadline)
)
expect(value).toEqual('0x00')
})
})
})
50 changes: 32 additions & 18 deletions sdks/v4-sdk/src/PositionManager.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
BigintIsh,
Percent,
CurrencyAmount,
validateAndParseAddress,
Currency,
NativeCurrency,
} from '@uniswap/sdk-core'
import { BigintIsh, Percent, validateAndParseAddress, Currency, NativeCurrency } from '@uniswap/sdk-core'
import JSBI from 'jsbi'
import { Position } from './entities/position'
import { MethodParameters, toHex } from './utils/calldata'
Expand Down Expand Up @@ -113,16 +106,6 @@ export interface CollectSpecificOptions {
*/
tokenId: BigintIsh

/**
* Expected value of tokensOwed0, including as-of-yet-unaccounted-for fees/liquidity value to be burned
*/
expectedCurrencyOwed0: CurrencyAmount<Currency>

/**
* Expected value of tokensOwed1, including as-of-yet-unaccounted-for fees/liquidity value to be burned
*/
expectedCurrencyOwed1: CurrencyAmount<Currency>

/**
* The account that should receive the tokens.
*/
Expand Down Expand Up @@ -322,6 +305,37 @@ export abstract class V4PositionManager {
}
}

/**
* Produces the calldata for collecting fees from a position
* @param position The position to collect fees from
* @param options Additional information necessary for generating the calldata
* @returns The call parameters
*/
public static collectCallParameters(position: Position, options: CollectOptions): MethodParameters {
const calldataList: string[] = []
const planner = new V4PositionPlanner()

const tokenId = toHex(options.tokenId)
const recipient = validateAndParseAddress(options.recipient)

/**
* To collect fees in V4, we need to:
* - encode a decrease liquidity by 0
* - and encode a TAKE_PAIR
*/

planner.addDecrease(tokenId, '0', '0', '0', options.hookData)

planner.addTakePair(position.pool.currency0, position.pool.currency1, recipient)

calldataList.push(V4PositionManager.encodeModifyLiquidities(planner.finalize(), options.deadline))

return {
calldata: Multicall.encodeMulticall(calldataList),
value: toHex(0),
}
}

// Initialize a pool
private static encodeInitializePool(poolKey: PoolKey, sqrtPriceX96: BigintIsh, hookData?: string): string {
return V4PositionManager.INTERFACE.encodeFunctionData(PositionFunctions.INITIALIZE_POOL, [
Expand Down

0 comments on commit d8d910c

Please sign in to comment.