0% found this document useful (0 votes)
4 views9 pages

Solidity Smart Contract Examples

The document contains multiple Solidity smart contract programs that demonstrate various concepts such as a Hello World program, a counter with increment and decrement functions, variable declarations, array manipulation, contract creation with a factory pattern, and inheritance in smart contracts. Each program includes code snippets and outlines the functionality provided by the contracts. The examples serve as educational material for understanding basic and advanced features of Solidity programming.

Uploaded by

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

Solidity Smart Contract Examples

The document contains multiple Solidity smart contract programs that demonstrate various concepts such as a Hello World program, a counter with increment and decrement functions, variable declarations, array manipulation, contract creation with a factory pattern, and inheritance in smart contracts. Each program includes code snippets and outlines the functionality provided by the contracts. The examples serve as educational material for understanding basic and advanced features of Solidity programming.

Uploaded by

Puranjay
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROGRAM 1 : HELLO WORLD PROGRAM

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

contract HelloWorld {

string public hello = "Hello World!";

OUTPUT ON REMIX :
PROGRAM 2 : PROGRAM TO INCREMENT AND DECREMENT THE VALUE
OF COUNT VARIABLE STORED IN SMART CONTRACT

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

contract Counter {

uint256 public count;

// Function to get the current count

function fetch() public view returns (uint256) {

return count;

// Function to increment count by 1

function increment() public {

count += 1;

// Function to decrement count by 1

function decrement() public {

// This function will fail if count = 0

count -= 1;

OUTPUT ON REMIX :
PROGRAM 3 : PROGRAM ON HOW TO DECLARE AND USE THE LOCAL,
GLOBAL AND STATE VARIABLE IN SMART CONTRACT

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

contract Variables {

// State variables are stored on the blockchain.

string public text = "INDIA";

uint256 public num = 123456789;

function doSomething() public view {

// Local variables are not saved to the blockchain.

uint256 i = 45234566;

// Here are some global variables

uint256 timestamp = [Link]; // Current block timestamp

address sender = [Link]; // address of the caller

OUTPUT ON REMIX :
PROGRAM 4 : PROGRAM ON HOW TO DECLARE AND INITIALIZE ARRAY
WITH PUSH AND POP FUNCTIONS CODED IN SMART CONTRACT
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

contract Array {

uint256[] public arr;

function fetch(uint256 i) public view returns (uint256) {

return arr[i];

function getArr() public view returns (uint256[] memory) {

return arr;

function push(uint256 i) public {

[Link](i);

function pop() public {

[Link]();

function returnLength() public view returns (uint256) {

return [Link];

function remove(uint256 index) public {

delete arr[index];

function myarr() external pure {

uint256[] memory a = new uint256[](5);

uint256[][] memory b = new uint256[][](2);

for (uint256 i = 0; i < [Link]; i++) {

b[i] = new uint256[](3);

b[0][0] = 111;

b[0][1] = 222;

b[0][2] = 333;

b[1][0] = 444;

b[1][1] = 555;
b[1][2] = 666;

OUTPUT ON REMIX :
PROGRAM 5 : PROGRAM ON HOW TO CREATE TWO CONTRACTS IN
SINGLE SMART CONTRACT: (1) CAR CONTRACT AND (2) CARFACTORY
CONTRACT WITH CREATE AND GET FUNCTIONS CODED INSIDE IT.
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

contract Car {

address public owner;

string public model;

address public carAddr;

constructor(address _owner, string memory _model) payable {

owner = _owner;

model = _model;

carAddr = address(this);

contract CarFactory {

Car[] public cars;

function create(address _owner, string memory _model) public {

Car car = new Car(_owner, _model);

[Link](car);

function createAndSendEther(address _owner, string memory _model)

public

payable

Car car = (new Car){value: [Link]}(_owner, _model);

[Link](car);

function create2(address _owner, string memory _model, bytes32 _salt)

public

Car car = (new Car){salt: _salt}(_owner, _model);

[Link](car);

}
function create2AndSendEther(

address _owner,

string memory _model,

bytes32 _salt

) public payable {

Car car = (new Car){value: [Link], salt: _salt}(_owner, _model);

[Link](car);

function getCar(uint256 _index)

public

view

returns (

address owner,

string memory model,

address carAddr,

uint256 balance

Car car = cars[_index];

return ([Link](), [Link](), [Link](), address(car).balance);

}
PROGRAM 6 : PROGRAM ON HOW TO PERFORM INHERITANCE IN
SMART CONTRACT

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.26;

/* Graph of inheritance

/\

B C

/\/

F D,E

*/

contract A {

function foo() public pure virtual returns (string memory) {

return "A";

// Contracts inherit other contracts by using the keyword 'is'.

contract B is A {

// Override [Link]()

function foo() public pure virtual override returns (string memory) {

return "B";

contract C is A {

// Override [Link]()

function foo() public pure virtual override returns (string memory) {

return "C";

// Contracts can inherit from multiple parent contracts.

// When a function is called that is defined multiple times in

// different contracts, parent contracts are searched from

// right to left, and in a depth-first manner.


contract D is B, C {

// [Link]() returns "C"

// since C is the right most parent contract with function foo()

function foo() public pure override(B, C) returns (string memory) {

return [Link]();

contract E is C, B {

// [Link]() returns "B"

// since B is the right most parent contract with function foo()

function foo() public pure override(C, B) returns (string memory) {

return [Link]();

// Inheritance must be ordered from "most base-like" to "most derived".

// Swapping the order of A and B will throw a compilation error.

contract F is A, B {

function foo() public pure override(A, B) returns (string memory) {

return [Link]();

PROGRAM OUTPUT

You might also like