0

I have 2 contracts: my contract and an oracle contract. I make a request to my smart contract and send some data that needs to be processed. I use mySmartContract.methods.availability(value1,value2).send({"from":myAddress}).

After my contract gets the data, it has to make an asynchronous request to the oracle smart contract which takes information from an external data source. I read the answer to this question https://stackoverflow.com/questions/55302995/how-to-return-value-to-caller-function-in-asynchronous-javascript-and-web3js, but in my case who calls the 'caller' function on my smart contrac that calls 'responder' on the oracle smart contract?

5
  • it has to make an asynchronous request to the oracle smart contract which takes information from an external data source - wrong description. – goodvibration Dec 22 '20 at 21:27
  • it has to make a (obviously synchronous) request to the oracle smart contract, which is updated asynchronously by an offchain service which takes information from an external data source - right description. – goodvibration Dec 22 '20 at 21:28
  • who calls the 'caller' function on my smart contrac that calls 'responder' on the oracle smart contract? - an offchain service (a bot if you will) that you need to set up, since you're the one who has the credentials (i.e., the private key of myAddress). – goodvibration Dec 22 '20 at 21:30
  • @goodvibration I am approaching blockchain and smart contracts for the first time. I am using web3js. can you give me an example with code? – Mario Roma Dec 22 '20 at 21:33
  • See below...... – goodvibration Dec 22 '20 at 21:39
1

Who calls the function on my contract that calls the function on the oracle contract?

An offchain service that you need to set up, since you're the one with the private key of myAddress.

For example, with web3.js v1.2, you could do something like this:

const transaction = mySmartContract.methods.availability(value1, value2);
const options = {
    to      : transaction._parent._address,
    data    : transaction.encodeABI(),
    gas     : await transaction.estimateGas({from: myAddress}),
    gasPrice: yourDesiredGasPrice
};
const signed = await web3.eth.accounts.signTransaction(tx, myAddressPrivateKey);
const receipt = await web3.eth.sendSignedTransaction(signed.rawTransaction);
2
  • but after this, my smart contract needs to call a function on the oracle smart contract. how does it happen? – Mario Roma Dec 22 '20 at 21:49
  • @MarioRoma: You need to call the oracle contract function from your contract function (i.e., it's a piece of code that you add in your contract). – goodvibration Dec 22 '20 at 22:05

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.