Skip to content

Commit

Permalink
Merge pull request #6 from Numoen/test
Browse files Browse the repository at this point in the history
Setup tests to use sepolia fork
  • Loading branch information
kyscott18 authored Sep 27, 2023
2 parents 7efc611 + b69d351 commit 6a0a5d4
Show file tree
Hide file tree
Showing 44 changed files with 1,399 additions and 4,524 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,13 @@ jobs:
with:
cache: pnpm
node-version: latest
- name: Install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- run: forge install
- run: pnpm install
- run: pnpm wagmi generate
- run: pnpm build
- name: Lint
run: pnpm lint
Expand All @@ -40,7 +46,13 @@ jobs:
with:
cache: pnpm
node-version: latest
- name: Install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- run: forge install
- run: pnpm install
- run: pnpm wagmi generate
- run: pnpm build
- name: Typecheck
run: pnpm typecheck
Expand All @@ -57,7 +69,13 @@ jobs:
with:
cache: pnpm
node-version: latest
- name: Install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- run: forge install
- run: pnpm install
- run: pnpm wagmi generate
- name: Build
run: pnpm build

Expand All @@ -73,6 +91,12 @@ jobs:
with:
cache: pnpm
node-version: latest
- name: Install foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly
- run: forge install
- run: pnpm install
- run: pnpm wagmi generate
- name: Run tests
run: pnpm test:ci
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "lib/panoptic-v1-core"]
path = lib/panoptic-v1-core
url = https://github.com/panoptic-labs/panoptic-v1-core
1 change: 1 addition & 0 deletions lib/panoptic-v1-core
Submodule panoptic-v1-core added at 3cd6bd
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"license": "MIT",
"devDependencies": {
"@biomejs/biome": "^1.2.2",
"@wagmi/cli": "^1.5.1",
"husky": "^8.0.3"
}
}
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
2 changes: 0 additions & 2 deletions packages/panoptic-sdk/src/_test/bytecode/collateralTracker.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/panoptic-sdk/src/_test/bytecode/mockERC20.ts

This file was deleted.

12 changes: 9 additions & 3 deletions packages/panoptic-sdk/src/_test/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
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://ethereum-sepolia.blockpi.network/v1/rpc/public",
},
});

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;
};
Loading

0 comments on commit 6a0a5d4

Please sign in to comment.