Solidity Programs
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 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));
}
Simple Storage:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
uint256 storedData;
Block Creation:
// SPDX-License-Identifier: MIT
contract BlockCreation {
struct Block {
uint blockNumber;
uint timestamp;
// Get the current block number (in the context of this contract, we use the length of the array)
blockNumber: blockNumber,
timestamp: timestamp,
data: _data,
});
blockchain.push(newBlock);
return newBlock.blockHash;
function calculateHash(uint _blockNumber, uint _timestamp, string memory _data) internal pure returns
(bytes32) {
function getBlock(uint _index) public view returns (uint, uint, string memory, bytes32) {
}
Calculator:
// SPDX-License-Identifier: MIT
contract Calculator {
return a + b;
return a - b;
return a * b;
return a / b;
ERC-20 Token:
// SPDX-License-Identifier: MIT
_mint(to, amount);
_burn(msg.sender, amount);
Simple Voting:
// SPDX-License-Identifier: MIT
contract SimpleVoting {
// Candidate names
candidates = _candidates;
votes[candidateName]++;
return votes[candidateName];
Simple Bank:
// SPDX-License-Identifier: MIT
contract SimpleBank {
balances[msg.sender] += msg.value;
balances[msg.sender] -= _amount;
payable(msg.sender).transfer(_amount);
return balances[msg.sender];