Skip to content

How do you handle rounding errors between querySwap and swap? #51

@jamescarter-le

Description

@jamescarter-le

It's only a small issue, especially when used for back testing and your not going to make any live transactions from the results, and you query the chain for its state at the time, however I've noticed an issue with regards to rounding with querySwap vs swap when sqrtPrice is applied, however I would expect it to resolve to the same as the Amounts in the EventLog of Swap?

This is how you determine amountRequested from your tests: https://github.com/Bella-DeFinTech/uniswap-v3-simulator/blob/dev/scripts/Events.ts

Sorry its not in your unit test format, I just wanted to verify it was working for my desired pool before hooking it up:

import JSBI from 'jsbi';
import { CorePool } from './src/core/CorePool';
import { FeeAmount } from './src/enum/FeeAmount';

function main()
{
    // Pool is placed in the Initialized state, with no operations yet invoked apart from it being Initialized.
    var pool = new CorePool("", "", FeeAmount.MEDIUM, 60, JSBI.BigInt(0), JSBI.BigInt(0), JSBI.BigInt("2505290050365003892876723467"),
    JSBI.BigInt(0), -69082, JSBI.BigInt(0), JSBI.BigInt(0));

    // Block 13578816   Tx 0x2f0461509914dbd045ae6946a26f69850b914f97a67dd6078fa5d871959fb105  LogIndex  16
    pool.mint("0xC36442b4a4522E871399CD717aBDD847Ab11FE88", 49800, 64020, JSBI.BigInt("556973545490136947176"));
    // Block 13578904   Tx 0x28fe258a62cf142fced434c5eb3ff051970470333b5770118c8df225e374849b  LogIndex  460
    pool.burn("0xC36442b4a4522E871399CD717aBDD847Ab11FE88", 49800, 64020, JSBI.BigInt("529124868215630099817"));
    // Block 13578904   Tx 0x28fe258a62cf142fced434c5eb3ff051970470333b5770118c8df225e374849b  LogIndex  462
    pool.burn("0xC36442b4a4522E871399CD717aBDD847Ab11FE88", 49800, 64020, JSBI.BigInt("0"));

    // Block 13578932   Tx 0x4d8a042cae977aa124cda85419461093fb87b04814126e024a05dc4f8252e27a  LogIndex  187
    var amount0 = JSBI.BigInt("-927248711787417535");
    var amount1 = JSBI.BigInt("226019397190954581334");
    var sqrtPrice = JSBI.BigInt("1596559182082899146010277864392");

    var zeroForOne: boolean = JSBI.greaterThan(amount0, JSBI.BigInt("0")) ? true : false;

    var queryResult = pool.querySwap(zeroForOne, amount1);
    if(!JSBI.equal(queryResult.amount0, amount0))
        console.log("Amount1: Amount 0 Wrong");
    if(!JSBI.equal(queryResult.amount1, amount1))
        console.log("Amount1: Amount 1 Wrong");

    var applyResult = pool.swap(zeroForOne, amount0, sqrtPrice);
    if(!JSBI.equal(applyResult.amount0, amount0))
        console.log("Apply: Amount 0 Wrong");
    if(!JSBI.equal(applyResult.amount1, amount1))
        console.log("Apply: Amount 1 Wrong: " + applyResult.amount1.toString() + "  vs expected  " + amount1.toString());// <---- Error, is out by 1 LSB.
}

main();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions