Skip to content

Commit

Permalink
Switch to AMM enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnarlycow committed Apr 1, 2024
1 parent 2bd7125 commit 4f64c6a
Show file tree
Hide file tree
Showing 5 changed files with 896 additions and 68 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@
"typechain": "hardhat typechain"
},
"dependencies": {
"@aperture_finance/uniswap-v3-automation-sdk": "3.0.0-alpha",
"@aperture_finance/uni-v3-lib": "^2.0.1",
"@openzeppelin/contracts": "^5.0.2",
"ethers": "5.7.2",
"viem": "^2.9.5"
"viem": "^2.9.6"
},
"devDependencies": {
"@ethersproject/abi": "5.7.0",
Expand Down
17 changes: 9 additions & 8 deletions src/viem/poolLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
EphemeralPoolTicks__factory,
} from "../../typechain";
import { callEphemeralContract } from "./caller";
import { AutomatedMarketMakerEnum } from "@aperture_finance/uniswap-v3-automation-sdk";

/**
* Fetches the liquidity within the tick range for the specified pool by deploying an ephemeral contract via `eth_call`.
Expand Down Expand Up @@ -42,13 +43,13 @@ export async function getPopulatedTicksInRange(
}

export async function getStaticSlots(
amm: AutomatedMarketMakerEnum,
pool: Address,
publicClient: PublicClient,
blockNumber?: bigint,
pcsV3InsteadOfUniV3 = false,
) {
return await callEphemeralContract(
pcsV3InsteadOfUniV3
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolSlots__factory.abi,
bytecode: EphemeralPCSV3PoolSlots__factory.bytecode,
Expand All @@ -65,15 +66,15 @@ export async function getStaticSlots(
}

export async function getTicksSlots(
amm: AutomatedMarketMakerEnum,
pool: Address,
tickLower: number,
tickUpper: number,
publicClient: PublicClient,
blockNumber?: bigint,
pcsV3InsteadOfUniV3 = false,
) {
return await callEphemeralContract(
pcsV3InsteadOfUniV3
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolTicks__factory.abi,
bytecode: EphemeralPCSV3PoolTicks__factory.bytecode,
Expand All @@ -90,13 +91,13 @@ export async function getTicksSlots(
}

export async function getTickBitmapSlots(
amm: AutomatedMarketMakerEnum,
pool: Address,
publicClient: PublicClient,
blockNumber?: bigint,
pcsV3InsteadOfUniV3 = false,
) {
return await callEphemeralContract(
pcsV3InsteadOfUniV3
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolTickBitmap__factory.abi,
bytecode: EphemeralPCSV3PoolTickBitmap__factory.bytecode,
Expand All @@ -118,14 +119,14 @@ export type PositionKey = AbiParametersToPrimitiveTypes<
>[1][0];

export async function getPositionsSlots(
amm: AutomatedMarketMakerEnum,
pool: Address,
keys: PositionKey[],
publicClient: PublicClient,
blockNumber?: bigint,
pcsV3InsteadOfUniV3 = false,
) {
return await callEphemeralContract(
pcsV3InsteadOfUniV3
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolPositions__factory.abi,
bytecode: EphemeralPCSV3PoolPositions__factory.bytecode,
Expand Down
37 changes: 18 additions & 19 deletions test/hardhat/pcsv3_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import {
} from "../../typechain";
import { computePoolAddress } from "@pancakeswap/v3-sdk";
import { Token } from "@pancakeswap/sdk";
import { AutomatedMarketMakerEnum, getAMMInfo } from "@aperture_finance/uniswap-v3-automation-sdk";

const PCSV3_DEPLOYER = "0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9";
const PCSV3_NPM = "0x46A15B0b27311cedF172AB29E4f4766fbE7F4364";
const AMM = AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3;
const PCSV3_INFO = getAMMInfo(bsc.id, AMM)!;
const USDT_ADDRESS = "0x55d398326f99059fF775485246999027B3197955";
const WBNB_ADDRESS = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";

Expand All @@ -37,7 +38,7 @@ describe("Pool lens test with PCSV3 on BSC", () => {
});
var blockNumber: bigint;
const pool = computePoolAddress({
deployerAddress: PCSV3_DEPLOYER,
deployerAddress: PCSV3_INFO.poolDeployer!,
tokenA: new Token(bsc.id, USDT_ADDRESS, 6, "USDT"),
tokenB: new Token(bsc.id, WBNB_ADDRESS, 18, "WBNB"),
fee: 500,
Expand All @@ -48,7 +49,7 @@ describe("Pool lens test with PCSV3 on BSC", () => {
client: publicClient,
});
const npm = getContract({
address: PCSV3_NPM,
address: PCSV3_INFO.nonfungiblePositionManager,
abi: INonfungiblePositionManager__factory.abi,
client: publicClient,
});
Expand Down Expand Up @@ -90,10 +91,10 @@ describe("Pool lens test with PCSV3 on BSC", () => {
tokenId,
position: { token0, token1, fee },
slot0: { sqrtPriceX96, tick },
} = await getPositionDetails(PCSV3_NPM, 4n, publicClient, blockNumber);
} = await getPositionDetails(PCSV3_INFO.nonfungiblePositionManager, 4n, publicClient, blockNumber);
expect(tokenId).to.be.eq(4n);
const poolAddress = computePoolAddress({
deployerAddress: PCSV3_DEPLOYER,
deployerAddress: PCSV3_INFO.poolDeployer!,
tokenA: new Token(bsc.id, token0, 0, "NOT_USED"),
tokenB: new Token(bsc.id, token1, 0, "NOT_USED"),
fee,
Expand Down Expand Up @@ -125,7 +126,7 @@ describe("Pool lens test with PCSV3 on BSC", () => {

it("Test getting position array", async () => {
const posArr = await getPositions(
PCSV3_NPM,
PCSV3_INFO.nonfungiblePositionManager,
Array.from({ length: 100 }, (_, i) => BigInt(i + 1)),
publicClient,
blockNumber,
Expand All @@ -136,7 +137,12 @@ describe("Pool lens test with PCSV3 on BSC", () => {
it("Test getting all positions by owner", async () => {
const totalSupply = await npm.read.totalSupply({ blockNumber });
const owner = await npm.read.ownerOf([totalSupply - 1n], { blockNumber });
const posArr = await getAllPositionsByOwner(PCSV3_NPM, owner, publicClient, blockNumber);
const posArr = await getAllPositionsByOwner(
PCSV3_INFO.nonfungiblePositionManager,
owner,
publicClient,
blockNumber,
);
await verifyPositionDetails(posArr);
});

Expand All @@ -152,24 +158,17 @@ describe("Pool lens test with PCSV3 on BSC", () => {
}

it("Test getting static storage slots", async () => {
const slots = await getStaticSlots(pool, publicClient, blockNumber, /*pcsV3InsteadOfUniV3=*/ true);
const slots = await getStaticSlots(AMM, pool, publicClient, blockNumber);
await verifySlots(slots);
});

