|
// SPDX-License-Identifier: MIT // license of your code, is it free and open-source or not? |
|
pragma solidity ^0.8.0; // specify the solidity compiler version |
|
|
|
contract WorkshopExtended { |
|
// Unsigned Integers (uint) |
|
// uints are positive integers only |
|
// uint8, uint16, uint24, ..., uint256 are available (multiples of 8 but size, until 256 bits) |
|
// uint is an alias for uint256 |
|
|
|
uint8 public smallestUint = 255; // Can hold 0 to 255 (2^8 - 1) // Binary range: 00000000 (0) to 11111111 (255) |
|
// Then you can get other uints by adding 8 to uint8, i.e, uint8+8 = uint16 and then continue, uint24, uint32, and so on to uint256 |
|
uint16 public uint16Example = 65535; // Can hold 0 to 65535 (2^16 - 1) // Binary range: 00000000 00000000 (0) to 11111111 11111111 (65,535) |
|
uint24 public uint24Example = 16777215; // Can hold 0 to 16777215 (2^24 - 1) // Binary range: 00000000 00000000 00000000 (0) to 11111111 11111111 11111111 (16,777,215) |
|
uint32 public uint32Example = 4294967295; // Can hold 0 to 4294967295 (2^32 - 1) // Binary range: 00000000 00000000 00000000 00000000 (0) to 11111111 11111111 11111111 11111111 (4,294,967,295) |
|
uint256 public largestUint = |
|
115792089237316195423570985008687907853269984665640564039457584007913129639935; // Can hold max of 256 bits positive integers, 2^256-1 |
|
|
|
// Signed Integers (int) |
|
// ints can be both positive and negative |
|
// int8, int16, int24, ..., int256 are available |
|
// int is an alias for int256 |
|
|
|
int8 public smallestInt = -128; // Can hold -128 to 127 (-(2^7) to 2^7 - 1) |
|
int16 public int16Example = -32768; // Can hold -32768 to 32767 (-(2^15) to 2^15 - 1) |
|
int24 public int24Example = 8388607; // Can hold -8388608 to 8388607 (-(2^23) to 2^23 - 1) |
|
int32 public int32Example = -2147483648; // Can hold -2147483648 to 2147483647 (-(2^31) to 2^31 - 1) |
|
int256 public largestInt = |
|
-57896044618658097711785492504343953926634992332820282019728792003956564819968; // Can hold -(2^255) to 2^255 - 1 |
|
|
|
// Address |
|
// Holds a 20 byte Ethereum address |
|
address public contractOwner = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; |
|
|
|
// Boolean |
|
// Can hold true or false |
|
bool public isActive = true; // or false |
|
|
|
// Bytes |
|
// Fixed-size byte arrays |
|
// bytes1, bytes2, bytes3, ..., bytes32 are available |
|
bytes1 public singleByte = 0x41; // Holds a single byte (8 bits) |
|
bytes32 public data = "Hello, Solidity!"; // Holds 32 bytes |
|
|
|
// Dynamic bytes array |
|
bytes public dynamicBytes = "Dynamic bytes array"; |
|
|
|
// String |
|
// Dynamic UTF-8 encoded string |
|
string public greeting = "Welcome to Solidity Workshop"; |
|
|
|
// Array |
|
// Dynamic array (can change in size) |
|
uint256[] public dynamicArray; |
|
// Fixed-size array (size cannot change after declaration) |
|
uint256[5] public fixedArray; |
|
|
|
// Mapping |
|
// Key-value store |
|
mapping(address => uint256) public balances; |
|
|
|
// Enum |
|
// User-defined type with a finite set of constant values |
|
enum Status { |
|
Pending, |
|
Approved, |
|
Rejected |
|
} |
|
Status public currentStatus; |
|
|
|
// Struct |
|
// User-defined type that groups together related data |
|
struct Person { |
|
string name; |
|
uint256 age; |
|
address wallet; |
|
} |
|
Person public workshopLead; |
|
|
|
// Events |
|
// Used to emit logs on the blockchain |
|
event StatusChanged(Status newStatus); |
|
event EtherReceived(address sender, uint256 amount); |
|
|
|
// Constructor |
|
// Called once when the contract is deployed |
|
constructor() { |
|
contractOwner = msg.sender; |
|
currentStatus = Status.Pending; |
|
workshopLead = Person("Jesse", 1000, msg.sender); |
|
} |
|
|
|
// Function to initialize dynamic array |
|
function initializeDynamicArray() public { |
|
dynamicArray.push(1); |
|
dynamicArray.push(2); |
|
dynamicArray.push(3); |
|
} |
|
|
|
// Function to initialize fixed array |
|
function initializeFixedArray() public { |
|
fixedArray[0] = 10; |
|
fixedArray[1] = 20; |
|
fixedArray[2] = 30; |
|
fixedArray[3] = 40; |
|
fixedArray[4] = 50; |
|
} |
|
|
|
// Function to demonstrate enum usage |
|
function changeStatus(Status newStatus) public { |
|
require(msg.sender == contractOwner, "Only owner can change status"); |
|
currentStatus = newStatus; |
|
emit StatusChanged(newStatus); |
|
} |
|
|
|
// Function to demonstrate struct usage |
|
function updateWorkshopLead( |
|
string memory _name, |
|
uint256 _age, |
|
address _wallet |
|
) public { |
|
require( |
|
msg.sender == contractOwner, |
|
"Only owner can update workshop lead" |
|
); |
|
workshopLead = Person(_name, _age, _wallet); |
|
} |
|
|
|
// Pure function (doesn't read or modify state) |
|
function add(uint256 a, uint256 b) public pure returns (uint256) { |
|
return a + b; |
|
} |
|
|
|
// View function (reads but doesn't modify state) |
|
function getContractBalance() public view returns (uint256) { |
|
return address(this).balance; |
|
} |
|
|
|
// Payable function (can receive Ether) |
|
function deposit() public payable { |
|
balances[msg.sender] += msg.value; |
|
} |
|
|
|
// Function demonstrating data location (memory) |
|
function processPersonMemory(Person memory _person) |
|
public |
|
pure |
|
returns (string memory) |
|
{ |
|
return _person.name; |
|
} |
|
|
|
// Internal function (can only be called within the contract or derived contracts) |
|
function internalFunction() internal pure returns (string memory) { |
|
return "This is an internal function"; |
|
} |
|
|
|
// Function to demonstrate sending Ether using transfer |
|
function withdrawUsingTransfer(uint256 _amount) public { |
|
require(balances[msg.sender] >= _amount, "Insufficient balance"); |
|
balances[msg.sender] -= _amount; |
|
payable(msg.sender).transfer(_amount); // transfer throws an error on failure |
|
} |
|
|
|
// Function to demonstrate sending Ether using call |
|
function withdrawUsingCall(uint256 _amount) public { |
|
require(balances[msg.sender] >= _amount, "Insufficient balance"); |
|
balances[msg.sender] -= _amount; |
|
(bool sent, ) = payable(msg.sender).call{value: _amount}(""); // call returns a boolean indicating success or failure |
|
require(sent, "Failed to send Ether"); |
|
} |
|
|
|
// Receive function (called when Ether is sent to the contract without data) |
|
receive() external payable { |
|
emit EtherReceived(msg.sender, msg.value); |
|
} |
|
|
|
// Fallback function (called when a function that doesn't exist is called or when Ether is sent with data) |
|
fallback() external payable { |
|
emit EtherReceived(msg.sender, msg.value); |
|
} |
|
} |