Skip to content

Commit

Permalink
deploy new pool
Browse files Browse the repository at this point in the history
  • Loading branch information
kyscott18 committed Sep 29, 2023
1 parent 5c791ba commit c62c964
Show file tree
Hide file tree
Showing 5 changed files with 295 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/panoptic-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ Typescript SDK for Panoptic

- [x] `PanopticFactory`: The factory contract in the Panoptic protocol
- [x] `PanopticSemiFungiblePositionManager`: The semi-fungible position manager in the Panoptic protocol
- [x] `PanopticPool`
- [x] `PanopticPoolData`
- [x] `PanopticCollateral`
- [x] `PanopticCollateralData`
- [x] `PanopticPosition`
- [x] `PanopticPositionData`
- [x] `PanopticCollateralPosition`
- [x] `PanopticCollateralPositionData`
- [x] `PanopticPool`: A pool created by `PanopticFactory`
- [x] `PanopticPoolData`: The state of a `PanopticPool`
- [x] `PanopticCollateral`: A collateral tracker in a `PanopticPool`
- [x] `PanopticCollateralData`: The state of a `PanopticCollateral`
- [x] `PanopticPosition`: A user position in a `PanopticPool`
- [x] `PanopticPositionData`: The state of a `PanopticPosition`
- [x] `PanopticCollateralPosition`: A user position in a `PanopticCollateral`
- [x] `PanopticCollateralPositionData`: The state of a `PanopticCollateralPosition`

- Utilities

Expand All @@ -41,11 +41,12 @@ Typescript SDK for Panoptic

- Public Actions

- [x] `getPanopticPool`
- [x] `getPanopticPoolData`
- [x] `getPanopticCollateralData`
- [x] `getPanopticCollateralPositionData`
- [x] `getPanopticPositionData`
- [ ] `simulatePanopticDeployNewPool`
- [x] `simulatePanopticDeployNewPool`
- [x] `simulatePanopticMintOptions`
- [x] `simulatePanopticRollOptions`
- [x] `simulatePanopticBurnOptions`
Expand Down
28 changes: 28 additions & 0 deletions packages/panoptic-sdk/src/publicActions/getPanopticPool.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { type Hex } from "viem";
import { beforeEach, expect, test } from "vitest";
import { deployPool, publicClient, testClient } from "../_test/utils.js";
import { sepoliaPanoptic } from "../chains/sepolia.js";
import type { PanopticPool } from "../types/index.js";
import { getPanopticPool } from "./getPanopticPool.js";

let id: Hex | undefined = undefined;

let pool: PanopticPool;

beforeEach(async () => {
if (id === undefined) {
pool = await deployPool();
} else {
await testClient.revert({ id });
}
id = await testClient.snapshot();
}, 100_000);

test("get panoptic pool", async () => {
const address = await getPanopticPool(publicClient, {
uniswapPool: pool.uniswapPool,
factory: sepoliaPanoptic.factory,
});

expect(address).toBe(pool.address);
});
33 changes: 33 additions & 0 deletions packages/panoptic-sdk/src/publicActions/getPanopticPool.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { type UniswapV3Pool } from "uniswap-v3-sdk";
import type {
Address,
Chain,
Client,
ReadContractParameters,
Transport,
} from "viem";
import { readContract } from "viem/actions";
import { panopticFactoryABI } from "../generated.js";
import type { PanopticFactory } from "../types/PanopticFactory.js";

export type GetPanopticPoolParameters = Omit<
ReadContractParameters<typeof panopticFactoryABI, "getPanopticPool">,
"address" | "abi" | "functionName" | "args"
> & {
uniswapPool: Pick<UniswapV3Pool, "address">;
factory: Pick<PanopticFactory, "address">;
};

export type GetPanopticPoolReturnType = Address;

