Got an exception at the line msg.sender.transfer(purchaseExcess);
in my truffle solidity unit test. Any idea the reason?
my contract code:
function purchase(uint256 _tokenId) public payable {
address oldOwner = tokenOwner[_tokenId];
uint256 sellingPrice = emojiIndexToPrice[_tokenId];
address newOwner = msg.sender;
require(oldOwner != newOwner);
require(newOwner != address(0));
require(msg.value >= sellingPrice);
uint256 percentage = SafeMath.sub(100, ownerCut);
uint256 payment = uint256(SafeMath.div(SafeMath.mul(sellingPrice, percentage), 100));
uint256 purchaseExcess = SafeMath.sub(msg.value, sellingPrice);
emojiIndexToPrice[_tokenId] = SafeMath.div(SafeMath.mul(sellingPrice, 150), percentage);
_transfer(oldOwner, newOwner, _tokenId);
if (oldOwner != address(this)) {
oldOwner.transfer(payment);
}
msg.sender.transfer(purchaseExcess);
}
unit tests code:
contract TestEmojiCoin {
uint public initialBalance = 1 ether;
function testPurchase() public {
address contractAddress = DeployedAddresses.EmojiCoin();
EmojiCoin emojiCoin = EmojiCoin(contractAddress);
// Emoji 0 is created in previous test.
emojiCoin.purchase.value(1 ether).gas(30000000000)(0);
}
}
error msg
Error: VM Exception while processing transaction: revert
at Object.InvalidResponse (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/errors.js:38:1)
at /usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/requestmanager.js:86:1
at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-provider/wrapper.js:134:1
at XMLHttpRequest.request.onreadystatechange (/usr/local/lib/node_modules/truffle/build/webpack:/~/web3/lib/web3/httpprovider.js:128:1)
at XMLHttpRequestEventTarget.dispatchEvent (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:64:1)
at XMLHttpRequest._setReadyState (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:354:1)
at XMLHttpRequest._onHttpResponseEnd (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:509:1)
at IncomingMessage.<anonymous> (/usr/local/lib/node_modules/truffle/build/webpack:/~/xhr2/lib/xhr2.js:469:1)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:114:19)
uint public initialBalance = 2 ether;
might solve the issue.