-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
211 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
packages/panoptic-sdk/src/publicActions/simulatePanopticMintOptions.test.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file removed
0
packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMRollTokenizedPosition.ts
Empty file.
139 changes: 139 additions & 0 deletions
139
packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMRollTokenizedPositions.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
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 { simulatePanopticSFPMInitializeAMMPool } from "./simulatePanopticSFPMInitializeAMMPool.js"; | ||
import { simulatePanopticSFPMMintTokenizedPosition } from "./simulatePanopticSFPMMintTokenizedPosition.js"; | ||
import { simulatePanopticSFPMRollTokenizedPositions } from "./simulatePanopticSFPMRollTokenizedPositions.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("roll tokenized position", async () => { | ||
const newPosition = createPanopticPosition( | ||
ALICE, | ||
pool, | ||
[ | ||
{ | ||
asset: "token0", | ||
optionRatio: 1, | ||
position: "short", | ||
tokenType: "token0", | ||
riskPartnerIndex: 0, | ||
tickLower: createUniswapV3Tick(10), | ||
tickUpper: createUniswapV3Tick(20), | ||
}, | ||
undefined, | ||
undefined, | ||
undefined, | ||
], | ||
sepolia.id, | ||
); | ||
|
||
const { request } = await simulatePanopticSFPMRollTokenizedPositions( | ||
publicClient, | ||
{ | ||
args: { | ||
oldPosition: position, | ||
newPosition, | ||
amount: 10n ** 18n, | ||
}, | ||
account: ALICE, | ||
}, | ||
); | ||
const hash = await walletClient.writeContract(request); | ||
await publicClient.waitForTransactionReceipt({ hash }); | ||
|
||
const oldBalance = await getERC1155BalanceOf(publicClient, { | ||
erc1155: position, | ||
address: position.owner, | ||
}); | ||
|
||
expect(oldBalance.amount).toBe(0n); | ||
|
||
const newBalance = await getERC1155BalanceOf(publicClient, { | ||
erc1155: newPosition, | ||
address: position.owner, | ||
}); | ||
|
||
expect(newBalance.amount).toBe(10n ** 18n); | ||
}); |
68 changes: 68 additions & 0 deletions
68
packages/panoptic-sdk/src/publicActions/simulatePanopticSFPMRollTokenizedPositions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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 PanopticSFPMRollTokenizedPositionsParameters = { | ||
oldPosition: PanopticPosition; | ||
newPosition: PanopticPosition; | ||
amount: bigint; | ||
}; | ||
|
||
export type SimulatePanopticSFPMRollTokenizedPositionsParameters< | ||
TChain extends Chain | undefined = Chain, | ||
TChainOverride extends Chain | undefined = Chain | undefined, | ||
> = Omit< | ||
SimulateContractParameters< | ||
typeof semiFungiblePositionManagerABI, | ||
"rollTokenizedPositions", | ||
TChain, | ||
TChainOverride | ||
>, | ||
"args" | "address" | "abi" | "functionName" | ||
> & { args: PanopticSFPMRollTokenizedPositionsParameters }; | ||
|
||
export type SimulatePanopticSFPMRollTokenizedPositionsReturnType< | ||
TChain extends Chain | undefined, | ||
TChainOverride extends Chain | undefined = undefined, | ||
> = SimulateContractReturnType< | ||
typeof semiFungiblePositionManagerABI, | ||
"rollTokenizedPositions", | ||
TChain, | ||
TChainOverride | ||
>; | ||
|
||
export const simulatePanopticSFPMRollTokenizedPositions = < | ||
TChain extends Chain | undefined, | ||
TChainOverride extends Chain | undefined, | ||
>( | ||
client: Client<Transport, TChain>, | ||
{ | ||
args: { oldPosition, newPosition, amount }, | ||
...request | ||
}: SimulatePanopticSFPMRollTokenizedPositionsParameters< | ||
TChain, | ||
TChainOverride | ||
>, | ||
): Promise< | ||
SimulatePanopticSFPMRollTokenizedPositionsReturnType<TChain, TChainOverride> | ||
> => | ||
simulateContract(client, { | ||
address: newPosition.pool.factory.semiFungiblePositionManager.address, | ||
abi: semiFungiblePositionManagerABI, | ||
functionName: "rollTokenizedPositions", | ||
args: [oldPosition.id, newPosition.id, amount, MIN_TICK, MAX_TICK], | ||
...request, | ||
} as unknown as SimulateContractParameters< | ||
typeof semiFungiblePositionManagerABI, | ||
"rollTokenizedPositions", | ||
TChain, | ||
TChainOverride | ||
>); |