From 039c192c4809cda47f70aeb06e5091850d1a8b85 Mon Sep 17 00:00:00 2001 From: Kyle Scott Date: Mon, 2 Oct 2023 14:18:16 -0400 Subject: [PATCH] mint tokenized position --- packages/panoptic-sdk/README.md | 2 +- ...ePanopticSFPMMintTokenizedPosition.test.ts | 86 +++++++++++++++++++ ...mulatePanopticSFPMMintTokenizedPosition.ts | 67 +++++++++++++++ .../src/utils/calculatePanopticTokenID.ts | 2 +- 4 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.test.ts diff --git a/packages/panoptic-sdk/README.md b/packages/panoptic-sdk/README.md index bc6ef71..12f03b2 100644 --- a/packages/panoptic-sdk/README.md +++ b/packages/panoptic-sdk/README.md @@ -57,7 +57,7 @@ Typescript SDK for Panoptic - [x] `simulatePanopticCollateralRedeem` - [x] `simulatePanopticCollateralWithdraw` - [x] `simulateSFPMInitializeAMMPool` - - [ ] `simulateSFPMMintTokenizedPosition` + - [x] `simulateSFPMMintTokenizedPosition` - [ ] `simulateSFPMRollTokenizedPosition` - [ ] `simulateSFPMBurnTokenizedPosition` diff --git a/packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.test.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.test.ts new file mode 100644 index 0000000..c38194f --- /dev/null +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.test.ts @@ -0,0 +1,86 @@ +import { createUniswapV3Tick } from "uniswap-v3-sdk"; +import { type Hex } from "viem"; +import { simulateContract, writeContract } from "viem/actions"; +import { sepolia } from "viem/chains"; +import { beforeEach, test } from "vitest"; +import { ALICE } from "../_test/constants.js"; +import { + deployPool, + publicClient, + testClient, + walletClient, +} from "../_test/utils.js"; +import { mockErc20ABI } from "../generated.js"; +import type { PanopticPool } from "../types/PanopticPool.js"; +import { createPanopticPosition } from "../utils/createPanopticPosition.js"; +import { simulatePanopticSFPMInitializeAMMPool } from "./simulatePanopticSFPMInitializeAMMPool.js"; +import { simulatePanopticSFPMMintTokenizedPosition } from "./simulatePanopticSFPMMintTokenizedPosition.js"; + +let id: Hex | undefined = undefined; + +let pool: PanopticPool; + +beforeEach(async () => { + if (id === undefined) { + pool = await deployPool(); + const { request: initializeRequest } = + await simulatePanopticSFPMInitializeAMMPool(publicClient, { + args: { + pool: pool.uniswapPool, + sfpm: pool.factory.semiFungiblePositionManager, + }, + }); + + const initializeHash = await walletClient.writeContract(initializeRequest); + await publicClient.waitForTransactionReceipt({ hash: initializeHash }); + + const { request: approveRequest } = await simulateContract(publicClient, { + address: pool.collateralTracker0.underlyingToken.address, + abi: mockErc20ABI, + functionName: "approve", + args: [pool.factory.semiFungiblePositionManager.address, 10n ** 24n], + account: ALICE, + }); + + const approveHash = await writeContract(walletClient, approveRequest); + await publicClient.waitForTransactionReceipt({ hash: approveHash }); + } else { + await testClient.revert({ id }); + } + id = await testClient.snapshot(); +}, 100_000); + +test("mint tokenized position", async () => { + const { request } = await simulatePanopticSFPMMintTokenizedPosition( + publicClient, + { + args: { + position: createPanopticPosition( + ALICE, + pool, + [ + { + asset: "token0", + optionRatio: 1, + position: "short", + tokenType: "token0", + riskPartnerIndex: 0, + tickLower: createUniswapV3Tick(0), + tickUpper: createUniswapV3Tick(10), + }, + undefined, + undefined, + undefined, + ], + sepolia.id, + ), + amount: 10n ** 18n, + }, + account: ALICE, + }, + ); + const hash = await walletClient.writeContract(request); + await publicClient.waitForTransactionReceipt({ hash }); + + // TODO: check the minted position and amounts taken +}); diff --git a/packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.ts index e69de29..f73ff05 100644 --- a/packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMMintTokenizedPosition.ts @@ -0,0 +1,67 @@ +import { MAX_TICK, MIN_TICK } from "uniswap-v3-sdk"; +import type { + Chain, + Client, + SimulateContractParameters, + SimulateContractReturnType, + Transport, +} from "viem"; +import { simulateContract } from "viem/contract"; +import { semiFungiblePositionManagerABI } from "../generated.js"; +import type { PanopticPosition } from "../types/PanopticPosition.js"; + +export type PanopticSFPMMintTokenizedPositionParameters = { + position: PanopticPosition; + amount: bigint; +}; + +export type SimulatePanopticSFPMMintTokenizedPositionParameters< + TChain extends Chain | undefined = Chain, + TChainOverride extends Chain | undefined = Chain | undefined, +> = Omit< + SimulateContractParameters< + typeof semiFungiblePositionManagerABI, + "mintTokenizedPosition", + TChain, + TChainOverride + >, + "args" | "address" | "abi" | "functionName" +> & { args: PanopticSFPMMintTokenizedPositionParameters }; + +export type SimulatePanopticSFPMMintTokenizedPositionReturnType< + TChain extends Chain | undefined, + TChainOverride extends Chain | undefined = undefined, +> = SimulateContractReturnType< + typeof semiFungiblePositionManagerABI, + "mintTokenizedPosition", + TChain, + TChainOverride +>; + +export const simulatePanopticSFPMMintTokenizedPosition = < + TChain extends Chain | undefined, + TChainOverride extends Chain | undefined, +>( + client: Client, + { + args: { position, amount }, + ...request + }: SimulatePanopticSFPMMintTokenizedPositionParameters< + TChain, + TChainOverride + >, +): Promise< + SimulatePanopticSFPMMintTokenizedPositionReturnType +> => + simulateContract(client, { + address: position.pool.factory.semiFungiblePositionManager.address, + abi: semiFungiblePositionManagerABI, + functionName: "mintTokenizedPosition", + args: [[position.id], amount, MIN_TICK, MAX_TICK], + ...request, + } as unknown as SimulateContractParameters< + typeof semiFungiblePositionManagerABI, + "mintTokenizedPosition", + TChain, + TChainOverride + >); diff --git a/packages/panoptic-sdk/src/utils/calculatePanopticTokenID.ts b/packages/panoptic-sdk/src/utils/calculatePanopticTokenID.ts index 63b785a..2620b32 100644 --- a/packages/panoptic-sdk/src/utils/calculatePanopticTokenID.ts +++ b/packages/panoptic-sdk/src/utils/calculatePanopticTokenID.ts @@ -20,7 +20,7 @@ export const calculatePanopticTokenID = ( legs: Tuple, ) => { let id = 0n; - id |= BigInt(pool.address) & 0xffffffffffffffffn; + id |= (BigInt(pool.uniswapPool.address) >> 96n) & 0xffffffffffffffffn; id |= legs[0] ? calculateLegID(legs[0], pool.uniswapPool.tickSpacing) << 64n : 0n;