Learn Ethereum Smart Contract Development for web3 2023

Dr. Gupta
8 min readFeb 2, 2023

--

In this article, we will look into the following objectives:

- Basics of Ethereum token,
- Setting up MetaMask,
- MainNet vs TestNet,
- Getting some testnet ETH from Faucet,
- Learn about smart contract basics
- Create a simple DollarFactory smart contract,
- Compile it using Remix IDE
- Create it in TestNet Blockchain
- At last learn how to interact with a smart contract

Ethereum Token:

  • Ether is the currency used in Ethereum Blockchain system. The smallest unit possible is wei. 1 Ether = 1 * 10¹⁸ wei (1,000,000,000,000,000,000)
  • Value of ether in Ethereum blockchain represents as an unsigned integer value denominated in wei ; 1 ether transaction represent 1000000000000000000 wei as value
  • unsigned integer : Integers are numbers without decimals, Unsigned integers can not be negative

Ether denomination is named with SI name and Common name which changes with every 3 zero added at last starting from wei which is smallest in number without any zero ; 1 wei, 10³ wei = Kilowei, 10⁶ = Megawei, 10⁹ = Gigawei, 10¹² = Microether, 10¹⁵= Milliether, 10¹⁸ wei = Ether, 10²¹ wei = Kiloether, 10²⁴ wei = Megaether

Choosing an Ethereum Wallet :

MetaMask.io : A crypto wallet
  • Wallet: A software application that help you manage your blockchain account, a gateway to Ethereum system.

Top wallets: MetaMask, MyEtherWallet, Exodus, TrustWallet

“Not your private keys, not your funds“

Getting started with MetaMask :

metamask download page screenshot
MetaMask extension download
  • A extension link will be open , click on Add to Chrome/Firefox
  • click on Create New Wallet
Metamask create a new wallet
  • click on ‘I agree
  • choose a long and complex password, and agree the terms and click on create a new wallet.
  • then you will be presented with secure you wallet page which will provide you a link, click on secure my wallet.
MetaMask reveal seed phrase screen

click on reveal secret recovery phrase and write it down on a diary with pen and paper. do not copy or save digitally, no screenshot or mobile photo.

  • click next and confirm by typing the remaining 3 seed phrase from your note and click confirm
  • Now you will greeted with wallet created successfully.
Ethereum different blockchain networks

Mainnet vs Testnet

  • As you can see on the MetaMask account page, you can choose between multiple Ethereum networks. By default, MetaMask will try to connect to main network. The other are public testnets, any nodes running private blockchains on your own computer (localhost).
  • Main Ethereum Network: main public Eth blockchain, real Eth, real value, real action
  • Goerli and Sepolia Test Network: public test blockchain and network. Eth on this network has no value. Previously there are Robsen and other test network which is closed now and these two have been launched.
  • Goerli works best for protocol testers and developers who must deal with a substantial existing state. Sepolia works best for consumers and developers that desire a more lightweight chain for synchronization and interaction.
  • Local 8545: connects to a node running on the same computer as browser, can be a public blockchain or private testnet
  • Custom RPC: allow you to connect to any node with Geth-compatible RPC interface, can be of any public or private part of blockchain

Remember: your metamask wallet will use the same private key and Ethereum address on all networks it connects to. But the balance on each network will be different.

Getting some Test Ether from Faucet:

ETH faucet for test network

Smart Contract:

  • contracts have addresses, can send receive ether, when transaction destination is contract addresses;- it causes contract to run in EVM using the transaction and transaction data as its input. Transaction contains ether and data indicating which specific function in the contract to run and what parameters to pass to that function, this way transaction call function within contracts.
  • contract doesn’t have privatekey so can not initiate a transaction. Only EOA(fancy name for normal user eth account) can initiate transactions and contracts can react to transaction by calling other contracts, building complex execution paths. In a typical DApp : Contract A calls Contract B to maintain a shared state.

Solidity Smart Contract:

  • Comment: // comment starts with double forward slash just like other programming language, this SPDX license is a must comment to compile.
// SPDX-License-Identifier: GPL-3.0
  • solidity compiler version declaration, use any below two methods to declare;
pragma solidity >=0.5.0 <0.6.0;

OR

pragma solidity 0.6.4;
  • start of contract starts with contract with contract name and curly braces;
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.4;
contract DollarFactory {
}
  • Default : accept any incoming amount: if a transaction doesn’t declare any data or function then it means it simply will transfer ether which every contract should be able to accept ether that why curly braces is empty. it simply accepts ether just like wallet.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.4;
