Complex struct in constructor

If a Smart Contract has complex structs that are also needed in the constructor, it is good to pass these in bytes format, in order to make the deployment easier using Remix

pragma solidity ^0.7.0;
pragma abicoder v2;

contract MyContract {

	struct MyStruct {
		uint256 foo;
		string bar;
	}
	
	MyStruct[] private _structs;
	
	constructor(bytes memory structsBytes) {
		MyStruct[] memory structs = abi.decode(structsBytes, (MyStruct[]));
		for(uint256 i = 0; i < structs.length; i++) {
			_structs.push(structs[i]);
		}
	}
}

Last updated