Skip to content

Commit

Permalink
panoptic public actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Sep 25, 2023
1 parent 05ae0a8 commit 9c62604
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/panoptic-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Typescript SDK for Panoptic
- [x] `getPanopticPoolData`
- [x] `getPanopticCollateralData`
- [x] `getPanopticCollateralPositionData`
- [ ] `getPanopticPositionData`
- [x] `getPanopticPositionData`

- Wallet Actions

Expand Down
36 changes: 36 additions & 0 deletions packages/panoptic-sdk/src/decorator/publicActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Chain, Client, Transport } from "viem";
import {
type GetPanopticCollateralDataParameters,
getPanopticCollateralData,
} from "../publicActions/getPanopticCollateralData.js";
import {
type GetPanopticCollateralPositionDataParameters,
getPanopticCollateralPositionData,
} from "../publicActions/getPanopticCollateralPositionData.js";
import {
type GetPanopticPoolDataParameters,
getPanopticPoolData,
} from "../publicActions/getPanopticPoolData.js";
import {
type GetPanopticPositionDataParameters,
getPanopticPositionData,
} from "../publicActions/getPanopticPositionData.js";

export const publicActionPanoptic = <
TChain extends Chain | undefined = Chain | undefined,
>(
client: Client<Transport, TChain>,
) => ({
getPanopticCollateralData: (args: GetPanopticCollateralDataParameters) =>
getPanopticCollateralData(client, args),

getPanopticCollateralPositionData: (
args: GetPanopticCollateralPositionDataParameters,
) => getPanopticCollateralPositionData(client, args),

getPanopticPoolData: (args: GetPanopticPoolDataParameters) =>
getPanopticPoolData(client, args),

getPanopticPositionData: (args: GetPanopticPositionDataParameters) =>
getPanopticPositionData(client, args),
});
26 changes: 26 additions & 0 deletions packages/panoptic-sdk/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export type {
PanopticCollateral,
PanopticCollateralData,
PanopticCollateralParamters,
PanopticCollateralPositionData,
PanopticFactory,
PanopticPool,
PanopticPoolData,
PanopticPosition,
PanopticPositionData,
PanopticSemiFungiblePositionManager,
} from "./types/index.js";

export { getPanopticCollateralData } from "./publicActions/getPanopticCollateralData.js";
export { getPanopticCollateralPositionData } from "./publicActions/getPanopticCollateralPositionData.js";
export { getPanopticPoolData } from "./publicActions/getPanopticPoolData.js";
export { getPanopticPositionData } from "./publicActions/getPanopticPositionData.js";

export { publicActionPanoptic } from "./decorator/publicActions.js";

export {
semiFungiblePositionManagerABI,
panopticFactoryABI,
panopticPoolABI,
collateralTrackerABI,
} from "./generated.js";
77 changes: 77 additions & 0 deletions packages/panoptic-sdk/src/publicActions/getPanopticPositionData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { createFraction } from "reverse-mirage";
import type { UniswapV3Tick } from "uniswap-v3-sdk";
import type { Chain, Client, ReadContractParameters, Transport } from "viem";
import { readContract } from "viem/actions";
import { panopticPoolABI } from "../generated.js";
import type {
PanopticPosition,
PanopticPositionData,
} from "../types/PanopticPosition.js";
import { getPanoptionLegData } from "./getPanoptionLegData.js";

export type GetPanopticPositionDataParameters = Omit<
ReadContractParameters<typeof panopticPoolABI, "totalSupply">,
"address" | "abi" | "functionName" | "args"
> & {
panopticPosition: PanopticPosition;
tick: UniswapV3Tick;
};

export type GetPanopticPositionDataReturnType = PanopticPositionData;

export const getPanopticPositionData = <TChain extends Chain | undefined>(
client: Client<Transport, TChain>,
{ panopticPosition, tick, ...request }: GetPanopticPositionDataParameters,
): Promise<GetPanopticPositionDataReturnType> =>
Promise.all([
readContract(client, {
abi: panopticPoolABI,
functionName: "optionPositionBalance",
address: panopticPosition.pool.address,
args: [panopticPosition.address, panopticPosition.id],
...request,
}),
panopticPosition.legs[0]
? getPanoptionLegData(client, {
leg: panopticPosition.legs[0],
address: panopticPosition.address,
pool: panopticPosition.pool,
tick,
...request,
})
: undefined,
panopticPosition.legs[1]
? getPanoptionLegData(client, {
leg: panopticPosition.legs[1],
address: panopticPosition.address,
pool: panopticPosition.pool,
tick,
...request,
})
: undefined,
panopticPosition.legs[2]
? getPanoptionLegData(client, {
leg: panopticPosition.legs[2],
address: panopticPosition.address,
pool: panopticPosition.pool,
tick,
...request,
})
: undefined,
panopticPosition.legs[3]
? getPanoptionLegData(client, {
leg: panopticPosition.legs[3],
address: panopticPosition.address,
pool: panopticPosition.pool,
tick,
...request,
})
: undefined,
] as const).then(([optionsBalance, leg0, leg1, leg2, leg3]) => ({
type: "panopticPositionData",
amount: optionsBalance[0],
token0Utilization: createFraction(optionsBalance[1], 2n ** 64n),
token1Utilization: createFraction(optionsBalance[2], 2n ** 64n),
token: panopticPosition,
legData: [leg0, leg1, leg2, leg3],
}));
86 changes: 86 additions & 0 deletions packages/panoptic-sdk/src/publicActions/getPanoptionLegData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import type { UniswapV3Tick } from "uniswap-v3-sdk";
import type {
Address,
Chain,
Client,
ReadContractParameters,
Transport,
} from "viem";
import { readContract } from "viem/actions";
import {
panopticPoolABI,
semiFungiblePositionManagerABI,
} from "../generated.js";
import type { PanopticPool } from "../types/PanopticPool.js";
import type {
PanoptionLeg,
PanoptionLegData,
} from "../types/PanopticPosition.js";
import type {} from "./getPanopticPositionData.js";

