solidity – Writing a Smart Contract That Executes Immediately After an Event

smart-contract-walletssolidity

Say for example i want to execute a smart contract that upon receiving $1000 from person A, immediately executes after it is received which then takes that $1000 and disperses to 5 addresses.

Is this possible? Any links I can read if it is? Im having a hard time finding out if it is. Thanks!

Best Answer

In general this is not possible as you meant it, every time that some EVM instructions are executed there must be someone who is paying for the GAS. So it's not possible to write functions that are automatically triggered and executed when arbitrary situations occur.

However, for your particular situation:

  • if you mean receiving Ether it would be possible simply writing a payable function
  • if the token that your contract is receiving implements a callback mechanism (as for the ERC1363) this would be possible implementing the ERC1363Receiver interface in your contract.
  • if you are deploying the token contract too you can virtually do whatever you want

I want to emphasize how in each of these three hypotheses someone has to pay for the GAS that for the first two cases it must be the one that transfers the token (not necessarily the owner).

Related Topic