solidity global variables uint256 in division its not work - solidity

example:
uint256 unit = 10000000000000000 * (100 / 1000);
it is work.
global variables:
uint256 ratio = 100; //in global
uint256 unit = 10000000000000000 * (ratio / 1000); //in function
not work, the result always be 0.

In Solidity fractional division is not supported. As to how the division works you can refer to the documentation. What is happening in your code is the result of the division rounding down to 0.
You can find information about how fixed point variables work in Solidity here. You can try to use these but you should really try to avoid using fractional numbers in your code.
In your case you can replace the expression with uint256 unit = 10000000000000000 / (1000 / ratio); as long as ratio < 1000

Related

Solidity converting decimal value to zero

I'm building a function that divides 40% of revenue with current supply and multiply that with holder's balance to calculate the token amount sent to token holder on the basis of balance.
revenue = 100000, totalSupply = 500000000, holdersBalance = 1000
It should return 0.08 as distribution amount, while I'm getting 0. Is there any way to fix this? any help will be appreciated, thanks
uint256 public revenue;
uint256 public distributionAmount; //Get final amount to distribute
function calculate(uint256 _revenue, uint256 _totalSupply, uint256 holdersBalance) public {
revenue = _revenue;
uint256 amount = (revenue * 40)/100;
amount = ((amount / _totalSupply) * holdersBalance);
distributionAmount = amount; //should get 0.08 in distributionAmount, but getting zero
}

How can I convert the returned value from latestRoundData()?

I've been following a tutorial on chainlink documentation, to get the current price of Matic using MATIC/USD Mumbai Testnet Data feed, the function getLatestPrice() returns 65990700. I read that latestRoundData() returns the value in Wei. Then when I converted it using this website https://polygonscan.com/unitconverter to see how much this value is worth of Matic. I got 0.000000000065485509 Matic.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
import "#chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract PriceConsumerV3 {
AggregatorV3Interface internal priceFeed;
/**
* Network: Mumbai
* Aggregator: MATIC/USD
* Address: 0xd0D5e3DB44DE05E9F294BB0a3bEEaF030DE24Ada
*/
constructor() {
priceFeed = AggregatorV3Interface(0xd0D5e3DB44DE05E9F294BB0a3bEEaF030DE24Ada);
}
/**
* Returns the latest price
*/
function getLatestPrice() public view returns (int) {
(
/*uint80 roundID*/,
int price,
/*uint startedAt*/,
/*uint timeStamp*/,
/*uint80 answeredInRound*/
) = priceFeed.latestRoundData();
return price;
}
}
am I missing something ?
Chainlink USD datafeeds return price data with 8 decimals precision, not 18.
If you want to convert the value to 18 decimals, you can add 10 zeros to the result:
price * 1e10
See the decimals function output on the specified feed contract.

Confused setting price/ratio per token in solidity

I am trying to set the price ratio 1 token per 0.01 USDT
Code
uint256 public priceTokenPerDai = 10000000000000000;
function calculateAmountTokensPurchased(uint256 _amountPaid)
public
view
returns (uint256)
{
console.log("_amountPaid %s", _amountPaid);
console.log("_____priceTokenPerDai %s", priceTokenPerDai);
return priceTokenPerDai / _amountPaid;
}
Ouput
_amountPaid 10000000000000000000000000000000000
_____priceTokenPerDai 10000000000000000
BigNumber { value: "1" }
_amountPaid 1
_____priceTokenPerDai 10000000000000000
I'm a bit confused because decimals in solidity don't exist. I want to make a purchase of 1 cent for 1 token
well in solidity usually most tokens use 18 decimals (this isn't always true so is better to check it), so if you want to set the price of 1 per 0.1, the amount paid should also include the decimals if not you are "sending" 0.000000000000000001 tokens instead of 1

Setting ether value in uint

I have a contract that looks something like this:
contract Contract {
uint public money = 2.5 ether
constructor(...) payable {...}
function setMoney(uint _money) public {
money = _money;
}
}
What to do if I want to set the value of money to 0.2 ether?
2.5 ether translates to 2500000000000000000 (which is 2.5 * 10^18) wei.
The actual value stored in the integer is the wei amount. The ether unit is usually used just to make the code more readable and to prevent human errors in converting decimals.
See documentation for more info.
So if you want to set the value to 0.2 ether, your can pass 200000000000000000 (which is 0.2 * 10^18) to the setMoney() function.

uint multiplication with fractions in solidity

I am trying to get a lower and upper threshold on a value in solidity.
function foo(uint value) public {
uint lower_threshold = value * 0.5;
uint upper_threshold = value * 1.5;
}
With the above code, I get the following error:
TypeError: Operator * not compatible with types uint32 and rational_const 1 / 2
My goal is to check that the value passed is within the threshold to perform some action. Is there a way to do this in Solidity?
As the documentions says Solidity does not fully support decimal operations yet. You have two options there.
You can convert .5 and 1.5 into multiplication and division operations. But as output will be uint you will have precision loss. Ex:
uint value = 5;
uint lower_threshold = value / 2;//output 2
uint upper_threshold = value * 3 / 2;//output 7
You can multiply value with some uint value so that performing
value / 2 won't have any precision loss. Ex:
uint value = 5;
uint tempValue = value * 10;//output 50
uint lower_threshold = tempValue / 2;//output 25
uint upper_threshold = tempValue * 3 / 2;//output 75
if(tempValue >= lower_threshold && tempValue <= lower_threshold) {
//do some stuff
}