While creating a marketplace project, I listen to events to store NFT prices on my node backend.
In the given sample I implemented a pre-save middleware that checks if the number is an integer or not and if the number is an integer, it assumes that the value is in Ether and runs next middleware.
If it is not an Integer I assume that this is a WEI value thus I convert from WEI to ETHER.
The problem arises when the ETHER value is in decimals, suppose "1.2 ETH". Since this is not an integer the next() callback is skipped and fromWei("1.2ETH") functions runs on an ETHER value.
nft.pre('save', async function(next) {
if (!this.isModified('nftPrice')) return next();
if(!(Number.isInteger(this.nftPrice))) return next();
this.nftPrice=web3.fromWei(`${this.nftPrice}`,'ether');
next();
});
This can cause problems in price value of an assets as the end user will be feeded wrong price.