0% found this document useful (0 votes)
13 views3 pages

Smart Contract in Solidity

The document provides an overview of the structure and components of a smart contract in Solidity, including pragma directives, contract declarations, state variables, constructors, functions, modifiers, and events. It emphasizes the importance of specifying the compiler version and outlines how each component contributes to the functionality of a contract. Additionally, it mentions that Solidity supports advanced features like inheritance and libraries for creating complex smart contracts.

Uploaded by

Lahari Betavolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Smart Contract in Solidity

The document provides an overview of the structure and components of a smart contract in Solidity, including pragma directives, contract declarations, state variables, constructors, functions, modifiers, and events. It emphasizes the importance of specifying the compiler version and outlines how each component contributes to the functionality of a contract. Additionally, it mentions that Solidity supports advanced features like inheritance and libraries for creating complex smart contracts.

Uploaded by

Lahari Betavolu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

SMART CONTRACT IN SOLIDITY

In Solidity, a contract is the fundamental building block of Ethereum smart contracts. It is written in a
language similar to JavaScript and is compiled into bytecode that can be executed on the Ethereum
Virtual Machine (EVM). Here's a basic structure of a contract in Solidity:

Pragma Directive: This is the first line of a Solidity contract and specifies the version of the Solidity
compiler to be used. It's important to specify the compiler version to ensure that the contract behaves
as expected with the specific compiler version.

Example:
pragma solidity ^0.8.0;
Contract Declaration: The contract keyword is used to define a new contract. It is followed by the
name of the contract and a pair of curly braces that enclose the contract's code.

Example:
contract MyContract {
// Contract code goes here
}
State Variables: State variables represent the state of the contract and can be accessed and modified by
its functions. They are declared within the contract body.

Example:
contract MyContract {
uint256 public myVariable;
}
Constructor: The constructor is a special function that is executed only once when the contract is
deployed. It is used to initialize contract state variables and can take arguments.

Example:
contract MyContract {
uint256 public myVariable;

constructor(uint256 initialValue) {
myVariable = initialValue;
}
}
Functions: Functions define the behavior of the contract. They can be public, private, internal, or
external. Public functions can be called externally, while private functions are only accessible within
the contract. Internal functions are like private functions but can also be called by contracts that
inherit from this one. External functions are similar to public functions but cannot be called internally.

Example:
contract MyContract {
uint256 public myVariable;

constructor(uint256 initialValue) {
myVariable = initialValue;
}

function setVariable(uint256 newValue) public {


myVariable = newValue;
}

function getVariable() public view returns (uint256) {


return myVariable;
}
}
Modifiers: Modifiers are used to modify the behavior of functions in a declarative way. They can be
used to restrict access to certain functions, check conditions before executing a function, etc.

Example:
contract MyContract {
address public owner;

constructor() {
owner = msg.sender;
}

modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_; // Continue executing the function if the condition is met
}

function changeOwner(address newOwner) public onlyOwner {


owner = newOwner;
}
}
Events: Events are used to notify external parties about state changes in the contract. They are
declared using the event keyword and can be emitted from within the contract.

Example:
contract MyContract {
event ValueChanged(uint256 newValue);

uint256 public myVariable;

constructor(uint256 initialValue) {
myVariable = initialValue;
}

function setVariable(uint256 newValue) public {


myVariable = newValue;
emit ValueChanged(newValue);
}
}
This is a basic outline of a Solidity contract. Solidity also supports many other features such as
inheritance, interfaces, libraries, and more, which can be used to write complex smart contracts.

You might also like