You need to add the constant
keyword to the test()
function like:
pragma solidity ^0.4.0;
contract Test {
mapping (address => uint256) weismap;
function bet(uint vote) payable returns (uint256 weis) {
if (msg.value==0)
throw;
weismap[msg.sender]= msg.value;
return weismap[msg.sender];
}
function test() constant returns (uint myVote) {
return 1;
}
}
Calling bet(...)
with the value 2. Note that I'm sending 2 ethers as well with the transaction:
Now I call test()
AND I've left the value 2. This call to test()
fails as the constant function is being called as a transaction, not a read of the values off the blockchain:
If I call test()
AND I've set value to blank, this call to test()
succeeds as the constant function is being called as a read of the values off the blockchain:
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
There is no error in your code, I think after sending a transaction to your contract using the
fallback
function you callgetbalance
function with a non null value. While getbalance isn't a payable function you will get the mentioned error. So after before calling getbalance make sure that the value is 0 in remix transaction value :