Smart_Contract_Unit3.2
Smart_Contract_Unit3.2
Dr B.Vasavi,
Associate Professor
Department of IT, MVSREC.
Smart Contract
A smart contract is a self-executing contract
with the terms of the agreement directly
written into lines of code. The code and
agreements contained within the contract
exist on a blockchain network. Smart
contracts automatically execute and enforce
the terms of the agreement without the need
for intermediaries or third parties.
Key Features of Smart Contracts
Self-executing: Once the predefined conditions are met,
the smart contract automatically performs the actions
specified in the contract.
Immutable: Once deployed to a blockchain, the smart
contract code cannot be changed or tampered with,
ensuring trust and reliability.
Distributed and Transparent: The contract is visible and
accessible to anyone on the blockchain network, and its
execution is confirmed and recorded by all network
participants (nodes).
Trustless: Since smart contracts are executed on a
decentralized network, they eliminate the need for a trusted
intermediary, such as a bank or lawyer, to enforce the terms
How Smart Contracts
Work
Smart contracts run on blockchain
platforms like Ethereum, Binance Smart
Chain, Polygon, etc.The contract contains
logic (code) and conditions that must be met
for it to execute certain actions.
For example, in a smart contract for a simple
payment, the contract could say:If Party A
sends 1 ETH to the contract, then the
contract sends ownership of a digital asset to
Party A.
Example of a Real-World Use Case
Decentralized Finance (DeFi): Smart
contracts enable financial services like
lending, borrowing, and trading without
traditional banks.
• For example, a smart contract can facilitate a
loan between two parties, where the loan
terms and conditions (interest rates,
collateral, repayment dates) are coded into
the contract. Once the borrower fulfills the
contract's terms, the funds are automatically
transferred.
Cont..
Supply Chain Management: Smart
contracts can track goods in a supply chain.
For example, once a product reaches a certain
checkpoint, the smart contract could
automatically release payment to the supplier.
Real Estate: A smart contract could
automate property transfers. When a buyer
sends the payment to the contract, the
ownership deed could be automatically
transferred.
Benefits of Smart Contract
Efficiency: No need for intermediaries,
reducing time and costs.
Security: Data is encrypted on the
blockchain, providing high levels of security.
Accuracy: Automated, reducing human
error.
Limitations
Immutability: Once deployed, the code
cannot be changed, even if there are
bugs.Legal Uncertainty: In many countries,
smart contracts still exist in a gray area when
it comes to legal enforcement.
Remix IDE
Remix IDE is generally used to compile and
run Solidity smart contracts. Below are the
steps for the compilation, execution, and
debugging of the smart contract.
Step 1: Open Remix IDE on any of your
browsers, select on New File and click
on Solidity to choose the environment.
Step 2: Write the Smart contract in the code
section, and click the Compile button under
the Compiler window to compile the
contract.
Solidity
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.22 <0.9.0;
contract SolidityTest
{
function getResult() public pure returns(uint)
{
uint a = 10;
uint b = 25;
uint sum;
sum=a+b;
return sum;
}
}
Step 3: To execute the code, click on
the Deploy button under Deploy and Run
Transactions window.
Step 4: After deploying the code click on the
method calls under the drop-down of
deployed contracts to run the program, and
for output, check to click on the drop-down
on the console.
Step 5: For debugging click on the Debug
button corresponding to the method call in
the console. Here you can check each
function call and variable assignments.
Hello world program
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.22 <0.9.0;
contract HelloWorld {
/**
* @dev Prints Hello World string
*/
function print() public pure returns (string memory) {
return "Hello World!";
}
}