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

Fix swap error in some pool #56

Merged
merged 3 commits into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file not shown.
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default {
hardhat: {
forking: {
url: process.env.FORKING_PROVIDER_URL,
blockNumber: 12464951,
blockNumber: 13578942,
},
mining: {
auto: true,
Expand Down
Binary file removed liquidity_events_usdc_weth_3000.db
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"test": "yarn mocha",
"lint": "yarn prettier --write .",
"clean": "hardhat clean",
"compile": "hardhat compile",
"compile": "hardhat compile && yarn generate-types",
"testcontracts": "hardhat test"
},
"author": "",
Expand Down
19 changes: 8 additions & 11 deletions scripts/Events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,18 @@ import { EventType } from "../src/enum/EventType";
import { ethers } from "hardhat";
import type { UniswapV3Pool2 } from "../src/typechain";

let liquidityEventDB: EventDBManager;
let swapEventDB: EventDBManager;
let eventDB: EventDBManager;
let poolAddress = "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8";
let fromBlock = 12777394;
let toBlock = 13556212; //13556212;
let batchSize = 500;

// @Deprecated
// See src/client/MainnetDataDownloader.ts instead
async function main() {
liquidityEventDB = await EventDBManager.buildInstance(
eventDB = await EventDBManager.buildInstance(
"liquidity_events_usdc_weth_3000.db"
);
swapEventDB = await EventDBManager.buildInstance(
"swap_events_usdc_weth_3000.db"
);
let uniswapV3Pool = (await ethers.getContractAt(
"UniswapV3Pool2",
poolAddress
Expand All @@ -42,7 +40,7 @@ async function main() {
for (let event of events) {
let block = await ethers.provider.getBlock(event.blockNumber);
let date = new Date(block.timestamp * 1000);
await liquidityEventDB.insertLiquidityEvent(
await eventDB.insertLiquidityEvent(
eventType,
event.args.amount.toString(),
event.args.amount0.toString(),
Expand All @@ -61,7 +59,7 @@ async function main() {
for (let event of events) {
let block = await ethers.provider.getBlock(event.blockNumber);
let date = new Date(block.timestamp * 1000);
await liquidityEventDB.insertLiquidityEvent(
await eventDB.insertLiquidityEvent(
eventType,
event.args.amount.toString(),
event.args.amount0.toString(),
Expand All @@ -80,7 +78,7 @@ async function main() {
for (let event of events) {
let block = await ethers.provider.getBlock(event.blockNumber);
let date = new Date(block.timestamp * 1000);
await swapEventDB.insertSwapEvent(
await eventDB.insertSwapEvent(
event.args.amount0.toString(),
event.args.amount1.toString(),
event.args.sqrtPriceX96.toString(),
Expand All @@ -94,8 +92,7 @@ async function main() {
}
}
}
await liquidityEventDB.close();
await swapEventDB.close();
await eventDB.close();
}

main()
Expand Down
32 changes: 21 additions & 11 deletions src/client/MainnetDataDownloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,15 @@ export class MainnetDataDownloader {
}

async parseEndBlockTypeWhenRecover(
latestDownloadedEventBlockNumber: number,
toBlock: EndBlockTypeWhenRecover,
poolAddress: string
): Promise<number> {
switch (toBlock) {
case "latestOnChain":
return (await this.RPCProvider.getBlock("latest")).number;
case "latestDownloaded":
return 0;
return latestDownloadedEventBlockNumber;
case "afterDeployment":
return await this.queryDeploymentBlockNumber(poolAddress);
case "afterInitialization":
Expand Down Expand Up @@ -199,21 +200,22 @@ export class MainnetDataDownloader {
);

// check toBlock then
let deploymentBlockNumber = await this.queryDeploymentBlockNumber(
poolAddress
);
let toBlockAsNumber = await this.parseEndBlockTypeWhenRecover(
toBlock,
poolAddress
);
if (toBlockAsNumber < deploymentBlockNumber)
throw new Error("toBlock is too small, the pool hasn't been deployed.");

let eventDB = await EventDBManager.buildInstance(mainnetEventDBFilePath);
try {
let latestEventBlockNumber = await eventDB.getLatestEventBlockNumber();
let deploymentBlockNumber = await this.queryDeploymentBlockNumber(
poolAddress
);
let toBlockAsNumber = await this.parseEndBlockTypeWhenRecover(
latestEventBlockNumber,
toBlock,
poolAddress
);
if (toBlockAsNumber < deploymentBlockNumber)
throw new Error("toBlock is too small, the pool hasn't been deployed.");

if (toBlockAsNumber < latestEventBlockNumber) {
console.log("It's already up to date.");
return;
}

Expand Down Expand Up @@ -241,6 +243,14 @@ export class MainnetDataDownloader {
);
}

if (
!updateInitializationEvent &&
toBlockAsNumber == latestEventBlockNumber
) {
console.log("It's already up to date.");
return;
}

// download events after initialization
await this.downloadEvents(
uniswapV3Pool,
Expand Down
22 changes: 11 additions & 11 deletions src/client/SimulatorClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ export class SimulatorClient {
let mainnetDataDownloader: MainnetDataDownloader =
new MainnetDataDownloader(RPCProviderUrl);
await mainnetDataDownloader.download(poolName, poolAddress, endBlock);
let endBlockInNumber =
await mainnetDataDownloader.parseEndBlockTypeWhenInit(
endBlock,
poolAddress
);
let eventDBFilePath = mainnetDataDownloader.generateMainnetEventDBFilePath(
poolName,
poolAddress
Expand All @@ -54,8 +49,12 @@ export class SimulatorClient {
let poolConfig = await eventDB.getPoolConfig();
let configurableCorePool: IConfigurableCorePool =
this.initCorePoolFromConfig(poolConfig!);

if (endBlock == "afterDeployment") return configurableCorePool;
let endBlockInNumber =
await mainnetDataDownloader.parseEndBlockTypeWhenInit(
endBlock,
poolAddress
);
await mainnetDataDownloader.initializeAndReplayEvents(
eventDB,
configurableCorePool,
Expand All @@ -79,17 +78,18 @@ export class SimulatorClient {
let { poolAddress } = mainnetDataDownloader.parseFromMainnetEventDBFilePath(
mainnetEventDBFilePath
);
let endBlockInNumber =
await mainnetDataDownloader.parseEndBlockTypeWhenRecover(
endBlock,
poolAddress
);
let eventDB = await EventDBManager.buildInstance(mainnetEventDBFilePath);
try {
let poolConfig = await eventDB.getPoolConfig();
let configurableCorePool: IConfigurableCorePool =
this.initCorePoolFromConfig(poolConfig!);
if (endBlock == "afterDeployment") return configurableCorePool;
let endBlockInNumber =
await mainnetDataDownloader.parseEndBlockTypeWhenRecover(
await eventDB.getLatestEventBlockNumber(),
endBlock,
poolAddress
);
await mainnetDataDownloader.initializeAndReplayEvents(
eventDB,
configurableCorePool,
Expand Down
16 changes: 10 additions & 6 deletions src/core/ConfigurableCorePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { PoolStateView } from "../interface/PoolStateView";
import { Transition as TransitionView } from "../interface/Transition";
import { SwapEvent } from "../entity/SwapEvent";
import { ZERO } from "../enum/InternalConstants";
import { FullMath } from "..";

export class ConfigurableCorePool implements IConfigurableCorePool, Visitable {
readonly id: string;
Expand Down Expand Up @@ -279,20 +280,23 @@ export class ConfigurableCorePool implements IConfigurableCorePool, Visitable {
sqrtPriceLimitX96
).then(({ amount0, amount1, sqrtPriceX96 }) => {
return (
(JSBI.equal(amount0, param.amount0) &&
JSBI.equal(amount1, param.amount1) &&
JSBI.equal(sqrtPriceX96, param.sqrtPriceX96)) ||
param.id == 10
JSBI.equal(amount0, param.amount0) &&
JSBI.equal(amount1, param.amount1) &&
JSBI.equal(sqrtPriceX96, param.sqrtPriceX96)
);
});
};

let solution1 = {
amountSpecified: param.amount0,
amountSpecified: JSBI.equal(param.liquidity, ZERO)
? FullMath.incrTowardInfinity(param.amount0)
: param.amount0,
sqrtPriceLimitX96: param.sqrtPriceX96,
};
let solution2 = {
amountSpecified: param.amount1,
amountSpecified: JSBI.equal(param.liquidity, ZERO)
? FullMath.incrTowardInfinity(param.amount1)
: param.amount1,
sqrtPriceLimitX96: param.sqrtPriceX96,
};
let solution3 = {
Expand Down
7 changes: 7 additions & 0 deletions src/util/FullMath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,11 @@ export abstract class FullMath {
}
return z;
}

static incrTowardInfinity(value: JSBI): JSBI {
assert(JSBI.notEqual(value, ZERO), "ZERO");
return JSBI.greaterThan(value, ZERO)
? JSBI.add(value, ONE)
: JSBI.subtract(value, ONE);
}
}
38 changes: 16 additions & 22 deletions test/ConfigurableCorePool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ describe("Test ConfigurableCorePool", function () {
let simulationDataManager: SimulationDataManager;
let configurableCorePool: IConfigurableCorePool;
let simulatorRoadmapManager: SimulatorRoadmapManager;
let liquidityEventDB: EventDBManager;
let swapEventDB: EventDBManager;
let eventDB: EventDBManager;
let sqrtPriceX96ForInitialization = JSBI.BigInt(
"0x43efef20f018fdc58e7a5cf0416a"
);
Expand All @@ -42,19 +41,17 @@ describe("Test ConfigurableCorePool", function () {
endDate: Date
): Promise<(LiquidityEvent | SwapEvent)[]> {
let events: (LiquidityEvent | SwapEvent)[] = [];
let mintEvents: LiquidityEvent[] =
await liquidityEventDB.getLiquidityEventsByDate(
EventType.MINT,
format(startDate, "yyyy-MM-dd HH:mm:ss"),
format(endDate, "yyyy-MM-dd HH:mm:ss")
);
let burnEvents: LiquidityEvent[] =
await liquidityEventDB.getLiquidityEventsByDate(
EventType.BURN,
format(startDate, "yyyy-MM-dd HH:mm:ss"),
format(endDate, "yyyy-MM-dd HH:mm:ss")
);
let swapEvents: SwapEvent[] = await swapEventDB.getSwapEventsByDate(
let mintEvents: LiquidityEvent[] = await eventDB.getLiquidityEventsByDate(
EventType.MINT,
format(startDate, "yyyy-MM-dd HH:mm:ss"),
format(endDate, "yyyy-MM-dd HH:mm:ss")
);
let burnEvents: LiquidityEvent[] = await eventDB.getLiquidityEventsByDate(
EventType.BURN,
format(startDate, "yyyy-MM-dd HH:mm:ss"),
format(endDate, "yyyy-MM-dd HH:mm:ss")
);
let swapEvents: SwapEvent[] = await eventDB.getSwapEventsByDate(
format(startDate, "yyyy-MM-dd HH:mm:ss"),
format(endDate, "yyyy-MM-dd HH:mm:ss")
);
Expand Down Expand Up @@ -203,11 +200,8 @@ describe("Test ConfigurableCorePool", function () {
simulationDataManager = await SQLiteSimulationDataManager.buildInstance(
"./test/database.db"
);
liquidityEventDB = await EventDBManager.buildInstance(
"liquidity_events_usdc_weth_3000.db"
);
swapEventDB = await EventDBManager.buildInstance(
"swap_events_usdc_weth_3000.db"
eventDB = await EventDBManager.buildInstance(
"events_0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8.db"
);
simulatorRoadmapManager = new SimulatorRoadmapManager(
simulationDataManager
Expand All @@ -222,8 +216,7 @@ describe("Test ConfigurableCorePool", function () {

afterEach(async function () {
await simulationDataManager.close();
await liquidityEventDB.close();
await swapEventDB.close();
await eventDB.close();
});

describe("Test get method", function () {
Expand Down Expand Up @@ -500,6 +493,7 @@ describe("Test ConfigurableCorePool", function () {
console.log(
"sqrtPriceX96: " + testPool.getCorePool().sqrtPriceX96.toString()
);
console.log("tick: " + testPool.getCorePool().tickCurrent);
console.log("liquidity: " + testPool.getCorePool().liquidity.toString());
});
});
Expand Down
12 changes: 9 additions & 3 deletions test/SimulatorClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { PoolConfig } from "../src/model/PoolConfig";
import { ConfigurableCorePool as IConfigurableCorePool } from "../src/interface/ConfigurableCorePool";
import { ConfigurableCorePool } from "../src/core/ConfigurableCorePool";
import JSBI from "jsbi";
import { EndBlockTypeWhenRecover } from "../src/entity/EndBlockType";
chai.use(chaiAsPromised);
const expect = chai.expect;

Expand All @@ -24,8 +25,8 @@ describe("Test SimulatorClient v2", function () {
// case 2
// 0x92560C178cE069CC014138eD3C2F5221Ba71f58a
// 13578943
let poolAddress = "0x8ad599c3A0ff1De082011EFDDc58f1908eb6e6D8";
let endBlock = 12374077;
let poolAddress = "0x92560C178cE069CC014138eD3C2F5221Ba71f58a";
let endBlock: EndBlockTypeWhenRecover = 13579000;
// Your customed RPCProviderUrl, or use config in tuner.config.js
let RPCProviderUrl: string | undefined = undefined;

Expand All @@ -42,7 +43,12 @@ describe("Test SimulatorClient v2", function () {
endBlock,
RPCProviderUrl
);
console.log(configurableCorePool.getCorePool().sqrtPriceX96.toString());
console.log(`tick: ${configurableCorePool.getCorePool().tickCurrent}`);
console.log(
`sqrtPriceX96: ${configurableCorePool
.getCorePool()
.sqrtPriceX96.toString()}`
);
await clientInstance.shutdown();
});
});
Expand Down
Loading