0

I have an event which is sent each time when a contract is deployed. It is launched in the constructor.

I have a Dapp where I want to watch this event to send a tx each time that a smart contract is deployed (it is the same one, but "updated").

Each time that I run my Dapp, the script is watching the event once, but if my Dapp is already running and if I deploy a contract I do not get the new event until re-run the Dapp.

The structure of my script is the following:

    App = {
      web3Provider: null,
      contracts: {},

      init: function() {
        return App.initWeb3();
      },

      initWeb3: function() {
        //some code
        return App.initContract();
      },
      initContract: function(){
        $.getJSON("MyContract.json", function(mycontract) {
          App.contracts.MyContract= TruffleContract(mycontract);
          //some code
          listeningEvents();
          return sendTx();
        })
      }
      sendTX: function() { /*some code*/ }
      listeningEvents: function(){
        App.contracts.MyContract.deployed().then(function (instance) {
          instance.Event({}, {
              fromBlock: 0,
              toBlock: 'latest'
          }).watch(function (error, event) {
                console.log("WATCHED EVENT")
          })
        })
      }

$(function() {
  $(window).load(function() {
    App.init();
  });
});

I am also trying to watch the address of the contract with the same purpose, but I am trying something like:

instance.watch('address', function (new, old) { ... })

and it does not work( err: instance.watch is not a function).

It seems that I have to run the script again because it is watching the previous smart contract, no the new one. Can someone explain to me in that case how to identify that the new contract is deployed? How can I watch the event? what is the right way to watch address?

1 Answer 1

0

deployed() will look for the address in your .json file which matches the network you are using. If you want to manually pass an address you can use .at() instead and pass an address.

1
  • So, watch this address in the .json to check if there is a new contract should be good, isn't it? what do you mean with "manually pass an address"? I cannot pass manually the address, it should be watched automatically when the smart contract is deployed. @TovarishFin Commented Oct 28, 2018 at 17:58

Your Answer

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

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