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
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;
  • if you want to know more about erc-20 token. then i would recommend you to go to this github.com/OpenZeppelin/openzeppelin-solidity you can add the uint8 private _decimals.in your smart contract.github.com/OpenZeppelin/openzeppelin-solidity/blob/master/… .or you can change the rate of your token specifically for the users. and for the 2nd part you can make another account with the name of wallet.and then tranfer the funds to that account. for more info about it look at refundable contracts.which uses soft cap system to do this
    – AVATAR
    Commented Oct 6, 2018 at 9:02

3 Answers 3


Another way to create a secure contract is to add SafeBlocks Firewall. It lets you create rules and limits that protect transactions in real-time.

  • Never asked for secure contract. 😅 Commented Oct 7, 2018 at 11:56

Your first problem is not actually a problem at all. You can't send "decimal" Ethers, Ethers are always in the amount of wei which is the smallest unit of Ether. So if a function receives 56 units of Ether, then it means it receives 56 weis of Ether (0.000000000000000056 Ethers). It's impossible to transfer decimal weis.

Therefore, whenever someone sends you any amount of weis, I assume you wish to send them the same amount of tokens.

Tokens are also always transferred in integer units, not decimals. The only difference is the decimals definition of your token which states how many decimal points your tokens have. If you use the same (18) as Ether your conversions are trivial - 1 wei for 1 token. You can read more about decimals here for example: Ethereum tokens and decimals

Now, as for your second questions, it depends on how you want the token contract to behave. You have two possible approaches:

1) If you have a static total supply (it doesn't ever change) then all the tokens are initially typically owned by the token contract. So when someone buys tokens, you transfer them from the contract's amount of tokens

2) If you want to create tokens on-the-fly whenever someone buys them (non-fixed total supply, called minting) you can have a look at different minting implementations. Basically they just create tokens from thin air (from nothing) when needed. So when someone sends your contract Ethers (weis) the contract mints tokens as needed, increases the total supply and transfers those tokens to the buyer.


I believe the best move is to pick one of existing reliable crowdsale contracts and either use it as-is, or inherit it and add minimum of alterations. Check https://github.com/OpenZeppelin/openzeppelin-solidity/tree/master/contracts/crowdsale and pick what suits you best. If you want to mint your tokens when receiving Ether from participants, then MintedCrowdsale.sol is your thing


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.