0

This is my Election smart contract.

pragma solidity ^0.4.17;
contract Election{
    struct candidate{
        string name;
        uint votecount;
    }
    struct voter{
        bool authorized;
        bool voted;
        uint vote;
    }
    address public owner;
    string public electionName;
    mapping(address=>voter) voters;
    candidate[] public candidates;
    uint public totalVotes;
    function Election(string _name) public{
        owner = msg.sender;
        electionName = _name;
    }
    
    modifier ownerOnly(){
        require(msg.sender==owner);
        _;
    }
    function addCandidate(string _name) ownerOnly public{
        candidates.push(candidate(_name,0));
    }
    function getNumcandidate() public view returns(uint){
        return candidates.length;
    }
    function authorize(address _person) ownerOnly public{
        voters[_person].authorized=true;
    }
    function vote(uint _voteIndex) public{
        require(!voters[msg.sender].voted);
        require(voters[msg.sender].authorized);
        
        voters[msg.sender].vote =_voteIndex;
        voters[msg.sender].voted = true;
        candidates[_voteIndex].votecount +=1;
        totalVotes+=1;
    }
    function whoWon() ownerOnly public view returns(string){
        uint maxi=0;
        string memory winner;
        for(uint i=0;i<getNumcandidate();i++)
        {
            if(candidates[i].votecount>maxi)
            {
                maxi=candidates[i].votecount;
                winner=candidates[i].name;
            }
        }
        return winner;
    }
    function end() ownerOnly public returns(uint){
        selfdestruct(owner);
    }
}

This is the react code where i'm trying to pass the string value in election function after submitting the form.

import React, {Component} from 'react';
import web3 from './web3';
import Election from './election';

class Conduct extends Component{
    constructor(props){
        super(props);
        this.state={
            owner:'',
            elename:''
        }
    }
    async componentDidMount(){
        window.ethereum.enable();
        const owner = await Election.methods.owner().call();
        const elename = await Election.methods.electionName().call();
        this.setState({owner,elename});
    }
    election = async event=>{
        event.preventDefault();
        const accounts = await web3.eth.getAccounts();
        <h1> wait for your transaction</h1>
        await Election.methods.Election(this.state.elename).send({
            from:accounts[0]
        });
        <h1>Yout transaction is successful</h1>
    }
    render(){
        return(
            <div>
                <form onSubmit={this.election}>
                    <h3>Enter the election name</h3>
                    <input type="text"
                    value={this.state.elename}
                    onChange={event=>this.setState({elename:event.target.value})}
                    />
                    <button>Held election</button>
                </form>
                <h1>The current name of election is {this.state.elename}</h1>
            </div>
        );
    }
}

export default Conduct;

Error:-Unhandled Rejection (TypeError): election__WEBPACK_IMPORTED_MODULE_3_.default.methods.Election is not a function

6
  • Change Election.methods.Election to Election.deploy or something similar. – goodvibration Dec 4 '20 at 10:32
  • yes, I change this 'Election.methods.Election(this.state.elename)' to Election.deploy(this.state.elename) and now it is giving me this error - Unhandled Rejection (TypeError): Cannot create property 'arguments' on string 'Indian election' – piyush shende Dec 4 '20 at 10:44
  • Change Election.deploy('Indian election') – goodvibration Dec 4 '20 at 10:52
  • To Election.deploy({data: "0xYourContractBinaryData", arguments: ['Indian election']}) – goodvibration Dec 4 '20 at 10:52
  • Wow that's great It works. Thank you so much. – piyush shende Dec 4 '20 at 11:05
0

Change this:

await Election.methods.Election(this.state.elename).send(...);

To this:

const options = {
    data: "0xYourContractBinaryData",
    arguments: [this.state.elename]
};
const transaction = Election.deploy(options);
const receipt = await transaction.send(...);
console.log("contract address:", receipt.contractAddress);
console.log("encoded arguments:", transaction.encodeABI().slice(options.data.length));

You can also use this printout in order to verify your contract on Etherscan.

2
  • Thank you so much – piyush shende Dec 4 '20 at 15:11
  • @piyushshende: You're welcome. Feel free to accept the answer by clicking on the V next to it. – goodvibration Dec 4 '20 at 15:17

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.