0

I have a smart contract that is compiled and deployed using Remix and verified on Sepolia testnet.

https://sepolia.etherscan.io/verifyContract-solc?a=0xA3Bc4ac115CF4C57b507797731F43c14Ff6A2A6c&c=v0.8.18%2bcommit.87f61d96&lictype=3

When I deploy it using go-ethereum I have this transaction https://sepolia.etherscan.io/tx/0x5b74977f9e0fc4feeccf62bd6d4a6c8758b24babc5674c90c1a7a2cf8d7ded34

This is the contract.

// File: @uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol

pragma solidity >=0.5.0;

interface IUniswapV2Pair {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external pure returns (string memory);
    function symbol() external pure returns (string memory);
    function decimals() external pure returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);

    function DOMAIN_SEPARATOR() external view returns (bytes32);
    function PERMIT_TYPEHASH() external pure returns (bytes32);
    function nonces(address owner) external view returns (uint);

    function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

    event Mint(address indexed sender, uint amount0, uint amount1);
    event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
    event Swap(
        address indexed sender,
        uint amount0In,
        uint amount1In,
        uint amount0Out,
        uint amount1Out,
        address indexed to
    );
    event Sync(uint112 reserve0, uint112 reserve1);

    function MINIMUM_LIQUIDITY() external pure returns (uint);
    function factory() external view returns (address);
    function token0() external view returns (address);
    function token1() external view returns (address);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
    function price0CumulativeLast() external view returns (uint);
    function price1CumulativeLast() external view returns (uint);
    function kLast() external view returns (uint);

    function mint(address to) external returns (uint liquidity);
    function burn(address to) external returns (uint amount0, uint amount1);
    function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
    function skim(address to) external;
    function sync() external;

    function initialize(address, address) external;
}

// File: @uniswap/v2-core/contracts/interfaces/IUniswapV2Factory.sol

pragma solidity >=0.5.0;

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

    function feeTo() external view returns (address);
    function feeToSetter() external view returns (address);

    function getPair(address tokenA, address tokenB) external view returns (address pair);
    function allPairs(uint) external view returns (address pair);
    function allPairsLength() external view returns (uint);

    function createPair(address tokenA, address tokenB) external returns (address pair);

    function setFeeTo(address) external;
    function setFeeToSetter(address) external;
}

// File: contracts/@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router01.sol

pragma solidity >=0.6.2;

interface IUniswapV2Router01 {
    function factory() external pure returns (address);
    function WETH() external pure returns (address);

    function addLiquidity(
        address tokenA,
        address tokenB,
        uint amountADesired,
        uint amountBDesired,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB, uint liquidity);
    function addLiquidityETH(
        address token,
        uint amountTokenDesired,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external payable returns (uint amountToken, uint amountETH, uint liquidity);
    function removeLiquidity(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETH(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountToken, uint amountETH);
    function removeLiquidityWithPermit(
        address tokenA,
        address tokenB,
        uint liquidity,
        uint amountAMin,
        uint amountBMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountA, uint amountB);
    function removeLiquidityETHWithPermit(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountToken, uint amountETH);
    function swapExactTokensForTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapTokensForExactTokens(
        uint amountOut,
        uint amountInMax,
        address[] calldata path,
        address to,
        uint deadline
    ) external returns (uint[] memory amounts);
    function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);
    function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
        external
        returns (uint[] memory amounts);
    function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
        external
        payable
        returns (uint[] memory amounts);

    function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
    function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
    function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
    function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
    function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}

// File: @uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol

// SPDX-License-Identifier: MIT
pragma solidity >=0.6.2;

interface IUniswapV2Router02 is IUniswapV2Router01 {
    function removeLiquidityETHSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline
    ) external returns (uint amountETH);
    function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
        address token,
        uint liquidity,
        uint amountTokenMin,
        uint amountETHMin,
        address to,
        uint deadline,
        bool approveMax, uint8 v, bytes32 r, bytes32 s
    ) external returns (uint amountETH);

    function swapExactTokensForTokensSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
    function swapExactETHForTokensSupportingFeeOnTransferTokens(
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external payable;
    function swapExactTokensForETHSupportingFeeOnTransferTokens(
        uint amountIn,
        uint amountOutMin,
        address[] calldata path,
        address to,
        uint deadline
    ) external;
}

