1

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 ?

1
  • 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, 2018 at 4:18

3 Answers 3

3

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

5
  • 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, 2018 at 4:18
  • I can pass string and uint arrays but the problem is with address[] Apr 3, 2018 at 17:14
  • 1
    could you share the javascript code you used to call the function with web3? Apr 4, 2018 at 6:58
  • what about web3.py
    – btc4cash
    Sep 21, 2020 at 18:30
  • Thanks, this is what made it work when interacting with the contract manually in web or remix.
    – Qwerty
    Jan 5, 2022 at 16:21
1

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});
3
  • I get TypeError: mycontract.passAddress.getData is not a function Mar 28, 2018 at 5:10
  • @Are you using any framework? Mar 28, 2018 at 7:20
  • myContract.methods.passAddress(params).encodeABI(); Mar 28, 2018 at 7:23
0

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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.