I want to create a smart contract which is able to create tokens, priced at some amount of ether, example uint price = 1 ether;
. Now after someone sends any amount of ether, the same amount of tokens are sent to the sender's ETH account.
Some of the main problems are
1. Since there is no minimum value, the sender might send ether in decimal, so how to store it and send the tokens in decimals?
2. How to create tokens within the smart contract without transferring it to the owner?
I already have seen the code to send tokens after someone sends ether to the smart contract - How to create a token smart contract that sends tokens after recieving payment in ether?
Also, I have the code to issue my own ERC-20 token - https://github.com/bitfwdcommunity/Issue-your-own-ERC20-token/blob/master/contracts/erc20_tutorial.sol
I'm quite advanced in Solidity platform and is still learning so help will be really appreciated.
Here's the code to send tokens after recieving payment in ether:-
uint256 public totalSupply;// total no of tokens in supply
uint remaining;
uint price;
mapping (address => uint) investors //it maps no of tokens given to each address
function token(uint _totalSupply) //constructor
{
totalSupply = _totalSupply;//set initial no of tokens in supply
remaining = 0;
}
function () payable// called when ether is send
{
assert(remaining < totalSupply);
uint noOfTokens = div(msg.value,price); // calculate no of tokens to be issued depending on the price and ether send
assert(noOfTokens < sub(totalSupply,remaining)); //no of tokens available should be greater than the one to be issued
add(investors[msg.sender],noOfTokens);
remaining = add(remaining,noOfTokens);
transfer(msg.sender, noOfTokens);
}
function setPrice(uint _price)
{ // price need to be set maually as it cannot be done via ethereum network
price = _price;
}