0

Is there a way in Ethereum to send coins to a smart contract without actually executing a smart contract? And also, as an extension to that question, is it possible to determine from Solidity that sending coins to a particular address will result in an external contract execution (ie, will it incur more gas than if you were simply sending the coins to a wallet address?).

1

selfdestruct(contractAddress) should do the trick. As a contract:

contract ForceSend {
  function ForceSend(address recipient) payable {
    selfdestruct(recipient);
  }
}

Deploy, send along the contract address as recipient and any Ether you want to send to the contract.

If you'd prefer something a little more permanent, consider a factory-based approach. In addition to the above:

contract ForceSender {
  function forceSend(address recipient) payable {
    require(msg.value != 0);
    ForceSend f = (new ForceSend).value(msg.value)(recipient);
    assert(address(f) != address(0));
  }
}

Note that I'm not actually 100% sure the factory here will work as intended. I'm not sure what address(f) returns when f has called selfdestruct. I think it will still return a non-0, but if it doesn't, it's hard to differentiate between a failed contract creation and a successful one.

  • heh that's actually pretty interesting. But is there a way to send a particular amount (rather than all coins belonging to a contract) and without destroying the contract? – Earlz Jul 10 '17 at 20:06
  • Then you send the particular amount to ForceSend on deployment. This contract is used to send Ether as per your request. That is the purpose of ForceSend. – Xavier Leprêtre B9lab Jul 10 '17 at 21:30
  • Added a factory pattern to go along with it, to make it slightly more reusable. You can adapt the factory pattern to send values other than msg.value, if you'd like. – DeviateFish Jul 10 '17 at 23:25
0

Sending ether to a contract does not execute it.

Contract's constructor gets called only once, when the contract is mined. After that you need to be calling a particular function. Without specifying a particular function, the invocation defaults to the default function. Every smart contract has a default function that does nothing and is not payable.

Look at implementing a fallback function; From solidity docs:

What is the deal with function () { ... } inside Solidity contracts? How can a function not have a name?

This function is called “fallback function” and it is called when someone just sent Ether to the contract without providing any data or if someone messed up the types so that they tried to call a function that does not exist.

The default behaviour (if no fallback function is explicitly given) in these situations is to throw an exception.

If the contract is meant to receive Ether with simple transfers, you should implement the fallback function as:

function() payable { }

Another use of the fallback function is to e.g. register that your contract received ether by using an event.

Attention: If you implement the fallback function take care that it uses as little gas as possible, because send() will only supply a limited amount.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.