I am trying to implement a function where, I want to first calculate the payable amount, whose result is most likely to come as a decimal value and then, transfer that decimal value to the address that has called that function.
To give an overview, this is how the function looks like:
function _distributeClaim(
address _claimant,
uint256 _balances
) internal returns (bool){
uint256 payableAmount = tradingProfits * (_balances / totalInvestedTFs);
require(
payableAmount < address(this).balance,
"TF: Insufficient Balance in Contract!"
);
isClaimed[_claimant] = !isClaimed[_claimant];
(bool success, ) = payable(_claimant).call{ value: payableAmount }("");
return success;
}
In the following function, my main concern is for the line:
uint256 payableAmount = tradingProfits * (_balances / totalInvestedTFs);
Here, I am certain, that balances
and totalInvestedTFs
are going to be in uint value. However, the tradingProfits is most likely to be in decimals, like 1.5 or 3.2, etc.
When, doing the calucaltions, since solidity does not have a fixed point math support, I always get the payableAmount value to be 0.
To get me past this issue, I tried to search and look for potential solutions, to which I found libraries like ABDK Maths but I am not able to implement the above library in my function.
Can someone help me out with it?
I also found this discussion topic, but right now I am naive to implement the fixed-point value Designing Fixed Point Math in OpenZeppelin Contracts
The Maths for the payableAmount may look like this:
_balances = 2000
_totalInvestedTF = 10000
Trading profit = 1.5
Hence, payable amount = 1.5 * (2000/10000)
= 0.3 eth.
Now, with the obtained 0.3 eth, can I send this decimal value to the claimant address by calling this line:
(bool success, ) = payable(_claimant).call{ value: payableAmount }("");
Please someone help me out to solve my above roadblocks.