Posted By : Krishna
Brief Into of Blockchain Smart Contract Through Remix IDE.
What is Smart Contract:
"Smart contract" is simply a program that runs on the Ethereum blockchain. It is a collection of code (its functions) and data (its state) located at a specific address on the Ethereum blockchain.
A smart contract is a type of Ethereum account. This means they have a balance and can send transactions over the network. However, they are not controlled by the user, but deployed on the network and run on a schedule. The user account can then interact with the smart contract by sending transactions that perform a function defined on the smart contract. Smart contracts can define rules, just like a regular contract, and execute them automatically through code. Smart contracts cannot be deleted by default and interactions with them are immutable.
Let's familiar with Remix IDE:
Remix IDE is an open-source desktop and web application. It promotes rapid development cycles and has a rich set of plugins with an intuitive GUI. Remix is "‹"‹used throughout the contract development journey and serves as a playground to learn and teach Ethereum. The Remix IDE is part of the Remix project, which is a platform for development tools that use the plugin architecture. It includes subprojects like Remix Plugin Engine, Remix Libs, and of course RemixIDE. Remix IDE is a powerful open-source tool that helps you write Solidity contracts directly from the browser. It is written in JavaScript and supports both uses in the browser, but runs locally and in the desktop version. Remix IDE has modules for testing, debugging, and deploying smart contracts, and more.
Let's get acquainted with the basic features of the IDE. In the sidebar you have 3 tabs, now:
Create your first smart contract:
Contract Code:
pragma solidity ^0.5.16;
contract Resort{
...
}
address payable tenant;
address payable owner;
uint public no_of_rooms = 0;
uint public no_of_agreement = 0;
uint public no_of_rent = 0;
struct Room{
uint roomid;
uint agreementid;
string roomname;
string roomaddress;
uint rent_per_month;
uint securityDeposit;
uint timestamp;
bool vacant;
address payable owner;
address payable currentTenant;
}
mapping(uint => Room) public Room_by_No;
struct RoomAgreement{
uint roomid;
uint agreementid;
string Roomname;
string RoomAddresss;
uint rent_per_month;
uint securityDeposit;
uint lockInPeriod;
uint timestamp;
address payable tenantAddress;
address payable ownerAddress;
}
mapping(uint => RoomAgreement) public RoomAgreement_by_No;
struct Rent{
uint rentno;
uint roomid;
uint agreementid;
string Roomname;
string RoomAddresss;
uint rent_per_month;
uint timestamp;
address payable tenantAddress;
address payable ownerAddress;
}
mapping(uint => Rent) public Rent_by_No;
This requires (...); which means if the given condition is not met, the function will not run and the given string will appear as an error code.
modifier onlyOwner(uint _index) {
require(msg.sender == Room_by_No[_index].owner, "Only owner can access this");
_;
}
modifier notOwner(uint _index) {
require(msg.sender != Room_by_No[_index].owner, "Only Tenant can access this");
_;
}
modifier OnlyWhileVacant(uint _index){
require(Room_by_No[_index].vacant == true, "Room is currently Occupied.");
_;
}
modifier enoughRent(uint _index) {
require(msg.value >= uint(Room_by_No[_index].rent_per_month), "Not enough Ether in your wallet");
_;
}
modifier enoughAgreementfee(uint _index) {
require(msg.value >= uint(uint(Room_by_No[_index].rent_per_month) + uint(Room_by_No[_index].securityDeposit)), "Not enough Ether in your wallet");
_;
}
modifier sameTenant(uint _index) {
require(msg.sender == Room_by_No[_index].currentTenant, "No previous agreement found with you & owner");
_;
}
modifier AgreementTimesLeft(uint _index) {
uint _AgreementNo = Room_by_No[_index].agreementid;
uint time = RoomAgreement_by_No[_AgreementNo].timestamp + RoomAgreement_by_No[_AgreementNo].lockInPeriod;
require(now < time, "Agreement already Ended");
_;
}
modifier AgreementTimesUp(uint _index) {
uint _AgreementNo = Room_by_No[_index].agreementid;
uint time = RoomAgreement_by_No[_AgreementNo].timestamp + RoomAgreement_by_No[_AgreementNo].lockInPeriod;
require(now > time, "Time is left for contract to end");
_;
}
modifier RentTimesUp(uint _index) {
uint time = Room_by_No[_index].timestamp + 30 days;
require(now >= time, "Time left to pay Rent");
_;
}
function addRoom(string memory _roomname, string memory _roomaddress, uint _rentcost, uint _securitydeposit) public {
require(msg.sender != address(0));
no_of_rooms ++;
bool _vacancy = true;
Room_by_No[no_of_rooms] = Room(no_of_rooms,0,_roomname,_roomaddress, _rentcost,_securitydeposit,0,_vacancy, msg.sender, address(0));
}
Before creating the signingOfAgreement
function, remember the following:
Use these modifiers here, for:
signingOfAgreement
function will only run if said room is empty and the tenant has enough ether in their wallet. function signAgreement(uint _index) public payable notOwner(_index) enoughAgreementfee(_index) OnlyWhileVacant(_index) {
require(msg.sender != address(0));
address payable _owner = Room_by_No[_index].owner;
uint totalfee = Room_by_No[_index].rent_per_month + Room_by_No[_index].securityDeposit;
_owner.transfer(totalfee);
no_of_agreement++;
Room_by_No[_index].currentTenant = msg.sender;
Room_by_No[_index].vacant = false;
Room_by_No[_index].timestamp = block.timestamp;
Room_by_No[_index].agreementid = no_of_agreement;
RoomAgreement_by_No[no_of_agreement]=RoomAgreement(_index,no_of_agreement,Room_by_No[_index].roomname,Room_by_No[_index].roomaddress,Room_by_No[_index].rent_per_month,Room_by_No[_index].securityDeposit,365 days,block.timestamp,msg.sender,_owner);
no_of_rent++;
Rent_by_No[no_of_rent] = Rent(no_of_rent,_index,no_of_agreement,Room_by_No[_index].roomname,Room_by_No[_index].roomaddress,Room_by_No[_index].rent_per_month,now,msg.sender,_landlord);
}
Before creating the payRent
function, remember the following:
function payRent(uint _index) public payable sameTenant(_index) RentTimesUp(_index) enoughRent(_index){
require(msg.sender != address(0));
address payable _owner = Room_by_No[_index].owner;
uint _rent = Room_by_No[_index].rent_per_month;
_owner.transfer(_rent);
Room_by_No[_index].currentTenant = msg.sender;
Room_by_No[_index].vacant = false;
no_of_rent++;
Rent_by_No[no_of_rent] = Rent(no_of_rent,_index,Room_by_No[_index].agreementid,Room_by_No[_index].roomname,Room_by_No[_index].roomaddress,_rent,now,msg.sender,Room_by_No[_index].landlord);
}
Before creating agreementCompleted
function, remember the following:
function agreementCompleted(uint _index) public payable onlyOwner(_index) AgreementTimesUp(_index){
require(msg.sender != address(0));
require(Room_by_No[_index].vacant == false, "Room is currently Occupied.");
Room_by_No[_index].vacant = true;
address payable _Tenant = Room_by_No[_index].currentTenant;
uint _securitydeposit = Room_by_No[_index].securityDeposit;
_Tenant.transfer(_securitydeposit);
}
aagreementCompleted
a function that the owner will use to mark the transaction as complete. Before creating a function, remember the following:
function agreementTerminated(uint _index) public onlyOwner(_index) AgreementTimesLeft(_index){
require(msg.sender != address(0));
Room_by_No[_index].vacant = true;
}
Compile the Smart Contract:
Now click on Solidity Compile option on the left sidebar.
Deploy the Smart Contract:
Benefits of smart contracts:
Now you may be wondering, "what is the benefit of smart contracts when there are many centralized methods?"
Let me explain some advantages of smart contracts over centralized systems:
Here, data cannot be altered or tampered with. Therefore, it is almost impossible for malicious actors to manipulate the data.
It is completely decentralized.
Unlike any centralized payment wallet, you don't have to pay a percentage of commission to the middleman to complete the transaction.
November 23, 2024 at 01:16 am
Your comment is awaiting moderation.