// File: ../solc/Pigfox-Imports.sol

pragma solidity ^0.8.18;



interface IERC20 {
    function totalSupply() external view returns (uint256);
    function decimals() external view returns (uint8);
    function balanceOf(address account) external view returns (uint256);
    function allowance(address owner, address spender) external view returns (uint256);

    function transfer(address recipient, uint256 amount) external returns (bool);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
}

contract ERC20Basic is IERC20 {
    string public constant name = "ERC20Basic";
    string public constant symbol = "ERC";
    uint8 public constant decimals = 18;
    mapping(address => uint256) balances;
    mapping(address => mapping (address => uint256)) allowed;
    uint256 totalSupply_ = 10 ether;

    constructor() {
        balances[msg.sender] = totalSupply_;
    }

    function totalSupply() public override view returns (uint256) {
        return totalSupply_;
    }

    function balanceOf(address tokenOwner) public override view returns (uint256) {
        return balances[tokenOwner];
    }

    function transfer(address receiver, uint256 numTokens) public override returns (bool) {
        require(numTokens <= balances[msg.sender]);
        balances[msg.sender] = balances[msg.sender]-numTokens;
        balances[receiver] = balances[receiver]+numTokens;
        emit Transfer(msg.sender, receiver, numTokens);
        return true;
    }

    function approve(address delegate, uint256 numTokens) public override returns (bool) {
        allowed[msg.sender][delegate] = numTokens;
        emit Approval(msg.sender, delegate, numTokens);
        return true;
    }

    function allowance(address owner, address delegate) public override view returns (uint) {
        return allowed[owner][delegate];
    }

    function transferFrom(address owner, address buyer, uint256 numTokens) public override returns (bool) {
        require(numTokens <= balances[owner]);
        require(numTokens <= allowed[owner][msg.sender]);

        balances[owner] = balances[owner]-numTokens;
        allowed[owner][msg.sender] = allowed[owner][msg.sender]+numTokens;
        balances[buyer] = balances[buyer]+numTokens;
        emit Transfer(owner, buyer, numTokens);
        return true;
    }
}