it("Test getting populated ticks slots", async () => {
const slots = await getTicksSlots(
pool,
TickMath.MIN_TICK,
TickMath.MAX_TICK,
publicClient,
blockNumber,
/*pcsV3InsteadOfUniV3=*/ true,
);
const slots = await getTicksSlots(AMM, pool, TickMath.MIN_TICK, TickMath.MAX_TICK, publicClient, blockNumber);
await verifySlots(slots);
});

it("Test getting tick bitmap slots", async () => {
const slots = await getTickBitmapSlots(pool, publicClient, blockNumber, /*pcsV3InsteadOfUniV3=*/ true);
const slots = await getTickBitmapSlots(AMM, pool, publicClient, blockNumber);
await verifySlots(slots);
});

Expand All @@ -186,7 +185,7 @@ describe("Pool lens test with PCSV3 on BSC", () => {
tickLower: tickLower!,
tickUpper: tickUpper!,
}));
const slots = await getPositionsSlots(pool, positions, publicClient, blockNumber, /*pcsV3InsteadOfUniV3=*/ true);
const slots = await getPositionsSlots(AMM, pool, positions, publicClient, blockNumber);
await verifySlots(slots);
});
});
30 changes: 18 additions & 12 deletions test/hardhat/univ3_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import {
} from "../../typechain";
import { mainnet } from "viem/chains";
import { Token } from "@uniswap/sdk-core";
import { AutomatedMarketMakerEnum, getAMMInfo } from "@aperture_finance/uniswap-v3-automation-sdk";

