# Deploy a Token Contract

## What is ERC20?

ERC20 is the token standard on Ethereum. An ERC20 smart contract manages fungible tokens — meaning every token is identical, with no special privileges or behaviors. In other words, one token owned by someone else has exactly the same value as one token you own. Because of this, ERC20 tokens are commonly used as a medium of exchange, voting, staking, and more.

The ERC20 standard defines 6 required functions and 3 optional functions that a smart contract must implement.

The required functions are as follows:

* `totalSupply`: A method that defines the total supply of the token.
* `balanceOf`: A method that returns the number of tokens held by a wallet address.
* `transfer`: A method that transfers a specific amount of tokens from the caller to another user.
* `transferFrom`: Another type of transfer method used for token transfers between users. Using this method, the sender does not need to call it directly. However, the recipient can only receive tokens within the `allowance` range.
* `approve`: A method to pre-authorize a certain amount of tokens for a specific user to spend.
* `allowance`: A method to check how many tokens one user has authorized another user to transfer.

In addition to the required functions listed above, there are optional functions that improve a token's usability.

* `name`: A method that returns the token's name.
* `symbol`: A method that returns the token's symbol.
* `decimals`: A method that returns the number of decimal places the token uses. It's used to define the token's smallest unit. For example, if an ERC20 token's decimals value is 6, it means the token can be divided down to six decimal places.

To make a token an ERC20 token, you must implement the ERC20 interface, which requires implementing these six methods.

***

## Developing an ERC20 contract

Across Ethereum and other blockchains, many ERC20-compatible tokens have already been deployed. Some implementations prioritize low gas costs, while others focus on enhanced security.

For robust and secure implementations, many developers rely on libraries like [OpenZeppelin](https://www.openzeppelin.com/) or [Solady](https://vectorized.github.io/solady/#/). These libraries provide well-audited, community-verified smart contracts, including ERC20 implementations, making them a trusted choice for token development.

In this example, we’ll use the OpenZeppelin library to build an ERC20 token contract.

***

## Set up the development environment

1. Install [Foundry](https://getfoundry.sh/).

```bash
curl -L https://foundry.paradigm.xyz | bash
foundryup
```

2. Create a Solidity Project

```bash
forge init sample-token
cd sample-token
```

3. Install the OpenZepplin library

```
forge install OpenZeppelin/openzeppelin-contracts
```

***

## Write contracts and tests

{% code title="src/SampleToken.sol" lineNumbers="true" fullWidth="false" %}

```solidity
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.28;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract SampleToken is ERC20 {
    uint256 public constant INITIAL_BALANCE = 1e24;

    constructor() ERC20("Sample", "SMP") {
        _mint(msg.sender, INITIAL_BALANCE);
    }
}
```

{% endcode %}

After writing a contract, you can add test code to verify it behaves as intended.

{% code title="test/SampleToken.t.sol" lineNumbers="true" %}

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Test} from "forge-std/Test.sol";
import {SampleToken} from "src/SampleToken.sol";

contract SampleTokenTest is Test {
    SampleToken public token;

    function setUp() public {
        token = new SampleToken();
    }

    function test_initialBalance() public view {
        assertEq(token.balanceOf(address(this)), token.INITIAL_BALANCE());
    }

    function test_nameAndSymbol() public view {
        assertEq(token.name(), "Sample");
        assertEq(token.symbol(), "SMP");
    }
}

```

{% endcode %}

You can build and test the contract with the following commands.

```bash
forge build
forge test
```

***

## Deploying the token

You can write a deployment script like the following.

{% code title="script/Deploy.s.sol" lineNumbers="true" %}

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import {Script} from "forge-std/Script.sol";
import {SampleToken} from "src/SampleToken.sol";

contract DeploySampleToken is Script {
    function run() public {
        vm.startBroadcast();
        SampleToken token = new SampleToken();
        vm.stopBroadcast();
    }
}

```

{% endcode %}

{% hint style="info" %}
You can learn how to run a deployment script and deploy a smart contract on the GIWA Sepolia Testnet [here](/giwa-chain/en/get-started/smart-contract/develop/foundry.md#undefined-3).
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.giwa.io/giwa-chain/en/get-started/smart-contract/issue-a-token.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
