[SOLVED]Calling a solidity function has the the expected behaviour

Hi guys, I’m learning Moralis and I’m facing my first problem.

Here my solidity function :

function addVoter(address _addr) external onlyOwner {
        require(workflowStatus == WorkflowStatus.RegisteringVoters, 'Voters registration is not open yet');
        require(voters[_addr].isRegistered != true, 'Already registered');
    
        voters[_addr].isRegistered = true;
        emit VoterRegistered(_addr);
    }

As you can see an address can be add only once.

Now when I call the function addVoter in JS :

const {account} = useMoralis();

const addingVoter = async function (){
        let options = {
            abi: [
                {
                    "inputs": [
                        {
                            "internalType": "address",
                            "name": "_addr",
                            "type": "address"
                        }
                    ],
                    "name": "addVoter",
                    "outputs": [],
                    "stateMutability": "nonpayable",
                    "type": "function"
                },
            ],
            contractAddress: 'the contract address',
            functionName: "addVoter",
            params: {
                _addr: account,
            },
        }

        await contractProcessor.fetch({
            params: options
        })
    };

 return (
        <div>
            <button
                onClick={() => AddingVoter()}
            >
                Fetch data
            </button>
        </div>
    );

When I click the button, Metamask pop up and I can sign the transaction, but I can do it again and again with the same address which I normally can’t do it. I don’t figure out my mistake can you help me ?

Do you have any view function to check if the voters[_addr].isRegistered is actually true or false?

Maybe test it in the remix to check if it works as expected.

Yes, I’ve got a struct Voter :

struct Voter {
        bool isRegistered;
        bool hasVoted;
        uint votedProposalId;
    }

All my tests passed with Truffle and even on Remix if I add twice the same address, the require is displayed

Did the metamask transaction go through successfully? The could be the possible reason if the code is correct.

I found my mistake… I was on ganache !
I did truffle migrate --reset --network ropsten and now it’s works :slight_smile:

Thanks for your time @johnversus

1 Like