If someone creates a smart contract (and is owner of that contract), is it possible for that person to collect ETH and other tokens, that are sent to the address of smart contract?
-
Possible duplicate of How to withdraw Ether from a contract – Richard Horrocks Oct 6 '17 at 16:41
-
1This is a superset from the possible duplicate as it's accessing funds like tokens, not just ETH. – eth♦ Oct 7 '17 at 6:56
This is only possible, if the creator of a smart contract makes it possible. If a contract does not implement any withdrawal() function, then the funds may be irretrievable by the owner.
This is a feature, not a bug. Would you want to ether delta smart contract creator to withdraw all the ether and tokens from the contract at a whim? No.
A smart contract does not have any owner or withdrawal mechanisms unless they have been coded in the contract. This gives smart contracts a power of being trustless: it is possible to use contracts without trusting the owner, by carefully understanding the code of the contract.
Withdrawing ERC-20 tokens
If you're a smart contract author, and want to be able to withdraw ERC-20 tokens, you need to do 2 steps.
Step 1: Add this interface (subset of ERC-20) above your contract
contract Token {
function transfer(address _to, uint _value) returns (bool success);
function balanceOf(address _owner) constant returns (uint balance);
}
Step 2: Here is a function you can add inside your contract
function withdrawTokens(address tokenContract) external onlyOwner {
Token tc = Token(tokenContract);
tc.transfer(owner, tc.balanceOf(this));
}
This assumes you've correctly written and configured yourself as the owner
. Since your contract may receive many different types of tokens, you need to withdrawTokens
from each tokenContract
. If owner
is another contract, it must also have a withdrawTokens
function to prevent tokens from getting stuck in the owner
contract.
Withdrawing ether
The code is simpler:
function withdrawEther() external onlyOwner {
owner.transfer(this.balance);
}
Again, if owner
is another contract, it must also have a withdrawEther
function to prevent ether from getting stuck in the owner
contract.
Another reference: How to withdraw Ether from a contract
-
Thanks, so if the contract is already up, and does not have such function, are funds stuck? Can it be upgraded, so that this can be added? – nikola ivkov Oct 7 '17 at 15:48
-
@nikolaivkov Yes, funds will be stuck. Similarly, a contract also cannot be upgraded unless it already has code allowing it to be upgraded in some way. – eth♦ Oct 10 '17 at 8:47