0

enter image description here

This is the err information.

enter image description here

This is the web I built.

Now the problem you see is that when I click the vote button, pay the gas, and send a transaction, but than error occurred.

I write my program based on the example of truffle called pet-shop-tutorial.

this is my Adoption.sol Adoption.sol

this is my app.js

App = {
    web3Provider: null,
    contracts: {},
    initWeb3: function() {
        // Is there an injected web3 instance?
        if (typeof web3 !== 'undefined') {
            App.web3Provider = web3.currentProvider;
        } else {
            // If no injected web3 instance is detected, fall back to Ganache
            App.web3Provider = new Web3.providers.HttpProvider('http://127.0.0.1:7545');
        }
        web3 = new Web3(App.web3Provider);
        return App.initContract();
    },

    initContract: function() {
        $.getJSON('Adoption.json', function(data) {
            var AdoptionArtifact = data;
            App.contracts.Adoption = TruffleContract(AdoptionArtifact);
            App.contracts.Adoption.setProvider(App.web3Provider);
            return App.markAdopted();
        });
        return App.bindEvents();
    },

    bindEvents: function() {
        $(document).on('click', '.btn-adopt', App.handleAdopt);
    },
    markAdopted: function() {
        let candidates = {
            "Rama": "candidate-1",
            "Nick": "candidate-2",
            "Jose": "candidate-3"
        };
        var adoptionInstance;
        var candidateNames = Object.keys(candidates);
        for (var i = 0; i < candidateNames.length; i++) {
            var name = candidateNames[i];
            App.contracts.Adoption.deployed().then(function(instance) {
                adoptionInstance = instance;
                return adoptionInstance.totalVotesFor.call(name);
            }).then(function(v) {
                var number = v;
                var na = candidates[name];
                document.getElementById(na).innerHTML = number;
            }).catch(function(err) {
                console.log(err.message);
            });
        }
    },


    handleAdopt: function(event) {
        event.preventDefault();
        var adoptionInstance;
        let candidates = {
            "Rama": "candidate-1",
            "Nick": "candidate-2",
            "Jose": "candidate-3"
        }
        var candidateName = $("#candidate").val();
        var candidateNames = Object.keys(candidates);
        var content = document.getElementById("candidate").value;
        $("#msg").html("Vote has been submitted. The vote count will increment as soon as the vote is recorded on the blockchain. Please wait.");
        $("#candidate").val("");
        web3.eth.getAccounts(function(error, accounts) {
            if (error) {
                console.log(error);
            }
            var account = accounts[0];
            App.contracts.Adoption.deployed().then(function(instance) {
                adoptionInstance = instance;
                return adoptionInstance.voteForCandidate(candidateName, {
                    from: account
                });
            }).than(function(result) {
                return App.markAdopted()
            }).catch(function(err) {
                console.log(err.message)
            });
        });
    }

};

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

The contract code is:

pragma solidity ^0.4.2;

contract Adoption {

  mapping (bytes32 => uint8) public votesReceived;
  bytes32[] public candidateList;

  function Voting(bytes32[] candidateNames) public {
    candidateList = candidateNames;
  }

  function totalVotesFor(bytes32 candidate) view public returns (uint8) {
    require(validCandidate(candidate));
    return votesReceived[candidate];
  }

  function voteForCandidate(bytes32 candidate) public {
    require(validCandidate(candidate));
    votesReceived[candidate]  += 1;
  }

  function validCandidate(bytes32 candidate) view public returns (bool) {
    for(uint i = 0; i < candidateList.length; i++) {
      if (candidateList[i] == candidate) {
        return true;
      }
    }
    return false;
   }
}
  • post you contract code in a way that can be copied and not as image. – mirg Mar 28 '18 at 4:16
  • i put the plain code on comment – user34940 Mar 28 '18 at 6:17
0

I can't read your code due to the poor formatting, however the tooltip visible in the screenshots seems to indicate two things :

  • First, the parameter or value you send into your transaction seems to be 'undefined'. Make sure you call the right spot of data
  • The data has to be converted to Hexadecimal, which doesn't seem to be the case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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