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
¿Qué hay detrás de las fases bajistas de Bitcoin?
MSCI China Consumer Discretionary ETF (CHIQ) Analyse Koersverwachting
Mickey Moniak Walk Up Song
7 C's of Communication | The Effective Communication Checklist
Google Sites Classroom 6X
Z-Track Injection | Definition and Patient Education
Crocodile Tears - Quest
Xrarse
Chastity Brainwash
Sports Clips Plant City
How do you like playing as an antagonist? - Goonstation Forums
Echo & the Bunnymen - Lips Like Sugar Lyrics
Hijab Hookup Trendy
Uky Linkblue Login
10-Day Weather Forecast for Santa Cruz, CA - The Weather Channel | weather.com
Site : Storagealamogordo.com Easy Call
If you bought Canned or Pouched Tuna between June 1, 2011 and July 1, 2015, you may qualify to get cash from class action settlements totaling $152.2 million
The best firm mattress 2024, approved by sleep experts
Grimes County Busted Newspaper
Keci News
Panola County Busted Newspaper
Cardaras Funeral Homes
Coindraw App
Angel Haynes Dropbox
A Man Called Otto Showtimes Near Carolina Mall Cinema
Japanese Emoticons Stars
Alternatieven - Acteamo - WebCatalog
Busch Gardens Wait Times
Sinai Sdn 2023
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
Kelley Fliehler Wikipedia
Myra's Floral Princeton Wv
Springfield.craigslist
Puerto Rico Pictures and Facts
Movies123.Pick
Midsouthshooters Supply
Dying Light Nexus
2008 DODGE RAM diesel for sale - Gladstone, OR - craigslist
The Holdovers Showtimes Near Regal Huebner Oaks
Noaa Marine Weather Forecast By Zone
Japanese Big Natural Boobs
Danielle Ranslow Obituary
Promo Code Blackout Bingo 2023
Hk Jockey Club Result
60 Days From May 31
Youravon Com Mi Cuenta
R/Gnv
Dancing Bear - House Party! ID ? Brunette in hardcore action
Bank Of America Appointments Near Me
Upcoming Live Online Auctions - Online Hunting Auctions
Used Curio Cabinets For Sale Near Me
Ranking 134 college football teams after Week 1, from Georgia to Temple
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.