0

I am trying to deploy the Uniswap v2 Factory contract on my ganache test network. I have compiled these contracts with my own compiler. But everything I attempt to deploy the Factory, I receive this error c: VM Exception while processing transaction: out of gas.

When I check the compiledFactory.evm.gasEstimates.creation here is what I receive : { codeDepositCost: '2155800', executionCost: 'infinite', totalCost: 'infinite' }.

I have heared problems about deploying the Uni v2 Router contract, but not the factory ? I tried to comment the allPairLength function in the UniswapV2Factory contract, but the result is the same.

Here is the UniswapV2Factory contract that I deploy (pulled from the Uniswap github, so it should be the same):

pragma solidity =0.5.16;

import './interfaces/IUniswapV2Factory.sol';
import './UniswapV2Pair.sol';

contract UniswapV2Factory is IUniswapV2Factory {
    address public feeTo;
    address public feeToSetter;

    mapping(address => mapping(address => address)) public getPair;
    address[] public allPairs;

    event PairCreated(address indexed token0, address indexed token1, address pair, uint);

    constructor(address _feeToSetter) public {
        feeToSetter = _feeToSetter;
    }

    function allPairsLength() external view returns (uint) {
        return allPairs.length;
    }

    function createPair(address tokenA, address tokenB) external returns (address pair) {
        require(tokenA != tokenB, 'UniswapV2: IDENTICAL_ADDRESSES');
        (address token0, address token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
        require(token0 != address(0), 'UniswapV2: ZERO_ADDRESS');
        require(getPair[token0][token1] == address(0), 'UniswapV2: PAIR_EXISTS'); // single check is sufficient
        bytes memory bytecode = type(UniswapV2Pair).creationCode;
        bytes32 salt = keccak256(abi.encodePacked(token0, token1));
        assembly {
            pair := create2(0, add(bytecode, 32), mload(bytecode), salt)
        }
        IUniswapV2Pair(pair).initialize(token0, token1);
        getPair[token0][token1] = pair;
        getPair[token1][token0] = pair; // populate mapping in the reverse direction
        allPairs.push(pair);
        emit PairCreated(token0, token1, pair, allPairs.length);
    }

    function setFeeTo(address _feeTo) external {
        require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN');
        feeTo = _feeTo;
    }

    function setFeeToSetter(address _feeToSetter) external {
        require(msg.sender == feeToSetter, 'UniswapV2: FORBIDDEN');
        feeToSetter = _feeToSetter;
    }
}

here is my test file :

const assert = require("assert");
const ganache = require("ganache-cli");
const Web3 = require("web3");
const web3 = new Web3(ganache.provider());

const compiledFactory = require("../ethereum/contracts/core/build/UniswapV2Factory.json");
const compiledERC20 = require("../ethereum/contracts/core/build/ERC20.json");

let accounts;
let erc20Contract;
let factory ;
let token1;
let token2;
let token1Address;
let token2Address;
let poolAddress;

beforeEach(async function() {
    accounts = await web3.eth.getAccounts();

    // console.log(compiledFactory.evm.gasEstimates.creation);
    // console.log(compiledFactory);

    token1 = await new web3.eth.Contract(compiledERC20.abi).deploy({ data: compiledERC20.evm.bytecode.object, arguments: [100] }).send({ from: accounts[0], gas: "1000000" });
    token2 = await new web3.eth.Contract(compiledERC20.abi).deploy({ data: compiledERC20.evm.bytecode.object, arguments: [100] }).send({ from: accounts[0], gas: "1000000" });

    factory = await new web3.eth.Contract(compiledFactory.abi).deploy({ data: compiledFactory.evm.bytecode.object, arguments: [accounts[0]] }).send({ from: accounts[0], gas: "1000000" });

    token1Address = token1.options.address;
    token2Address = token2.options.address;

    // poolAddress = factory.methods.createPair(token1Address, token2Address);
});

describe("Tokens", function() {
    it("deploy two tokens", () => {
        assert.ok(token1.options.address);
        assert.ok(token2.options.address);
    });
});

describe("Factory", function() {
    it("deploy the factory", () => {
        assert.ok(factory);
    });
    it("deploy a liquidity pool", () => {
        assert.ok(poolAddress);
    });
});

here is my full repo (https://github.com/d-s-i/uniswap-fork) if you want to see the whole code (all contracts, compiled contract, and compile script are in the ethereum/contracts/core folder).

Thanks to anyone for considering helping

6
  • You provide only 1 000 000 of gas. Usually deployment of a contract costs more gas. Jul 25 at 15:49
  • if you are short on money to deploy, you can call functions in other similar contracts, but it will cost you more gas at runtime since external call costs more gas
    – Nulik
    Jul 25 at 15:53
  • deploy on Sunday/Saturday morning NY time and you will get lowest gas price
    – Nulik
    Jul 25 at 15:53
  • Yes I see but I tried upgrading to "100000000000" for example and I receive this error : n: Exceeds block gas limit. Side note, this is only ganache so it's not real money ... I could spend any gas amount I want. Note that the gasEstimates function return "infinity", I don't know why
    – Cizia
    Jul 25 at 16:04
  • 1
    Oh it worked. For whatever reason I didn't think a too high gas in a transaction was possible, and that the error returned a general error for the whole block gas spent. Thank you so much Prorok ...
    – Cizia
    Jul 25 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.