I was getting a "EVM error", so I commented out some code and backtracked to find the issue. I added my _feeAmount to an Emit, and getting a what seems to be a bizarre math answer on a simple math problem.
results displayed from Truffle Unit Test with console.log statements. Everything converted from WeiToEth.
event.feePercent =10
event.FeeAmount =-18
event.amountGet =2
event.amountGive =1
FeeAmount should be 2 / 100 * 10 which should be .2 but I'm getting -18. No idea why it's negative.
import "openzeppelin-solidity/contracts/math/SafeMath.sol";
...
function _trade(uint _orderId, address _user, address _tokenGet,
uint _amountGet, address _tokenGive, uint _amountGive) internal {
// Fee paid by the user that fills the order, a.k.a. msg.sender.
// charge fees - paid by msg.sender - user filling the order
// taken from the amount he gets
uint _feeAmount = _amountGet.div(100).mul(feePercent);
// do the trade, _user created the order, msg.sender is filling the order
//tokens[_tokenGet][msg.sender] = tokens[_tokenGet][msg.sender].sub(_amountGet.add(_feeAmount));
/*
tokens[_tokenGet][_user] = tokens[_tokenGet][_user].add(_amountGet);
// send the fees to the feeAccount
tokens[_tokenGet][feeAccount] = tokens[_tokenGet][feeAccount].add(_feeAmount);
tokens[_tokenGive][_user] = tokens[_tokenGive][_user].sub(_amountGive);
tokens[_tokenGive][msg.sender] = tokens[_tokenGive][msg.sender].add(_amountGive);
*/
// emit trade event
emit Trade(_orderId, _user, _tokenGet, _amountGet, _tokenGive, _amountGive, _feeAmount,
feePercent, msg.sender, now);
}
Truffle test code:
it('emits a "Trade" event', async () => { // this is why we needed the "result=" above; // "result" is what a subscriber would get subscribing to this event //console.log(result); console.log("Number of logs=" + result.logs.length)
const log = result.logs[0];
let event;
// get event from logs
event = log.args
console.log("event.feePercent =" + event.feePercent);
console.log("event.FeeAmount =" + WeiToEth(event.feeAmount))
console.log("event.amountGet =" + WeiToEth(event.amountGet))
console.log("event.amountGive =" + WeiToEth(event.amountGive))
Definition of Trade event:
event Trade(
uint id,
address user,
address tokenGet,
uint amountGet,
address tokenGive,
uint amountGive,
uint feeAmount,
uint feePercent,
address userFill,
uint timestamp
);
Helper function:
export const WeiToEth = (n) => {
return new web3.utils.BN(
web3.utils.fromWei(n.toString(),'ether')
)
}
Definition of feePercent:
uint public feePercent; // Fee percentage
Everything in WEI:
event.feePercent =10
event.FeeAmount =200000000000000000
event.amountGet =2000000000000000000
event.amountGive =1000000000000000000
.2
??? What variable type do you use for storing this value?unsigned
??? What off-chain tool are you using in order to call this transaction and parse the returned value? If it's a programmatic tool (for example, web3.js), then what does your code look like?event Trade
? Are we suppose to assume that you are passing_feeAmount
in the correct place?2 / 100 * 10 == (2 / 100) * 10 == 0 * 10 == 0
.