Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore!: Remove PancakeSwap dependencies and related code #7

Merged
merged 4 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
PoolLensTest:testFuzz_GetPositions(int24,int24) (runs: 22, μ: 1364083, ~: 1404255)
PoolLensTest:testFuzz_GetPositions(int24,int24) (runs: 16, μ: 1368690, ~: 1404195)
PoolLensTest:test_GetPopulatedTicksInRange() (gas: 4544800)
PoolLensTest:test_GetPositions() (gas: 1132120)
PoolLensTest:test_GetPositions() (gas: 1131691)
PoolLensTest:test_GetSlots() (gas: 3710586)
PoolLensTest:test_GetTickBitmap() (gas: 3399608)
PositionLensTest:testFuzz_GetPosition(uint256) (runs: 22, μ: 270296, ~: 272574)
PositionLensTest:testFuzz_GetPosition(uint256) (runs: 16, μ: 270283, ~: 272574)
PositionLensTest:test_AllPositions() (gas: 1140527)
PositionLensTest:test_GetFeesOwed() (gas: 2444865)
PositionLensTest:test_GetPositions() (gas: 940419)
PositionLensTest:test_GetTotalAmounts() (gas: 2456617)
StorageLensTest:testFuzz_extsload(bytes32) (runs: 22, μ: 33735, ~: 33755)
PositionLensTest:test_GetTotalAmounts() (gas: 2456483)
StorageLensTest:testFuzz_extsload(bytes32) (runs: 16, μ: 33692, ~: 33575)
StorageLensTest:test_extsload() (gas: 66705)
TickLensTest:test_GetPopulatedTicksInRange() (gas: 11486985)
8 changes: 0 additions & 8 deletions .github/workflows/foundry.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:
env:
FOUNDRY_PROFILE: ci
MAINNET_RPC_URL: ${{ secrets.MAINNET_RPC_URL }}
BNB_RPC_URL: ${{ secrets.BNB_RPC_URL }}

jobs:
check:
Expand Down Expand Up @@ -48,10 +47,3 @@ jobs:
env:
CHAIN_ID: 1
id: testMainnet

- name: Run Forge tests on BNB mainnet
run: |
forge test -vvv
env:
CHAIN_ID: 56
id: testBNB
9 changes: 0 additions & 9 deletions contracts/EphemeralPoolPositions.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,3 @@ contract EphemeralPoolPositions is PoolUtils {
}
}
}

contract EphemeralPCSV3PoolPositions is EphemeralPoolPositions {
constructor(V3PoolCallee pool, PositionKey[] memory keys) payable EphemeralPoolPositions(pool, keys) {}

function getPositionsSlot() internal pure override returns (uint256) {
// Storage slot of the `positions` mapping in PancakeSwapV3Pool.
return 8;
}
}
82 changes: 0 additions & 82 deletions contracts/EphemeralPoolSlots.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@pancakeswap/v3-core/contracts/interfaces/IPancakeV3Pool.sol";
import "./PoolUtils.sol";

/// @notice A lens for fetching static state variables in a Uniswap v3 pool without deployment
Expand Down Expand Up @@ -79,84 +78,3 @@ contract EphemeralPoolSlots is PoolUtils {
}
}
}

