事件阅读行为不一致

时间:2019-03-24 04:04:54

标签: javascript solidity web3js

我正在侦听以太坊专用网络上的合同中的事件,一旦听到该事件便触发了回调。

加载页面时,将调用事件侦听器函数;当这种情况发生时,没有问题,一切都会根据回调适当地呈现。但是,当事件发生且页面已加载时,第二次回调将运行两次(之所以会调用此回调,是因为在使用MetaMask上传时,我只能使用“ fromBank”帐户查看此页面)。

我不知道是什么原因导致了两次通话。

事件功能

fraudListen = () => {
  KYCinstance.events.ReportedFraudA({ filter: {toBank:ethereum.selectedAddress}, fromBlock:0 }, listenCallbackTo); 
  KYCinstance.events.ReportedFraudA({ filter: {fromBank:ethereum.selectedAddress}, fromBlock:0 }, listenCallbackFrom);
  console.log('now listening for events');
}

回叫

listenCallbackFrom = async (error, event) => {
  if (error) { console.log(error); }
  else {
    let values = event.returnValues;
    let fromB = await window.KYCinstance.methods.banks(values.fromBank).call({from: ethereum.selectedAddress, gas:3000000}); 
    let toB = await window.KYCinstance.methods.banks(values.toBank).call({from: ethereum.selectedAddress, gas:3000000}); 
    if (fromB.name == toB.name) return;
    var fromBank = document.createTextNode("From Bank: " + web3.utils.toAscii(fromB.name));
    var fromAccount = document.createTextNode("From Account: " + web3.utils.toAscii(values.fromAccount));
    var toBank = document.createTextNode("To Bank: " + web3.utils.toAscii(toB.name));
    var toAccount = document.createTextNode("To Account: " + web3.utils.toAscii(values.toAccount));
    var amount = document.createTextNode("Amount: " + values.amount);
    var time = document.createTextNode("Transaction Date: " + timeConverter(values.txDate / 1000));

    const elements = [fromBank, fromAccount, toBank, toAccount, amount, time];

    const fraudEvents = document.getElementById("fraudEvents");
    const divItem = document.createElement('div');
    divItem.setAttribute('class', "fraudEvent")

    for (var i = 0; i < elements.length; i++) {
      var listItem = document.createElement('ul');
      listItem.appendChild(elements[i]);
      divItem.appendChild(listItem);
    }

    fraudEvents.insertBefore(divItem, fraudEvents.firstChild);
    var linebreak = document.createElement('br');
    fraudEvents.insertBefore(linebreak, divItem);
    console.log("callback2");
  }
}

固体

function reportFraud (address fromBank, address toBank, bytes32 fromAccount, bytes32 toAccount, uint256 amount, uint256 txDate, uint256 time) external returns(uint256 fraudID) {

    Fraud memory fraud = Fraud(fromBank, toBank, fromAccount, toAccount, amount, txDate, time);
    frauds.push(fraud);

    emit ReportedFraudA(fromBank, toBank, fromAccount, toAccount, amount, txDate, time);
    emit ReportedFraudB(fromBank, toBank, fromAccount, toAccount, amount, txDate, time);
}

0 个答案:

没有答案