This line in createNFT
is the problem:
farmersHistoryViewable[msg.sender].weightByBatch[0][farmersHistoryViewable[msg.sender].totalBatches] = _weight;
farmersHistoryViewable
maps addresses to structs of type farmersHistory
:
struct farmersHistory {
uint totalBatches;
uint256 totalWeight;
uint[1][] weightByBatch;
}
weightByBatch
is really strange... it's a dynamic array of 1-length arrays. (What's the purpose of a 1-length array?)
In any case, weightByBatch[0]
is always going to fail, since the array always has length zero. (Nothing in the code ever adjusts its length.)
This replacement sort of works, but it's unclear to me what you're trying to do, so I don't know if it accomplishes your goals. (It seems likely that you want to get rid of the second dimension of the array altogether.)
farmersHistoryViewable[msg.sender].weightByBatch.push([_weight]);
farmersHistoryViewable[msg.sender].totalBatches = farmersHistoryViewable[msg.sender].totalBatches.add(1);
(The second line of code is because your existing code didn't actually add one to totalBatches
. It computed the result of adding one but then discarded it.)
There are other issues here: notably, you create a token with the ID from nft.totalSupply()
, but then everything else you store is keyed off of the new value of nft.totalSupply()
(which has increased in the call to mint
). So your IDs are going to be mismatched between the nft
contract and the agService
instance.
Best Answer
Invalid opcode can be the result of either one of the following runtime operations:
assert
of an expression which evaluates tofalse
array[i]
wherei >= array.length
In your code, only the second option appears viable.
It is viable in the expression
userNotes[msg.sender][_noteId]
.So the error must be the result of
_noteId >= userNotes[msg.sender].length
.