/// @notice A lens for fetching static state variables in a PancakeSwap v3 pool without deployment
/// @author Aperture Finance
/// @dev The return data can be accessed externally by `eth_call` without a `to` address or internally by catching the
/// revert data, and decoded by `abi.decode(data, (Slot[]))`
contract EphemeralPCSV3PoolSlots is PoolUtils {
constructor(V3PoolCallee pool) payable {
Slot[] memory slots = getSlots(pool);
bytes memory returnData = abi.encode(slots);
assembly ("memory-safe") {
revert(add(returnData, 0x20), mload(returnData))
}
}

/// @notice Get the static storage slots of a pool
/// @dev Public function to expose the abi for easier decoding using TypeChain
/// @param pool The PancakeSwap v3 pool
/// @return slots An array of storage slots and their raw data
function getSlots(V3PoolCallee pool) public payable returns (Slot[] memory slots) {
unchecked {
uint256 length;
{
(
uint160 sqrtPriceX96,
int24 tick,
uint16 observationIndex,
uint16 observationCardinality,
uint16 observationCardinalityNext,
uint32 feeProtocol,
bool unlocked
) = IPancakeV3Pool(V3PoolCallee.unwrap(pool)).slot0();
// PancakeV3Pool's slot0() fields actually span two storage slots (slot 0 and 1) as a result of their changing `feeProtocol` from uint8 to uint32.
// The first 5 fields are packed into slot 0, and the last 2 fields (`feeProtocol` and `unlocked`) are packed into slot 1.
// See https://evm.storage/eth/19541394/0x6ca298d2983ab03aa1da7679389d955a4efee15c/slot0#map for a visual representation.
uint256 slot0;
uint256 slot1;
assembly {
slot0 := shl(216, observationCardinalityNext)
slot0 := or(shl(200, observationCardinality), slot0)
slot0 := or(shl(184, observationIndex), slot0)
slot0 := or(shl(160, and(0xffffff, tick)), slot0)
slot0 := or(sqrtPriceX96, slot0)
slot1 := shl(32, unlocked)
slot1 := or(feeProtocol, slot1)
}
length = observationCardinality;
slots = new Slot[](length + 6);
slots[0] = Slot(0, slot0);
slots[1] = Slot(1, slot1);
}
slots[2] = Slot(2, pool.feeGrowthGlobal0X128());
slots[3] = Slot(3, pool.feeGrowthGlobal1X128());
{
(uint128 token0, uint128 token1) = pool.protocolFees();
uint256 slot3;
assembly {
slot3 := or(shl(128, token1), token0)
}
slots[4] = Slot(4, slot3);
}
slots[5] = Slot(5, pool.liquidity());
for (uint256 i; i < length; ++i) {
(
uint32 blockTimestamp,
int56 tickCumulative,
uint160 secondsPerLiquidityCumulativeX128,
bool initialized
) = pool.observations(i);
uint256 observation;
assembly {
observation := shl(248, initialized)
observation := or(shl(88, secondsPerLiquidityCumulativeX128), observation)
observation := or(shl(32, and(0xffffffffffffff, tickCumulative)), observation)
observation := or(blockTimestamp, observation)
}
// PancakeSwapV3Pool's `observations` struct array starts at slot 9.
slots[i + 6] = Slot(i + 9, observation);
}
}
}
}
9 changes: 0 additions & 9 deletions contracts/EphemeralPoolTickBitmap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,3 @@ contract EphemeralPoolTickBitmap is PoolUtils {
}
}
}

contract EphemeralPCSV3PoolTickBitmap is EphemeralPoolTickBitmap {
constructor(V3PoolCallee pool) payable EphemeralPoolTickBitmap(pool) {}

function getTickBitmapSlot() internal pure override returns (uint256) {
// Storage slot of the `tickBitmap` mapping in PancakeSwapV3Pool.
return 7;
}
}
13 changes: 0 additions & 13 deletions contracts/EphemeralPoolTicks.sol
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,3 @@ contract EphemeralPoolTicks is PoolUtils {
}
}
}

