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
Related
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.
I have a problem. The problem is that i want a max mint supply for the whitelist sale and public sale. So for example;
In total i have 10.000 NFT's. There will be an whitelist sale and a public sale. For the whitelist sale i have 2.000 wallet addresses, but i only want them to be able to mint 1.500 NFT's. and in the public sale i want the remaining 8.500 NFT's to be sold.
I already tried somethings with the normal maxsupply but can't figure it out. I want a max mint per wallet like 10 and cant seem to limit the supply to 1500 for only the whitelist
Does anyone can explain this to me or have a code example?
I don't recommend limiting your whitelist per-wallet minting amount (you want to mint out, don't you?!), but I understand the reasons why you might. I'll provide both options. All code is abbreviated for brevity sake:
*Note 1: We will be using OpenZeppelin Utilities - Counters for tracking minted progress. You can also consider using totalSupply(), however, if burned tokens are a concern totalSupply() will decrement and throw off your count, whereas Counters will not.
Note 2: This assumes you're whitelist occurs before public and that you're not also juggling a reserve count as well - additional checks and counters would be required for that.
Note 3: This covers ONLY the check for limiting whitelist; you will obviously also need additional checks for valid whitelist account, sufficient payment, etc.
Limit Whitelist Total Supply
...
import "#openzeppelin/contracts/utils/Counters.sol";
...
error ExceededWhitelistSupply();
...
using Counters for Counters.Counter;
uint256 public maxSupply = 10000;
uint256 public maxWhitelistSupply = 1500;
Counters.Counter private totalWhitelistSupply;
...
function mintWhitelist(uint256 _qty) external payable {
if ( totalWhitelistSupply.current() + _qty > maxWhitelistSupply ) revert ExceededWhitelistSupply();
for (uint256 i = 0; i < _qty; i++) {
totalWhitelistSupply.increment();
}
_mint(msg.sender, _qty, '', true);
}
Limit Wallet && Limit Whitelist Total Supply
...
import "#openzeppelin/contracts/utils/Counters.sol";
...
error ExceededWhitelistSupply();
error ExceededMaxPerWallet();
...
using Counters for Counters.Counter;
uint256 public maxSupply = 10000;
uint256 public maxWhitelistSupply = 1500;
uint256 public maxWhitelistPerWallet = 10;
Counters.Counter private totalWhitelistSupply;
mapping(address => uint256) public whitelistMintedAmount;
...
function mintWhitelist(uint256 _qty) external payable {
if ( whitelistMintedAmount[msg.sender] + _qty > maxWhitelistPerWallet ) revert ExceededMaxPerWallet();
if ( totalWhitelistSupply.current() + _qty > maxWhitelistSupply ) revert ExceededWhitelistSupply();
for (uint256 i = 0; i < _qty; i++) {
totalWhitelistSupply.increment();
}
whitelistMintedAmount[msg.sender] += _qty;
_mint(msg.sender, _qty, '', true);
}
Here, we've used mapping - good tutorial here - to track the number of NFTs that have been minted to this wallet (preferred method, as this won't be fooled by the account transferring NFTs out of the wallet and then minting more). If you want to go WAY down the rabbit hole, you can also look at trash-canning this whole approach and learn up on this approach for handling your whitelist.
Keep in mind that there are more checks and balances that you'll need to add (e.g., visualizing this in the front end of your dApp to avoid minting when they shouldn't be able to, additional validation layers in your mint functions, etc.), but this should provide you with the core pieces needed for limiting by a max wallet and max supply. I apologize for any code errors - this is my first StackOverflow answer and the short-handing and readability of the code is a bit difficult to error check.
Have a whitelistMaxSupply
Have a getMaxSupply function
Have a finishWhitelist function
Have a maxSupply
Unless whitelist finished, getMaxSupply will return whitelistMaxSupply, after finished, it will return maxSupply
Profit
There are many ways to solve your problem, this is just the first one that came to my mind
function getMaxSupply() view public returns(uint256){
if(whitelistFinished){
return maxSupply;
}
return whitelistMaxSupply;
}
function finishWhitelist() public{
whitelistFinished = true;
}
This is probably an easy error I'm missing, but I cannot for the life of me figure out how to set the msg.value variable in this contract. I've read online that this value is the amount of wei associated with the transaction, but how do I, as a caller of the contract, specifically set that value. Here's the contract I'm struggling with.
pragma solidity 0.8.7;
contract VendingMachine {
// Declare state variables of the contract
address public owner;
mapping (address => uint) public cupcakeBalances;
// When 'VendingMachine' contract is deployed:
// 1. set the deploying address as the owner of the contract
// 2. set the deployed smart contract's cupcake balance to 100
constructor() {
owner = msg.sender;
cupcakeBalances[address(this)] = 100;
}
// Allow the owner to increase the smart contract's cupcake balance
function refill(uint amount) public {
require(msg.sender == owner, "Only the owner can refill.");
cupcakeBalances[address(this)] += amount;
}
// Allow anyone to purchase cupcakes
function purchase(uint amount) public payable {
require(msg.value >= amount * 1 ether, "You must pay at least 1 ETH per cupcake");
require(cupcakeBalances[address(this)] >= amount, "Not enough cupcakes in stock to complete this purchase");
cupcakeBalances[address(this)] -= amount;
cupcakeBalances[msg.sender] += amount;
}
}
Every time I enter an amount, I'm getting thrown the error that says "You must pay at least 1 ETH per cupcake"
There's nowhere for me to specifically enter in a value for how much I'm going to pay for this, any help would be great
here's what I'm able to input when I deploy the contract on Remix
Top of the Deploy Button you can see the Value Field :
when you want to call the purchase , first fill the value field and select Ether after that calls your function.
I try this way with your code and it works fine.
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.
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.