Basic Syntax
A Solidity source files can contain
• any number of contract definitions
• import directives
• pragma directives
pragma solidity ^0.4.0;-
B.G.Mgala
Basic Syntax
Pragma-version of solidity compiler
Contract-collection of code (its functions) and data (its state) that resides at a specific address
on the Ethereum blockchain.
B.G.Mgala
B.G.Mgala
B.G.Mgala
Datatypes
• Int
• Boolean
• Strings
• Address
B.G.Mgala
Datatypes
Address: address datatype holds the 20-byte value representing the
size of an Ethereum address.
• An address can be used to get the balance using .balance method
• Can be used to transfer balance to another address using .transfer
method.
B.G.Mgala
Solidity variables.
B.G.Mgala
Global Variables
These are special variables which exist in global workspace and provide
information about the blockchain and transaction properties.
B.G.Mgala
Global Variables
These are special variables which exist in global workspace and provide
information about the blockchain and transaction properties.
B.G.Mgala
Global Variables
B.G.Mgala
Variables Scope: Scope of local variables is limited
to function in which they are defined but State
variables can have three types of scopes
B.G.Mgala
operators :Solidity supports the following
types of operators
B.G.Mgala
Solidity - Loops
B.G.Mgala
Solidity – flow control
B.G.Mgala
Arrays
B.G.Mgala
Arrays
B.G.Mgala
ENUMS
Enums in Solidity allow you to create user-defined types with a set of predefined
values. They help make your code more readable and reduce errors when
working with fixed states, such as voting states, or user roles.
B.G.Mgala
Struct
Structs in Solidity allow you to define custom data types by grouping multiple
variables into a single unit. They help in organizing data and making smart
contracts more readable and efficient.
B.G.Mgala
Mapping
Mappings in Solidity are key-value stores similar to hash tables or dictionaries in other
programming languages
B.G.Mgala
Function
B.G.Mgala
Function modifier
Modifiers in Solidity are used to add reusable conditions to functions.
They help enforce rules such as access control, validations, and state
changes before executing a function
Hence used to modify the behaviour of a function.
For example, to add a prerequisite to a function.
First, we create a modifier with or without parameter.
B.G.Mgala
Modifier:Restrict Access-> only the contract owner can perform certain actions
B.G.Mgala
Events
• Events in Solidity allow smart contracts to log information
that can be read by external applications(such as front-end
interfaces or blockchain explorers)
• Event is an inheritable member of a contract.
• An event is emitted, it stores the arguments passed in
transaction logs.
• These logs are stored on blockchain and are accessible using
address of the contract till the contract is present on the
blockchain.
• An event generated is not accessible from within contracts,
not even the one which have created and emitted them
B.G.Mgala
Events
Main function of Events
•Tracking transactions
•Emitting alerts
•Logging state change
B.G.Mgala
Events
B.G.Mgala
Contract compilation and deploying
B.G.Mgala
CONTRACT DEPLOYMENT
B.G.Mgala
CONTRACT DEPLOYMENT
We use a tool called truffle to get the compiled bytecode to
the desired blockchain network
ABI: js interpretation of what contracts is
Bytecode: lower level compiled source code that get
deployed into the blockchain node
B.G.Mgala
Compiling contracts: folder structure
Install solidity compiler: npm install solc@0.8.26
B.G.Mgala
Inbox.sol Compile.js
B.G.Mgala
the return value from compile method is an object inside the it there
contracts property=>another object(our contract object )-our contract
object contains all the different contracts that were just compiled by our
compiler. The solidity compiler is built assuming that you might want to
compile multiple contracts in one go.
Two important properties of our contract object we most care about are:
bytecode-> actual codes that will be demployed on the blockchain.
interface-> this is contract ABI, Iists out all the different functions that exist on the
contract, all the different functions that can be called. It also specifies how many
arguments, what type of arguments, what type of return values are handled with
each of those functions.
B.G.Mgala
Testing Contracts
tests codes should be calling function inside our contracts and test them
Web3 is the library used to get programmatic
access to the smart contract via ABI:
npm install mocha ganache web3
B.G.Mgala
Web3 Providers
make an instance of Web3 and feed a
provider(communication layer between the
Web3 and some specific Ethereum network.)
B.G.Mgala
Fetching Accounts from Ganache with web3
B.G.Mgala
Deploying with web3
Investigate the properties of inbox objects
for better understanding B.G.Mgala
DEPLOYING SMART
CONTRACT INTO REAL
TEST BLOCKCHAIN
B.G.Mgala
INFURA API
• Infura is a blockchain infrastructure service that provides developers with
easy access to Ethereum and IPFS (Interplanetary File System) networks
without having to run their own full nodes.
• It acts like a remote Ethereum node that your app can talk to via an API.
Instead of setting up and maintaining your own Ethereum node (which can be
resource-intensive and complex), you can connect to Infura's high-availability
infrastructure and interact with the blockchain
B.G.Mgala
INFURA API keys Features
• Access to Ethereum & IPFS: Read and write data to the blockchain
or IPFS network.
• APIs: HTTP and WebSocket endpoints to send transactions, query
balances, read smart contracts, and more.
• Multiple Networks: Supports mainnet and testnets like Ropsten,
Goerli, Sepolia, etc.
• Scalability: Designed to handle high-traffic apps like wallets, dApps,
and DeFi platforms.
B.G.Mgala
INFURA API Common Use Cases
•dApps (Decentralized Apps) use Infura to
interact with Ethereum.
•Wallets like MetaMask use Infura to fetch
blockchain data.
•Smart Contract Deployment and interaction
via frameworks like Hardhat or Truffle.
B.G.Mgala
Deployment with Infura
gives us ability to get access to a node that is hosted on the sepolia test network,it is a portal
beyond web three into test network.
The company that runs this infra API also has nodes that are available on the main network
B.G.Mgala
@truffle/hdwallet-provider
it is a helper module that loads wallet keys
(from mnemonic or private key),Connects to a
remote Ethereum node (e.g., Infura) and Signs
transactions locally, then sends them to the
Ethereum network
B.G.Mgala
Infura (Metamask
Developer) Signup
B.G.Mgala
Tools for observing deployed contract
https://sepolia.etherscan.io/
we use the Injected Web3 provider in Remix to interact with the
contract that was previously deployed.
When switching from JavaScript VM to Injected Web3, Metamask will
now require you to provide explicit permissions to connect with Remix.
A popup from Metamask should appear immediately. Click the Next
button.
B.G.Mgala
Migrating to hardhat
B.G.Mgala
Migrating to hardhat
B.G.Mgala
Asserting deployment
B.G.Mgala
Linking Contract to Front-End
• the return value form the compiler is an object
• the object's property contract its value is another object
• important properties of the object are: bytecode and interface(ABI)
• ABI list different function that exit and can be called on the contract
object with their argument and return values
B.G.Mgala