Introduction To Smart Contract and Ethereum
Smart contracts are the backbone of web3. You must have a smart contract to create a dApp. Deploying a smart contract directly on the mainnet is not ideal. You must first test the smart contracts on the local blockchain development network.
Ethereum is a blockchain-based software platform primarily used to support the world's second largest cryptocurrency by market capitalization after Bitcoin. Like other cryptocurrencies, Ethereum can be used to send and receive value around the world without the need for third-party tracking or unexpected interference
Remix IDE (Deployment for Smart Contract)
Remix is "‹"‹a set of tools that make it easy to develop, compile, test, and deploy smart contracts. These tools also function as a core part of the original Remix IDE plugins.
Remix IDE is an IDE for Solidity dApp developers powered by Remix. The Remix IDE repository is available here and the online version is available at https://remix.ethereum.org.
For Detail Insight to IDE , see the Remix IDE documentation.
Smart Contract Creation
Open REMIX IDE and Lets Create Our First Ethereum Smart Contract:-
Lets take an example of goods export service in which we are going to create and implement library named ServiceProvider.sol to define roles of different department of services.
pragma solidity ^0.5.0;
/**
* @title ServiceProviders
* @dev Library for managing addresses assigned to a ServiceProvider.
*/
library ServiceProviders {
struct ServiceProvider {
mapping (address => bool) bearer;
}
/**
* @dev give an account access to this service Providers
*/
function add(ServiceProviders storage serviceProviders, address account) internal {
require(account != address(0));
require(!has(role, account));
serviceProviders.bearer[account] = true;
}
/**
* @dev remove an account's access to this serviceProviders
*/
function remove(ServiceProvider storage serviceProviders, address account) internal {
require(account != address(0));
require(has(role, account));
serviceProviders.bearer[account] = false;
}
/**
* @dev check if an account has this serviceProviders
* @return bool
*/
function has(ServiceProvider storage serviceProvider, address account)
internal
view
returns (bool)
{
require(account != address(0));
return serviceProvider.bearer[account];
}
}
We are finished with our Service provider code, now we are going to implement a ethereum smart contract named CourierRole for a specific service called Courier service.
Importing Roles Library
pragma solidity ^0.5.0;
// Import the library 'Roles'
import "./ServiceProvider.sol";
// Define a contract 'CourierRole' to manage this role - add, remove, check
contract CourierRole {
using ServiceProvider for ServiceProviders.ServiceProvider;
// Define 2 events, one for Adding, and other for Removing
event CourierPersonAdded(address indexed account);//, string name,uint panNumber);
event CourierPersonRemoved(address indexed account);//, string name,uint panNumber);
// Define a struct 'CourierItem' by inheriting from 'ServiceProvider' library, struct ServiceProvider
ServiceProviders.ServiceProvider private CourierPerson;
// In the constructor make the address that deploys this contract the 1st Courier
constructor() public {
_addCourierPerson(msg.sender);
}
// Define a modifier that checks to see if msg.sender has the appropriate service provider
modifier onlyCourierPerson() {
require(isCourierPerson(msg.sender));
_;
}
// Define a function 'isDelivery' to check this service
function isDeliveryPerson(address account) public view returns (bool) {
return DeliveryPerson.has(account);
}
// Define a function 'addCourier' that adds this service
function addCourierPerson(address account) public {
_addCourierPerson(account);
}
// Define a function 'renounceCourier' to renounce this service
function renounceCourierPerson(address account) public {
_removeCourierPerson(account);
}
// Define an internal function '_addCourier' to add this role, called by 'addCourier'
function _addCourierPerson(address account) internal {
CourierPerson.add(account);
emit CourierPersonAdded(account);
}
// Define an internal function '_removeCourier' to remove this role, called by 'removeCourier'
function _removeCourierPerson(address account) internal {
CourierPerson.remove(account);
emit CourierPersonRemoved(account);
}
}
Use Cases of Smart Contracts
- Use cases for smart contracts range from simple to complex.
- These can be used for simple economic transactions such as money transfers from A to B, or for intelligent access management in the sharing economy. Smart contracts can confuse many industries.
- There are use cases in banking, insurance, energy, e-government, telecommunications, music business, arts, mobility, education and many other industries.
November 21, 2024 at 11:38 am
Your comment is awaiting moderation.