0

I want to transfer tokens from my smart contract to my wallet, but it isn't working. My smart contract (Which I deploy to Mumbai testnet with hardhat):

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "hardhat/console.sol";

contract Test {
    using SafeMath for uint256;
    address payable public owner;
    uint256 public minBalanceMatic = 10000000000000000;
    address public matic = 0x0000000000000000000000000000000000001010;

    constructor() {
        owner = payable(msg.sender);
    }
    modifier onlyOwner() {
        require(msg.sender == owner, "Only Owner can call this function!");
        _;
    }
    receive() external payable {}
    function withdrawOnlyOwner(address token) external onlyOwner {
        IERC20 token_transfer = IERC20(token);
        require(balance(token) > minBalanceMatic.mul(2), "Balance too low"); //If not, it is not worth ist
        uint256 transfer_amount = balance(token).sub(minBalanceMatic);
        if (matic == token) {
            owner.transfer(transfer_amount);
        } else {
            token_transfer.transfer(owner, transfer_amount);
        }
    }

}

Now I deploy this contract and it works fine (Here is the link to mumbai polygonscan) I funded the contract with a bit of matic from my own wallet. Now I want to withdraw the matic for testing:

require("dotenv").config();
const { ethers } = require("hardhat");
const mumbai_contract_json = require("../../artifacts/contracts/test.sol/Test.json");
const mumbai_contract_abi = mumbai_contract_json.abi;
const matic = process.env.ADDR_MATIC;
async function main() {
  const infura = new ethers.providers.InfuraProvider(
    "maticmum", 
    process.env.INFURA_MUMBAI_API_KEY
  );

  const wallet = new ethers.Wallet(process.env.PRIVATE_KEY, infura);

  const Contract = new ethers.Contract(
    process.env.CONTRACT_ADDRESS_MUM,
    mumbai_contract_abi,
    wallet
  );
  console.log(
    "Balance MATIC before: ",
    ethers.utils.formatUnits(await Contract.balance(matic), 18)
  );
  console.log("Contract Owner: ", await Contract.owner());
  console.log(
    "Min MATIC Balance: ",
    ethers.utils.formatUnits(await Contract.minBalanceMatic(), 18)
  );

  const gasPrice = parseFloat(await ethers.provider.getGasPrice()) * 2;
  const gasLimit = 200000;
  const tx = await Contract.withdrawOnlyOwner(matic, {
    gasLimit: ethers.BigNumber.from(gasLimit.toString()),
    gasPrice: ethers.BigNumber.from(gasPrice.toString()),
  });
  console.log(tx);

  console.log(
    "Balance MATIC after: ",
    ethers.utils.formatUnits(await Contract.balance(matic), 18)
  );
}
main().catch((error) => {
  console.error(error);
  process.exit(1);
});

The Output is:

Balance MATIC before:  0.03
Contract Owner:  0x5C42F93937389e73A7317e2108cbcFdECf658002
Min MATIC Balance:  0.01
{
  type: 2,
  chainId: 80001,
  nonce: 28,
  maxPriorityFeePerGas: BigNumber { value: "3300000018" },
  maxFeePerGas: BigNumber { value: "3300000018" },
  gasPrice: null,
  gasLimit: BigNumber { value: "200000" },
  to: '0xA2f3b5Ee51ba564E03bC73038133ABba968EFe0e',
  value: BigNumber { value: "0" },
  data: '0x5ba79add0000000000000000000000000000000000000000000000000000000000001010',
  accessList: [],
  hash: '0x9e0aebdcd1621136c4812821f390b9ca687f6857bc26978490ea9c82df9b7a14',
  v: 0,
  r: '0x38eef277a18ba98ad1c74e88b2a4ccc9aee82085dd754931b4f13bd69fdccb5c',
  s: '0x01bf9e8ddddcdc0be17495c9a5635da55a34d8c2b75ed53e4e0a79947dc11e6d',
  from: '0x5C42F93937389e73A7317e2108cbcFdECf658002',
  confirmations: 0,
  wait: [Function (anonymous)]
}
Balance MATIC after:  0.03

Can someone tell me, why matic is not paid out to my wallet (owner) again?

Thank you!

0

Your Answer

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