I'd like to have a contract A which upon being sent ether, it transfers the ether to contract B, which upon receiving an eth transfer executes some logic.

Is there a way to do this without contract A having to know anything about contract B? Meaning, the user would just send eth to contract A's address which would then initiate the logic above.

So far, I've tried the following: There's contract A which upon sending ether to it gets its fallback function executed.

Said fallback function calls contractB.transfer(msg.value) to forward that ether to contract B.

Contract B has a fallback function which calls another function where its logic lies (say store the eth received in an internal state variable).

The problem here is that this approach fails due to the 2300 gas stipend.

The simplest approach would be to have contract A call some contract B's function instead of transfer(), but I'd like contract A not having to know anything about contract B other than the address.

Is there any other way to solve this?

I am aware of limitations in fallback functions. What I'm asking is if there's another way to do this that doesn't require Contract A knowing anything of Contract B other than its address.

  • 1
    @LuizSoares fallback functions are widely used and not insecure if used properly. Please stop sharing the same answer from 1 year ago over and over on every question... – pabloruiz55 Nov 23 '17 at 18:52
  • 2
    @pabloruiz55 If A has enought gas you can use call instead of transfer to explicitely set a larger gas: require(addressB.call.gas(100000).value(msg.value)()). – Ismael Nov 23 '17 at 19:52

Your Answer

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

Browse other questions tagged or ask your own question.