Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Sep 28, 2023
1 parent 4bd872d commit 4c6c46c
Show file tree
Hide file tree
Showing 17 changed files with 299 additions and 49 deletions.
2 changes: 1 addition & 1 deletion packages/panoptic-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Typescript SDK for Panoptic
- [ ] `calculatePanopticForceExercise`
- [ ] `calcuatePanopticLiquidateAccount`
- [ ] `calculatePanopticCollateralMint`
- [ ] `calculatePanopticCollateralDeposit`
- [x] `calculatePanopticCollateralDeposit`
- [ ] `calculatePanopticCollateralRedeem`
- [ ] `calculatePanopticCollateralWithdraw`
- [ ] `calculateSFPMInitializeAMMPool`
Expand Down
37 changes: 37 additions & 0 deletions packages/panoptic-sdk/src/_test/constants.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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,
);
4 changes: 3 additions & 1 deletion packages/panoptic-sdk/src/_test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -111,6 +112,7 @@ export const deployPool = async (): Promise<PanopticPool> => {
functionName: "mint",
args: [ALICE, 10n ** 36n],
});
console.log(walletClient);
const mintHashA = await walletClient.writeContract(mintRequestA);
await publicClient.waitForTransactionReceipt({
hash: mintHashA,
Expand Down
32 changes: 16 additions & 16 deletions packages/panoptic-sdk/src/decorator/publicActions.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions packages/panoptic-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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"),
);
});
Original file line number Diff line number Diff line change
@@ -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,
);
});
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Loading

0 comments on commit 4c6c46c

Please sign in to comment.