contract Pigfox {
    event AssetSold(address sender, uint256 amount);
    event AssetBought(address sender, uint256 amount);
    event EtherReceived(address sender, uint amount);
    event LogMessage(string message);
    event LogMessages(string message, string message2);
    address private owner;
    address private _currentRouter0;
    address private _currentRouter1;
    address private _token;

    constructor() {
        owner = msg.sender; // The wallet that deploys the contract becomes the owner
    }

    //Enforce security everywhere
    modifier onlyOwner() {
        require(msg.sender == owner, "Only contract owner can call this function");
        _;
    }

    function _log(string memory message) private {
        emit LogMessage(message);
    }

    function swap(address token, uint256 ethToBorrow,  address tokenPairedWithWeth, address routerAddress0, address routerAddress1) public payable onlyOwner {
        _currentRouter0 = routerAddress0;
        _currentRouter1 = routerAddress1;
        _token = token;
        require(token != tokenPairedWithWeth, "Can't borrow ETH from the same pair as the one you're trading");
        address weth = IUniswapV2Router02(routerAddress0).WETH();
        address pairWeth = IUniswapV2Factory(IUniswapV2Router02(routerAddress0).factory()).getPair(weth, tokenPairedWithWeth);
        require(pairWeth != address(0), "This pool does not exist on router0");
        // Make sure the pools exist on both routers
        address pairAddress0 = IUniswapV2Factory(IUniswapV2Router02(routerAddress0).factory()).getPair(token, weth);
        require(pairAddress0 != address(0), "This pool does not exist on router0");
        address pairAddress1 = IUniswapV2Factory(IUniswapV2Router02(routerAddress1).factory()).getPair(token, weth);
        require(pairAddress1 != address(0), "This pool does not exist on router1");
        address token0 = IUniswapV2Pair(pairWeth).token0();
        address token1 = IUniswapV2Pair(pairWeth).token1();
        uint256 amount0 = weth == token0 ? ethToBorrow : 0;
        uint256 amount1 = weth == token1 ? ethToBorrow : 0;
        IUniswapV2Pair(pairWeth).swap(amount0, amount1, address(this), bytes("not empty"));

        // Revert to zero
        _currentRouter0 = address(0);
        _currentRouter1 = address(0);
        _token = address(0);
    }

    function uniswapV2Call(address, uint256 amount0, uint256 amount1, bytes calldata) external {

        address[] memory path = new address[](2);
        address token0 = IUniswapV2Pair(msg.sender).token0();
        address token1 = IUniswapV2Pair(msg.sender).token1();

        IUniswapV2Router02 router0 = IUniswapV2Router02(_currentRouter0);
        IUniswapV2Router02 router1 = IUniswapV2Router02(_currentRouter1);

        require(msg.sender == IUniswapV2Factory(router0.factory()).getPair(token0, token1), "Unauthorized"); // ensure that msg.sender is a V2 pair

        require(amount0 == 0 || amount1 == 0, "Invalid amounts");

        path[0] = router1.WETH();
        path[1] = _token;

        IERC20 wethContract = IERC20(path[0]);
        IERC20 tokenContract = IERC20(path[1]);
        uint256 wethBorrowed = wethContract.balanceOf(address(this));
        wethContract.approve(_currentRouter1, wethBorrowed);

        // Buy Token with WETH on Router 1 (where it's cheaper)
        uint256 tokensReceived = router1.swapExactTokensForTokens(wethBorrowed, 0, path, address(this), block.timestamp)[1];

        // Sell Token for WETH on Router 0 (where it's more expensive)
        tokenContract.approve(_currentRouter0, tokensReceived);
        address path1 = path[0];
        path[0] = path[1]; // Reverse path
        path[1] = path1;
        uint256 wethReceived = router0.swapExactTokensForTokens(tokensReceived, 0, path, address(this), block.timestamp)[1];

        uint256 profit = wethReceived > wethBorrowed ? wethReceived - wethBorrowed : 0;
        require(wethReceived > wethBorrowed, "Not enough to reimburse loan");

        wethContract.transfer(msg.sender, wethBorrowed);         
        wethContract.transfer(tx.origin, profit);
    }

    function sendProfitToWallet() private {
        //send profit from swap back to wallet
    }

    /**
    Only the current owner can transfer ownership to a new owner
     */
    function updateOwner(address _newOwner) public onlyOwner {
        owner = _newOwner;
    }

    // Function to receive Ether
    receive() external payable {
        emit EtherReceived(msg.sender, msg.value);
    }

    function getRemainingGas() public view returns (uint256) {
        return gasleft();
    }
}

And the Go code I use to deploy it.

package main

import (
    "context"
    "crypto/ecdsa"
    "deploy-contract-tool/api"
    "errors"
    "fmt"
    "github.com/ethereum/go-ethereum/accounts/abi/bind"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "log"
    "math/big"
)

