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 rounding error in replaying swap events #53

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
2 changes: 1 addition & 1 deletion src/core/ConfigurableCorePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ export class ConfigurableCorePool implements IConfigurableCorePool, Visitable {
zeroForOne: boolean,
amountSpecified: JSBI,
sqrtPriceLimitX96?: JSBI
): Promise<{ amount0: JSBI; amount1: JSBI }> {
): Promise<{ amount0: JSBI; amount1: JSBI; sqrtPriceX96: JSBI }> {
return Promise.resolve(
this.corePool.querySwap(zeroForOne, amountSpecified, sqrtPriceLimitX96)
);
Expand Down
6 changes: 3 additions & 3 deletions src/core/CorePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class CorePool {
zeroForOne: boolean,
amountSpecified: JSBI,
sqrtPriceLimitX96?: JSBI
): { amount0: JSBI; amount1: JSBI } {
): { amount0: JSBI; amount1: JSBI; sqrtPriceX96: JSBI } {
return this.handleSwap(
zeroForOne,
amountSpecified,
Expand All @@ -213,7 +213,7 @@ export class CorePool {
amountSpecified: JSBI,
sqrtPriceLimitX96: JSBI | undefined,
isStatic: boolean
): { amount0: JSBI; amount1: JSBI } {
): { amount0: JSBI; amount1: JSBI; sqrtPriceX96: JSBI } {
if (!sqrtPriceLimitX96)
sqrtPriceLimitX96 = zeroForOne
? JSBI.add(TickMath.MIN_SQRT_RATIO, ONE)
Expand Down Expand Up @@ -386,7 +386,7 @@ export class CorePool {
JSBI.subtract(amountSpecified, state.amountSpecifiedRemaining),
];

return { amount0, amount1 };
return { amount0, amount1, sqrtPriceX96: state.sqrtPriceX96 };
}

private checkTicks(tickLower: number, tickUpper: number) {
Expand Down
2 changes: 1 addition & 1 deletion src/interface/ConfigurableCorePool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export interface ConfigurableCorePool {
zeroForOne: boolean,
amountSpecified: JSBI,
sqrtPriceLimitX96?: JSBI
): Promise<{ amount0: JSBI; amount1: JSBI }>;
): Promise<{ amount0: JSBI; amount1: JSBI; sqrtPriceX96: JSBI }>;

// user custom PostProcessor will be called after pool state transition finishes
updatePostProcessor(
Expand Down
164 changes: 75 additions & 89 deletions test/ConfigurableCorePool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,100 +102,25 @@ describe("Test ConfigurableCorePool", function () {
expect(amount1.toString()).to.eql(param.amount1.toString());
break;
case EventType.SWAP:
let zeroForOne: boolean = JSBI.greaterThan(param.amount0, ZERO)
? true
: false;

// try-error to find `amountSpecified` and `sqrtPriceLimitX96` to resolve to the same result as swap event records
let errArr: Error[] = [];

async function tryWithDryRun(
amountSpecified: JSBI
): Promise<boolean> {
({ amount0, amount1 } = await configurableCorePool.querySwap(
zeroForOne,
amountSpecified
));
let dryRunRes =
JSBI.equal(amount0, param.amount0) &&
JSBI.equal(amount1, param.amount1);

if (!dryRunRes) {
errArr.push(
new Error(
`Result is not match. amount0: ${amount0}, amount1: ${amount1}`
)
);
}
return Promise.resolve(dryRunRes);
}

async function trySwap(
amountSpecified: JSBI,
expectedSqrtPriceX96: JSBI
): Promise<boolean> {
if (
!JSBI.equal(
param.sqrtPriceX96,
configurableCorePool.getCorePool().sqrtPriceX96
)
) {
if (
JSBI.equal(
expectedSqrtPriceX96,
configurableCorePool.getCorePool().sqrtPriceX96
)
) {
({ amount0, amount1 } = await configurableCorePool.swap(
zeroForOne,
amountSpecified
));
} else {
({ amount0, amount1 } = await configurableCorePool.swap(
zeroForOne,
amountSpecified,
expectedSqrtPriceX96
));
}

let res = JSBI.equal(
configurableCorePool.getCorePool().sqrtPriceX96,
(param as SwapEvent).sqrtPriceX96
);

if (!res) {
errArr.push(
new Error(
`Result is not match. actual price: ${
configurableCorePool.getCorePool().sqrtPriceX96
}, expected price: ${
(param as SwapEvent).sqrtPriceX96
}. amount0: ${amount0}, amount1: ${amount1}`
)
);
configurableCorePool.stepBack();
}
return res;
}

let trySwapRes = await tryWithDryRun(param.amount0);
if (trySwapRes) {
let swapRes = await trySwap(param.amount0, param.sqrtPriceX96);
if (swapRes) {
// add AmountSpecified column to swap event database
// await swapEventDB.addAmountSpecified(
// param.id,
// param.amount0.toString()
// );
await tryAndSwap(errArr, param, param.amount0, param.sqrtPriceX96)
)
break;
}
}
trySwapRes = await tryWithDryRun(param.amount1);
if (trySwapRes) {
errArr = [];
let swapRes = await trySwap(param.amount1, param.sqrtPriceX96);
if (swapRes) {
// add AmountSpecified column to swap event database
// await swapEventDB.addAmountSpecified(
// param.id,
// param.amount1.toString()
// );
else if (
await tryAndSwap(errArr, param, param.amount1, param.sqrtPriceX96)
)
break;
}
}
if (await tryAndSwap(errArr, param, param.amount0)) break;
else if (await tryAndSwap(errArr, param, param.amount1)) break;

if (errArr.length != 0) {
console.log(param.id);
Expand All @@ -211,6 +136,67 @@ describe("Test ConfigurableCorePool", function () {
const exhaustiveCheck: never = param;
}
}

async function swapWithDryRun(
errArr: Error[],
param: SwapEvent,
amountSpecified: JSBI,
sqrtPriceLimitX96?: JSBI
): Promise<boolean> {
let zeroForOne: boolean = JSBI.greaterThan(param.amount0, ZERO)
? true
: false;
let { amount0, amount1, sqrtPriceX96 } =
await configurableCorePool.querySwap(
zeroForOne,
amountSpecified,
sqrtPriceLimitX96
);
let dryRunRes =
JSBI.equal(amount0, param.amount0) &&
JSBI.equal(amount1, param.amount1) &&
JSBI.equal(sqrtPriceX96, param.sqrtPriceX96);

if (!dryRunRes) {
errArr.push(
new Error(
`Result is not match. amount0: ${amount0}, amount1: ${amount1}, actual sqrtPriceX96: ${sqrtPriceX96}`
)
);
}
return Promise.resolve(dryRunRes);
}

async function tryAndSwap(
errArr: Error[],
param: SwapEvent,
amountSpecified: JSBI,
sqrtPriceLimitX96?: JSBI
): Promise<boolean> {
let zeroForOne: boolean = JSBI.greaterThan(param.amount0, ZERO)
? true
: false;
let trySwapRes = await swapWithDryRun(
errArr,
param,
amountSpecified,
sqrtPriceLimitX96
);
if (trySwapRes) {
await configurableCorePool.swap(
zeroForOne,
amountSpecified,
sqrtPriceLimitX96
);

// add AmountSpecified column to swap event database
neptune-v marked this conversation as resolved.
Show resolved Hide resolved
// await swapEventDB.addAmountSpecified(
// param.id,
// amountSpecified.toString()
// );
}
return trySwapRes;
}
}

beforeEach(async function () {
Expand Down