How to Make and Upload Your Own Smart Contract

Ever thought nearly making your own crypto? Now you can, and it'southward way easier than yous recall.

This tutorial is going to teach you how to write and deploy a smart contract on Ethereum, and that smart contract will contain all the rules of your token. By the stop of information technology you'll exist able to run into your new token in your crypto wallet and transfer to other wallets!

Token vs coin

Creating your own coin may seem like an extremely complicated task - and you're right. Today we won't be creating a coin, we'll be creating a token.

What's the deviation?

A coin has its own blockchain. Bitcoin operates on the Bitcoin blockchain, Ethereum operates on the Ethereum blockchain, and Dogecoin operates on the Dogechain blockchain. They each accept their own transaction ledger.

A token, nonetheless, is built on an existing blockchain. They practise not have their own transaction ledger, and instead are simply smart contracts that are deployed to a blockchain. A token can represent literally annihilation - lottery tickets, a painting, an ounce of golden, XP points in a video game, yous proper name it. Some of these examples are non-fungible tokens (or NFTs), which means that they all have different values and are not equal to i another. Just that's not what we want from a currency - my dollar should exist equal in value to your dollar.

That's where something called ERC-20 comes in. ERC-20 introduces a standard for fungible tokens, i.e. tokens that are equal in value. 1 of my token will be equal to i of yours. ERC-20 tokens are congenital on the Ethereum network, and act similarly to $ETH in this manner. An ERC-20 token has other characteristics too, which yous can check out here. In this tutorial we will be edifice an ERC-20 token.

Installing metamask

Before standing with any sort of web3 evolution, y'all'll need to install Metamask. It's a crypto wallet browser extension that allows you to interact with blockchain apps. Install that to your called browser and follow the steps to set an Ethereum wallet. When you lot're done, you should see something like this:

Metamask extension

