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

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.

Related

What is the meaning of comma sequence like ,,,,, in Solidity?

I recently came across the following Solidity function:
function testHealthFactor() public {
(, , , , , uint256 healthFactor) = ILendingPool(lendingPool).getUserAccountData(user);
console.log("health factor", healthFactor);
assertEq(healthFactor < 1 ether, true);
}
I don't know Solidity enought yet, so I wander what is the mining of that sequence of 5 commas?
Solidity allows you to return multiple values within a function. If you don't need these values, you can omit them, and move to the next with the ,.
For example:
function returnStuff() public returns (uint256, uint256) {
return (1, 3);
}
( , uint256 ourNum) = returnStuff();
// ourNum = 3

gas estimation errored with message: "execution reverted: Below agreed payment"

I am having issues when trying to use the Chainlink random number generator and deploying to Rinkeby. Relevant code pieces are the following:
Constructor from the importing contract (should be working fine).
// RandomNumberConsumer parameters for RINKEBY testnet
address _vrfCoordinator = 0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B;
address _link = 0x01BE23585060835E02B77ef475b0Cc51aA1e0709;
bytes32 _keyHash = 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311;
uint256 _fee = 0.1 * 10 ** 18; // 0.1 LINK
constructor() RandomNumberConsumer(_vrfCoordinator, _link, _keyHash, _fee) {}
RandomNumberConsumer.sol. As specified in the chainlink docs, with a few tweaks needed for my approach.
pragma solidity ^0.8.7;
import "#chainlink/contracts/src/v0.8/VRFConsumerBase.sol";
import "#openzeppelin/contracts/access/Ownable.sol";
import "hardhat/console.sol";
contract RandomNumberConsumer is VRFConsumerBase, Ownable{
// Variables
bytes32 internal s_keyHash;
uint256 internal s_fee;
uint256 private constant ROLL_IN_PROGRESS = 150;
mapping(bytes32 => address) private s_rollers;
mapping(address => uint256) private s_results;
//address vrfCoordinator = 0x3d2341ADb2D31f1c5530cDC622016af293177AE0;
//address link = 0xb0897686c545045aFc77CF20eC7A532E3120E0F1;
// Events
event DiceRolled(bytes32 indexed requestId, address indexed roller);
event DiceLanded(bytes32 indexed requestId, uint256 indexed result);
/**
* Constructor inherits VRFConsumerBase
*
* Network: Rinkeby
* Chainlink VRF Coordinator address: 0xb3dCcb4Cf7a26f6cf6B120Cf5A73875B7BBc655B
* LINK token address: 0x01BE23585060835E02B77ef475b0Cc51aA1e0709
* Key Hash: 0x2ed0feb3e7fd2022120aa84fab1945545a9f2ffc9076fd6156fa96eaff4c1311
*/
constructor(address vrfCoordinator, address link, bytes32 keyHash, uint256 fee)
VRFConsumerBase(vrfCoordinator, link){
s_keyHash = keyHash;
s_fee = fee;
}
// Functions
function rollDice(address roller) public onlyOwner returns (bytes32 requestId){
console.log("RNG Contract address",address(this));
// Checking LINK balance
require(LINK.balanceOf(address(this)) >= s_fee, "Not enough LINK in contract.");
// Checking if roller has already rolled dice since each roller can only ever be assigned to a single house. TODO: this can be changed
require(s_results[roller] == 0, "Already rolled");
// Requesting randomness
requestId = requestRandomness(s_keyHash, s_fee); // Error is happening here!
// Storing requestId and roller address
s_rollers[requestId] = roller;
// Emitting event to signal rolling of dice
s_results[roller] = ROLL_IN_PROGRESS;
emit DiceRolled(requestId, roller);
}
// fulfillRandomness is a special function defined within the VRFConsumerBase contract that our contract extends from.
// The coordinator sends the result of our generated randomness back to fulfillRandomness.
function fulfillRandomness(bytes32 requestId, uint256 randomness) override internal {
// Transform the result to a number between 0 and 100, both included. Using % as modulo
require(randomness!=0, "Modulo zero!");
uint256 d100Value = (randomness % 100) + 1; // +1 so s_results[player] can be 0 if no dice has been rolled
// Assign the transformed value to the address in the s_results mapping variable.
s_results[s_rollers[requestId]] = d100Value;
// Emit a DiceLanded event.
emit DiceLanded(requestId, d100Value);
}
// playerWins determines whether the player wins or lose the battle, based on a fixed chance (0-100)
function playerWins (address player, uint8 chance) internal view returns (bool wins){
require(s_results[player] != 0, "Player has not engaged in battle!");
require(s_results[player] != ROLL_IN_PROGRESS, "Battle in progress!");
return s_results[player] <= (chance + 1); //+1 because dice is 1-101
}
}
RNG call from the importing contract(simplified to relevant part only. _player address is working correctly).
address _player = ownerOf(_monId);
rollDice(_player);
I have the certainty that the error occurs inside the rollDice function, more specifically in the call to requestRandomness. Apart from that, I cannot seem to find why the error is hapenning, nor any references to the error message (Below agreed payment) inside any of the dependency contracts. Cannot find any references online either.
Any help is appreciated, thanks.
Below agreed payment error message comes from the sufficientLINK modifier of the VRFCoordinator.sol contract. You can see it here and here.
Double-check the constructor parameters, especially the fee value.
Also, make sure to fund your smart contract with Rinkeby LINK tokens which you can claim from the faucet.

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

Why is my solidity constructor expecting two variables that aren't specified in the parameters?

I am using solidity 0.5.0 My smart contract is expecting two variables in the constructor that aren't in the constructor. rate and wallet. This is my smart contract
contract Crowdsale is Owned{
using SafeMath for uint256;
// The token being sold
IERC20 _token;
// Address where funds are collected
// How many token units a buyer gets per wei.
// The rate is the conversion between wei and the smallest and indivisible token unit.
// So, if you are using a rate of 1 with a ERC20Detailed token with 3 decimals called TOK
// 1 wei will give you 1 unit, or 0.001 TOK.
uint256 rate;
// Amount of wei raised
uint256 _weiRaised;
bool status;
address payable wallet;
/**
* Event for token purchase logging
* #param purchaser who paid for the tokens
* #param beneficiary who got the tokens
* #param value weis paid for purchase
* #param amount amount of tokens purchased
*/
event TokensPurchased(address purchaser, address beneficiary, uint256 value, uint256 amount);
/**
* #param rate Number of token units a buyer gets per wei
* #dev The rate is the conversion between wei and the smallest and indivisible
* token unit. So, if you are using a rate of 1 with a ERC20Detailed token
* with 3 decimals called TOK, 1 wei will give you 1 unit, or 0.001 TOK.
* #param wallet Address where collected funds will be forwarded to
* #param token Address of the token being sold
*/
constructor (IERC20 token) public {
rate = 100;
_token = token;
// address payable token = Token();
wallet = 0x64eCe92B79b096c2771131870C6b7EBAE8C2bd7E;
status = true;
}
This is the error I keep getting
DocstringParsingError: Documented parameter "rate" not found in the parameter list of the function.
,DocstringParsingError: Documented parameter "wallet" not found in the parameter list of the function.
Your constructor takes only one parameter - token. But your docstring describes two additional parameters - rate and wallet:
* #param rate Number of token units a buyer gets per wei
* #param wallet Address where collected funds will be forwarded to
You need to remove them from the list of params specified in the Docstring. I.e. remove the lines or mark them as a regular comment - but not use the #param keyword.

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.