0

I need to create an array of structs for a struct that looks like this:

struct myStruct {
   address _address;
   uint _num;
}

And I need to create the array from function args.

I'm doing this by using 2 params (address[] & uint[]) that represent the 2 struct members, and for looping through them to create the array of structs. Is there a simpler way? Potentially using

ethers.utils.AbiCoder.prototype.encode(
  ['address', 'uint'],
  [<address>, <number>]
);
0

1 Answer 1

1

you don't have to encode it.

you can build the input like this:

   let  myStruct = {
    _address: accounts[2].address,
    _num: web3.utils.toWei("1")
  }

Then just call your function with the struct as the input

3
  • Realized this eventually, thanks
    – Ryan Sea
    Commented Sep 12, 2022 at 16:29
  • It will actually be an array of structs that will be saved to storage. Is there a way, other than for looping, to save a struct[] memory arg to struct[] storage?
    – Ryan Sea
    Commented Sep 12, 2022 at 16:31
  • 1
    haven't done that personally, so wouldn't know, but I doubt it. I've had troubles even doing arrays in a struct
    – thefett
    Commented Sep 12, 2022 at 17:08

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.