Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
31 views8 pages

Solidity Programs

Uploaded by

ghn249525
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)
31 views8 pages

Solidity Programs

Uploaded by

ghn249525
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
You are on page 1/ 8

Solidity Programs

Block Info:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract BlockInfo {
// Function to get the current block number
function getBlockNumber() public view returns (uint) {
return block.number;
}

// Function to get the current block's timestamp


function getBlockTimestamp() public view returns (uint) {
return block.timestamp;
}

// Function to get the hash of a specific block (within 256 most recent blocks)
function getBlockHash(uint blockNumber) public view returns (bytes32) {
return blockhash(blockNumber);
}
}

Hello World:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HelloWorld {
// Function to return the "Hello, World!" string
function sayHello() public pure returns (string memory) {
return "Hello, World!";
}
}

Hashing:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract HashingExample {
// Function to generate hash for a given input
function hashString(string memory data) public pure returns (bytes32) {
return keccak256(abi.encodePacked(data));
}

// Function to generate hash for multiple inputs (string and uint)


function hashMultipleInputs(string memory data, uint number) public pure returns
(bytes32) {
return keccak256(abi.encodePacked(data, number));
}
}

Simple Storage:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract SimpleStorage {
uint256 storedData;

// Function to store a value


function set(uint256 x) public {
storedData = x;
}

// Function to retrieve the stored value


function get() public view returns (uint256) {
return storedData;
}
}

Block Creation:
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract BlockCreation {

// Struct to represent a Block

struct Block {

uint blockNumber;

uint timestamp;

string data; // This represents the transactions or any other data

bytes32 blockHash; // Store the hash of the block

// Array to store all created blocks

Block[] public blockchain;

// Function to create a new block

function createBlock(string memory _data) public returns (bytes32) {

// Get the current block number (in the context of this contract, we use the length of the array)

uint blockNumber = blockchain.length + 1;

// Get the current timestamp


uint timestamp = block.timestamp;

// Create the block

Block memory newBlock = Block({

blockNumber: blockNumber,

timestamp: timestamp,

data: _data,

blockHash: calculateHash(blockNumber, timestamp, _data)

});

// Add the block to the blockchain

blockchain.push(newBlock);

// Return the hash of the block

return newBlock.blockHash;

// Function to calculate the block's hash

function calculateHash(uint _blockNumber, uint _timestamp, string memory _data) internal pure returns
(bytes32) {

// Hashing the block's contents using keccak256

return keccak256(abi.encodePacked(_blockNumber, _timestamp, _data));

// Function to get block details

function getBlock(uint _index) public view returns (uint, uint, string memory, bytes32) {

require(_index < blockchain.length, "Block does not exist");

Block memory b = blockchain[_index];

return (b.blockNumber, b.timestamp, b.data, b.blockHash);

}
Calculator:
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract Calculator {

// Add two numbers

function add(uint256 a, uint256 b) public pure returns (uint256) {

return a + b;

// Subtract two numbers

function subtract(uint256 a, uint256 b) public pure returns (uint256) {

return a - b;

// Multiply two numbers

function multiply(uint256 a, uint256 b) public pure returns (uint256) {

return a * b;

// Divide two numbers

function divide(uint256 a, uint256 b) public pure returns (uint256) {

require(b > 0, "Cannot divide by zero.");

return a / b;

ERC-20 Token:
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

// Importing OpenZeppelin's ERC20 implementation


import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract MyToken is ERC20 {

// Constructor to initialize the token with name and symbol

constructor() ERC20("MyToken", "MTK") {

// Mint initial supply of tokens to the contract deployer

_mint(msg.sender, 1000 * 10 ** decimals());

// Function to mint new tokens to a specific address

function mintTokens(address to, uint256 amount) public {

_mint(to, amount);

// Function to burn tokens from the caller's account

function burnTokens(uint256 amount) public {

_burn(msg.sender, amount);

Simple Voting:
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract SimpleVoting {

// Candidate names

string[] public candidates;

// Mapping to store votes for each candidate

mapping(string => uint) public votes;

// Constructor to initialize the candidates


constructor(string[] memory _candidates) {

candidates = _candidates;

// Function to vote for a candidate

function vote(string memory candidateName) public {

// Increment the vote count for the given candidate

votes[candidateName]++;

require(votes[candidateName]<=15,"voters does not exist");

// Function to get the total votes for a candidate

function getVotes(string memory candidateName) public view returns (uint) {

return votes[candidateName];

Simple Bank:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

contract SimpleBank {

// Mapping to keep track of balances for each address

mapping(address => uint) public balances;

// Function to deposit Ether into the bank

function deposit() public payable {

require(msg.value > 0, "You must send some ether");

balances[msg.sender] += msg.value;

// Function to withdraw Ether from the bank


function withdraw(uint _amount) public {

require(balances[msg.sender] >= _amount, "Insufficient balance");

balances[msg.sender] -= _amount;

payable(msg.sender).transfer(_amount);

// Function to check the balance of the caller

function checkBalance() public view returns (uint) {

return balances[msg.sender];

You might also like