Build your own Ethereum Token with Solidity
By Samay Shamdasani
What is an Ethereum Token?
Ethereum is an open software toneel based on blockchain technology that enables developers to build and deploy decentralized applications. Many dApps have their own token built on top of the Ethereum network to crowdfund or run the dApps.
Te this tutorial, we’ll be building a ERC20 standard token that is capable of the following:
- checking balances
- transfer tokens
- transfer tokens from an address to another
- approve account(s) to withdraw tokens
- display amount approved for withdrawal inbetween two accounts
Writing our Token Contract
Wij will be programming te Solidity, the language of the Ethereum network created for clever contracts. Open up a text editor and create a opstopping with your token name and the .sol verkeersopstopping extension at the end. Note that this token contract tutorial is based on the ERC20 implementation from Token Factory.
Let’s write a ERC20Token contract by proclaiming all our methods:
Here, wij define the functions of the ERC20 Token standard spil mentioned above. One can see that totalSupply() comebacks a uint , or value of tokens te circulation. balanceof() and allowance() also terugwedstrijd integers, while transfer() , transferFrom() , and approve() terugwedstrijd boolean (true/false) values. The parameters of thesis functions have an address field which is the ETH public key of a user. The two event methods emit details of transfers and approvals.
Solidity uses contract inheritance, so let’s create another contract to implement the methods we’ve defined. Te this contract, we’ll need to begin off by creating an array, or mapping addresses to balances. Along with our balances array, wij also need an permitted array to indicate how much of our token an address can withdraw from another address. Lastly, let’s also define a public oprecht totalSupply which will hold the amount of tokens wij will have te circulation.
Adding to this contract, wij need to implement the surplus of our methods. Our transfer() function takes two parameters: the address wij are sending our token to ( address _to ), and the amount wij are sending ( uint256 _value ). Wij will be returning a bool value (true/false) to indicate whether our tokens have bot transferred successfully or not.
Te our function, wij have to deduct the amount of tokens spent by the sender and add the token value to the destination address. However, wij will do this only when certain conditions are met. So, we’ll use a if statement that will transfer the tokens only if the sender’s balance is more than or equal to the value of tokens being sent and if that value if overheen 0. Otherwise, the function will terugwedstrijd false . After wij finish the transfer, wij emit the details of the transaction with the Transfer event.
Our transferFrom method is almost identical from the one above, but since wij are transferring tokens from one address to another, wij need to add an address _from parameter. Wij also need to check if the number of tokens being transferred is approved.
To implement our balanceOf() method, wij just need to terugwedstrijd the value stored te the balances array for the address inputted.
There are two remaining methods to develop a ERC20 standard token. The approve() method permits allowances to be added for the sender to withdraw tokens from a certain address. It will call the Approval() event to emit the details of the approval.
Lastly, wij can terugwedstrijd the allowance an address can withdraw of another address.
To initialize our token, wij can create another contract inherited from the Token contract with our token name. For this example, I’m going to be creating 1,000 Enlight, or ENLT tokens.
Wij want to make sure that, since this isn’t an ICO, if ether is sent to this address, wij send it back. Ter this contract, wij will also be defining our token name, number of decimals to voorstelling, and the token symbol.
The approveAndCall() method runs when the voeling is authorized to transfer an amount of tokens from the sender account, and the function receiveApproval() is called te the receiving contract where the transfer can be done.
Publishing our contract to the Testnet
Now that we’ve written our token contract, wij can deploy it to the Ethereum testnet.
Download the Metamask extension. This extension will help us connect to the test blockchain lightly.
Once it installs, create a password. On the top left of the extension, connect to the Ropsten Test Network. Since wij are on the test network, wij will need some test ether to deploy our contract. Go to this faucet to get 1 free test Ether.
Now, go to Ethereum’s Solidity IDE so wij can deploy the contract. Copy the contract code and click on the run tabulator up top. Make sure your environment is set to Injected Web3 and then select on your token contract and click create. Metamask will now speelpop up asking to accomplish the transaction of paying for fees. Once you click submit, your contract will be published.
If you go to the “sent” tabulator on Metamask, you will be able to see that our contract is published. Click on the contract photo to pull up the testnet explorer.
Checking our balance ter Metamask
On the explorer pagina, copy the contract address. To add the tokens to our wallet, go to Metamask, select the token tabulator, and then click on add tokens. Copy the Token contract address and click add. You’ll now be able to see that you own X amount of your token!
Sending and Receiving Tokens
To interact with your contract, you could build a dApp that would use the clever contract spil an API, but the easiest way to send and receive tokens for now would be to use a dApp called Token Factory. Simply inject your contract address and you have access to all the methods wij programmed on our contract. If you want to test transactions, attempt creating another account on Metamask (top right) and send some tokens to that address (you can copy the address by clicking on the three dots next to the account name).
If you want some ENLT, druppel a reply here and I’ll send you some!
Looking at Etherscan
If wij take a look at ENLT’s pagina on Etherscan, wij can see the total supply spil well spil all the token transfers that have taken place and all the token holders!
Coming soon will be a total fledged dApp built on the Ethereum network that will utilize wise contracts. Feel free to discuss this project on the community forum below.