4. Writing and compiling smart contracts | Ethereum development environment for professionals by Nomic Foundation (2024)

We're going to create a simple smart contract that implements a token that can be transferred. Token contracts are most frequently used to exchange or store value. We won't go in depth into the Solidity code of the contract in this tutorial, but there's some logic we implemented that you should know:

  • There is a fixed total supply of tokens that can't be changed.
  • The entire supply is assigned to the address that deploys the contract.
  • Anyone can receive tokens.
  • Anyone with at least one token can transfer tokens.
  • The token is non-divisible. You can transfer 1, 2, 3, or 37 tokens but not 2.5.

TIP

You might have heard about ERC-20, which is a token standard in Ethereum. Tokens such as DAI and USDC implement the ERC-20 standard which allows them all to be compatible with any software that can deal with ERC-20 tokens. For the sake of simplicity, the token we're going to build does not implement the ERC-20 standard.

# Writing smart contracts

Start by creating a new directory called contracts and create a file inside the directory called Token.sol.

Paste the code below into the file and take a minute to read the code. It's simple and it's full of comments explaining the basics of Solidity.

TIP

To get syntax highlighting and editing assistance for Solidity in Visual Studio Code, try Hardhat for Visual Studio Code.

//SPDX-License-Identifier: UNLICENSED// Solidity files have to start with this pragma.// It will be used by the Solidity compiler to validate its version.pragma solidity ^0.8.0;// This is the main building block for smart contracts.contract Token { // Some string type variables to identify the token. string public name = "My Hardhat Token"; string public symbol = "MHT"; // The fixed amount of tokens, stored in an unsigned integer type variable. uint256 public totalSupply = 1000000; // An address type variable is used to store ethereum accounts. address public owner; // A mapping is a key/value map. Here we store each account's balance. mapping(address => uint256) balances; // The Transfer event helps off-chain applications understand // what happens within your contract. event Transfer(address indexed _from, address indexed _to, uint256 _value); /** * Contract initialization. */ constructor() { // The totalSupply is assigned to the transaction sender, which is the // account that is deploying the contract. balances[msg.sender] = totalSupply; owner = msg.sender; } /** * A function to transfer tokens. * * The `external` modifier makes a function *only* callable from *outside* * the contract. */ function transfer(address to, uint256 amount) external { // Check if the transaction sender has enough tokens. // If `require`'s first argument evaluates to `false`, the // transaction will revert. require(balances[msg.sender] >= amount, "Not enough tokens"); // Transfer the amount. balances[msg.sender] -= amount; balances[to] += amount; // Notify off-chain applications of the transfer. emit Transfer(msg.sender, to, amount); } /** * Read only function to retrieve the token balance of a given account. * * The `view` modifier indicates that it doesn't modify the contract's * state, which allows us to call it without executing a transaction. */ function balanceOf(address account) external view returns (uint256) { return balances[account]; }}

TIP

*.sol is used for Solidity files. We recommend matching the file name to the contract it contains, which is a common practice.

# Compiling contracts

To compile the contract run npx hardhat compile in your terminal. The compile task is one of the built-in tasks.

$ npx hardhat compileCompiled 1 Solidity file successfully (evm target: paris).

The contract has been successfully compiled and it's ready to be used.

4. Writing and compiling smart contracts | Ethereum development environment for professionals by Nomic Foundation (2024)
Top Articles
AI Revolutionizing Investment Decision-Making - ARC Group
Yu-Gi-Oh Master Duel: How to Earn Gems (The Fast Way)
Hocus Pocus Showtimes Near Harkins Theatres Yuma Palms 14
Driving Directions To Fedex
Activities and Experiments to Explore Photosynthesis in the Classroom - Project Learning Tree
Autobell Car Wash Hickory Reviews
Stl Craiglist
Craigslist Vermillion South Dakota
Owatc Canvas
Nieuwe en jong gebruikte campers
Citi Card Thomas Rhett Presale
Giovanna Ewbank Nua
PGA of America leaving Palm Beach Gardens for Frisco, Texas
Bme Flowchart Psu
Hallelu-JaH - Psalm 119 - inleiding
Notisabelrenu
6001 Canadian Ct Orlando Fl
Saberhealth Time Track
Craigslist List Albuquerque: Your Ultimate Guide to Buying, Selling, and Finding Everything - First Republic Craigslist
Diesel Mechanic Jobs Near Me Hiring
Mineral Wells Independent School District
Star Wars: Héros de la Galaxie - le guide des meilleurs personnages en 2024 - Le Blog Allo Paradise
Noaa Ilx
Axe Throwing Milford Nh
Account Suspended
Craigslist Maui Garage Sale
Kayky Fifa 22 Potential
Boxer Puppies For Sale In Amish Country Ohio
8002905511
Delete Verizon Cloud
Armor Crushing Weapon Crossword Clue
Ourhotwifes
Appleton Post Crescent Today's Obituaries
Prima Healthcare Columbiana Ohio
The Legacy 3: The Tree of Might – Walkthrough
Emerge Ortho Kronos
Bbc Gahuzamiryango Live
“Los nuevos desafíos socioculturales” Identidad, Educación, Mujeres Científicas, Política y Sustentabilidad
159R Bus Schedule Pdf
Hellgirl000
Insideaveritt/Myportal
Top 25 E-Commerce Companies Using FedEx
Newsweek Wordle
Rush Copley Swim Lessons
4k Movie, Streaming, Blu-Ray Disc, and Home Theater Product Reviews & News
Craigslist Binghamton Cars And Trucks By Owner
Makes A Successful Catch Maybe Crossword Clue
Gabrielle Abbate Obituary
Sea Guini Dress Code
Mountainstar Mychart Login
Lesly Center Tiraj Rapid
Latest Posts
Article information

Author: Rev. Leonie Wyman

Last Updated:

Views: 6069

Rating: 4.9 / 5 (59 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Rev. Leonie Wyman

Birthday: 1993-07-01

Address: Suite 763 6272 Lang Bypass, New Xochitlport, VT 72704-3308

Phone: +22014484519944

Job: Banking Officer

Hobby: Sailing, Gaming, Basketball, Calligraphy, Mycology, Astronomy, Juggling

Introduction: My name is Rev. Leonie Wyman, I am a colorful, tasty, splendid, fair, witty, gorgeous, splendid person who loves writing and wants to share my knowledge and understanding with you.