I am trying to deploy the following smart contract in ganache-cli using truffle framework.
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract Sample {
address[] public owners;
constructor(address[] memory temp) {
for(uint256 i=0; i<temp.length; i++) {
owners[i] = temp[i];
}
}
}
But I am getting the following error.
Deploying 'Sample'
------------------
✖ Transaction submission failed with error -32000: 'VM Exception while processing transaction: revert'
*** Deployment Failed ***
"Sample" hit a require or revert statement with the following reason given:
* Panic: Index out of bounds
Exiting: Review successful transactions manually by checking the transaction hashes above on Etherscan.
Error: *** Deployment Failed ***
"Sample" hit a require or revert statement with the following reason given:
* Panic: Index out of bounds
Help me to fix this issue.
address[] public owners;
This line declares the array with 0 items.
owners[i] = temp[i];
And this line is trying to assign into i
th item that does not exist in the array.
Solution: You need to use the push()
function to resize the array and set the new item value.
for(uint256 i=0; i<temp.length; i++) {
owners.push(temp[i]);
}