I'm trying to create a helper in a contract that lets the user deposit a single token and have it internally swap to the other token and add it as liquidity.

Lets say there is an LP with ETH and BTC and a user supplies only ETH, then how much of that ETH should be swapped to BTC to maximize the liquidity returned?

You could swap 50% percent but I'm looking for the exact answer that takes into account the 0.3% fee and the change in ratio because of the swap.

Zapper calculates as follows:

    function calculateSwapInAmount(uint256 reserveIn, uint256 userIn)
        returns (uint256)
                reserveIn.mul(userIn.mul(3988000) + reserveIn.mul(3988009))
                .sub(reserveIn.mul(1997)) / 1994;

Zapper uniswap add contract (https://etherscan.io/address/0x5ACedBA6C402e2682D312a7b4982eda0Ccf2d2E3#code)

How is this formula derived and is it correct?

2 Answers 2


I have the math to this exactly worked out via my python package UniswapPy; the step-by-step walkthrough is in the jupyter notebook

Here are a few image clippings from it:

enter image description here

enter image description here

Then solve the quadratic for alpha; but you really have to read the notebook to get the full description


Got an answer via Zapper discord which lead me to https://blog.alphafinance.io/onesideduniswap/

  • 2
    You should copy the relevant bits from the linked article here in case it is taken down.
    – Qwerty
    Oct 19, 2021 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.