Hi, I ve been struggling with dynamic arrays. I am checking for eligible orders and don't know how long the array will be at begging.

function getEligibleOrders() public view returns (uint256[] memory) {
/// get prices for tracking assets
AssetInfo[] memory assetsInfo = priceConsumer.batchGetter();
uint256[] memory eligibleOrdersIds;
uint256 pointer = 0;

for (uint256 i = 0; i < assetsInfo.length; i++) {
address asset = assetsInfo[i].asset;
int256 assetPrice = assetsInfo[i].price;
for (uint256 j = 0; j < Orders[asset].length; j++) {
int256 orderPrice = Orders[asset][j].price;
OrderType orderType = Orders[asset][j].orderType;
if (
(orderType == OrderType.SELL && assetPrice >= orderPrice) ||
(orderType == OrderType.BUY && assetPrice <= orderPrice)
) {
console.log("orderId: ", j);
eligibleOrdersIds[pointer] = j;
pointer += 1;
}
}
}
return eligibleOrdersIds;
}

But it failes, I get Error: VM Exception while processing transaction: reverted with panic code 0x32 (Array accessed at an out-of-bounds or negative index)

Aug 22, 2022, 8:30 PM

© 2024 Draquery.com All rights reserved.