Help With Smart Contract Withdraw

Hi guys, I am making this post since I am currently creating an ERC 1155 Smart Contract using OpenZeppelin and have run into a problem. When implementing the ERC2981 contract and putting the royalties to be received in the Smart Contract, in currencies other than ETH (for example MATIC and USDC), I am unable to withdraw them from the contract. So since I am a beginner I would like to ask for some help. How can I make the contract allow to withdraw other ERC20 Tokens than Ethereum? Is it possible to do this on an ERC 1155 contract? How should I do it?

Thank you so much! :slightly_smiling_face:

Hi @thiago_cerq
you can use something like this:

function withdrawERC20(address tokenAddr, uint256 amount) external onlyOwner{
  IERC20(tokenAddr).transfer(owner(), amount);
1 Like

Thank you for your help, but I still had some problems, because when I use this function, it does not accept decimals and this is the quantity that the contract has:
Captura de Tela 2022-10-22 às 16.10.55
So after the contract gets the token I try to take out this exact amount and I got this response:

After that, I tried to take out a larger number, because I thought the function would try to give me the quantity I asked for, and after seeing that the Contract has only "0.01", it would give me everything, and the transaction is accepted:

However, it did not send me the "0.01 WETH", it sent me "0.00000...1 WETH":

This is expected behaviour. Most blockchains don’t really support decimals because they are very inefficient to work with. So whenever you transfer 1 ether, you are actually transferring 1000000000000000000 wei because we work with round numbers. The token simply states how many decimals it uses, which for most tokens is 18 (including ethereum). What that means is that 1e18 is actually 1 eth or whatever the token symbol is.

You can read more about that here:

1 Like

So there is no way I can withdraw this amount? Or can I make decimals “understandable” for my Smart Contract? Because I want to test the Smart Contract Function on chain… :melting_face:

Yes can use EtherScan's wei converter:

If you want to withdraw for example 1.5 eth then it would be 1500000000000000000 wei for example.

Thank you for this link CryptoWorld, but this works specifically for Wei , right? And if the case was to withdraw another token like USDC or MATIC? because since I am planing to deploy it on polygon, I would like to be sure I could be able to withdraw this currencies…

This is for any cryptocurrency or token that uses 18 decimals, which is pretty much default for most cryptocurrencies/tokens. So this also works for and matic and 95% of erc20 tokens


Edit: apperently not usdt :slight_smile: usdt uses 6 decimals


to convert your amount in from readable format to the value's used in smart contracts you do:
youramount * (10 ^ decimals ) in javascript and solidity this would be expressed as value = youramount * (10 ** decimals)