'How to extract ethereum from the smart contract

I am a newbie in the blockchain technology and I have question. I just deployed an Ethereum smart contract to the Rinkeby testnet network and minted a few NFTs. When checking my contract address on rinkeby.etherscan.io I can see the smart contract's balance.

The question is, how can I transfer these ethereum in the smart contract balance to my wallet. Since I am the owner I should receive these ETH somehow to my metamask wallet no?

Smart contract includes the following function

    // This will transfer the remaining contract balance to the owner.
    // Do not remove this otherwise you will not be able to withdraw the funds.
    (bool os, ) = payable(owner()).call{value: address(this).balance}('');
    require(os);
  }```

so it should be possible...


Solution 1:[1]

To be able to withdraw the funds from your contract to a wallet you own you must implement a withdraw method like so:

 address public primary_wallet = YOUR_WALLET_ADDRESS
 
 function withdraw() public payable onlyOwner {
   (bool os,)= payable(primary_wallet).call{value:address(this).balance}("");
   require(os);
 }

You will also need to make sure that you import import "@openzeppelin/contracts/access/Ownable.sol"; to use the onlyOwner modifier. This allows only the person who deployed the contract to withdraw the funds and nobody else. This is a must. Hope this helps.

Solution 2:[2]

Per your current implementation, you need to manually invoke the withdraw() function from the owner address each time you want to withdraw ETH from the contract.

Your mint functions accept ETH and keep it on the contract address.

If you want to transfer the funds to the owner each time a mint is performed, you need to explicitly invoke the transfer from each of the mint functions. Example:

function mint(uint256 _mintAmount) public payable mintCompliance(_mintAmount) mintPriceCompliance(_mintAmount) {
    require(!paused, 'The contract is paused!');

    _safeMint(_msgSender(), _mintAmount);

   // auto transfer received funds to the owner
   payable(owner()).transfer(msg.value);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael
Solution 2 Petr Hejda