I'm doing a simple Dapp where I register a star as an NFT, using openzeppelin ERC721 contract, and then I can put it up for sale and buy it. In truffle I'm writing a test where I create a star, put it for sale then buy it. I get the balance of the buyer before and after the purchase, compute the difference and assert if it is the same as the price of the star. I took into account the gas used but there is still a tiny difference I can't understand. Here is my code:
Contract function
function buyStar(uint256 _tokenId) public payable {
require(starsForSale[_tokenId] > 0, "The Star should be up for sale");
uint256 starCost = starsForSale[_tokenId];
address ownerAddress = ownerOf(_tokenId);
require(msg.value > starCost, "You need to have enough Ether");
transferFrom(ownerAddress, msg.sender, _tokenId); // We can't use _addTokenTo or_removeTokenFrom functions, now we have to use _transferFrom
address payable ownerAddressPayable = payable(ownerAddress); // We need to make this conversion to be able to use transfer() function to transfer ethers
ownerAddressPayable.transfer(starCost);
if(msg.value > starCost) {
payable(msg.sender).transfer(msg.value - starCost);
}
Truffle test
it('lets user2 buy a star and decreases its balance in ether', async() => {
let instance = await StarNotary.deployed();
let user1 = accounts[1];
let user2 = accounts[2];
let starId = 5;
let starPrice = web3.utils.toWei(".01", "ether");
let balance = web3.utils.toWei(".05", "ether");
await instance.createStar('awesome star 4', starId, {from: user1});
await instance.putStarUpForSale(starId, starPrice, {from: user1});
let balanceOfUser1BeforeTransaction = await web3.eth.getBalance(user2);
const balanceOfUser2BeforeTransaction = await web3.eth.getBalance(user2);
const receipt = await instance.buyStar(starId, {from: user2, value: balance});
const tx = await web3.eth.getTransaction(receipt.tx);
const gasCost = tx.gasPrice * receipt.receipt.gasUsed;
console.log(`GasCost + Star Price: ${Number(gasCost) + Number(starPrice)}`);
const balanceAfterUser2BuysStar = await web3.eth.getBalance(user2);
let value = Number(balanceOfUser2BeforeTransaction) - Number(balanceAfterUser2BuysStar);
console.log(`Difference: ${ value - (Number(gasCost) + Number(starPrice))}`);
assert.equal(value, starPrice);
});
Test output: