Skip to content

Commit

Permalink
fix test setup using sepolia
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Sep 27, 2023
1 parent fbba9ad commit 2da3d3b
Show file tree
Hide file tree
Showing 27 changed files with 654 additions and 635 deletions.
23 changes: 11 additions & 12 deletions packages/panoptic-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Typescript SDK for Panoptic
- [x] `createPanopticPosition`
- [x] `calculatePanopticTokenID`
- [x] `calculatePanopticPositionKey`
- [ ] `calculatePanopticPoolAddress`
- [ ] `calculateCollateralTrackerAddress`
- [ ] `calculatePanopticMintOptions`
- [ ] `calculatePanopticRollOptions`
- [ ] `calculatePanopticBurnOptions`
Expand All @@ -40,18 +42,15 @@ Typescript SDK for Panoptic
- [x] `getPanopticCollateralData`
- [x] `getPanopticCollateralPositionData`
- [x] `getPanopticPositionData`

- Wallet Actions

- [x] `writePanopticMintOptions`
- [x] `writePanopticRollOptions`
- [x] `writePanopticBurnOptions`
- [x] `writePanopticForceExercise`
- [x] `writePanopticLiquidateAccount`
- [x] `writePanopticCollateralMint`
- [x] `writePanopticCollateralDeposit`
- [x] `writePanopticCollateralRedeem`
- [x] `writePanopticCollateralWithdraw`
- [x] `simulatePanopticMintOptions`
- [x] `simulatePanopticRollOptions`
- [x] `simulatePanopticBurnOptions`
- [x] `simulatePanopticForceExercise`
- [x] `simulatePanopticLiquidateAccount`
- [x] `simulatePanopticCollateralMint`
- [x] `simulatePanopticCollateralDeposit`
- [x] `simulatePanopticCollateralRedeem`
- [x] `simulatePanopticCollateralWithdraw`

- Chains