export type GetPanoptionLegDataParameters = Omit<
ReadContractParameters<typeof panopticPoolABI, "totalSupply">,
"address" | "abi" | "functionName" | "args"
> & {
leg: PanoptionLeg;
address: Address;
pool: PanopticPool;
tick: UniswapV3Tick;
};

export type GetPanoptionLegDataReturnType = PanoptionLegData;

export const getPanoptionLegData = <TChain extends Chain | undefined>(
client: Client<Transport, TChain>,
{ leg, pool, address, tick, ...request }: GetPanoptionLegDataParameters,
): Promise<GetPanoptionLegDataReturnType> =>
Promise.all([
readContract(client, {
abi: semiFungiblePositionManagerABI,
functionName: "getAccountLiquidity",
address: pool.factory.semiFungiblePositionManager.address,
args: [
pool.uniswapPool.address,
address,
leg.tokenType === "token0" ? 0n : 1n,
leg.tickLower.tick,
leg.tickUpper.tick,
],
...request,
}),
readContract(client, {
abi: semiFungiblePositionManagerABI,
functionName: "getAccountPremium",
address: pool.factory.semiFungiblePositionManager.address,
args: [
pool.uniswapPool.address,
address,
leg.tokenType === "token0" ? 0n : 1n,
leg.tickLower.tick,
leg.tickUpper.tick,
tick.tick,
leg.position === "short" ? 0n : 1n,
],
...request,
}),
readContract(client, {
abi: semiFungiblePositionManagerABI,
functionName: "getAccountFeesBase",
address: pool.factory.semiFungiblePositionManager.address,
args: [
pool.uniswapPool.address,
address,
leg.tokenType === "token0" ? 0n : 1n,
leg.tickLower.tick,
leg.tickUpper.tick,
],
...request,
}),
] as const).then(([legLiquidity, legPremium, legFeesBase]) => ({
liquidityAdded: legLiquidity && 2n ** 128n - 1n,
liquidityRemoved: legLiquidity >> 128n,
accountPremium0: legPremium[0],
accountPremium1: legPremium[1],
baseFee0: legFeesBase[0],
baseFee1: legFeesBase[1],
}));
2 changes: 2 additions & 0 deletions packages/panoptic-sdk/src/types/PanopticFactory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import type { WETH } from "reverse-mirage";
import type { UniswapV3Factory } from "uniswap-v3-sdk";
import type { Address } from "viem/accounts";
import type { PanopticSemiFungiblePositionManager } from "./PanopticSemiFungiblePositionManager.js";

export type PanopticFactory = {
type: "panopticFactory";
address: Address;
owner: Address;
uniswapFactory: UniswapV3Factory;
semiFungiblePositionManager: PanopticSemiFungiblePositionManager;
weth: WETH;
blockCreated: bigint;
};
12 changes: 7 additions & 5 deletions packages/panoptic-sdk/src/types/PanopticPosition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ export type PanopticPosition = BaseERC1155<"panopticPosition"> & {
legs: Tuple<PanoptionLeg | undefined, 4>;
};

export type PanopticPositionData = ERC1155Data<PanopticPosition> & {
export type PanoptionLegData = {
liquidityAdded: bigint;
liquidityRemoved: bigint;
accountPremiumOwed0: bigint;
accountPremiumOwed1: bigint;
accountPremiumGross0: bigint;
accountPremiumGross1: bigint;
accountPremium0: bigint;
accountPremium1: bigint;
baseFee0: bigint;
baseFee1: bigint;
};

export type PanopticPositionData = ERC1155Data<PanopticPosition> & {
token0Utilization: Fraction;
token1Utilization: Fraction;
legData: Tuple<PanoptionLegData | undefined, 4>;
};
4 changes: 2 additions & 2 deletions wagmi.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ export default defineConfig([
foundry({
project: "lib/panoptic-v1-core/",
include: [
"SemiFuniblePositionManager.sol/**",
"SemiFungiblePositionManager.sol/**",
"PanopticFactory.sol/**",
"PanopticPool.sol/**",
"CollateralTracker.sol/**"
"CollateralTracker.sol/**",
],
}),
],
Expand Down

0 comments on commit 9c62604

Please sign in to comment.