export const getPanopticPool = <TChain extends Chain | undefined,>(
client: Client<Transport, TChain>,
{ uniswapPool, factory, ...request }: GetPanopticPoolParameters,
): Promise<GetPanopticPoolReturnType> =>
readContract(client, {
address: factory.address,
abi: panopticFactoryABI,
functionName: "getPanopticPool",
args: [uniswapPool.address],
...request,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { createERC20 } from "reverse-mirage";
import invariant from "tiny-invariant";
import {
type UniswapV3Pool,
createUniswapV3Pool,
uniswapV3FactoryABI,
uniswapV3PoolABI,
} from "uniswap-v3-sdk";
import { type Hex } from "viem";
import { sepolia } from "viem/chains";
import { beforeEach, expect, test } from "vitest";
import MockERC20Bytecode from "../../../../lib/panoptic-v1-core/artifacts/contracts/MockERC20.sol/MockERC20.json";
import { ALICE } from "../_test/constants.js";
import { publicClient, testClient, walletClient } from "../_test/utils.js";
import { sepoliaPanoptic } from "../chains/sepolia.js";
import { mockErc20ABI } from "../generated.js";
import { getPanopticPool } from "./getPanopticPool.js";
import { simulatePanopticDeployNewPool } from "./simulatePanopticDeployNewPool.js";

let id: Hex | undefined = undefined;

let uniswapPool: UniswapV3Pool;

beforeEach(async () => {
if (id === undefined) {
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,
});

uniswapPool = createUniswapV3Pool(
tokenA,
tokenB,
500,
sepoliaPanoptic.factory.uniswapFactory,
);

const { request: initializeRequest } = await publicClient.simulateContract({
address: uniswapPool.address,
abi: uniswapV3PoolABI,
functionName: "initialize",
args: [2n ** 96n],
account: ALICE,
});
const initializeHash = await walletClient.writeContract(initializeRequest);
await publicClient.waitForTransactionReceipt({
hash: initializeHash,
});
} else {
await testClient.revert({ id });
}
id = await testClient.snapshot();
}, 100_000);

test("simulate deploy pool", async () => {
const { request } = await simulatePanopticDeployNewPool(publicClient, {
args: {
uniswapPool,
factory: sepoliaPanoptic.factory,
salt: 0n,
},
account: ALICE,
});

const hash = await walletClient.writeContract(request);
await publicClient.waitForTransactionReceipt({ hash });

const address = await getPanopticPool(publicClient, {
uniswapPool,
factory: sepoliaPanoptic.factory,
});

expect(address).toBeTruthy();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import type { UniswapV3Pool } from "uniswap-v3-sdk";
import type {
Chain,
Client,
SimulateContractParameters,
SimulateContractReturnType,
Transport,
} from "viem";
import { simulateContract } from "viem/contract";
import { panopticFactoryABI } from "../generated.js";
import type { PanopticFactory } from "../types/PanopticFactory.js";

export type PanopticDeployNewPoolParameters = {
factory: PanopticFactory;
uniswapPool: UniswapV3Pool;
salt: bigint;
};

export type SimulatePanopticDeployNewPoolParameters<
TChain extends Chain | undefined = Chain,
TChainOverride extends Chain | undefined = Chain | undefined,
> = Omit<
SimulateContractParameters<
typeof panopticFactoryABI,
"deployNewPool",
TChain,
TChainOverride
>,
"args" | "address" | "abi" | "functionName"
> & { args: PanopticDeployNewPoolParameters };

export type SimulatePanopticDeployNewPoolReturnType<
TChain extends Chain | undefined,
TChainOverride extends Chain | undefined = undefined,
> = SimulateContractReturnType<
typeof panopticFactoryABI,
"deployNewPool",
TChain,
TChainOverride
>;

// TODO: return PanopticPool
export const simulatePanopticDeployNewPool = <
TChain extends Chain | undefined,
TChainOverride extends Chain | undefined,
>(
client: Client<Transport, TChain>,
{
args: { factory, uniswapPool, salt },
...request
}: SimulatePanopticDeployNewPoolParameters<TChain, TChainOverride>,
): Promise<SimulatePanopticDeployNewPoolReturnType<TChain, TChainOverride>> =>
simulateContract(client, {
address: factory.address,
abi: panopticFactoryABI,
functionName: "deployNewPool",
args: [
uniswapPool.token0.address,
uniswapPool.token1.address,
uniswapPool.feeTier,
salt,
],
...request,
} as unknown as SimulateContractParameters<
typeof panopticFactoryABI,
"deployNewPool",
TChain,
TChainOverride
>);

0 comments on commit c62c964

Please sign in to comment.