💓 If you enjoyed the information and the web site, please donate or become a Patreon.
💹 Join Robinhood with my link and we will both pick a free stock.
🌀 Code Your Token
- Technologies used: Remix, Solidity, MetaMask, Uniswap, ERC-20 Token Standard
- You can start with reviewing the code. Here is the your token’s code using the ERC-20 standard:
- I named my token my name backwards
Esyle
. You can changeEsyleERC20
(change the Esyle part and leave ERC20, for example CalvinERC20),Esyle
, andESYLE
to your token’s name.
- I named my token my name backwards
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract EsyleERC20 {
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
string public constant name = "Esyle"; # change to your token
string public constant symbol = "ESYLE"; # change to your token
uint8 public constant decimals = 18;
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
uint256 totalSupply_;
constructor(uint256 total) {
totalSupply_ = total;
balances[msg.sender] = totalSupply_;
}
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
function balanceOf(address tokenOwner) public view returns (uint) {
return balances[tokenOwner];
}
function transfer(address receiver, uint numTokens) public returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] -= numTokens;
balances[receiver] += numTokens;
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
function approve(address delegate, uint numTokens) public returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
function allowance(address owner, address delegate) public view returns (uint) {
return allowed[owner][delegate];
}
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
require(numTokens <= balances[owner]);
require(numTokens <= allowed[owner][msg.sender]);
balances[owner] -= numTokens;
allowed[owner][msg.sender] -= numTokens;
balances[buyer] += numTokens;
emit Transfer(owner, buyer, numTokens);
return true;
}
}
💳 Here is $10 off your first purchase with Afterpay. Shop now and pay later in 4 interest-free payments.
🧠 Understanding The Code
You just wrote your own crypto token using the ERC-20 standard. Here is a break down of each piece of the code:
- You set the license identifier and the version of Solidity. The version of Solidity is 0.7.0 to 0.9.0.
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
- You declared the events Transfer and Approval:
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
- Set the name of your token, its symbol, and the decimal to use:
string public constant name = "Esyle";
string public constant symbol = "ESYLE";
uint8 public constant decimals = 18;
Your token name is Esyle and the symbol is ESYLE. You can change this in your code.
You have two mappings declared. A mapping in Solidity is similar to a key-value pair. So in the balances, an address is the key while the unit256 (unsigned integer of 256 bits) is the value:
mapping(address => uint256) balances;
mapping(address => mapping (address => uint256)) allowed;
In the Solidity documentation, an address type is a 160-bit value that does not allow any arithmetic operations. It is suitable for storing addresses of contracts or a hash of the public half of a key pair belonging to external accounts.
The balances maps an address to a unit256 int:
Address unit256 0x01 23 0x02 10 0x03 1 An address refers to its balance. The allowed mapping is also a key-value pair that maps addresses to another mapping. This maps addresses to their unit256 values. It allows you to store the number of tokens that can be transferred to a recipient.
This stores the number of tokens that are available in your contract:
uint256 totalSupply_;
- The constructor is called when the class is being created. In smart contracts, the constructor is called when the contract is deployed to the network. The constructor is called with the total number of tokens you want to be in your contract. The total is set to totalSupply_, and the balance of the deploying address is set to the total tokens. The msg.sender contains the Ethereum account of the currently executing contract function.
constructor(uint256 total) {`
totalSupply_ = total;
balances[msg.sender] = totalSupply_;`
}
- tokenOwner is an argument for the address of the token owner to whom you want to show the balance of the token in the contract. The method gets the balance by referencing the tokenOwner address from the balances.
function balanceOf(address tokenOwner) public view returns (uint) {
return balances[tokenOwner];
}
- The transfer method has the following arguments:
- receiver - the address of the account that will receive tokens
- numTokens - the number of tokens that will be sent to the receiver account
- The numTokens is subtracted from the deployer’s balance and credited to the receiver’s balance. Then a Transfer event is sent out. Next the boolean true is returned.
- You can see that a check is made to verify that the number of tokens to be sent to the recipient is enough according to the deployer’s address balance.
function transfer(address receiver, uint numTokens) public returns (bool) {
require(numTokens <= balances[msg.sender]);
balances[msg.sender] -= numTokens;
balances[receiver] += numTokens;
emit Transfer(msg.sender, receiver, numTokens);
return true;
}
- This arguments called delegate and numTokens:
- delegate is the address you want to set the number of tokens that the deployer can send to the address
- numTokens is the number of tokens the deployer can send to the delegate
- You reference the delegate map in the allowed mapping to set the number of tokens to it. Then, send the Approval event and return true.
function approve(address delegate, uint numTokens) public returns (bool) {
allowed[msg.sender][delegate] = numTokens;
emit Approval(msg.sender, delegate, numTokens);
return true;
}
- The arguments owner and delegate mean:
- owner is the address to return the number of tokens transferable to the recipient in the delegate
function allowance(address owner, address delegate) public view returns (uint) {
return allowed[owner][delegate];
}
- transferFrom has arguments called owner, buyer, and numTokens:
- owner is the address of the balances from which we will transfer the numTokens
- buyer is the address in the balances that you will credit the numTokens
- numTokens is the number of tokens to be transferred from owner to buyer
- You must first check whether the balance in the owner is enough and whether the owner is approved to send that amount of tokens to the buyer.
- The Transfer is made by subtracting the number of tokens from the owner’s balance and allowed balance. The number of tokens is added to the buyer’s balance. The Transfer event is sent out and the boolean true is returned.
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) {
require(numTokens <= balances[owner]);
require(numTokens <= allowed[owner][msg.sender]);
balances[owner] -= numTokens;
allowed[owner][msg.sender] -= numTokens;
balances[buyer] += numTokens;
emit Transfer(owner, buyer, numTokens);
return true;
}
💶 Earn Bitcoin by viewing ads at CoinPayU
🥏 Deploy Your Created Token
You will be using Remix to deploy your token to the Ropsten Testnet Network and your MetaMask wallet. When you deploy your contract on the real Ethereum network, it costs real money. While learning and practicing, use the test network and free fake ETH to deploy your contracts.
Install the MetaMask extension on your desktop browser and create an account.
Go to Remix and create a new .sol file under the contracts folder. For the example, it is called Esyle.sol. You can name your smart contract whatever you want. Paste your whole smart contract code from above:
You need free test ETH. You will get free test ETH from a Ropsten Test Network Faucet. Connect your MetaMask wallet account to the site. Choose your account and receive 0.01005 ROPSTENETH to deploy your smart contract. Now you can deploy your smart contract to the Ropsten Test Network.
Go back to the Remix dashboard and click on the Ethereum logo in the dashboard. This will load a page where you can deploy and run transactions.
On the left sidebar, see that your .sol contract is already selected and the account in your MetaMask is already set as the deploying address. In the ENVIRONMENT, you will see that JavaScript VM (London) is already selected. Change it so your deploying network will be the Ropsten Testnet Network. Select Injected Web3. You should see (0.3 ether) in the ACCOUNT section right beside your deploying address.
Click the button on the left to compile the contract. This makes sure the coding is accurate.
Enter the original amount of tokens that will be traded for your token. Enter this in the input beside the Deploy button. Enter 100 and click the Deploy button. This will open your MetaMask and ask you to confirm the pending transaction. Click the Confirm button on the MetaMask popup.
This will deploy your smart contract on the Ropsten Test Network.
Check to make sure the smart contract is deployed with no errors.
On your Remix page, scroll down on the left sidebar, you will see the names of methods in your smart contract and an inputs beside them. This is where you can run the methods in your smart contract and get results. You can check the balance of your deploying address and it should return 100:
You have successfully created and deployed an ERC-20 token on an Ethereum test network.
- Here is the transaction on the Ropsten Testnet Network Etherscan. The Etherscan is where all blockchain transactions are logged and you can input your smart contract address to verify the transaction.
🔑 Keep your crypto secure with a Ledger wallet.
🥇 Add Your Token to Uniswap and Your MetaMask Wallet
Your token is compatible with Uniswap. People buy tokens on decentralized exchanges like Uniswap. Your token will now show up on Uniswap under the Ropsten network.
Click on Manage Token Lists, then Tokens. Copy and paste your token address from the Contract Created from the contract’s etherscan link.
The token will auto populate and click Import.
Next you will import the token into your MetaMask wallet. Scroll down on the MetaMask wallet extension until you see Import tokens on the bottom. Take that same contract address from your Etherscan link and put it into Token Contract Address. The Token Symbol and Token Decimal will auto populate.
Now the token will show on your main wallet page.
🔶 Summary
You created your own token on the Ropsten Testnet Network. You also imported your token into Uniswap and your MetaMask wallet. Take this knowledge and create more tokens for fun.
💓 If you enjoyed the information and the web site, please donate.
💸 Peer to peer bitcoin lending through BTCPop
Elyse Y. Robinson is the Founder of Switch Into Tech where she does monthly seminars, posts weekly freebies to help you switch into tech, Writer of Nube: Switch Into A Cloud Career, podcaster for Nobody Wants To Work Tho, creator of FullTuitionScholarships.org to help you not go into college debt, and in school for Data Science. Elyse is in love with Mexico, researching any and everything, and helping people switch into tech.