We'll exist deploying our smart contract on the Kovan testnet for this tutorial. A testnet is a network that simulates Ethereum, and so you can deploy and test your code on the blockchain without using existent assets. You'll need to connect your Metamask to Kovan from the drop-downward at the height (if you've just installed Metamask this dropdown volition default to Ethereum Mainnet).

Fun fact: there are 5 testnets currently in use, and they're all named after subway stations effectually the earth.

The next step is to get some ETH into your Kovan wallet. You can utilize this faucet - simply paste your wallet address. This isn't existent ETH so don't go too excited! Afterward doing that, you should see the number of ETH in your wallet go up, and if you click under the Activity tab you'll see the transaction.

Remix IDE

Ethereum smart contracts are written in a programming linguistic communication called Solidity. For the sake of this tutorial, nosotros won't go into the nitty details of its syntax or keywords. If you'd like to larn Solidity, i of the nigh popular ways to do so is actually through a game called CryptoZombies.

An easy way to get started with writing smart contracts in Solidity is by using Remix. It's an online IDE that allows you lot to easily write and deploy smart contracts to Ethereum. It will wait like this when you open up it:

Remix online IDE

Ignore the code that is already there for now. They are proficient examples of smart contracts that you can expect into it afterwards to larn more than about Solidity. I'll be making some short video tutorials soon going through each of these examples and so brand sure you're following me on Twitter and won't miss that announcement!

Before y'all starting time writing code, update Remix'due south compiler settings to utilize the nearly recent version of Solidity. You can do this by clicking the Compiler button in the side bar and selecting the meridian option in the dropdown under Compiler. At the time of writing, I'm using version 0.8.ix. (Note: Solidity changes fast, so if you're following this tutorial months later information technology is written and using the most recent version, you may run into some problems. If this happens, delight attain out to me on Twitter and I'll update this tutorial.)

Solidity compiler tab

Create a new file by clicking this icon:

New file icon

and name information technology any y'all'd similar your token to exist chosen. It is best exercise to requite the file the same proper name as your contract. In this tutorial nosotros're going to phone call it KittyToken.

Kittytoken.sol

Now we're prepare for some lawmaking.

The code

The beginning thing we accept to include is our pragma statement. It tells Solidity which version our code is written for. The version here should exist the same every bit the version you selected for your compiler.

          pragma solidity ^0.8            .9;                  

Side by side nosotros're going to import a ERC-twenty implementation from Open up Zeppelin. This does most of our work for united states, and whatsoever other code we write will be customizing our token. Easy, correct?

                      import            'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol';                  

I highly recommend you lot check out this link yourself and have a look into its functions to go an understanding of how an ERC-20 token is working.

Next, let's write our contract and inherit ERC-20.

          contract KittyToken is ERC20 {            constructor() ERC20('Kitty Token', 'CAT') {}   }                  

Here, the is keyword inherits the ERC20 implementation that we've imported. We've passed two arguments to our ERC-xx constructor, the token name and the ticker (like Bitcoin and BTC.)

That's it. You've created an ERC-20 token smart contract.

But no tokens be.

If you accept a look at Open Zeppelin's ERC-twenty lawmaking, y'all'll encounter a annotate at the top:

          This implementation            is            agnostic to the way tokens are created. This ways that a supply mechanism has to be added in a derived                          contract                              using                          {_mint}.                  

For the purpose of this tutorial, let's mint some tokens on deploy and transport them to our wallet. Minting is basically creating new tokens, increasing the number of tokens that exist. The _mint function already exists in Open Zeppelin, then we'll phone call it in our constructor.

          contract KittyToken is ERC20 {            constructor() ERC20('Kitty Token', 'CAT') {             _mint(msg.sender,            100            *            ten            **            xviii);         }     }                  

What'due south happening here?

_mint takes two arguments: the accost of the account the token is minted to, and the number of tokens that are minted.

msg.sender is a congenital-in variable in Solidity and refers to the person currently connecting with the contract. Since we're the ones who has deployed the contract & executed the constructor, nosotros are msg.sender in this case.

100 * x ** 18 looks a bit complicated, just all it's doing is describing the number of decimals tokens, which allows your token to be exist split into smaller parts i.due east. you tin send 2.ane or 0.73 tokens. * 10 ** 18 is the standard for tokens, and you can accept a look into what that means here .

Your full code volition expect like this:

                                    pragma              solidity              ^0.8.9;            import            'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol';                          contract              KittyToken              is              ERC20            {                          constructor()              ERC20(                'Kitty Token',                'True cat'              )            {             _mint(msg.sender,            100            *            10            *            *            18);         }     }                  

At present yous've created a smart contract that volition mint and send you 100 of your new ERC-xx tokens when it's deployed! There are loads more customizations you can practice (ability to mint more tokens, limit to how many exist, requirements for burning tokens, etc) merely for now, let's get onto the most exciting stride.

Deploying our smart contract

Before nosotros tin deploy our contract, we need to compile it. With your token smart contract open, open the Compile tab similar you did earlier, and click Compile KittyToken.sol.

Compile KittyToken.sol

Your contract is now compiled and ready for deployment! Side by side, we're going to connect Remix to our injected web3 provider, which in our case is Metamask. This volition allow us to run into our all our tokens in our wallet. Remix makes this incredibly easy and only requires a few clicks of a button.

Click the deploy button in the side bar, as shown below:

Deploy button

Under Environment, find Injected web3 in the dropdown. When y'all click this, you'll be redirected to a Metamask permission screen that looks something similar this:

Metamask permission screen

After giving permission, y'all should meet the word Kovan underneath the Environs dropdown. You should besides come across your wallet address in the Account department. Check to make sure this is correct before continuing.

Connected to Kovan testnet with my injected Metamask web3

Then find your smart contract from the dropdown and hit Deploy. You'll be redirected to Metamask request for permission (information technology'll toll some of the Kovan ETH that y'all got earlier). Click Confirm.

Deploy KittyToken.sol Metamask confirm deployment

That's it - your smart contract is deployed onto the Kovan testnet!

Earlier we view this on Metamask, allow'south brand sure everything worked. If you scroll down in the Deploy and run transactions tab and click the dropdown of your contract, you'll meet some orange and blueish buttons and fields.

Contract actions

These are the different functions that your ERC-20 token contains. As you'll notice, we didn't write them - they are the functions our token inherited from the Open up Zeppelin ERC-20 lawmaking. An orange push lets you write, while a blue button lets you read. To make sure that our contract was deployed and our constructor worked properly, re-create your account address into the blue balanceOf field and click the balanceOf push button. You should see a number that looks similar this:

Balance of new account

That number is not the number of tokens in base x, it is a uint256 value that includes that * ten ** decimal that we specified earlier. Really, this number means 100.

Now let's see this on Metamask!

To do this, yous'll need to copy the contract address. You'll find this under the Deployed contracts section.

Copy contract address

So open Metamask, open your Assets tab and click Add token. Click Custom Token at the top and paste the contract address. The Token Symbol and Token Decimal fields should auto-populate.

Add CAT token

Then, later on yous click Next you lot should meet your new token under your ETH in Metamask! You'll too be able to come across that you have 100 tokens, which is exactly what nosotros set in the smart contract. You can ship these tokens between accounts in Metamask and it will act exactly like any other token on the Ethereum blockchain.

100 CAT token in wallet

What side by side?

Now you lot've written and deployed your outset smart contract on Ethereum! If you'd like to play around a little more with your token more, try creating a function to let y'all mint more than exterior of the constructor. You can even gear up limits on who tin can mint tokens, or the maximum number of tokens allowed to exist on the network - cheque out the require keyword. Retrieve, each time you deploy a smart contract, it will have a new address on Ethereum. That's a beautiful thing nigh blockchain - it's immutable.

Happy Solidity coding!

paschketund1953.blogspot.com

Source: https://blog.mcgee.cat/how-to-make-your-very-own-cryptocurrency-deploy-your-first-smart-contract

0 Response to "How to Make and Upload Your Own Smart Contract"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel