Skip to content

Commit

Permalink
Remove dependency on Aperture's UniV3 automation sdk to avoid circula…
Browse files Browse the repository at this point in the history
…r dependency.
  • Loading branch information
gnarlycow committed Apr 1, 2024
1 parent 4f64c6a commit b58a5df
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 870 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +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.6"
"viem": "^2.9.6",
"zod":"^3.22.4"
},
"devDependencies": {
"@ethersproject/abi": "5.7.0",
Expand Down
8 changes: 7 additions & 1 deletion src/viem/poolLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import {
EphemeralPoolTicks__factory,
} from "../../typechain";
import { callEphemeralContract } from "./caller";
import { AutomatedMarketMakerEnum } from "@aperture_finance/uniswap-v3-automation-sdk";
import { z } from 'zod';

export const AutomatedMarketMakerEnum = z.enum([
'UNISWAP_V3',
'PANCAKESWAP_V3',
]);
export type AutomatedMarketMakerEnum = z.infer<typeof AutomatedMarketMakerEnum>;

/**
* Fetches the liquidity within the tick range for the specified pool by deploying an ephemeral contract via `eth_call`.
Expand Down
22 changes: 9 additions & 13 deletions test/hardhat/pcsv3_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { expect } from "chai";
import { ContractFunctionReturnType, createPublicClient, getContract, http, toHex } from "viem";
import { bsc } from "viem/chains";
import {
AutomatedMarketMakerEnum,
getAllPositionsByOwner,
getPopulatedTicksInRange,
getPositionDetails,
Expand All @@ -21,10 +22,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 AMM = AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3;
const PCSV3_INFO = getAMMInfo(bsc.id, AMM)!;
const PCSV3_NPM = "0x46A15B0b27311cedF172AB29E4f4766fbE7F4364";
const PCSV3_POOL_DEPLOYER = "0x41ff9AA7e16B8B1a8a8dc4f0eFacd93D02d071c9";
const USDT_ADDRESS = "0x55d398326f99059fF775485246999027B3197955";
const WBNB_ADDRESS = "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c";

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

it("Test getting position array", async () => {
const posArr = await getPositions(
PCSV3_INFO.nonfungiblePositionManager,
PCSV3_NPM,
Array.from({ length: 100 }, (_, i) => BigInt(i + 1)),
publicClient,
blockNumber,
Expand All @@ -137,12 +138,7 @@ 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_INFO.nonfungiblePositionManager,
owner,
publicClient,
blockNumber,
);
const posArr = await getAllPositionsByOwner(PCSV3_NPM, owner, publicClient, blockNumber);
await verifyPositionDetails(posArr);
});

Expand Down
22 changes: 9 additions & 13 deletions test/hardhat/univ3_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { TickMath, computePoolAddress } from "@uniswap/v3-sdk";
import { expect } from "chai";
import { Address, ContractFunctionReturnType, createPublicClient, getContract, http, toHex } from "viem";
import {
AutomatedMarketMakerEnum,
getAllPositionsByOwner,
getPopulatedTicksInRange,
getPositionDetails,
Expand All @@ -20,10 +21,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 AMM = AutomatedMarketMakerEnum.enum.UNISWAP_V3;
const UNIV3_INFO = getAMMInfo(mainnet.id, AMM)!;
const UNIV3_NPM = "0xC36442b4a4522E871399CD717aBDD847Ab11FE88";
const UNIV3_FACTORY = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
const USDC_ADDRESS = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const WETH_ADDRESS = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";

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

it("Test getting position array", async () => {
const posArr = await getPositions(
UNIV3_INFO.nonfungiblePositionManager,
UNIV3_NPM,
Array.from({ length: 100 }, (_, i) => BigInt(i + 1)),
publicClient,
blockNumber,
Expand All @@ -135,12 +136,7 @@ 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_INFO.nonfungiblePositionManager,
owner,
publicClient,
blockNumber,
);
const posArr = await getAllPositionsByOwner(UNIV3_NPM, owner, publicClient, blockNumber);
await verifyPositionDetails(posArr);
});

Expand Down
Loading

0 comments on commit b58a5df

Please sign in to comment.