Search code examples
fluttersoliditysmartcontractsremixdecentralized-applications

How to manage and communicate with two smart contracts


I am working on an election project. initially, I created a smart contract (Election) where an admin will start the election and voters can vote. the problem is there will be multiple elections at the same time and voters must be able to vote for all of the elections. how can I do that by creating another contract?

here is the election contract: Election.sol

//SPDX-License-Identifier: UNLICENSED
pragma solidity >= 0.5.0<0.9.0;

***contract ElectionList{

    mapping (address => Election)public electionsmap;
}***

//contract for election
contract Election{

    //candidatte  name,numvotes
    struct Candidate{
        string name; 
        uint numVotes; 
    }
    
    //voter name,authorised,whom(voting to),voted(or not)
    struct Voter{
        string name; 
        bool authorised; 
        uint whom;
        bool voted; 
    }


    address public owner;  //is the admin starts election
    string public electionName;  //the name of the election
    bool public electionStarted = false;  //election started or not

    mapping(address => Voter) public voters; // voters = i.voter[adres]
    Candidate[] public candidates; //array of candidate 
    uint public totalVotes; //total votes voted 

//modifiers to limit or require
    modifier ownerOnly(){ 
        require(msg.sender == owner); 
        _;
    }
    modifier ifnoElection(){ 
        require(electionStarted==false); 
        _;
    }
    
    //function to start election
    function startElection(string memory _electionName)ifnoElection public{ 
        owner = msg.sender; 
        electionName = _electionName; 
        electionStarted = true;
    }

    //adding candidate only admin candidate(name, number of votes)
    function addCandidate(string memory _candidateName)ownerOnly public{ 
        candidates.push(Candidate(_candidateName, 0));
    }

    //authorize voter admin only can authorize voter needs voter adress
    function authorizeVoter(address _voterAddress) ownerOnly public{ 
        voters[_voterAddress].authorised= true; 
    }

    //function to get total candidates number
    function getNumCandidates() public view returns(uint){ 
        return candidates.length; 
    }

    //function to vote requires the index of candidate to vote
    function vote(uint candidateIndex) public{ 
        require(!voters[msg.sender].voted); 
        require(voters[msg.sender].authorised); 
        voters[msg.sender].whom = candidateIndex; 
        voters[msg.sender].voted = true; 

        candidates[candidateIndex].numVotes++; 
        totalVotes++;
    }

    //to get total votes
    function getTotalVotes()public view returns(uint) {
        return totalVotes;
    }

    //get info of candidate 
    function candidateInfo(uint index) public view returns(Candidate memory){ 
        return candidates[index];
    }

    function closeElection()public {
        remove();
      //  electionStarted = false;
    }

    function remove() public ownerOnly{
        
        uint n = candidates.length;
        for(uint i=0;i<n;i++){
            delete candidates[i];
        }

    // // Reset the value to the default value.
     }
    
}

Solution

  • you can create a Factory contract which deploys Election contract

    contract ElectionFactory {
        address[] public deployedElections;
        uint public electionsCount;
        address public owner;
    
      modifier ownerOnly(){ 
            require(msg.sender == owner); 
            _;
        }
        constructor(){
            owner=msg.sender();
        }
        // If Election contract had constructor parameters you would pass them to this function
        // you want onlyOwner who is the creator of this contract create new Election
        function createElection() public ownerOnly {
            Election newElection = new Campaign();
            deployedElections.push(address(newElection));
            electionsCount++;
        }
    
        function getDeployedCampaign(uint index) public view returns (address) {
            return deployedElections[index];
        }
    
        function getCampaignCounts() public view returns (uint) {
            return campaignsCount;
        }
    }
    

    Your Election contract has no constructor function. You could add constructor to make your contract more specific. Maybe each election has different duration so you can specify it during creation of Election contract