1

i am wanting to import an array of json data into my contract on construct. I want to pass it an array of address to be saved into an array in my contract. Is this possible? I have seen a few posts about phrasing json into a contract but could't quite extract what i am looking for.

The json data i have is structured like this; - i am wanting to extract only the HolderAddress's.

[
  {
    "HolderAddress": "0x00007569643bc1709561ec2e86f385df3759e5dd",
    "Balance": 100,
    "PendingBalanceUpdate": "No"
  },
  {
    "HolderAddress": "0x00089553ab5a08bfdf1dc324845a4ad7a63e26b8",
    "Balance": 9,
    "PendingBalanceUpdate": "No"
  },
  {
    "HolderAddress": "0x0017bf3bca94a016a27d496363cec4d868758bb7",
    "Balance": 650,
    "PendingBalanceUpdate": "No"
  }
]

Can this data be passed at the deployment of the contract and how would i link the contract to read the json file?

Many thanks

1 Answer 1

0

ATM, what you can do is:

constructor(address[] memory addresses, uint256[] memory balances, bool[] memory flags) public {
    uint256 length = addresses.length;
    require(length == balances.length, "INVALID_INPUT");
    require(length == flags.length, "INVALID_INPUT");
    for (uint256 i = 0; i < length; i++) {
        // do whatever you want to do with the input data
    }
}

With arr being the array of objects in your question, you can use:

  • arr.map(x => x.HolderAddress) as the 1st parameter
  • arr.map(x => x.Balance) as the 2nd parameter
  • arr.map(x => x.PendingBalanceUpdate != "No") as the 3rd parameter
7
  • Thanks for this. How do i initially pass the json to the constructor/contract?
    – Tuck
    Sep 30, 2020 at 15:11
  • @Tuck: See updated answer. Sep 30, 2020 at 15:19
  • Thank you for you time and response. I apologies for for asking obvious and basic questions. This is my first time playing around with json but have been learning solidity for a couple of months. Is this deployed from within the migration.js script? so it would look like deployer.deploy(MyToken, arr.map(x => x.HolderAddress), arr.map(x => x.Balance)); ? Would i also call the json array into the script or have to copy it in?
    – Tuck
    Sep 30, 2020 at 15:35
  • @Tuck: You're missing the last input argument (or maybe you just omitted it in order to keep the question short). As with regards to your last question, I don't understand the meaning of "call the json array into the script". Sep 30, 2020 at 15:46
  • yes just keeping the reply shorter. So i have my array of data in a json file, do i need to import/declare/not sure on the correct term that into the migrations script? Is it best to just copy the whole thing in?
    – Tuck
    Sep 30, 2020 at 15:50

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.