contract DollarFactory {
receive () external payable {}
}
  • first function: In Solidity, functions are public by default. This means anyone (or any other contract) can call your contract’s function and execute its code. withdraw function takes one unsigned integer (uint) argument named _amount .
Remember: its uint NOT unit.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.4;
contract DollarFactory {
receive () external payable {};
function withdraw(uint _amount) public {
require(_amount <= 1000000000000000000);
}
}
  • the first part of function uses built-in solidity function require to test a precondition, _amount is less then or equal to 10¹⁸wei, if the withdraw function is called with a withdraw _amount greater than that amount, the require function here will cause contract execution to stop and fail with an exception.
  • statements need to be terminated with a semicolon in solidity.
  • inside a function , main logic of contract is written.
  • another main logic: msg object; is an input, all contracts can access, represents the transaction that triggered the execution of this contract, Atrribute: sender is the sender address of the transaction, function: transfer; build in function that transfers ether from current contract to address of the sender, transfer function takes an amount as its only argument, _amount value that was the parameter to the withdraw function.
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.6.4;
contract DollarFactory {
receive () external payable{}
function withdraw(uint _amount) public {
require(_amount <= 1000000000000000000);
msg.sender.transfer(_amount);
}
}

Compiling the Contract: Solidity compiler is used to convert solidity code into EVM bytecode, executed by EVM on blockchain.

Compiling smart contract via Remix
  • Solidity compiler, as a standalone executable, part of various framework, IDEs, popularly Remix : https://remix.ethereum.org
  • create a new workspace and a new file with name dollarfactory.sol
  • paste the smart contract code in that file and right click and click on compile
  • if everything is followed exactly like above, congratulation you will see a green tick which means you have successfully compiled the contract.
  • if you are geting error, try changing the compiler version to 0.6.4 from dropdown list.

Deploy the Contract on Blockchain:

Deploying smart contract on Eth Testnet Blockchain network
  • Now, we’ve compiled the solidity contract into bytecode, we are going to register the contract on the Ethereum blockchain. We will use Goerli test network blockchain to test our contract
  • Registering a contract on blockchain involves creating a special transaction whose destination is zero address : 0x000000000000000000000000…
  • the zero address is the special address which tells the Ethereum blockchain that you want to register a contract.
  • on Remix IDE, click on ‘Deploy and RUN transaction’ tab and select “Injected provider — MetaMask” in the Environment drop-down selection box.
  • and it will ask to connect to your MetaMask, accept it and confirm it.
  • Now it will show your ETH address with blance in Account section.
  • Below there is a option “Deploy” , above that confirm your contract name which you are going to deploy in blockchain, in this case it is “DollarFactory — dollarfactory.sol” ready to be created
  • Click on Deploy button, Remix will construct the creation transaction and MetaMask will ask you to approve it. Notice, the transaction has no ether in it but it has bytes of data (compiled contract) and will consume some gas , Click on Confirm to approve it.
  • Now Below the Deploy option, there is “Deployed Contracts”: check there you will your deployed contract with 0 eth balance and contract has its own address with withdraw function to interact.
  • You can also check the history of contract creation in test network block explorer. Below our smart contract code, you can see the output which is the result of deployment, check there and click on “view on etherscan” it will open https://goerli.etherscan.io/

Interaction with Smart Contract:

A deployed smart contract in Eth testnet network
  • Copy the contract address and send some eth via MetaMask as normal transaction.
  • Then open Remix page and use the withdraw function in deployed contract list and withdraw less than equal to any amount you deposited.
  • while using the withdraw function, remember the contract only knows wei for internal transaction so we have to type in wei whatever we want to withdraw from contract.
  • so we transferred 0.003eth to contract which is 3,000,000,000,000,000 and let withdraw 0.0025 eth = 2,500,000,000,000,000 wei (for calculation refer to Ether denomination we learned above.
  • Approve the Remix transaction in MetaMask and you can see after your transaction of withdrawal , a internal message transaction happened in contract which transferred the balance you asked via withdraw function to your address as you are the msg.sender when you deposited.

Mastering Ethereum by Andreas M. Antonopoulos and Dr. Gavin Wood (O’Reilly). Copyright 2019 The Ethereum Book LLC and Gavin Wood, 978–1–491–97194–9.”

Follow me on Twitter: https://twitter.com/BgxDoc

--

--

Dr. Gupta

ll Insomniac Doctor loves Terminal || #Security_Researcher #DayTrader || Learn and Earn || #Web3_Doctor || Rookie Smart Contract Dev & Security Researcher ||