top of page
  • doctorsmonsters

How to Withdraw Earnings from an NFT Smart Contract





Recently, I worked on launching an NFT collection with my friend. Until then, I had no experience with Solidity. There are tons of templates available online for free to help you build your contract however it took me a while to figure out how withdraw from the contract. It’s actually a lot easier than you may think.

Every contract should have a withdraw function and withdrawing will depend on how it is written. Typically, it will look something like this:

function withdraw() public payable onlyOwner {
    (bool success, ) = payable(msg.sender).call{
    value: address(this).balance
    }(“”);
    require(success);
}

Let’s break it down. The “onlyOwner” would mean that it can only be called by the owner of the contract, so one should make sure it is there. “adress(this).balance” would mean the balance of the contract, so line 2 tries to call payable, to send the whole balance to msg.sender which would be the owner. If it goes through, the value of boolean success is set to true, otherwise false.

There are many ways to call functions from the contract after deployment, like writing a web3.js frontend, using etherscan or myetherwallet but easiest I found was using the Remis IDE, which is what you would likely use to deploy your contract anyway. To load a deployed contract into remix, you will need the source code and the address.

Open up a new file, paste your source code and compile it. This will generate the ABI for the contract which is basically a manual for interacting with the contract. Now click on the “deploy and run” tab on the left side. For the environment, select injected web3. Make sure you set your Metamask wallet to the account you used to launch your contract. Next pick the contract to the filename where you posted your source code. Do not click transact! Instead, paste the contract address next to “At Address” and then click at address. In a few seconds, your contract will load below the “Deployed Contracts”. You can expand it and see all the functions! You will see the withdraw function as well. You can click it, Metamask will load up, accept the transaction and that’s it!

270 views0 comments

Recent Posts

See All
Post: Blog2_Post
bottom of page