# Simple ERC20 token example

**URL:** <https://forum.openzeppelin.com/t/simple-erc20-token-example/4403>\
**Category:** Guides and Tutorials\
**Created:** [November 5, 2020, 12:43am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403 "2020-11-05T00:43:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![abcoathup](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/abcoathup/32/415_2.png) [@abcoathup](https://forum.openzeppelin.com/u/abcoathup)\
**Post date:** [November 5, 2020, 12:43am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/1 "2020-11-05T00:43:36Z")

</div>

# Simple ERC20 token example.

⚠ The following code has not been tested nor audited.  
Seek appropriate advice on **regulatory compliance** and your solution should have appropriate **testing** and **auditing**

Recommend reading: [Points to consider when creating a fungible token (ERC20, ERC777)](https://forum.openzeppelin.com/t/points-to-consider-when-creating-a-fungible-token-erc20-erc777/2915)

❗ There is an open issue to add a fixed supply ERC20 preset: [https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2377](https://github.com/OpenZeppelin/openzeppelin-contracts/issues/2377)

This example smart contract merely sets a name, symbol and intial supply. All tokens are pre-assigned to the creator using the internal mint function.

## SimpleToken.sol

```auto
// contracts/SimpleToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.2;

import "@openzeppelin/contracts/token/ERC20/ERC20.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.
 * Based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/examples/SimpleToken.sol
 */
contract SimpleToken is ERC20 {
    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor(
        string memory name,
        string memory symbol,
        uint256 initialSupply
    ) public ERC20(name, symbol) {
        _mint(msg.sender, initialSupply);
    }
}

```

## Migration (Truffle)

`2_deploy.js`

```auto
// migrations/2_deploy.js
const SimpleToken = artifacts.require('SimpleToken');

module.exports = async function (deployer) {
  await deployer.deploy(SimpleToken, 'SimpleToken', 'SIM', '10000000000000000000000');
};

```

## Test

### SimpleToken.test.js

```auto
// test/SimpleToken.test.js
// SPDX-License-Identifier: MIT

// Based on https://github.com/OpenZeppelin/openzeppelin-solidity/blob/v2.5.1/test/examples/SimpleToken.test.js

const { expect } = require('chai');

// Import utilities from Test Helpers
const { BN, expectEvent, expectRevert, constants } = require('@openzeppelin/test-helpers');

// Load compiled artifacts
const SimpleToken = artifacts.require('SimpleToken');

// Start test block
contract('SimpleToken', function ([creator, other]) {

  const NAME = 'SimpleToken';
  const SYMBOL = 'SIM';
  const TOTAL_SUPPLY = new BN('10000000000000000000000');

  beforeEach(async function () {
    this.token = await SimpleToken.new(NAME, SYMBOL, TOTAL_SUPPLY, { from: creator });
  });

  it('retrieve returns a value previously stored', async function () {
    // Use large integer comparisons
    expect(await this.token.totalSupply()).to.be.bignumber.equal(TOTAL_SUPPLY);
  });

  it('has a name', async function () {
    expect(await this.token.name()).to.be.equal(NAME);
  });

  it('has a symbol', async function () {
    expect(await this.token.symbol()).to.be.equal(SYMBOL);
  });

  it('assigns the initial total supply to the creator', async function () {
    expect(await this.token.balanceOf(creator)).to.be.bignumber.equal(TOTAL_SUPPLY);
  });
});

```

## Crowdsale

If you are looking for a crowdsale example, see: [Simple ERC20 Crowdsale](https://forum.openzeppelin.com/t/simple-erc20-crowdsale/4863).

---

<div class="post-metadata">

**Author:** ![crypto\_economics](https://avatars.discourse-cdn.com/v4/letter/c/ed8c4c/32.png) [@crypto\_economics](https://forum.openzeppelin.com/u/crypto_economics)\
**Post date:** [November 5, 2020, 3:20pm UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/2 "2020-11-05T15:20:34Z")

</div>

@abcoathup this is great! I actually managed to successfully write 5 tests last night after 10 painful days of mental brincsmanship. Your example is extremely helpul – i will take a good look at these. Thank you so much!!

---

<div class="post-metadata">

**Author:** ![MaxflowO2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxflowo2/32/8022_2.png) [@MaxflowO2](https://forum.openzeppelin.com/u/MaxflowO2)\
**Post date:** [August 7, 2021, 2:00am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/4 "2021-08-07T02:00:16Z")

</div>

Tried this, caught the ^0.8.0 error...

Now throws a constructor error..

---

<div class="post-metadata">

**Author:** ![Skyge](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/skyge/32/343_2.png) [@Skyge](https://forum.openzeppelin.com/u/Skyge)\
**Post date:** [August 7, 2021, 2:14am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/5 "2021-08-07T02:14:00Z")

</div>

Hi, welcome! 👋

> [@abcoathup](#):
>
> `pragma solidity ^0.6.2;`

The case that shows above uses the compiler version of `0.6.x`, but the latest version of the dependency `@openzeppelin/contracts` uses `0.8.x`, so I think you should run this command to install a right dependency:

```auto
npm i @openzeppelin/contracts@3.4.0

```

---

<div class="post-metadata">

**Author:** ![MaxflowO2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxflowo2/32/8022_2.png) [@MaxflowO2](https://forum.openzeppelin.com/u/MaxflowO2)\
**Post date:** [August 7, 2021, 2:23am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/6 "2021-08-07T02:23:47Z")

</div>

```auto
// contracts/SimpleToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.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.
 * Based on https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.1/contracts/examples/SimpleToken.sol
 */
contract SimpleToken is ERC20 {
    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor(string memory name, string memory symbol, uint256 initialSupply) ERC20(name, symbol) {
      _mint(msg.sender, initialSupply);

    }
}

```

There's the update for you

also launched on Rinkeby...

> **[SimpleToken (SIM) Token Tracker | Etherscan](https://rinkeby.etherscan.io/token/0x3bd7d345b9eb37cee32ac42b64bb5601eda93a7f#readContract)**
>
> SimpleToken (SIM) Token Tracker on Etherscan shows the price of the Token $0.00, total supply 10,000, number of holders 1 and updated information of the token. The token tracker page also shows the analytics and historical data.

---

<div class="post-metadata">

**Author:** ![ffernandomaximo](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/ffernandomaximo/32/9570_2.png) [@ffernandomaximo](https://forum.openzeppelin.com/u/ffernandomaximo)\
**Post date:** [October 14, 2021, 8:10am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/7 "2021-10-14T08:10:11Z")

</div>

Did you start your local blockchain?  
I was getting the same error, then configured the truffle-config.js and started Ganache.

---

<div class="post-metadata">

**Author:** ![MaxflowO2](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/maxflowo2/32/8022_2.png) [@MaxflowO2](https://forum.openzeppelin.com/u/MaxflowO2)\
**Post date:** [October 15, 2021, 5:20am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/8 "2021-10-15T05:20:50Z")

</div>

Yeah I got this, tossed it on Rinkeby before the faucet shut off. Just needed to update the constructor to the 0.8.x standards.

---

<div class="post-metadata">

**Author:** ![parin13](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.openzeppelin.com/parin13/32/231_2.png) [@parin13](https://forum.openzeppelin.com/u/parin13)\
**Post date:** [September 9, 2022, 10:19am UTC](https://forum.openzeppelin.com/t/simple-erc20-token-example/4403/9 "2022-09-09T10:19:33Z")

</div>

this : ` new BN('10000000000000000000000');` still gives error !!

```
 Error: Number can only safely store up to 53 bits

```
