â—† Purpose

Uniswap charges 0.3% fee after swap

So how much you get after transaction fee? Get the value about

â—† Input & Return value

(uint amountIn, uint reserveIn, uint reserveOut) ⇒ (uint amountOut)

â—† Example

amountIn = 1000 reserveIn (WETH) = 43086702975516 reserveOut (USDC) = 41997552572672937696884


amountInWithFee = 1000*997 = 997000

numerator = 997000*41997552572672937696884

denominator = 43086702975516*1000 + 9997000

amountOut=(99700041997552572672937696884)/(430867029755161000+9997000)

⇒ 971797724440 ( 974721890337 : This value is using quote🌟 function)

â—† Code and Explain

function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
        require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
        require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
				//Solidity cannnot calculate decimal point so, using 997/1000	to get 0.3%
        uint amountInWithFee = amountIn.mul(997);
        uint numerator = amountInWithFee.mul(reserveOut);
        uint denominator = reserveIn.mul(1000).add(amountInWithFee);
        amountOut = numerator / denominator;
    }

â—† Resources

https://github.com/Uniswap/v2-periphery/blob/master/contracts/libraries/UniswapV2Library.sol#L43