Ausgegebene Solidity-Ereignisse, die in der Frontend-Trüffelvertragsinstanz nicht überwacht werden

Also, ich habe VIELE Seiten über Ereignisse in Solidity gelesen und wie man mit ihnen im Frontend interagiert, aber ich bin noch ziemlich neu darin und es scheint, als hätte es mehrere Änderungen gegeben, da viele Websites, die ich gesehen habe, veraltete Methoden verwendet haben Achten Sie auf Änderungen bei Solidity-Ereignissen. Irgendwann funktionierte es, aber dann funktionierte es plötzlich ohne ersichtlichen Grund nicht mehr.

Ich habe das mit allen meinen 4 Solidity-Events versucht, der Einfachheit halber zeige ich mein Beispiel nur mit einer Methode

pragma solidity ^0.5.0;

contract Platoonfactory {
    event PlatoonCreated(address owner, uint256 platoonId);

    uint256 public currentPlatoonID = 1;
    uint256 public contractBalance = address(this).balance;

    /**
     * A platoon has atleast one truck driving in front and 0..n trucks following
     */
    struct Platoon {
        uint256 platoonId;
        address payable owner;
        uint costPerMinute;
        uint startDate;
        uint endDate;
        // participants contains all joined trucks inside this platoon
        mapping(uint /* index */ => TruckInPlatoon) participants;
        uint participantsSize;
    }

    /**
     * A TruckInPlatoon struct describes exactly one truck inside a platoon *besides* the leaders
     */
    struct TruckInPlatoon {
        uint platoonId;
        address truckOwner;
        uint startDate;
        uint allowedParticipationUntilDate;
        uint payedAmountOfWei;
    }

    // map the owner of a platoon to the unique ID
    mapping(address => uint) public platoonOwners;
    // map the unique ID to each platoon
    mapping(uint => Platoon) public platoons;


    /**
     * Create a new platoon with required cost to join and the amount of hours in which this platoon is available
     */
    function createPlatoon(uint cpm, uint endDate) public returns (uint) {
        require(platoonOwners[msg.sender] == 0, "You have already started a platoon");
        require(endDate >= 1, "Platoon must at least be available for one hour");

        // create a new platoon and push it to the storage
        platoons[currentPlatoonID] = Platoon({platoonId: currentPlatoonID, owner: msg.sender, costPerMinute: cpm, startDate: now, endDate: (now + endDate*3600), participantsSize:0});
        platoonOwners[msg.sender] = currentPlatoonID;

        //Increment current ID
        currentPlatoonID++;

        // fire event
        emit PlatoonCreated(msg.sender, currentPlatoonID);
        return currentPlatoonID - 1;
    }

}

JavaScript:

App = {
    contracts: {},
    address: "",
    web3js: null,
    PlatoonContract: null,

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

    initWeb3: function () {
        if (typeof web3 !== 'undefined') {
            App.web3js = new Web3(web3.currentProvider);
        } else {
            App.web3js = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
        }
        return App.initHTML();
    },

    initHTML: function () {
        App.web3js.eth.getAccounts((err, accounts) => {
            if (!err) {
                document.getElementById("account-id").innerHTML = accounts[0];
                App.address = accounts[0];
            } else {
                console.log(err);
            }
        });
        return App.initContract();
    },

    initContract: function () {
        $.getJSON('../build/contracts/Platoonfactory.json', function (data) {
            var PlatoonArtifact = data;
            App.contracts.Platoonfactory = TruffleContract(PlatoonArtifact);
            App.contracts.Platoonfactory.setProvider(web3.currentProvider);

            App.contracts.Platoonfactory.deployed().then(function (instance) {
                App.PlatoonContract = instance;
                return App.listenFor();
            });
        });
    },

    listenFor: function () {
        App.PlatoonContract.PlatoonCreated({}, {fromBlock:0, toBlock: 'latest'}).watch(function(err, result) {
            if (err) {
                console.log(err);
            }

            if (result) {
                console.log(result);
                document.getElementById("platoonList").innerText = "ID: " + res.args.platoonId.c[0] + " Owner: " + res.args.owner;
            }
        });
        return App.bindEvents();
    },

    bindEvents: function () {
        $("#btnCreatePlatoon").click(event => {
            event.preventDefault();
            let cpm = document.getElementById("costPerMinute").value;
            let duration = document.getElementById("hoursAvailable").value;
            App.PlatoonContract.createPlatoon(cpm, duration);
        });

        $("#btnContractBalance").click(event => {
            event.preventDefault();
            App.PlatoonContract.contractBalance().then(result => console.log(result.c[0]));
        });
    },

};

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

Der HTML-Code sollte in diesem Fall keine Rolle spielen. Ich versuche seit fast einer Woche, dies zu beheben, und fing schon an, verrückt danach zu werden, da es so viele veraltete Beiträge gibt. Ich wäre sehr dankbar, wenn man endlich herausfinden könnte, was falsch läuft. Ich habe den Solidity-Code in Remix getestet und alle Methoden haben gut funktioniert und Ganache hat sogar die Ereignisse im Contract-Tab erkannt, der Fehler scheint an der Stelle zu liegen, an der ich den Listener anhänge.

ERGÄNZUNG: Ich verwende Truffle, Ganache und Metamask, um meine DApp zu testen

Welche Version von web3 verwendest du? Ist Metamask mit Ganache verbunden? Wurde die Transaktion erfolgreich ausgeführt? Ich würde anfangen, Dinge zu kommentieren oder zu deaktivieren, bis die Dinge wieder funktionieren.
@Ismael danke für die Antwort. Ich verwende web3 @ 2.0.0-alpha.1, die aktuelle Vorabversion. Metamask ist mit Ganache verbunden und alle Transaktionen sind erfolgreich und können in Ganache angezeigt werden. Ich habe bereits alles bis auf den Kern der Ausführung dieser Funktion und des Abhörens des Ereignisses deaktiviert. An dieser Stelle habe ich keine Ahnung, was ich zusätzlich ausschließen soll. Könnten Sie versuchen, meinen Code in Ihrem Build auszuführen, um zu überprüfen, ob meine Konfigurationen fehlerfrei sind?
Dein Code scheint korrekt zu sein. Sie verwenden eine Alpha-Version von web3, also würde ich anfangen, eine stabilere Version auszuprobieren.
@Ismael Ich habe den Code korrigiert, indem ich den toBlock-Parameter entfernt habe. Ich bin mir nicht ganz sicher, warum Metamask das nicht ganz mochte, aber zumindest funktioniert es jetzt

Antworten (1)

Also habe ich es endlich behoben! Es scheint, als ob der zweite Parameter im Ereignishandler, der fromBlock und toBlock angibt, nicht mit Metamask kompatibel ist. Es funktionierte, nachdem ich den toBlock-Parameter entfernt hatte. Arbeitsausschnitt:

App.PlatoonContract.PlatoonCreated({}, {fromBlock:0}).watch(function(err, result) {
        if (err) {
            console.log(err);
        }

        if (result) {
            console.log(result);
            document.getElementById("platoonList").innerText = "ID: " + res.args.platoonId.c[0] + " Owner: " + res.args.owner;
        }
    });