DEV Community

Grace Omole
Grace Omole

Posted on

How to Transfer Tokens To The Owner Address and Not stuck In Contract Address In Solidity.

I recently faced a bug when I was building a web dapp with react.js and smart contract(solidity). The project was an NFT minting Dapp where customers can mint NFTs and own them.

I was sure everything was working very fine until I tested and noticed that the money customers were using to mint is not going to the owner's wallet but going to the contract address.

That is so strange! What is the point of selling NFTs and you aren't getting the money in your or where you can access it.

This is a very strange bug that we might not take note of until we see the effect. I learnt that CrytoPunk also faced the same issue which was why they launched version 2 of their NFTs.

So how do you solve this problem?

Money being stuck in the contract address can be removed if there is a withdrawal function that allows the owner to withdraw from the contract. See the code below.

//only owner
function withdraw() public payable onlyOwner {
    payable(msg.sender).transfer(address(this).balance);
  }
Enter fullscreen mode Exit fullscreen mode

This function allows the owner to withdraw from the contract address by going to the scan address of the blockchain used like polygonscan for Polygon and etherscan for Ethereum.

Noticed the only owner in the code, it means it is only the owner that can withdraw. If the withdraw function is not restricted with only the owner, anybody will be able to withdraw.

Also note that, to withdraw from the contract, you must have more than the amount you want to withdraw in your wallet.

But how do we make it so that when customers mint NFTs, it goes straight to the owner's address while the transactions still show on the contract?

Inside your mint function in your contract, add the following line of code;

payable(owner()).transfer(msg.value);

owner() is a function in Ownable on the smart contract that stores the address that is used to deploy the contract as the owner's address.

The full mint function looks like this;

function mint(address _to, uint256 _mintAmount) public payable {
    uint256 supply = totalSupply();
    require(!paused);
    require(_mintAmount > 0);
    require(_mintAmount <= maxMintAmount);
    require(supply + _mintAmount <= maxSupply);

    if (msg.sender != owner()) {
        if(whitelisted[msg.sender] != true) {
          require(msg.value >= cost * _mintAmount);
        }
    }

    for (uint256 i = 1; i <= _mintAmount; i++) {
      _safeMint(_to, supply + i);
    }
    payable(owner()).transfer(msg.value);
  }
Enter fullscreen mode Exit fullscreen mode

So when customers mint their NFTs to acquire them and they are being charged the amount to mint from Metamask, the last line of code in the mint function transfers the funds used to mint to the owner's address i.e the owner of the contract.

These two functions have solved the issue of tokens getting stuck in the contract address and not being able to access or withdraw it.

I hope you find this article helpful. Follow me on twitter for more tips on Frontend and Blockchain

Top comments (1)

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Probably better if you share this on MetaPunk, part of the Forem network of communities dedicated to this topic - there's no coding content to this post, so it is less suitable for DEV.