Sunday, April 16, 2023

Creating A Simple Ethereum Token And Deploying In Remix (Ethereum's Browser Based IDE)

If you wanted to dabble in Solidity and Ethereum tokens, you can create a simple token that you can send to you and your friends, or for anything else. Once a wallet has the tokens, while you can have a smart contract that trades Eth for your token, you can just give them away as well, or if you wanted to, have them go through an off-chain process (like a registration on a web site) and then send them the tokens from the main account (which you could automate as well). 

Here's the contract to create a simple token (and yes, this is using an older version, but it works for this simple token and speaks backwards compatibility).

pragma solidity ^0.4.16;

interface tokenRecipient {
    function receiveApproval(address _from, uint256 _value, address _token, 
 bytes _extraData) public;
    }

contract AnhAnhTokenv1 {
    string public name;
    string public symbol;
    uint8 public decimals = 18;
    uint256 public totalSupply;

    mapping (address => uint256) public balanceOf;
    mapping (address => mapping (address => uint256)) public allowance;

    event Transfer(address indexed from, address indexed to, uint256 value);

    function AnhAnhTokenv1 (
        uint256 initialSupply,
        string tokenName,
        string tokenSymbol
    ) public {
        totalSupply = initialSupply * 10 ** uint256(decimals);
        balanceOf[msg.sender] = totalSupply;
        name = tokenName;
        symbol = tokenSymbol;
    }

    function _transfer(address _from, address _to, uint _value) internal {
        // Prevent transfer to 0x0 address. Use burn() instead
        require(_to != 0x0);
        // Check if the sender has enough
        require(balanceOf[_from] >= _value);
        // Check for overflows
        require(balanceOf[_to] + _value > balanceOf[_to]);
        // Save this for an assertion in the future
        uint previousBalances = balanceOf[_from] + balanceOf[_to];
        // Subtract from the sender
        balanceOf[_from] -= _value;
        // Add the same to the recipient
        balanceOf[_to] += _value;
        Transfer(_from, _to, _value);
        // Asserts are used to use static analysis to find bugs in your code. 
// They should never fail
        assert(balanceOf[_from] + balanceOf[_to] == previousBalances);
    }

    function transfer(address _to, uint256 _value) public {
        _transfer(msg.sender, _to, _value);
    }

}

The program creates a basic ERC-20 token with some additional functions and interfaces. Here are some if the main pieces of code to understand.

1. The interface "tokenRecipient" allows external contracts to interact with the token contract, specifically through a function called "receiveApproval".

2. The beginning variables are used to help set values when you are deploying, and creating the token (in the constructor). You'll see these in Remix when you deploy your contract.

3. "balanceOf" is a key/value pair that maps addresses to their balances, which anyone can check. "allowance" as a data structure works similar but the value is another data structure of addresses and values as it checks token that one address has allowed another address to spend on their behalf.

4. The event "Transfer" is emitted to the blockchain when tokens are transferred.

The contract can go anywhere (you can save as a text file or more specifically a ".sol" file) Ultimately though you will put it into Remix.

Compile And Deploy

Got to https://remix.ethereum.org. When you first open it you will see the menu on the left and other information in the main window. You want to focus on the left menu and click on the top icon (which is the File Explorer)

Right-click afterwards on "contracts" and select "New File" from the menu.

You will get a box in contracts to create the file name. Name it "MyTokenTest.sol" and then paste in the contract. 

 
Once you have the file created with the code click on the third menu option which is Solidity compiler. Once you get to that page, make sure you have selected the same compiler version as in the code (sometimes Remix might say it is updating it and it may get close but you can choose the specific version).

After that - you can click on the "Compile MyTokenTest.sol" button and it should compile without any issues which is indicated by the green check mark, versus any errors which you would see under the Compilation Details section. As everything has compiled and is looking good, the only thing left to do is deploy it to a test network! 

To deploy the contract click on the menu item below the compiler:

When you first go to that screen the "ENVIRONMENT" input is set to Remix. 

You want to change that to be MetaMask and then connect to your wallet and select the network (a test network) and an account that has some ether in it.

Next to the orange Deploy button select the down arrow and you will see the parameters to fill in for your token:

Initial supply is the amount you want of your tokens without commas: 1000000.

Token name is the name of the token: Anh Anh Token

And the token symbol is: AANHTI.


Everything else can be left the same and then just press on the transact button and MetaMask will open, confirming the transaction--which is creating a new contract on the Ethereum test blockchain. Remix will then also show the log as well.

After the transaction is complete you will see a line in your wallet like this:


When you click on that entry you can click to view the entry on the "blockchain explorer" (which is Etherscan). 

The entry should look similar to the one below:


The From should be the address you were connected to when you deployed it in Remix. When you click on the To link that will take you to the Contract Address for the token you created (tokens can only be created via smart contracts). That page will show you information about the deployment/transaction as well as to drill into your token.

It's not the most advanced contract, but it's an ERC-20 token and everything works like it should. You can check out the actual token for this post here: https://goerli.etherscan.io/address/0xf3ab79837d63c4105f15b4d777c407d176c1760b