const UNIV3_FACTORY = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
const UNIV3_NPM = "0xC36442b4a4522E871399CD717aBDD847Ab11FE88";
const AMM = AutomatedMarketMakerEnum.enum.UNISWAP_V3;
const UNIV3_INFO = getAMMInfo(mainnet.id, AMM)!;
const USDC_ADDRESS = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const WETH_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

Expand All @@ -36,7 +37,7 @@ describe("Pool lens test with UniV3 on mainnet", () => {
});
let blockNumber: bigint;
const pool = computePoolAddress({
factoryAddress: UNIV3_FACTORY,
factoryAddress: UNIV3_INFO.factory,
tokenA: new Token(mainnet.id, USDC_ADDRESS, 6, "USDC"),
tokenB: new Token(mainnet.id, WETH_ADDRESS, 18, "WETH"),
fee: 500,
Expand All @@ -47,7 +48,7 @@ describe("Pool lens test with UniV3 on mainnet", () => {
client: publicClient,
});
const npm = getContract({
address: UNIV3_NPM,
address: UNIV3_INFO.nonfungiblePositionManager,
abi: INonfungiblePositionManager__factory.abi,
client: publicClient,
});
Expand Down Expand Up @@ -89,11 +90,11 @@ describe("Pool lens test with UniV3 on mainnet", () => {
tokenId,
position: { token0, token1, fee },
slot0: { sqrtPriceX96, tick },
} = await getPositionDetails(UNIV3_NPM, 4n, publicClient, blockNumber);
} = await getPositionDetails(UNIV3_INFO.nonfungiblePositionManager, 4n, publicClient, blockNumber);
expect(tokenId).to.be.eq(4n);
const [_sqrtPriceX96, _tick] = await getContract({
address: computePoolAddress({
factoryAddress: UNIV3_FACTORY,
factoryAddress: UNIV3_INFO.factory,
tokenA: new Token(mainnet.id, token0, 0, "NOT_USED"),
tokenB: new Token(mainnet.id, token1, 0, "NOT_USED"),
fee,
Expand Down Expand Up @@ -123,7 +124,7 @@ describe("Pool lens test with UniV3 on mainnet", () => {

it("Test getting position array", async () => {
const posArr = await getPositions(
UNIV3_NPM,
UNIV3_INFO.nonfungiblePositionManager,
Array.from({ length: 100 }, (_, i) => BigInt(i + 1)),
publicClient,
blockNumber,
Expand All @@ -134,7 +135,12 @@ describe("Pool lens test with UniV3 on mainnet", () => {
it("Test getting all positions by owner", async () => {
const totalSupply = await npm.read.totalSupply({ blockNumber });
const owner = await npm.read.ownerOf([totalSupply - 1n], { blockNumber });
const posArr = await getAllPositionsByOwner(UNIV3_NPM, owner, publicClient, blockNumber);
const posArr = await getAllPositionsByOwner(
UNIV3_INFO.nonfungiblePositionManager,
owner,
publicClient,
blockNumber,
);
await verifyPositionDetails(posArr);
});

Expand All @@ -150,17 +156,17 @@ describe("Pool lens test with UniV3 on mainnet", () => {
}

it("Test getting static storage slots", async () => {
const slots = await getStaticSlots(pool, publicClient, blockNumber);
const slots = await getStaticSlots(AMM, pool, publicClient, blockNumber);
await verifySlots(slots);
});

it("Test getting populated ticks slots", async () => {
const slots = await getTicksSlots(pool, TickMath.MIN_TICK, TickMath.MAX_TICK, publicClient, blockNumber);
const slots = await getTicksSlots(AMM, pool, TickMath.MIN_TICK, TickMath.MAX_TICK, publicClient, blockNumber);
await verifySlots(slots);
});

it("Test getting tick bitmap slots", async () => {
const slots = await getTickBitmapSlots(pool, publicClient, blockNumber);
const slots = await getTickBitmapSlots(AMM, pool, publicClient, blockNumber);
await verifySlots(slots);
});

Expand All @@ -177,7 +183,7 @@ describe("Pool lens test with UniV3 on mainnet", () => {
tickLower: tickLower!,
tickUpper: tickUpper!,
}));
const slots = await getPositionsSlots(pool, positions, publicClient, blockNumber);
const slots = await getPositionsSlots(AMM, pool, positions, publicClient, blockNumber);
await verifySlots(slots);
});
});
Loading

0 comments on commit 4f64c6a

Please sign in to comment.