I want to make a page for direct sale of my token, just like the one in Loomx, with a fixed token price (changeable later by me) and immediately transfer token to buyer securely after purchase.

I try to "steal" their smart contract but it's not verified and public. When I try search online, most are Crowdsale, which seems to be over complicated.

I also found a direct-corwdsale library, but it seems not being used widely.

Here's my code which is very simple:

pragma solidity ^0.4.24;

import 'http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/ownership/Ownable.sol';
import 'http://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC20/ERC20.sol';

contract Directsale is Ownable {

    uint public tokensPerEther;
    ERC20 public token;
    bool public saleClosed = false;

    event FundTransfer(address backer, uint amount);

    constructor (
        uint tokenPerEachEther,
        address tokenAddress
    ) public {
        tokensPerEther = tokenPerEachEther * 1 ether;
        token = ERC20(tokenAddress);

    function closeSale() external onlyOwner {
        saleClosed = true;

    function setTokenPrice(uint tokenPerEachEther) external onlyOwner {
       tokensPerEther  = tokenPerEachEther * 1 ether;

    function () payable public {
        require(msg.value > 0);
        uint amount = msg.value;
        token.transfer(msg.sender, amount * tokensPerEther);
        emit FundTransfer(msg.sender, amount);


Would that be ok and would not cause any security issue?

And, is there any good open-source example including the web3 javascript, so I can study with?

1 Answer 1


Just a quick answer to my own question, I turned out using the CappedCrowdsale.sol and AllowanceCrowdsale.sol from OpenZeppelin. It should be safe and stable enough, my final code is here:

pragma solidity ^0.4.24;

import 'openzeppelin-solidity/contracts/crowdsale/emission/AllowanceCrowdsale.sol';
import 'openzeppelin-solidity/contracts/crowdsale/validation/CappedCrowdsale.sol';
import "openzeppelin-solidity/contracts/ownership/HasNoTokens.sol";

contract CubikCrowdsale is CappedCrowdsale, AllowanceCrowdsale, HasNoTokens {

  event RateChange(uint256 amount);

  constructor (uint256 _rate, address _wallet, ERC20 _token, uint256 _cap) public Crowdsale(_rate, _wallet, _token) CappedCrowdsale(_cap) AllowanceCrowdsale(_wallet) {

  function setRate(uint256 _rate) external onlyOwner {
    rate = _rate;
    emit RateChange(_rate);


For Web3 frontend, I couldn't find a good completed example and eventually wrote my own one here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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