0

I've created a function which gets a struct which contains int+struct. Now when I want to test it, what data should I send for example on Remix:

here's the code, got from another SO question on - How to send an array of structs as arguments

pragma solidity ^0.4.24;
pragma experimental ABIEncoderV2;

contract ExchangeContract {
  enum CurrencyType { USD, TWOKEY, BTC, ETH, DAI, USDT, TUSD, EUR, JPY, GBP}

  mapping(uint256 => CurrencyPrice) public priceByCurrencyType;

  struct Price{
    uint price;
    uint decimals;
  }

  struct CurrencyPrice{
    uint currencyInt;
    Price price;
  }

  function updatePrices(CurrencyPrice[] memory _array) public {
    for(uint i=0; i<_array.length; i++){
      priceByCurrencyType[_array[0].currencyInt].price=_array[i].price;
    }
  }
}

Now, I want to interact with that function from Remix - is it possible?

1 Answer 1

5

The struct used as the parameter type in your updatePrices has a particularity, it contains a member which type is another struct. We will come back to that later, for now let's look how it goes with a "simple" struct.

contract Contract {

  struct Simple {
      int a;
      int b;
  }

  Simple[] public array;

  function add(Simple[] memory _array) public {
    for(uint i=0; i<_array.length; i++){
        array.push(_array[i]);
    } 
  }

}

You can see it's rather simple. On Remix, if you want to call add, it's simple too. You just create an array, like you would for any other type, and then add "sub-arrays" which will represent each struct.

Here's an example :

[[1,2],[3,4]]

You are adding two structs. The first one contains 1and 2, which will be respectively assigned to the a and b members of the struct, and same thing for the second one which has 3and 4.

Now, back to your contract.

Logically, we would call updatePrices with the following input [[5,[2,2]]]. It's an array of length 1, with 5 being the currencyInt member and [2,2] the corresponding values for the Price struct.

It's not possible on remix.ethereum.org for now, it won't work.

However, it's implemented on alpha remix, and if you used the input above, it will work fine.

enter image description here

Regarding your contract itself

I think there is an error on this line

priceByCurrencyType[_array[0].currencyInt].price=_array[i].price;
                           ^

You probably intended to do _array[i].

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.