1

imagine that I have a list of voters data and I want to send a file to the function addVotersList to add all the voters by one click rather than sending the data one by one for each voter

this is the function :

function addVotersList(string name,uint id , address voter)public restricted {
       
          ....
    }
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Sep 1 at 11:29
0

You can't send a file to the function. But if you want to get done all of the process with one click, you can use arrays.

function addVotersList(string[] calldata name,uint[] calldata id , address[] calldata voter) external

I changed function visibility. Because if you are getting arrays in your paramaters, external very much gas saver. And I added calldata keyword to all parameters. Again, it is for gas saving.

4
  • so, if i try to build app using react and web3 and the data of the voters{name,id ,address }available in json file can i use something in react to read the content of the file an then send it as transaction to the function addVotersList(string[] calldata _names,uint[] calldata _ids , address[] calldata _voters)public , in another words can i fetch the data in the json file and passing it as a parameters to the addVotersList function? Sep 1 at 20:39
  • You can fetch names, ids and votes from json to separate arrays. And then you can send those arrays as paramaters to your contract. Sep 1 at 21:45
  • thx a lot . is there any link that have a code to this idea "of fetching file to arrays" Sep 2 at 12:40
  • stackoverflow.com/a/9991872/10699632 maybe this one can solve your problem Sep 2 at 20:24
0

Solidity doesn't read external files. if you want to add a list of voters provide them using arrays. For example :

function addVotersList(string[] calldata _names,uint[] calldata _ids , address[] calldata _voters)public  {
           
                     for (uint i = 0; i < _names.length; i++)
                       { 
                        global_names[i]=_names[i];
                        global_ids[i]=_ids[i];
                        global_voter[i]=_voter[i];
                           
                       }

        }
5
  • so, if i try to build app using react and web3 and the data of the voters{name,id ,address }available in json file can i use something in react to read the content of the file an then send it as transaction to the function addVotersList(string[] calldata _names,uint[] calldata _ids , address[] calldata _voters)public , in another words can i fetch the data in the json file and passing it as a parameters to the addVotersList function? Sep 1 at 20:38
  • you can parse your json file and then construct your call by passing the coressponding arrays to your addVotersList function
    – Badr Bellaj
    Sep 1 at 21:49
  • thx a lot . is there any link that have a code to this idea "of fetching file to arrays" Sep 2 at 12:40
  • if one of the provided answers is helpful don't forget to accept it
    – Badr Bellaj
    Sep 2 at 17:34

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.