While working on my Ethereum and Next.js project, I encountered an error during the initialization of the project:
Error: data out-of-bounds (length=3, offset=32, code=BUFFER_OVERRUN, version=abi/5.0.7)
at Logger.makeError (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/logger/lib/index.js:199:21)
at Logger.throwError (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/logger/lib/index.js:208:20)
at Reader._peekBytes (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/abstract-coder.js:149:24)
at Reader.readBytes (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/abstract-coder.js:161:26)
at Reader.readValue (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/abstract-coder.js:167:48)
at NumberCoder.decode (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/number.js:49:28)
at /home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/array.js:106:31
at Array.forEach (<anonymous>)
at Object.unpack (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/array.js:85:12)
at TupleCoder.decode (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/coders/tuple.js:39:49)
at AbiCoder.decode (/home/Documents/projects/ethereum/kickstarter/node_modules/@ethersproject/abi/lib/abi-coder.js:93:22)
at ABICoder.decodeParametersWith (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-abi/lib/index.js:303:30)
at ABICoder.decodeParameters (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-abi/lib/index.js:284:17)
at Contract._decodeMethodReturn (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-contract/lib/index.js:469:22)
at Method.outputFormatter (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-eth-contract/lib/index.js:759:42)
at Method.formatOutput (/home/Documents/projects/ethereum/kickstarter/node_modules/web3-core-method/lib/index.js:146:54) {
reason: 'data out-of-bounds',
code: 'BUFFER_OVERRUN',
length: 3,
offset: 32
}
The error occurred inside getServerSideProps. Initially, this was my code snippet:
let campaigns;
try {
// This returns an array of contracts in Solidity contract
campaigns = await factory.methods.getDeployedCampaign().call();
} catch (e) {
console.log("error in index server", e);
}
Even after thoroughly checking the code, I couldn't find any issue. I suspected that the problem might be related to returning an array of addresses. Although we cannot return arrays of structs from Solidity, arrays of addresses are permissible. Nonetheless, I decided to retrieve each campaign individually. So, I updated the contract code as follows:
contract CampaignFactory{
address[] public deployedCampaigns;
uint public campaignsCount;
function createCampaign(uint minimum)public{
Campaign newCampaign=new Campaign(minimum,msg.sender);
deployedCampaigns.push(address(newCampaign));
campaignsCount++;
}
// Return individual campaign
function getDeployedCampaign(uint index) public view returns(address ){
return deployedCampaigns[index];
}
function getCampaignCounts() public view returns(uint){
return campaignsCount;
}
}
I subsequently corrected the server-side code accordingly:
export async function getServerSideProps(context) {
let campaigns;
let campaignsCount;
try {
// Get the number of campaigns
campaignsCount = await factory.methods.getCampaignCounts().call();
campaigns = await Promise.all(
Array(parseInt(campaignsCount))
.fill()
.map((element, index) => {
return factory.methods.getDeployedCampaign(index).call();
})
);
console.log("camapigns in index", campaigns);
} catch (e) {
console.log("error in index server", e);
}
return {
props: { campaigns: campaigns || [] },
};
}
After making these changes, the same error persisted. However, upon refreshing the page, the errors vanished, and I could see the data displayed on the UI. This made me believe that there were no issues with the contract itself but rather with the interaction between JavaScript.