Search code examples
arraysethereumsoliditydynamic-arrays

How to create a dynamic memory array in Solidity?


I'm creating an ERC777 from a contract and I'm having trouble with the default operators. My code compiles fine but when I try to deploy it it doesn't work.

Here's a simplified version of my code

contract Amacoin is ERC777 {
  address public amaclicker;

  constructor(address[] memory defaultOperators) ERC777("Amacoin", "AMAC", defaultOperators) {
    amaclicker = defaultOperators[0];
  }
}

contract Amaclicker {
  Amacoin public amacoin;

  constructor() {
    address[] memory defaultOperators;
    defaultOperators[0] = address(this);
    amacoin = new Amacoin(defaultOperators);
  }
}

From my testing, it's the defaultOperators[0] = address(this); that doesn't work. Also just putting [address(this)] as the argument for Amacoin or defaultOperators doesn't work.

Edit: I still haven't found a solution but a workaround I've done is create a state variable and then delete it after creating Amacoin


Solution

  • Try this way. This should work.

    address[] memory defaultOperators = new address[](1);
    defaultOperators[0] = address(this);