Skip to content

Commit

Permalink
roll tokenized position
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Oct 2, 2023
1 parent 039c192 commit aa85a70
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 25 deletions.
8 changes: 4 additions & 4 deletions packages/panoptic-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ Typescript SDK for Panoptic
- [x] `calculatePanopticPositionKey`
- [ ] `calculatePanopticDeployNewPool`
- [ ] `calculatePanopticMintOptions`
- [ ] `calculatePanopticRollOptions`
- [ ] `calculatePanopticBurnOptions`
- [ ] `calculatePanopticRollOptions`
- [ ] `calculatePanopticForceExercise`
- [ ] `calcuatePanopticLiquidateAccount`
- [x] `calculatePanopticCollateralMint`
Expand All @@ -36,8 +36,8 @@ Typescript SDK for Panoptic
- [x] `calculatePanopticCollateralWithdraw`
- [ ] `calculateSFPMInitializeAMMPool`
- [ ] `calculateSFPMMintTokenizedPosition`
- [ ] `calculateSFPMRollTokenizedPosition`
- [ ] `calculateSFPMBurnTokenizedPosition`
- [ ] `calculateSFPMRollTokenizedPosition`

- Public Actions

Expand All @@ -48,8 +48,8 @@ Typescript SDK for Panoptic
- [x] `getPanopticPositionData`
- [x] `simulatePanopticDeployNewPool`
- [x] `simulatePanopticMintOptions`
- [x] `simulatePanopticRollOptions`
- [x] `simulatePanopticBurnOptions`
- [x] `simulatePanopticRollOptions`
- [x] `simulatePanopticForceExercise`
- [x] `simulatePanopticLiquidateAccount`
- [x] `simulatePanopticCollateralMint`
Expand All @@ -58,8 +58,8 @@ Typescript SDK for Panoptic
- [x] `simulatePanopticCollateralWithdraw`
- [x] `simulateSFPMInitializeAMMPool`
- [x] `simulateSFPMMintTokenizedPosition`
- [x] `simulateSFPMBurnTokenizedPosition`
- [ ] `simulateSFPMRollTokenizedPosition`
- [ ] `simulateSFPMBurnTokenizedPosition`

- Chains

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { getERC1155BalanceOf } from "reverse-mirage";
import { createUniswapV3Tick } from "uniswap-v3-sdk";
import { type Hex } from "viem";
import { simulateContract, writeContract } from "viem/actions";
import { sepolia } from "viem/chains";
import { beforeEach, expect, 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 type { PanopticPosition } from "../types/PanopticPosition.js";
import { createPanopticPosition } from "../utils/createPanopticPosition.js";
import { simulatePanopticSFPMBurnTokenizedPosition } from "./simulatePanopticSFPMBurnTokenizedPosition.js";
import { simulatePanopticSFPMInitializeAMMPool } from "./simulatePanopticSFPMInitializeAMMPool.js";
import { simulatePanopticSFPMMintTokenizedPosition } from "./simulatePanopticSFPMMintTokenizedPosition.js";

let id: Hex | undefined = undefined;

let pool: PanopticPool;

let position: PanopticPosition;

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 });

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,
);

const { request } = await simulatePanopticSFPMMintTokenizedPosition(
publicClient,
{
args: {
position,
amount: 10n ** 18n,
},
account: ALICE,
},
);
const hash = await walletClient.writeContract(request);
await publicClient.waitForTransactionReceipt({ hash });
} else {
await testClient.revert({ id });
}
id = await testClient.snapshot();
}, 100_000);

test("burn tokenized position", async () => {
const { request } = await simulatePanopticSFPMBurnTokenizedPosition(
publicClient,
{
args: {
position,
amount: 10n ** 18n,
},
account: ALICE,
},
);
const hash = await walletClient.writeContract(request);
await publicClient.waitForTransactionReceipt({ hash });

const balance = await getERC1155BalanceOf(publicClient, {
erc1155: position,
address: position.owner,
});

expect(balance.amount).toBe(0n);
});
Original file line number Diff line number Diff line change
@@ -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 PanopticSFPMBurnTokenizedPositionParameters = {
position: PanopticPosition;
amount: bigint;
};

export type SimulatePanopticSFPMBurnTokenizedPositionParameters<
TChain extends Chain | undefined = Chain,
TChainOverride extends Chain | undefined = Chain | undefined,
> = Omit<
SimulateContractParameters<
typeof semiFungiblePositionManagerABI,
"burnTokenizedPosition",
TChain,
TChainOverride
>,
"args" | "address" | "abi" | "functionName"
> & { args: PanopticSFPMBurnTokenizedPositionParameters };

export type SimulatePanopticSFPMBurnTokenizedPositionReturnType<
TChain extends Chain | undefined,
TChainOverride extends Chain | undefined = undefined,
> = SimulateContractReturnType<
typeof semiFungiblePositionManagerABI,
"burnTokenizedPosition",
TChain,
TChainOverride
>;

export const simulatePanopticSFPMBurnTokenizedPosition = <
TChain extends Chain | undefined,
TChainOverride extends Chain | undefined,
>(
client: Client<Transport, TChain>,
{
args: { position, amount },
...request
}: SimulatePanopticSFPMBurnTokenizedPositionParameters<
TChain,
TChainOverride
>,
): Promise<
SimulatePanopticSFPMBurnTokenizedPositionReturnType<TChain, TChainOverride>
> =>
simulateContract(client, {
address: position.pool.factory.semiFungiblePositionManager.address,
abi: semiFungiblePositionManagerABI,
functionName: "burnTokenizedPosition",
args: [[position.id], amount, MIN_TICK, MAX_TICK],
...request,
} as unknown as SimulateContractParameters<
typeof semiFungiblePositionManagerABI,
"burnTokenizedPosition",
TChain,
TChainOverride
>);
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { getERC1155BalanceOf } from "reverse-mirage";
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 { beforeEach, expect, test } from "vitest";
import { ALICE } from "../_test/constants.js";
import {
deployPool,
Expand Down Expand Up @@ -51,29 +52,31 @@ beforeEach(async () => {
}, 100_000);

test("mint tokenized position", async () => {
const 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,
);

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,
),
position,
amount: 10n ** 18n,
},
account: ALICE,
Expand All @@ -82,5 +85,10 @@ test("mint tokenized position", async () => {
const hash = await walletClient.writeContract(request);
await publicClient.waitForTransactionReceipt({ hash });

// TODO: check the minted position and amounts taken
const balance = await getERC1155BalanceOf(publicClient, {
erc1155: position,
address: position.owner,
});

expect(balance.amount).toBe(10n ** 18n);
});

0 comments on commit aa85a70

Please sign in to comment.