contract EphemeralPCSV3PoolTicks is EphemeralPoolTicks {
constructor(
V3PoolCallee pool,
int24 tickLower,
int24 tickUpper
) payable EphemeralPoolTicks(pool, tickLower, tickUpper) {}

function getTicksSlot() internal pure override returns (uint256) {
// Storage slot of the `ticks` mapping in PancakeSwapV3Pool.
return 6;
}
}
2 changes: 1 addition & 1 deletion contracts/PositionUtils.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {INonfungiblePositionManager as INPM, IPCSV3NonfungiblePositionManager as IPCSV3NPM} from "@aperture_finance/uni-v3-lib/src/interfaces/INonfungiblePositionManager.sol";
import {IUniswapV3NonfungiblePositionManager as INPM} from "@aperture_finance/uni-v3-lib/src/interfaces/IUniswapV3NonfungiblePositionManager.sol";
shuhuiluo marked this conversation as resolved.
Show resolved Hide resolved
import {NPMCaller, PositionFull} from "@aperture_finance/uni-v3-lib/src/NPMCaller.sol";
import {PoolAddress} from "@aperture_finance/uni-v3-lib/src/PoolAddress.sol";
import {PoolAddressPancakeSwapV3} from "@aperture_finance/uni-v3-lib/src/PoolAddressPancakeSwapV3.sol";
Expand Down
27 changes: 13 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@
"test:hardhat": "hardhat test",
"snapshot": "forge snapshot --isolate",
"snapshot:diff": "forge snapshot --diff --isolate",
"prettier": "prettier -c contracts/{*,**/*}.sol test/**/*.sol ./**/*.ts",
"prettier:fix": "prettier -w contracts/{*,**/*}.sol test/**/*.sol ./**/*.ts",
"prettier": "prettier -c contracts/{*,**/*}.sol test/**/*.sol {src,test}/**/*.ts",
"prettier:fix": "prettier -w contracts/{*,**/*}.sol test/**/*.sol {src,test}/**/*.ts",
"typechain": "hardhat typechain"
},
"dependencies": {
"@aperture_finance/uni-v3-lib": "^2.0.1",
"@aperture_finance/uni-v3-lib": "^3.0.3",
"@openzeppelin/contracts": "^5.0.2",
"ethers": "5.7.2",
"viem": "^2.8.4",
Expand All @@ -56,23 +56,22 @@
"devDependencies": {
"@ethersproject/abi": "5.7.0",
"@ethersproject/providers": "5.7.2",
"@nomicfoundation/hardhat-foundry": "^1.1.1",
"@nomicfoundation/hardhat-foundry": "^1.1.2",
"@nomiclabs/hardhat-ethers": "^2.2.3",
"@pancakeswap/v3-sdk": "^3.8.0",
"@typechain/ethers-v5": "^11.1.2",
"@typechain/hardhat": "^9.1.0",
"@types/chai": "^4.3.14",
"@types/mocha": "^10.0.6",
"@types/node": "^20.11.30",
"@uniswap/v3-sdk": "^3.11.0",
"@types/chai": "^4.3.19",
"@types/mocha": "^10.0.7",
"@types/node": "^22.5.1",
"@uniswap/v3-sdk": "^3.13.1",
"chai": "^4.4.1",
"hardhat": "^2.22.2",
"mocha": "^10.4.0",
"prettier": "^3.2.5",
"prettier-plugin-solidity": "^1.3.1",
"hardhat": "^2.22.9",
"mocha": "^10.7.3",
"prettier": "^3.3.3",
"prettier-plugin-solidity": "^1.4.1",
"ts-node": "^10.9.2",
"typechain": "^8.3.2",
"typescript": "^5.4.3"
"typescript": "^5.5.4"
},
"prettier": {
"plugins": [
Expand Down
1 change: 0 additions & 1 deletion remappings.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@aperture_finance/uni-v3-lib/=node_modules/@aperture_finance/uni-v3-lib/
@openzeppelin/=node_modules/@openzeppelin/
@pancakeswap/=node_modules/@pancakeswap/
@uniswap/=node_modules/@uniswap/
forge-std/=lib/forge-std/src/
solady/=node_modules/solady/
6 changes: 3 additions & 3 deletions src/position_lens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ mod tests {
use crate::{
bindings::{
ephemeralgetposition::EphemeralGetPosition::{PositionFull, Slot0},
inonfungiblepositionmanager::INonfungiblePositionManager::INonfungiblePositionManagerInstance,
iuniswapv3nonfungiblepositionmanager::IUniswapV3NonfungiblePositionManager,
iuniswapv3pool::IUniswapV3Pool::IUniswapV3PoolInstance,
},
tests::*,
Expand Down Expand Up @@ -280,15 +280,15 @@ mod tests {
Some(*BLOCK_NUMBER),
)
.await?;
let _npm = INonfungiblePositionManagerInstance::new(NPM_ADDRESS, provider);
let _npm = IUniswapV3NonfungiblePositionManager::new(NPM_ADDRESS, provider);
Ok(())
// verify_position_details(positions, npm).await
}

#[tokio::test]
async fn test_get_all_positions_by_owner() -> Result<()> {
let provider = PROVIDER.clone();
let npm = INonfungiblePositionManagerInstance::new(NPM_ADDRESS, provider.clone());
let npm = IUniswapV3NonfungiblePositionManager::new(NPM_ADDRESS, provider.clone());
let total_supply: U256 = npm.totalSupply().block(*BLOCK_NUMBER).call().await?._0;
let owner = npm
.ownerOf(total_supply - uint!(1_U256))
Expand Down
75 changes: 22 additions & 53 deletions src/viem/poolLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,15 @@ import { AbiParametersToPrimitiveTypes, ExtractAbiFunction } from "abitype";
import { Address, PublicClient } from "viem";
import {
EphemeralGetPopulatedTicksInRange__factory,
EphemeralPCSV3PoolPositions__factory,
EphemeralPCSV3PoolSlots__factory,
EphemeralPCSV3PoolTickBitmap__factory,
EphemeralPCSV3PoolTicks__factory,
EphemeralPoolPositions__factory,
EphemeralPoolSlots__factory,
EphemeralPoolTickBitmap__factory,
EphemeralPoolTicks__factory,
} from "../../typechain";
import { callEphemeralContract } from "./caller";
import { z } from 'zod';
import { z } from "zod";

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

/**
Expand Down Expand Up @@ -55,17 +48,11 @@ export async function getStaticSlots(
blockNumber?: bigint,
) {
return await callEphemeralContract(
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolSlots__factory.abi,
bytecode: EphemeralPCSV3PoolSlots__factory.bytecode,
args: [pool],
}
: {
abi: EphemeralPoolSlots__factory.abi,
bytecode: EphemeralPoolSlots__factory.bytecode,
args: [pool],
},
{
abi: EphemeralPoolSlots__factory.abi,
bytecode: EphemeralPoolSlots__factory.bytecode,
args: [pool],
},
publicClient,
blockNumber,
);
Expand All @@ -80,17 +67,11 @@ export async function getTicksSlots(
blockNumber?: bigint,
) {
return await callEphemeralContract(
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolTicks__factory.abi,
bytecode: EphemeralPCSV3PoolTicks__factory.bytecode,
args: [pool, tickLower, tickUpper],
}
: {
abi: EphemeralPoolTicks__factory.abi,
bytecode: EphemeralPoolTicks__factory.bytecode,
args: [pool, tickLower, tickUpper],
},
{
abi: EphemeralPoolTicks__factory.abi,
bytecode: EphemeralPoolTicks__factory.bytecode,
args: [pool, tickLower, tickUpper],
},
publicClient,
blockNumber,
);
Expand All @@ -103,17 +84,11 @@ export async function getTickBitmapSlots(
blockNumber?: bigint,
) {
return await callEphemeralContract(
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolTickBitmap__factory.abi,
bytecode: EphemeralPCSV3PoolTickBitmap__factory.bytecode,
args: [pool],
}
: {
abi: EphemeralPoolTickBitmap__factory.abi,
bytecode: EphemeralPoolTickBitmap__factory.bytecode,
args: [pool],
},
{
abi: EphemeralPoolTickBitmap__factory.abi,
bytecode: EphemeralPoolTickBitmap__factory.bytecode,
args: [pool],
},
publicClient,
blockNumber,
);
Expand All @@ -132,17 +107,11 @@ export async function getPositionsSlots(
blockNumber?: bigint,
) {
return await callEphemeralContract(
amm === AutomatedMarketMakerEnum.enum.PANCAKESWAP_V3
? {
abi: EphemeralPCSV3PoolPositions__factory.abi,
bytecode: EphemeralPCSV3PoolPositions__factory.bytecode,
args: [pool, keys],
}
: {
abi: EphemeralPoolPositions__factory.abi,
bytecode: EphemeralPoolPositions__factory.bytecode,
args: [pool, keys],
},
{
abi: EphemeralPoolPositions__factory.abi,
bytecode: EphemeralPoolPositions__factory.bytecode,
args: [pool, keys],
},
publicClient,
blockNumber,
);
Expand Down
2 changes: 1 addition & 1 deletion src/viem/storageLens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export async function getStorageAt(
): Promise<readonly Hex[]> {
const overrides = {
[address]: {
code: "0x60806040526004361061001e5760003560e01c8063dbd035ff14610023575b600080fd5b610036610031366004610081565b61004c565b60405161004391906100f6565b60405180910390f35b60606020600052816020528160051b6040016040845b81831461007a57803554825260209182019101610062565b5050806000f35b6000806020838503121561009457600080fd5b823567ffffffffffffffff808211156100ac57600080fd5b818501915085601f8301126100c057600080fd5b8135818111156100cf57600080fd5b8660208260051b85010111156100e457600080fd5b60209290920196919550909350505050565b6020808252825182820181905260009190848201906040850190845b8181101561012e57835183529284019291840191600101610112565b5090969550505050505056fea164736f6c6343000816000a" as Hex,
code: "0x60806040526004361061001e5760003560e01c8063dbd035ff14610023575b600080fd5b61003661003136600461007c565b61004c565b60405161004391906100f3565b60405180910390f35b60606020600052816020528160051b6040016040845b803554825260209182019101828210610062575050806000f35b6000806020838503121561008f57600080fd5b823567ffffffffffffffff8111156100a657600080fd5b8301601f810185136100b757600080fd5b803567ffffffffffffffff8111156100ce57600080fd5b8560208260051b84010111156100e357600080fd5b6020919091019590945092505050565b602080825282518282018190526000918401906040840190835b8181101561012b57835183526020938401939092019160010161010d565b50909594505050505056fea164736f6c634300081a000a" as Hex,
},
};
const data = await staticCallWithOverrides(
Expand Down
Loading
Loading