1

I would like to automate the response of a Smart Contract from a script. Is there any way to identify when the contract was deployed again in the network to send a response?

UPDATED: If the solution is using events, how is possible to identify from the script of the launched dapp when a contract sends the event if you dont call any function?

Scenario: Dapp running. Contract is deployed again and sends a event to the script. Script should get the event without call any function.

In other words, how is it possible to keep the script "listening" until receiving an event.

5
  • 3
    read about Events
    – Nulik
    Sep 15, 2018 at 17:19
  • I know that the events can be called when you execute a function in the deployed contract to obtain information, but in this case, I dont know how the event can be used to identify when the contract is updated. Sep 15, 2018 at 17:59
  • 1
    what do you mean by updated? contracts are immutable
    – Nulik
    Sep 15, 2018 at 18:22
  • 1
    events are not "called" they are "emitted"
    – Nulik
    Sep 15, 2018 at 18:23
  • Yes, I explained it wrong, sorry. I wanted to say when a contract is deployed again (no updated), and to obtain the value of the emited event i thought you need to call a function in the contract. I will modify that explanation in the question. Sep 16, 2018 at 11:46

3 Answers 3

1

There are a few ways to do this:

Events

As @Nulik said in the comments above, you can set up a worker to read all the events that come out of a contract and perform your action on these events. This does not work if there is no event emitted from the contract to check what you are trying to do.

Calling the Contract

You can set up a worker (similar to the one mentioned above) that constantly calls the relevant public variables from the contract to read if they have been updated. It will function similarly to an event, in that you can look for updates to the contract and perform actions based on those updates. This does not work if what you are looking for does not change the state or update a public variable in the contract.

Watching the Address

A final solution may be to simply set up a watcher to watch the address of the contract itself and inspect it for incoming transactions. If there is a specific function you are looking for, you know that each transaction going in with the data to call that function is going to be what you are looking for (when a function is called, the first 4 bytes of data are the method ID). From here, you can analyze the data for whatever it is you are searching for.

4

Contract code can't be updated.

The contract state can be updated by sending transactions to contract functions. Contracts can and should emit events for every important state change so interested clients can learn what they need know by listening to event logs.

On the client side, software clients "watch" event logs with callbacks. Once these listeners are set up, callbacks fire whenever matching events are observed in received blocks.

This explainer gave me a good impression. https://coursetro.com/posts/code/100/Solidity-Events-Tutorial---Using-Web3.js-to-Listen-for-Smart-Contract-Events

Tip: When looking at examples/docs, be watchful for confusion between 0.x Web3 API and 1.0 beta. The 1.0 beta changes the original methods a little.

Hope it helps.

1

Once you deploy a contract you can not update it. Contracts are immutable. Every contract has the unique account address. If you at all want to update some function of already deployed contract you need to redeploy the contract again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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