Expand Down
1 change: 1 addition & 0 deletions packages/panoptic-sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"devDependencies": {
"@biomejs/biome": "^1.2.2",
"@uniswap/v3-core": "^1.0.1",
"@viem/anvil": "^0.0.6",
"tsup": "^7.2.0",
"typescript": "^5.2.2",
Expand Down
13 changes: 10 additions & 3 deletions packages/panoptic-sdk/src/_test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { startProxy } from "@viem/anvil";
import { testClient } from "./utils.js";

export default async function () {
return await startProxy({
port: 8545, // By default, the proxy will listen on port 8545.
host: "::", // By default, the proxy will listen on all interfaces.
const shutdown = await startProxy({
options: {
forkUrl:
"https://eth-sepolia.g.alchemy.com/v2/kw-ycolm1dq00fqT_Y-b-FkV-Ai6h38m",
},
});

await testClient.setAutomine(true);

return shutdown;
}
188 changes: 183 additions & 5 deletions packages/panoptic-sdk/src/_test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
import { createFraction } from "reverse-mirage";
import { createERC20, createFraction } from "reverse-mirage";
import invariant from "tiny-invariant";
import {
createUniswapV3Pool,
uniswapV3FactoryABI,
uniswapV3PoolABI,
} from "uniswap-v3-sdk";
import {
http,
type Hex,
createPublicClient,
createTestClient,
createWalletClient,
} from "viem";
import { foundry } from "viem/chains";
import { sepolia } from "viem/chains";
import type { Chain } from "viem/chains";
import type { PanopticCollateralParamters } from "../index.js";
import { readContract } from "viem/contract";
import MockERC20Bytecode from "../../../../lib/panoptic-v1-core/artifacts/contracts/MockERC20.sol/MockERC20.json";
import { sepoliaPanoptic } from "../chains/sepolia.js";
import {
mockErc20ABI,
panopticFactoryABI,
panopticPoolABI,
} from "../generated.js";
import type {
PanopticCollateralParamters,
PanopticPool,
} from "../types/index.js";
import { createPanopticCollateral } from "../utils/createPanopticCollateral.js";
import { createPanopticPool } from "../utils/createPanopticPool.js";
import { ALICE } from "./constants.js";

export const pool = Number(process.env.VITEST_POOL_ID ?? 1);
export const anvil = {
...foundry, // We are using a mainnet fork for testing.
id: foundry.id,
...sepolia, // We are using a mainnet fork for testing.
rpcUrls: {
// These rpc urls are automatically used in the transports.
default: {
Expand Down Expand Up @@ -57,3 +76,162 @@ export const baseParameters: PanopticCollateralParamters = {
saturatedPoolUtilization: createFraction(9_000, 10_000),
exerciseCost: createFraction(-1_024, 10_000),
};

export const deployPool = async (): Promise<PanopticPool> => {
// Deploy tokens
let deployHash = await walletClient.deployContract({
account: ALICE,
abi: mockErc20ABI,
bytecode: MockERC20Bytecode.bytecode.object as Hex,
args: ["name", "symbol", 18],
});
const { contractAddress: tokenAAddress } =
await publicClient.waitForTransactionReceipt({
hash: deployHash,
});
invariant(tokenAAddress);
const tokenA = createERC20(tokenAAddress, "name", "symbol", 18, sepolia.id);
deployHash = await walletClient.deployContract({
account: ALICE,
abi: mockErc20ABI,
bytecode: MockERC20Bytecode.bytecode.object as Hex,
args: ["name", "symbol", 18],
});
const { contractAddress: tokenBAddress } =
await publicClient.waitForTransactionReceipt({
hash: deployHash,
});
invariant(tokenBAddress);
const tokenB = createERC20(tokenBAddress, "name", "symbol", 18, sepolia.id);

// Mint
const { request: mintRequestA } = await publicClient.simulateContract({
abi: mockErc20ABI,
address: tokenA.address,
functionName: "mint",
args: [ALICE, 10n ** 36n],
});
const mintHashA = await walletClient.writeContract(mintRequestA);
await publicClient.waitForTransactionReceipt({
hash: mintHashA,
});

const { request: mintRequestB } = await publicClient.simulateContract({
abi: mockErc20ABI,
address: tokenB.address,
functionName: "mint",
args: [ALICE, 10n ** 36n],
});
const mintHashB = await walletClient.writeContract(mintRequestB);
await publicClient.waitForTransactionReceipt({
hash: mintHashB,
});

// Approve
const { request: approveRequestA } = await publicClient.simulateContract({
abi: mockErc20ABI,
address: tokenA.address,
functionName: "approve",
args: [sepoliaPanoptic.factory.address, 10n ** 36n],
account: ALICE,
});
const approveHashA = await walletClient.writeContract(approveRequestA);
await publicClient.waitForTransactionReceipt({
hash: approveHashA,
});

const { request: approveRequestB } = await publicClient.simulateContract({
abi: mockErc20ABI,
address: tokenB.address,
functionName: "approve",
args: [sepoliaPanoptic.factory.address, 10n ** 36n],
account: ALICE,
});
const approveHashB = await walletClient.writeContract(approveRequestB);
await publicClient.waitForTransactionReceipt({
hash: approveHashB,
});

// Create pool
const { request } = await publicClient.simulateContract({
address: sepoliaPanoptic.factory.uniswapFactory.address,
abi: uniswapV3FactoryABI,
functionName: "createPool",
args: [tokenA.address, tokenB.address, 500],
account: ALICE,
});
const hash = await walletClient.writeContract(request);
await publicClient.waitForTransactionReceipt({
hash,
});

const uniPool = createUniswapV3Pool(
tokenA,
tokenB,
500,
sepoliaPanoptic.factory.uniswapFactory.address,
);

const { request: initializeRequest } = await publicClient.simulateContract({
address: uniPool.address,
abi: uniswapV3PoolABI,
functionName: "initialize",
args: [2n ** 96n],
account: ALICE,
});
const initializeHash = await walletClient.writeContract(initializeRequest);
await publicClient.waitForTransactionReceipt({
hash: initializeHash,
});
const { request: newPoolRequest, result: panopticPoolAddres } =
await publicClient.simulateContract({
address: sepoliaPanoptic.factory.address,
abi: panopticFactoryABI,
functionName: "deployNewPool",
args: [tokenA.address, tokenB.address, 500, 0n],
account: ALICE,
});

const newPoolHash = await walletClient.writeContract(newPoolRequest);
await publicClient.waitForTransactionReceipt({
hash: newPoolHash,
});

const collateralTracker0Address = await readContract(publicClient, {
abi: panopticPoolABI,
address: panopticPoolAddres,
functionName: "collateralToken0",
});

const collateralTracker1Address = await readContract(publicClient, {
abi: panopticPoolABI,
address: panopticPoolAddres,
functionName: "collateralToken1",
});

const collateral0 = createPanopticCollateral(
collateralTracker0Address,
uniPool.token0,
uniPool.token1,
uniPool.feeTier,
baseParameters,
);

const collateral1 = createPanopticCollateral(
collateralTracker1Address,
uniPool.token1,
uniPool.token0,
uniPool.feeTier,
baseParameters,
);

const pool = createPanopticPool(
panopticPoolAddres,
sepoliaPanoptic.factory,
collateral0,
collateral1,
uniPool,
);

return pool;
};
82 changes: 82 additions & 0 deletions packages/panoptic-sdk/src/decorator/publicActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
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 @@ -15,6 +31,27 @@ import {
type GetPanopticPositionDataParameters,
getPanopticPositionData,
} from "../publicActions/getPanopticPositionData.js";
import {
type SimulatePanopticBurnOptionsParameters,
simulatePanopticBurnOptions,
} from "../publicActions/simulateBurnOptions.js";
import {
type SimulatePanopticForceExerciseParameters,
simulatePanopticForceExercise,
} from "../publicActions/simulateForceExercise.js";
import {
type SimulatePanopticLiquidateAccountParameters,
simulatePanopticLiquidateAccount,
} from "../publicActions/simulateLiquidateAccount.js";
import {
type SimulatePanopticMintOptionsParameters,
simulatePanopticMintOptions,
} from "../publicActions/simulateMintOptions.js";
import {
type SimulatePanopticRollOptionsParameters,
simulatePanopticRollOptions,
} from "../publicActions/simulateRollOptions.js";
import type { PanopticCollateral } from "../types/PanopticCollateral.js";

export const publicActionPanoptic = <
TChain extends Chain | undefined = Chain | undefined,
Expand All @@ -33,4 +70,49 @@ export const publicActionPanoptic = <

getPanopticPositionData: (args: GetPanopticPositionDataParameters) =>
getPanopticPositionData(client, args),
simulatePanopticCollateralDeposit: <
TPanopticCollateral extends PanopticCollateral,
>(
args: SimulatePanopticCollateralDepositParameters<
TPanopticCollateral,
TChain
>,
) => simulatePanopticCollateralDeposit(client, args),
simulatePanopticCollateralMint: <
TPanopticCollateral extends PanopticCollateral,
>(
args: SimulatePanopticCollateralMintParameters<TPanopticCollateral, TChain>,
) => simulatePanopticCollateralMint(client, args),
simulatePanopticCollateralWithdraw: <
TPanopticCollateral extends PanopticCollateral,
>(
args: SimulatePanopticCollateralWithdrawParameters<
TPanopticCollateral,
TChain
>,
) => simulatePanopticCollateralWithdraw(client, args),
simulatePanopticCollateralRedeem: <
TPanopticCollateral extends PanopticCollateral,
>(
args: SimulatePanopticCollateralRedeemParameters<
TPanopticCollateral,
TChain
>,
) => simulatePanopticCollateralRedeem(client, args),

simulatePanopticMintOptions: (
args: SimulatePanopticMintOptionsParameters<TChain>,
) => simulatePanopticMintOptions(client, args),
simulatePanopticRollOptions: (
args: SimulatePanopticRollOptionsParameters<TChain>,
) => simulatePanopticRollOptions(client, args),
simulatePanopticBurnOptions: (
args: SimulatePanopticBurnOptionsParameters<TChain>,
) => simulatePanopticBurnOptions(client, args),
simulatePanopticForceExercise: (
args: SimulatePanopticForceExerciseParameters<TChain>,
) => simulatePanopticForceExercise(client, args),
simulatePanopticLiquidateAccount: (
args: SimulatePanopticLiquidateAccountParameters<TChain>,
) => simulatePanopticLiquidateAccount(client, args),
});
Loading

0 comments on commit 2da3d3b

Please sign in to comment.