Say i have a contract as follows:

contract A {
  function passAddress(address[] _addr) {
    // do something

How do i call this function in my frontend application using web3. I tried using javascript array but it throws revert exception.

Any ideas ?

  • 2
    A JavaScript array is the right way to do this via web3.js. Can you share your code? (Also share the rest of the contract code so we can guess at where the revert was.) – user19510 Mar 28 '18 at 4:18

You can pass array as follows :-

  var contractAbi = eth.contract(AbiOfContract);
  var myContract = contractAbi.at(contractAddress);
  var getData = myContract.passAddress.getData([address1,address2,address3]);
  // var getData = myContract.passAddress.getData(["0x0","0x0","0x0"]);
  web3.eth.sendTransaction({to:Contractaddress, from:Accountaddress, data: getData});
  • I get TypeError: mycontract.passAddress.getData is not a function – Jagjot Singh Mar 28 '18 at 5:10
  • @Are you using any framework? – Crissi Mariam Robert Mar 28 '18 at 7:20
  • myContract.methods.passAddress(params).encodeABI(); – Crissi Mariam Robert Mar 28 '18 at 7:23

You are only able to pass datatypes which the contract recognises, so passing a javascript array is not an option.

If you have an array of all your addresses, you could try joining them using javascript, and passing the resulting string using web3. ie,

if you have

array = [0x0,0x0,0x0];

and you made that

string = '["0x0","0x0","0x0"]'

and pass that string as the "array" then that could do the trick

  • 2
    This is incorrect for web3.js. A JavaScript array is the right way to pass an array to a smart contract. – user19510 Mar 28 '18 at 4:18
  • I can pass string and uint arrays but the problem is with address[] – Jagjot Singh Apr 3 '18 at 17:14
  • 1
    could you share the javascript code you used to call the function with web3? – AnAllergyToAnalogy Apr 4 '18 at 6:58
  • what about web3.py – btc4cash Sep 21 '20 at 18:30

Adoption is my smart contract. I using this way to call function which array of address as an argument.

App.contracts.Adoption.deployed().then(function(instance) {
  adoptionInstance = instance;
  return adoptionInstance.passAddress("address1, address2,...");
}).then(function(adopters) {
  console.log("function success");
}).catch(function(err) {
  console.log("function error" + err.message);

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.