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
1
pragma solidity ^0.7.0;
2
pragma abicoder v2;
3
4
contract MyContract {
5
6
struct MyStruct {
7
uint256 foo;
8
string bar;
9
}
10
11
MyStruct[] private _structs;
12
13
constructor(bytes memory structsBytes) {
14
MyStruct[] memory structs = abi.decode(structsBytes, (MyStruct[]));
15
for(uint256 i = 0; i < structs.length; i++) {
16
_structs.push(structs[i]);
17
}
18
}
19
}
Copied!
Last modified 9mo ago
Copy link