diff --git a/packages/panoptic-sdk/README.md b/packages/panoptic-sdk/README.md index a6bb4d7..76782ec 100644 --- a/packages/panoptic-sdk/README.md +++ b/packages/panoptic-sdk/README.md @@ -31,7 +31,7 @@ Typescript SDK for Panoptic - [ ] `calculatePanopticForceExercise` - [ ] `calcuatePanopticLiquidateAccount` - [ ] `calculatePanopticCollateralMint` - - [ ] `calculatePanopticCollateralDeposit` + - [x] `calculatePanopticCollateralDeposit` - [ ] `calculatePanopticCollateralRedeem` - [ ] `calculatePanopticCollateralWithdraw` - [ ] `calculateSFPMInitializeAMMPool` diff --git a/packages/panoptic-sdk/src/_test/constants.ts b/packages/panoptic-sdk/src/_test/constants.ts index 6d5adfc..b6dbc5d 100644 --- a/packages/panoptic-sdk/src/_test/constants.ts +++ b/packages/panoptic-sdk/src/_test/constants.ts @@ -1,3 +1,8 @@ +import { createERC20 } from "reverse-mirage"; +import { sepolia } from "viem/chains"; +import { createPanopticCollateral } from "../utils/createPanopticCollateral.js"; +import { baseParameters } from "./utils.js"; + // Test accounts export const ACCOUNTS = [ "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266", @@ -6,3 +11,35 @@ export const ACCOUNTS = [ // Named accounts export const [ALICE, BOB] = ACCOUNTS; + +export const token0 = createERC20( + "0x0000000000000000000000000000000000000001", + "name", + "symbol", + 18, + sepolia.id, +); + +export const token1 = createERC20( + "0x0000000000000000000000000000000000000002", + "name", + "symbol", + 18, + sepolia.id, +); + +export const collateralToken0 = createPanopticCollateral( + "0x0000000000000000000000000000000000000003", + token0, + token1, + 500, + baseParameters, +); + +export const collateralToken1 = createPanopticCollateral( + "0x0000000000000000000000000000000000000004", + token1, + token0, + 500, + baseParameters, +); diff --git a/packages/panoptic-sdk/src/_test/utils.ts b/packages/panoptic-sdk/src/_test/utils.ts index ff9eacc..caff1d9 100644 --- a/packages/panoptic-sdk/src/_test/utils.ts +++ b/packages/panoptic-sdk/src/_test/utils.ts @@ -62,7 +62,8 @@ export const publicClient = createPublicClient({ export const walletClient = createWalletClient({ chain: anvil, transport: http(), - account: ALICE, + // @ts-ignore: Viem bug? + account: { address: ALICE }, }); export const baseParameters: PanopticCollateralParamters = { @@ -111,6 +112,7 @@ export const deployPool = async (): Promise => { functionName: "mint", args: [ALICE, 10n ** 36n], }); + console.log(walletClient); const mintHashA = await walletClient.writeContract(mintRequestA); await publicClient.waitForTransactionReceipt({ hash: mintHashA, diff --git a/packages/panoptic-sdk/src/decorator/publicActions.ts b/packages/panoptic-sdk/src/decorator/publicActions.ts index c16cd3d..0729240 100644 --- a/packages/panoptic-sdk/src/decorator/publicActions.ts +++ b/packages/panoptic-sdk/src/decorator/publicActions.ts @@ -1,20 +1,4 @@ import type { Chain, Client, Transport } from "viem"; -import { - type SimulatePanopticCollateralDepositParameters, - simulatePanopticCollateralDeposit, -} from "../publicActions/collateralTracker/simulatePanopticCollateralDeposit.js"; -import { - type SimulatePanopticCollateralMintParameters, - simulatePanopticCollateralMint, -} from "../publicActions/collateralTracker/simulatePanopticCollateralMint.js"; -import { - type SimulatePanopticCollateralRedeemParameters, - simulatePanopticCollateralRedeem, -} from "../publicActions/collateralTracker/simulatePanopticCollateralRedeem.js"; -import { - type SimulatePanopticCollateralWithdrawParameters, - simulatePanopticCollateralWithdraw, -} from "../publicActions/collateralTracker/simulatePanopticCollateralWithdraw.js"; import { type GetPanopticCollateralDataParameters, getPanopticCollateralData, @@ -35,6 +19,22 @@ import { type SimulatePanopticBurnOptionsParameters, simulatePanopticBurnOptions, } from "../publicActions/simulatePanopticBurnOptions.js"; +import { + type SimulatePanopticCollateralDepositParameters, + simulatePanopticCollateralDeposit, +} from "../publicActions/simulatePanopticCollateralDeposit.js"; +import { + type SimulatePanopticCollateralMintParameters, + simulatePanopticCollateralMint, +} from "../publicActions/simulatePanopticCollateralMint.js"; +import { + type SimulatePanopticCollateralRedeemParameters, + simulatePanopticCollateralRedeem, +} from "../publicActions/simulatePanopticCollateralRedeem.js"; +import { + type SimulatePanopticCollateralWithdrawParameters, + simulatePanopticCollateralWithdraw, +} from "../publicActions/simulatePanopticCollateralWithdraw.js"; import { type SimulatePanopticForceExerciseParameters, simulatePanopticForceExercise, diff --git a/packages/panoptic-sdk/src/index.ts b/packages/panoptic-sdk/src/index.ts index 423e114..c684189 100644 --- a/packages/panoptic-sdk/src/index.ts +++ b/packages/panoptic-sdk/src/index.ts @@ -16,10 +16,10 @@ export { getPanopticCollateralPositionData } from "./publicActions/getPanopticCo export { getPanopticPoolData } from "./publicActions/getPanopticPoolData.js"; export { getPanopticPositionData } from "./publicActions/getPanopticPositionData.js"; -export { simulatePanopticCollateralDeposit } from "./publicActions/collateralTracker/simulatePanopticCollateralDeposit.js"; -export { simulatePanopticCollateralWithdraw } from "./publicActions/collateralTracker/simulatePanopticCollateralWithdraw.js"; -export { simulatePanopticCollateralMint } from "./publicActions/collateralTracker/simulatePanopticCollateralMint.js"; -export { simulatePanopticCollateralRedeem } from "./publicActions/collateralTracker/simulatePanopticCollateralRedeem.js"; +export { simulatePanopticCollateralDeposit } from "./publicActions/simulatePanopticCollateralDeposit.js"; +export { simulatePanopticCollateralWithdraw } from "./publicActions/simulatePanopticCollateralWithdraw.js"; +export { simulatePanopticCollateralMint } from "./publicActions/simulatePanopticCollateralMint.js"; +export { simulatePanopticCollateralRedeem } from "./publicActions/simulatePanopticCollateralRedeem.js"; export { simulatePanopticMintOptions } from "./publicActions/simulatePanopticMintOptions.js"; export { simulatePanopticRollOptions } from "./publicActions/simulatePanopticRollOptions.js"; export { simulatePanopticBurnOptions } from "./publicActions/simulatePanopticBurnOptions.js"; diff --git a/packages/panoptic-sdk/src/publicActions/getPanopticCollateralData.test.ts b/packages/panoptic-sdk/src/publicActions/getPanopticCollateralData.test.ts new file mode 100644 index 0000000..1da9424 --- /dev/null +++ b/packages/panoptic-sdk/src/publicActions/getPanopticCollateralData.test.ts @@ -0,0 +1,70 @@ +import { createAmountFromString } from "reverse-mirage"; +import { type Hex } from "viem"; +import { simulateContract, writeContract } from "viem/actions"; +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/index.js"; +import { getPanopticCollateralData } from "./getPanopticCollateralData.js"; +import { simulatePanopticCollateralDeposit } from "./simulatePanopticCollateralDeposit.js"; + +let id: Hex | undefined = undefined; + +let pool: PanopticPool; + +beforeEach(async () => { + if (id === undefined) { + pool = await deployPool(); + + const { request: approveRequest } = await simulateContract(publicClient, { + address: pool.collateralTracker0.underlyingToken.address, + abi: mockErc20ABI, + functionName: "approve", + args: [pool.collateralTracker0.address, 10n ** 18n], + account: ALICE, + }); + + const approveHash = await writeContract(walletClient, approveRequest); + await publicClient.waitForTransactionReceipt({ hash: approveHash }); + + const { request: depositRequest } = await simulatePanopticCollateralDeposit( + publicClient, + { + args: { + collateral: pool.collateralTracker0, + amount: createAmountFromString( + pool.collateralTracker0.underlyingToken, + "1", + ), + to: ALICE, + }, + account: ALICE, + }, + ); + const depositHash = await writeContract(walletClient, depositRequest); + await publicClient.waitForTransactionReceipt({ hash: depositHash }); + } else { + await testClient.revert({ id }); + } + id = await testClient.snapshot(); +}, 100_000); + +test("get collateral data", async () => { + const collateralData = await getPanopticCollateralData(publicClient, { + panopticCollateral: pool.collateralTracker0, + }); + + expect(collateralData.collateral).toStrictEqual(pool.collateralTracker0); + expect(collateralData.totalSupply).toStrictEqual( + createAmountFromString(pool.collateralTracker0, ".999"), + ); + expect(collateralData.poolAssets).toStrictEqual( + createAmountFromString(pool.collateralTracker0.underlyingToken, "1"), + ); +}); diff --git a/packages/panoptic-sdk/src/publicActions/getPanopticCollateralPositionData.test.ts b/packages/panoptic-sdk/src/publicActions/getPanopticCollateralPositionData.test.ts new file mode 100644 index 0000000..58c9b49 --- /dev/null +++ b/packages/panoptic-sdk/src/publicActions/getPanopticCollateralPositionData.test.ts @@ -0,0 +1,71 @@ +import { createAmountFromString } from "reverse-mirage"; +import { type Hex } from "viem"; +import { simulateContract, writeContract } from "viem/actions"; +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/index.js"; +import { getPanopticCollateralPositionData } from "./getPanopticCollateralPositionData.js"; +import { simulatePanopticCollateralDeposit } from "./simulatePanopticCollateralDeposit.js"; + +let id: Hex | undefined = undefined; + +let pool: PanopticPool; + +beforeEach(async () => { + if (id === undefined) { + pool = await deployPool(); + + const { request: approveRequest } = await simulateContract(publicClient, { + address: pool.collateralTracker0.underlyingToken.address, + abi: mockErc20ABI, + functionName: "approve", + args: [pool.collateralTracker0.address, 10n ** 18n], + account: ALICE, + }); + + const approveHash = await writeContract(walletClient, approveRequest); + await publicClient.waitForTransactionReceipt({ hash: approveHash }); + + const { request: depositRequest } = await simulatePanopticCollateralDeposit( + publicClient, + { + args: { + collateral: pool.collateralTracker0, + amount: createAmountFromString( + pool.collateralTracker0.underlyingToken, + "1", + ), + to: ALICE, + }, + account: ALICE, + }, + ); + const depositHash = await writeContract(walletClient, depositRequest); + await publicClient.waitForTransactionReceipt({ hash: depositHash }); + } else { + await testClient.revert({ id }); + } + id = await testClient.snapshot(); +}, 100_000); + +test("get collateral data", async () => { + const collateralPosition = await getPanopticCollateralPositionData( + publicClient, + { + panopticCollateral: pool.collateralTracker0, + address: ALICE, + }, + ); + + expect(collateralPosition.token).toStrictEqual(pool.collateralTracker0); + expect(collateralPosition.amount).toStrictEqual( + createAmountFromString(pool.collateralTracker0, ".999").amount, + ); +}); diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralDeposit.test.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralDeposit.test.ts similarity index 88% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralDeposit.test.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralDeposit.test.ts index 5a66cbc..04e9cff 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralDeposit.test.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralDeposit.test.ts @@ -2,15 +2,15 @@ import { createAmountFromString } from "reverse-mirage"; import { type Hex } from "viem"; import { simulateContract, writeContract } from "viem/actions"; import { beforeEach, test } from "vitest"; -import { ALICE } from "../../_test/constants.js"; +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/index.js"; +} from "../_test/utils.js"; +import { mockErc20ABI } from "../generated.js"; +import type { PanopticPool } from "../types/index.js"; import { simulatePanopticCollateralDeposit } from "./simulatePanopticCollateralDeposit.js"; let id: Hex | undefined = undefined; diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralDeposit.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralDeposit.ts similarity index 93% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralDeposit.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralDeposit.ts index 1047b74..cbe8b75 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralDeposit.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralDeposit.ts @@ -8,8 +8,8 @@ import type { Transport, } from "viem"; import { simulateContract } from "viem/contract"; -import { collateralTrackerABI } from "../../generated.js"; -import type { PanopticCollateral } from "../../types/PanopticCollateral.js"; +import { collateralTrackerABI } from "../generated.js"; +import type { PanopticCollateral } from "../types/PanopticCollateral.js"; export type PanopticCollateralDepositParameters< TPanopticCollateral extends PanopticCollateral, diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralMint.test.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralMint.test.ts similarity index 85% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralMint.test.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralMint.test.ts index 88b76fd..891cf1a 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralMint.test.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralMint.test.ts @@ -2,18 +2,15 @@ import { createAmountFromString } from "reverse-mirage"; import { type Hex } from "viem"; import { simulateContract, writeContract } from "viem/actions"; import { beforeEach, test } from "vitest"; -import { ALICE } from "../../_test/constants.js"; +import { ALICE } from "../_test/constants.js"; import { deployPool, publicClient, testClient, walletClient, -} from "../../_test/utils.js"; -import { mockErc20ABI } from "../../generated.js"; -import { - type PanopticPool, - simulatePanopticCollateralMint, -} from "../../index.js"; +} from "../_test/utils.js"; +import { mockErc20ABI } from "../generated.js"; +import { type PanopticPool, simulatePanopticCollateralMint } from "../index.js"; let id: Hex | undefined = undefined; diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralMint.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralMint.ts similarity index 93% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralMint.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralMint.ts index e7f9e53..36f1662 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralMint.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralMint.ts @@ -8,8 +8,8 @@ import type { Transport, } from "viem"; import { simulateContract } from "viem/contract"; -import { collateralTrackerABI } from "../../generated.js"; -import type { PanopticCollateral } from "../../types/PanopticCollateral.js"; +import { collateralTrackerABI } from "../generated.js"; +import type { PanopticCollateral } from "../types/PanopticCollateral.js"; export type PanopticCollateralMintParameters< TPanopticCollateral extends PanopticCollateral, diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralRedeem.test.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralRedeem.test.ts similarity index 92% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralRedeem.test.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralRedeem.test.ts index 7f25238..0282a62 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralRedeem.test.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralRedeem.test.ts @@ -2,19 +2,19 @@ import { createAmountFromString } from "reverse-mirage"; import { type Hex } from "viem"; import { simulateContract, writeContract } from "viem/actions"; import { beforeEach, test } from "vitest"; -import { ALICE } from "../../_test/constants.js"; +import { ALICE } from "../_test/constants.js"; import { deployPool, publicClient, testClient, walletClient, -} from "../../_test/utils.js"; -import { mockErc20ABI } from "../../generated.js"; +} from "../_test/utils.js"; +import { mockErc20ABI } from "../generated.js"; import { type PanopticPool, simulatePanopticCollateralDeposit, simulatePanopticCollateralRedeem, -} from "../../index.js"; +} from "../index.js"; let id: Hex | undefined = undefined; diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralRedeem.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralRedeem.ts similarity index 93% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralRedeem.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralRedeem.ts index 1d18e94..54d004f 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralRedeem.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralRedeem.ts @@ -8,8 +8,8 @@ import type { Transport, } from "viem"; import { simulateContract } from "viem/contract"; -import { collateralTrackerABI } from "../../generated.js"; -import type { PanopticCollateral } from "../../types/PanopticCollateral.js"; +import { collateralTrackerABI } from "../generated.js"; +import type { PanopticCollateral } from "../types/PanopticCollateral.js"; export type PanopticCollateralRedeemParameters< TPanopticCollateral extends PanopticCollateral, diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralWithdraw.test.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralWithdraw.test.ts similarity index 92% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralWithdraw.test.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralWithdraw.test.ts index 4094bf0..9d1e393 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralWithdraw.test.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralWithdraw.test.ts @@ -2,19 +2,19 @@ import { createAmountFromString } from "reverse-mirage"; import { type Hex } from "viem"; import { simulateContract, writeContract } from "viem/actions"; import { beforeEach, test } from "vitest"; -import { ALICE } from "../../_test/constants.js"; +import { ALICE } from "../_test/constants.js"; import { deployPool, publicClient, testClient, walletClient, -} from "../../_test/utils.js"; -import { mockErc20ABI } from "../../generated.js"; +} from "../_test/utils.js"; +import { mockErc20ABI } from "../generated.js"; import { type PanopticPool, simulatePanopticCollateralDeposit, simulatePanopticCollateralWithdraw, -} from "../../index.js"; +} from "../index.js"; let id: Hex | undefined = undefined; diff --git a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralWithdraw.ts b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralWithdraw.ts similarity index 93% rename from packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralWithdraw.ts rename to packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralWithdraw.ts index e4aa54f..1e727d9 100644 --- a/packages/panoptic-sdk/src/publicActions/collateralTracker/simulatePanopticCollateralWithdraw.ts +++ b/packages/panoptic-sdk/src/publicActions/simulatePanopticCollateralWithdraw.ts @@ -8,8 +8,8 @@ import type { Transport, } from "viem"; import { simulateContract } from "viem/contract"; -import { collateralTrackerABI } from "../../generated.js"; -import type { PanopticCollateral } from "../../types/PanopticCollateral.js"; +import { collateralTrackerABI } from "../generated.js"; +import type { PanopticCollateral } from "../types/PanopticCollateral.js"; export type PanopticCollateralWithdrawParameters< TPanopticCollateral extends PanopticCollateral, diff --git a/packages/panoptic-sdk/src/utils/collateralTracker/calculatePanopticCollateralDeposit.test.ts b/packages/panoptic-sdk/src/utils/collateralTracker/calculatePanopticCollateralDeposit.test.ts new file mode 100644 index 0000000..4e51633 --- /dev/null +++ b/packages/panoptic-sdk/src/utils/collateralTracker/calculatePanopticCollateralDeposit.test.ts @@ -0,0 +1,34 @@ +import { createAmountFromString } from "reverse-mirage"; +import { beforeEach, expect, test } from "vitest"; +import { collateralToken0 } from "../../_test/constants.js"; +import type { PanopticCollateralData } from "../../types/PanopticCollateral.js"; +import { calculatePanopticCollateralDeposit } from "./calculatePanopticCollateralDeposit.js"; + +let collateralData: PanopticCollateralData; + +beforeEach(() => { + collateralData = { + type: "panopticCollateralData", + collateral: collateralToken0, + poolAssets: createAmountFromString(collateralToken0.underlyingToken, "0"), + inAmm: createAmountFromString(collateralToken0.underlyingToken, "0"), + totalSupply: createAmountFromString(collateralToken0, "0"), + }; +}); + +test("calculate collateral deposit", () => { + const collateralDeposit = calculatePanopticCollateralDeposit({ + collateralData, + amount: createAmountFromString(collateralToken0.underlyingToken, "1"), + }); + + expect(collateralDeposit).toStrictEqual( + createAmountFromString(collateralToken0, ".999"), + ); + expect(collateralData.poolAssets).toStrictEqual( + createAmountFromString(collateralToken0.underlyingToken, "1"), + ); + expect(collateralData.totalSupply).toStrictEqual( + createAmountFromString(collateralToken0, ".999"), + ); +}); diff --git a/packages/panoptic-sdk/src/utils/collateralTracker/calculatePanopticCollateralDeposit.ts b/packages/panoptic-sdk/src/utils/collateralTracker/calculatePanopticCollateralDeposit.ts index e69de29..cea87fa 100644 --- a/packages/panoptic-sdk/src/utils/collateralTracker/calculatePanopticCollateralDeposit.ts +++ b/packages/panoptic-sdk/src/utils/collateralTracker/calculatePanopticCollateralDeposit.ts @@ -0,0 +1,39 @@ +import { + type ERC20Amount, + amountAdd, + amountEqualTo, + createAmountFromRaw, +} from "reverse-mirage"; +import type { + PanopticCollateralData, + PanopticCollateralPositionData, +} from "../../types/PanopticCollateral.js"; + +export const calculatePanopticCollateralDeposit = < + TPanopticCollateralData extends PanopticCollateralData, +>({ + collateralData, + amount, +}: { + collateralData: TPanopticCollateralData; + amount: ERC20Amount; +}): PanopticCollateralPositionData => { + // TODO: error for too large + // TODO: poke panoptic median + + const amountSubFee = + amount.amount - + (amount.amount * + collateralData.collateral.parameters.commissionFee.numerator) / + collateralData.collateral.parameters.commissionFee.denominator; + + const shares = amountEqualTo(collateralData.totalSupply, 0) + ? amountSubFee + : (amountSubFee * collateralData.totalSupply.amount) / + (collateralData.inAmm.amount + collateralData.poolAssets.amount); + + collateralData.poolAssets = amountAdd(collateralData.poolAssets, amount); + collateralData.totalSupply.amount += shares; + + return createAmountFromRaw(collateralData.collateral, shares); +};