func deploy() (string, string, error) {
    // Get the private key
    privateKey, err := crypto.HexToECDSA(wallet.privateKey)
    if err != nil {
        log.Fatal(err)
    }

    // Get the public key and derive the sender address
    publicKey := privateKey.Public()
    publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
    if !ok {
        log.Fatal("Error casting public key to ECDSA")
    }

    fromAddress := crypto.PubkeyToAddress(*publicKeyECDSA)
    // Get the nonce for the sender address
    nonce, err := connection.PendingNonceAt(context.Background(), fromAddress)
    if err != nil {
        log.Fatal(err)
    }

    pendingBalance, err := connection.PendingBalanceAt(context.Background(), fromAddress)
    if err != nil {
        log.Fatal(err)
    }
    //https://dev.to/foxgem/troubleshooting-exceeds-block-gas-limit-366p
    // Set the gas price and gas limit
    suggestedGasPrice, err := connection.SuggestGasPrice(context.Background())
    if err != nil {
        log.Fatal(err)
    }

    // Create a new transaction instance
    auth, err := bind.NewKeyedTransactorWithChainID(privateKey, currentConfig.ChainID)
    if err != nil {
        log.Fatal(err)
    }
    auth.Nonce = big.NewInt(int64(nonce))
    auth.Value = big.NewInt(0)
    maxGasLimit := new(big.Int).Div(pendingBalance, suggestedGasPrice)
    auth.GasLimit = maxGasLimit.Uint64()
    fmt.Println("auth.GasLimit: ", auth.GasLimit)

    blockGasLimit := getBlockGasLimit()
    if blockGasLimit < auth.GasLimit {
        auth.GasLimit = blockGasLimit
    }

    auth.GasPrice = suggestedGasPrice
    // Total cost = (auth.GasLimit * auth.GasPrice) + auth.Value
    // Convert auth.GasLimit to a big.Int
    gasLimitBigInt := new(big.Int).SetUint64(auth.GasLimit)
    // Perform the multiplication
    totalCost := new(big.Int).Mul(gasLimitBigInt, auth.GasPrice)
    totalCost.Add(totalCost, auth.Value)
    if pendingBalance.Cmp(totalCost) < 0 {
        e := "Stopping, need " + totalCost.String() + " have" + pendingBalance.String()
        log.Fatal(e)
    }

    fmt.Println("pendingBalance: ", pendingBalance)
    fmt.Println("blockGasLimit: ", blockGasLimit)
    fmt.Println("suggestedGasPrice: ", suggestedGasPrice)
    fmt.Println("auth.GasLimit: ", auth.GasLimit)
    fmt.Println("auth.GasPrice: ", auth.GasPrice)
    fmt.Println("total cost: ", totalCost)
    fmt.Println("nonce: ", auth.Nonce)

    // Deploy the contract
    address, tx, instance, err := api.DeployPigfox(auth, connection)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("----After deploy----")
    fmt.Println("Token deployed at:", address)
    fmt.Println("Token tx:", tx.Hash())
    fmt.Println("Token cost:", tx.Cost())
    fmt.Println("Token gas:", tx.Gas())
    fmt.Println("Token gas price:", tx.GasPrice())//<-last line to printed

    receipt, err := bind.WaitMined(context.Background(), connection, tx)
    if err != nil {
        log.Fatal(err)
    }

    if receipt.Status == types.ReceiptStatusSuccessful {
        fmt.Println("Token deployed successfully")
    } else {
        err = errors.New("token deployment failed")
    }

    fmt.Println(instance)

    return address.Hex(), tx.Hash().Hex(), err
}

Any clue to what is wrong with my code?

1 Answer 1

2

Your Go code calculates the gas price for the contract deployment transaction incorrectly — it sets it too low.

Your Go code seems okay (I think?) but maybe it fetched the gas price at the state (average across blocks) when you were deploying the contract incorrectly.

The transaction that was deployed through Remix (I asssume you used MetaMask?) has the gas price: 0.000000002500000284 ETH

The transaction that was submitted using Go has the gas price: 0.000000000000001818 ETH <— very low for a validator node to pick up and include in a block.

You also want to double check what node you are using to deploy the contract (and fetch the average gas price to construct the transaction) — is it the same node when deploying through Remix + MetaMask and then with Go, or are you using a provider node (eg default MetaMask node) for the former and your own node for the latter? For example, if your own node is not synced, it may get a very low average gas price according to the network state that was way in the past, and use that value in the calcs.

Try using a fully synced node or get one from a provider like Chainstack, for example, and deploy using your Go code through a synced node.

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.