help me write an erc20 contract with a fixed issue of 1,000,000 tokens, half (500,000) of which will belong to the creator of the contract, and the rest can be bought at a price of 0.0012 ETH for one token, always until they are all sold . Something like a stable coin. Ethers must be sent to the contract itself, and the creator can transfer them to another ethereum account at any time.
Hi @andrei,
I recommend reading the documentation on ERC20 and Crowdsales
The following are simple examples of a token and a crowdsale. (based on examples: https://github.com/OpenZeppelin/openzeppelin-contracts/tree/master/contracts/examples)
Deploy both contracts (setting the required rate, wallet address and token address in the crowdsale) and then transfer the required tokens to the crowdsale.
For experimenting in a test environment you could set the rate to 1.
To calculate required rates, please see the documentation: https://docs.openzeppelin.com/contracts/2.x/crowdsales#crowdsale-rate
I haven’t tested the code below other than manually deploying in truffle console.
Any such contracts need thorough and appropriate testing and auditing.
For testing, I recommend following: Test smart contracts like a rockstar
Before an audit I recommend following this checklist: https://blog.openzeppelin.com/follow-this-quality-checklist-before-an-audit-8cc6a0e44845/
OpenZeppelin can perform audits: https://openzeppelin.com/security-audits/
SimpleToken
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20Detailed.sol";
/**
* @title SimpleToken
* @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
* Note they can later distribute these tokens as they wish using `transfer` and other
* `ERC20` functions.
*/
contract SimpleToken is Context, ERC20, ERC20Detailed {
/**
* @dev Constructor that gives _msgSender() all of existing tokens.
*/
constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {
_mint(_msgSender(), 1000000 * (10 ** uint256(decimals())));
}
}
SimpleCrowdsale
pragma solidity ^0.5.0;
import "@openzeppelin/contracts/crowdsale/Crowdsale.sol";
/**
* @title SimpleCrowdsale
* @dev This is an example of a fully fledged crowdsale.
*/
contract SimpleCrowdsale is Crowdsale {
constructor (
uint256 rate,
address payable wallet,
IERC20 token
)
public
Crowdsale(rate, wallet, token)
{
}
}
Where should we declare values for these variables?
You can hard code the values in the contracts constructor or pass them in when you deploy.
For values that don’t change with deployment I prefer to hard code, for values that do change I pass in (also makes testing easier).
Okay, I get the hard code way. Can you share the example of the other way? ( mainly coz I will be changing the rate multiple times during crowd sale). Also, how do I set the rate to a US dollar value? Like for example, I wanna sell the token for $1000 on the starting date, how do I implement that? And again, thanks for your ridiculously prompt response.
If you want to change the rate overtime, you would need to code this functionality or use IncreasingPriceCrowdsale. If you want to change the rate manually, again you would need to code this functionality so that you could change it. Though you would want to be clear with your community when, why and by how much you would change the rate by. A good place to start to see how to do this would be IncreasingPriceCrowdsale.
The Crowdsale contracts accept Ether. If you want to accept a token, you will need to create your own version to accept a stable token. You may want to look at how you could launch your token on a DEX instead.
For anyone else in the community reading this, I suggest looking at Points to consider when creating a fungible token (ERC20, ERC777)
Hi @abcoathup. So, IncreasingPriceCrowdsale won’t work for me but after trying multiple options like Oracalize, I am settled LinkChain to get the current eth price and use Math on it to charge the amount according to our auction. I know I am a broken record, it’s just I am on a tightine but I have considered things as you mentioned while pointing to that article. Update on DAI situation, due to current status, we will be only accepting eth. But, I am still struggling to get a simple Crowdsale up and running without errors. For reference, here is my code.
pragma solidity ^0.5.0;
import '@openzeppelin/contracts/crowdsale/Crowdsale.sol';
contract BooksSale is Crowdsale{
constructor(
uint256 rate,
address payable wallet,
IERC20 token
)
Crowdsale(rate, wallet, token)
public
{
}
}
I have tried multple things to pass values here. Can you share a example where you are inserting the values? I have tried things from Openzeppelin Docs but … not able to get it working in my case.
Hi @PradhumnaPancholi, could you share what version of OpenZeppelin Contracts are you using and what error are you getting? Bear in mind that the crowdsale contracts are not included in Contracts since v3.
I literally figured this out 15 mins before your response. So, here’s what I was doing wrong.
Instead of this,
module.exports = (deployer) => {
deployer.deploy(BooksSale, 1000, '0x6F4B6536cA5bd14631584BE382353e4683843575', '0x7F4E50095C5c059ebb69639c698CB93878202ccE')
}
I was doing this
module.exports = (deployer) => {
deployer.deploy(BooksSale (1000, '0x6F4B6536cA5bd14631584BE382353e4683843575', '0x7F4E50095C5c059ebb69639c698CB93878202ccE'))
}
Which I know is a really stupid mistake. But, I wasn’t able to find an example where someone actually inserts value.
It’s finally fixed now. So, I guess this can be marked as “solved”.
Thank you @martriay and @abcoathup for your help and for maintaining your patience with me.
Glad to hear you were able to solve it!
Well actually this thread is already marked as solved from before haha. Next time consider creating a new post for your question, this helps to build a larger and easy to search question/answer base for future users
8 posts were split to a new topic: Issue compiling Crowdsale