@Isa_O @gperezalba
I had similar issues and after doing some debugging I found that there is an issue with
UniswapV2Library function. For some reason function pairFor of this library returns different pair address.
using SafeMath for uint;
// returns sorted token addresses, used to handle return values from pairs sorted in this order
function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
require(tokenA != tokenB, 'UniswapV2Library: IDENTICAL_ADDRESSES');
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
require(token0 != address(0), 'UniswapV2Library: ZERO_ADDRESS');
}
// calculates the CREATE2 address for a pair without making any external calls
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
))));
}
// fetches and sorts the reserves for a pair
Change this function to this and it will work:
import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol';
function pairFor(address factory, address tokenA, address tokenB) internal view returns (address pair) {
pair = IUniswapV2Factory(factory).getPair(tokenA,tokenB);
}
4 Likes