I am trying to create and add liquidity to a pool inside the contract constructor


// SPDX-License-Identifier: MIT

pragma solidity ^0.8;

import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@uniswap/v2-core/contracts/interfaces/iUniswapV2Factory.sol";
import "@uniswap/v2-core/contracts/interfaces/iUniswapV2Pair.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IWETH.sol";

contract BarnaTokenInu is Ownable, ERC20 {
    using Address for address;

    string constant __name = "BARNATOKENINU";
    string constant __symbol = "BTI";
    uint256 constant __decimals = 18;

    uint256 constant fractionForOwner = 20;
    uint256 constant deadlineForLiquidityProvision = 20 * 60; 
    uint256 constant initialSupply = 1e9 * 10**__decimals; 
    uint256 constant amountForOwner = (initialSupply * fractionForOwner) / 100; 
    uint256 constant amountForContract =
        (initialSupply * (100 - fractionForOwner)) / 100;

    address public router_address; // address of router contract
    IUniswapV2Router02 public router; // router interface object
    IUniswapV2Factory public factory; // factory interface object
    IUniswapV2Pair public pair; // pair interface object
    address public liquidityPoolPair; // address of liquidity pool pair
    IWETH public iweth; // weth object interface
    IERC20 public weth;

    constructor(address _router_address) ERC20(__name, __symbol) payable {
        router_address = _router_address;
        router = IUniswapV2Router02(router_address);
        factory = IUniswapV2Factory(router.factory()); 
        liquidityPoolPair = factory.createPair(router.WETH(), address(this)); 
        pair = IUniswapV2Pair(liquidityPoolPair);
        liquidityPools[liquidityPoolPair] = true;
        iweth = IWETH(router.WETH());
        weth = IERC20(router.WETH());

        _approve(owner(), router_address, type(uint256).max); 
        _approve(address(this), router_address, type(uint256).max);

        _mint(owner(), amountForOwner); // mint token to owner
        _mint(address(this), amountForContract); // mint token to contract

        router.addLiquidityETH{value: msg.value}(
            block.timestamp + deadlineForLiquidityProvision

The script used to deploy the contract is as follows:

def deploy_token_contract(_account, _value):
    router = config["networks"][network.show_active()]["router_address"]
    return BarnaTokenInu.deploy(router, {"from": _account, "value": _value})

def main():
    account = get_account()
    value = 0.02 * 10**18
    contract = deploy_token_contract(account, value)


I keep getting revert error. Has anybody some hint?

  • what does the revert message says?
    – Casareafer
    Nov 8 at 9:39


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.