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?