0

I am using truffle and I am trying to pass during the deployment execution script a double array of prices inside my contract's constructor. ie

In the migration file I have a 2 dimensional array. It's nested array contains a pair of prices.

var prices = [
  ['1223.456', '1223.456'],
  ['123.46', '12.1']
];
contract A {

   struct Price {
        string Price0;
        string Price1;
    }

    mapping (uint => Price) prices;

    constructor (int[][] memory _prices) public
    {

        for(uint i=0; i< _prices.length; i++) 
        {

          prices[i] = Price(
              _prices[i][0],
              _prices[i][1]
          );

        }
    }
}

How can I declare this inside my contract's constructor so that I can parse it right after? What I saw so far is that I can declare a 2 dimensional array with the same type ie

uint[][] prices;
// or
string[][] prices;

but these are not working since in my double array the 1-level key is of type integer and the value of the 2-level is of type string.

12
  • If you want to get suggestions for the actual solution that you should apply, then you'll need to show what you intend to do with the input to the constructor. – goodvibration Jan 4 at 14:09
  • @goodvibration I added the code of constructor. – spyrAlex Jan 4 at 14:28
  • Why do you need a 2D array if you always access [1]? – goodvibration Jan 4 at 14:45
  • Thank you for the indication. That was a mistake :) – spyrAlex Jan 4 at 16:11
  • Your code doesn't make sense (and doesn't compile either). Fields Price0 and Price1 are of type string. The input _prices array contains items of type int, which you cannot assign to variables of type string!!! – goodvibration Jan 4 at 16:34
0

Try this:

constructor(string[] memory _prices0, string[] memory _prices1) public {
    uint256 length = _prices0.length;
    require(length == _prices1.length, "invalid input length");
    for (uint256 i = 0; i < length; i++) {
        prices[i] = Price({
            Price0: _prices0[i],
            Price1: _prices1[i]
        });
    }
}

Then, from JS, you can call it with:

var prices0 = ['1223.456', '123.46'];
var prices1 = ['1223.456', '12.1'];
4
  • Thank you for the suggestion. This actually works really good. The only thing is that I need to prepare the data in javascript accordingly so to meet your logic. Out of curiosity is there any other way where you don't have to split the data to two groups just like the javascript example that I provided? – spyrAlex Jan 4 at 19:02
  • @spyrAlex: As of solc 0.8.0, passing structs as input to public and external functions is natively supported. If you switch to this compiler version, then I suppose that you can pass a single array of structures. BTW, feel free to accept my answer by clicking on the V next to it (if it has indeed answered your question). – goodvibration Jan 4 at 19:16
  • I tried also with the latest solc 0.8.0 and also works as I wanted initially. Thanks again! – spyrAlex Jan 4 at 19:29
  • @spyrAlex: You're welcome. – goodvibration Jan 4 at 19:30

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.