Sale Setup

Tutorial 1 of 4

Introduction

Goal of the tutorial

In this tutorial, we will guide you through the process of setting up a token sale. A token sale, also known as an initial coin offering (ICO), is a fundraising mechanism in which new projects sell their underlying crypto tokens in exchange for bitcoin and ether.

Learning Objectives

By the end of this tutorial, you will be able to:

  • Understand the factors to consider when setting up a token sale
  • Set up a token sale that meets your project goals

Prerequisites

Before you start, you should have a basic understanding of blockchain technology, cryptocurrencies, and smart contracts. Familiarity with Solidity, a popular language for writing smart contracts on Ethereum, is also recommended.

Step-by-Step Guide

Conceptual Understanding

A token sale involves the creation and sale of digital tokens to fund new projects. The first step in setting up a token sale is to define your project's goals and how much funding you aim to raise. You also need to decide on the type of token to issue (security or utility), the token price, and the sale duration.

Best Practices and Tips

  • Clear communication: Ensure your project details, goals, and token information are clear and easily accessible to potential investors.
  • Legal compliance: Make sure your token sale complies with relevant regulations.
  • Smart contract security: Ensure your smart contract code is secure and tested thoroughly to prevent hacks and loss of funds.

Code Examples

Here is an example of a simple ICO smart contract using Solidity:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {
    constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
        _mint(msg.sender, initialSupply);
    }
}

contract MyTokenSale {
    address payable admin;
    MyToken public tokenContract;
    uint256 public tokenPrice;
    uint256 public tokensSold;

    event Sell(address _buyer, uint256 _amount);

    constructor(MyToken _tokenContract, uint256 _tokenPrice) {
        admin = payable(msg.sender);
        tokenContract = _tokenContract;
        tokenPrice = _tokenPrice;
    }

    function multiply(uint x, uint y) internal pure returns (uint z) {
        require(y == 0 || (z = x * y) / y == x);
    }

    function buyTokens(uint256 _numberOfTokens) public payable {
        require(msg.value == multiply(_numberOfTokens, tokenPrice));
        require(tokenContract.balanceOf(address(this)) >= _numberOfTokens);
        require(tokenContract.transfer(msg.sender, _numberOfTokens));

        tokensSold += _numberOfTokens;

        emit Sell(msg.sender, _numberOfTokens);
    }

    function endSale() public {
        require(msg.sender == admin);
        require(tokenContract.transfer(admin, tokenContract.balanceOf(address(this))));

        // Just to transfer the balance to the admin
        admin.transfer(address(this).balance);
    }
}

Summary

In this tutorial, we covered the basics of setting up a token sale, including the factors to consider, how to set up a sale that meets your goals, and a code example of a simple ICO smart contract.

Practice Exercises

  1. Modify the smart contract code to include a cap on the maximum number of tokens that can be sold.
  2. Add a function to the smart contract that allows the admin to adjust the token price.
  3. Create a function that allows token holders to refund their tokens and get their ether back.

Remember, practice is key in understanding and mastering smart contracts and Solidity.

Additional Resources