[SOLVED] Solidity Calculation issue

I have a Struct set that stores uint256 price.
So, when I have to store a decimal number into the ā€œpriceā€. I do
const price = Moralis.Units.ETH(price)
So, now the mapping in contract stores the data of price as 1100000000000000000 for 1.1 ETH.
When I have to call a function to send ETH to the contract will I need to change the amount of ETH sent by the user?
Like:

if(listing.price > msg.value) // this returns true. as the listing price has been set by 10**18 but the msg.value is only 1.1

Will it cause any security problem if I did:
uint256 val = msg.value * 10**18;
(listing.price > msg.value) // here it returns false as both has same amt of value now.

Hey @resourcelink123, hope you are well.

Smart Contracts will save the funds or balance of an account in wei, which is measure in uint256.

So if you have to send ETH from one account to another, in the frontend, it will be inputed as string (like sending 1.1eth), but internally, the amount will be converted in wei.

You could use Moralis.Units.ETH() to send the amount from a float number (like 0.5) which will be sended as string to this function but to the contract the amount is specified in wei.

If you call a function that returns the funds or balance of an account, this will be returned in wei, so if you want to show this balance result in readable format, you must use Moralis.Units.FromWei(amount, decimals).

https://docs.moralis.io/moralis-server/sdk-utils/moralis-units

Carlos Z

1 Like

Yes ser. Thank you for your answer. I figured that the problem in my contract was not due to the price/balance. It was because the ERC721 contract was returning the error ā€œTransfer of token that is not ownā€. Any idea what might be the cause? I have already approved the transfer of token using ā€œsetApprovalForAllā€ method. Itā€™s returning true even when I do a call of ā€œisApprovedForAllā€.
But the transaction returns error when the contract tries to transfer the nft.

It looks for me that you ask two different questions, for the first one, you donā€™t have to use uint256 val = msg.value * 10**18; because msg.value is already in WEI.
For the transfer of token error, I donā€™t know too many details, like what parameters did you use for setApprovalForAll.

Thanks for the response! Yeah I found the error ughh. Stupid mistake. Iā€™m done now. Itā€™s all working fine!
Thanks :slight_smile: