How can I make an asynchronous interaction between two contracts? I have contract A and contract B.

  • contract A calls contract B
  • contract B obtains data from an external source
  • contract B returns the data to A.

is it possible to realize the interaction between A and B through callback?


The 2nd bullet is not possible strictly from on-chain.

The asynchronous part of your system can only be an off-chain service which sends the data.

The rest of the process, as depicted in the 1st and 3rd bullets, will be synchronous of course.

  • can you give me an example of how B returns data to A? – Mario Roma Dec 24 '20 at 17:02
  • @MarioRoma: When "contract A calls contract B" (your 1st bullet), immediately "contract B returns the data to A" (your 3rd bullet). By immediately, I mean within the same transaction. – goodvibration Dec 24 '20 at 17:15
  • the only way to make the interaction asynchronous is to use events? When contract B has obtained the data it emits an event and A makes a transaction to B to retrieve the data.Is it right? – Mario Roma Dec 24 '20 at 17:23
  • @MarioRoma: No, that's wrong. Events are used only for recording data on the blockchain. Contracts can only emit events, they cannot read them. Events can be used only in an off-chain script/app/service. Please read the answer again. You cannot achieve an asynchronous process strictly in the on-chain. The asynchronous part of your system must be implemented in the off-chain. – goodvibration Dec 24 '20 at 17:26
  • I meant that a node receives the event it subscribed to and calls contract A. contract A calls contract B and retrieves the data. – Mario Roma Dec 24 '20 at 17:29

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.