Last active
September 15, 2023 06:09
-
-
Save cod3cow/5480724004b6a76a42ab6ecef732b7dc to your computer and use it in GitHub Desktop.
Basic Solidity Snippets for Smart Contracts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @title Solidity Snippets | |
* @author Alexander Schmidt | |
* @notice Smart Contract Bootcamp | |
*/ | |
/** | |
* Table of Content | |
* | |
* Constructor | |
* Decimals | |
* Events | |
* Inheritance | |
* Interface | |
* Keccak256 | |
* Library | |
* Mapping | |
* Modifier | |
* Overloading | |
* Pure vs View | |
* Require | |
* Sender | |
* Signature | |
* Struct | |
*/ | |
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity ^0.8.18; | |
/** | |
* Constructor | |
*/ | |
contract ConstructorExample { | |
uint256 public number; | |
constructor(uint256 _number) { | |
number = _number; | |
} | |
} | |
/** | |
* Decimals | |
*/ | |
contract Decimals { | |
uint256 decimalsUSDC = 1e6; | |
uint256 decimalsETHER = 1e18; | |
uint256 priceOfEtherInUSDC = 2_000 * decimalsUSDC; | |
uint256 usdcAmount = 4_000 * decimalsUSDC; | |
uint256 etherAmount = usdcAmount * decimalsETHER / priceOfEtherInUSDC; | |
uint256 repition = 4_000 * 1e6 * 1e18 / 2_000 * 1e6; | |
} | |
/** | |
* Events | |
*/ | |
contract Events { | |
uint256 nextPositionId; | |
event PositionOpened(address indexed account, uint256 indexed positionId, uint256 amount); | |
function openPosition(uint256 amount) external { | |
// ... some logic | |
emit PositionOpened(msg.sender, nextPositionId, amount); | |
} | |
} | |
/** | |
* Inheritance | |
*/ | |
contract ERC20 { | |
string public name; | |
string public symbol; | |
constructor(string memory name_, string memory symbol_) { | |
name = name_; | |
symbol = symbol_; | |
} | |
function _mint(address account, uint256 amount) internal virtual { | |
// omitted for brevity | |
} | |
} | |
contract RealToken is ERC20 { | |
constructor() ERC20("CoolToken", "CT") { | |
_mint(msg.sender, 1_000_000 * 1e18); | |
} | |
} | |
/** | |
* Interface | |
*/ | |
interface ExampleInterface { | |
function getNumber() external view returns (uint256); | |
} | |
contract ExampleContract { | |
ExampleInterface exampleContract; | |
constructor(address _exampleContract) { | |
exampleContract = ExampleInterface(_exampleContract); | |
} | |
function getNumberFromOtherContract() external view returns (uint256) { | |
return exampleContract.getNumber(); | |
} | |
} | |
/** | |
* Keccak256 | |
*/ | |
contract Keccak256 { | |
bytes32 public hash; | |
function set(string memory message) external { | |
hash = keccak256(abi.encode(message)); | |
} | |
function verify(string memory message) external view returns (bool) { | |
return hash == keccak256(abi.encode(message)); | |
} | |
} | |
/** | |
* Library | |
*/ | |
library LibraryExample { | |
function add(uint256 a, uint256 b) external pure returns (uint256) { | |
return a + b; | |
} | |
} | |
contract LibraryUser { | |
function useLibrary() external pure returns (uint256) { | |
return LibraryExample.add(1, 2); | |
} | |
} | |
/** | |
* Library Safe ERC20 | |
*/ | |
// import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | |
// contract SafeERC20Example { | |
// using SafeERC20 for IERC20; | |
// IERC20 public token; | |
// constructor(address _token) { | |
// token = IERC20(_token); | |
// } | |
// function transfer(address to, uint256 amount) external { | |
// token.safeTransfer(to, amount); | |
// } | |
// } | |
/** | |
* Library Struct | |
*/ | |
library LibraryExample2 { | |
struct Position { | |
uint256 collateral; | |
int256 entryPrice; | |
} | |
function addCollateral(Position storage self_, uint256 amount) external { | |
self_.collateral += amount; | |
} | |
} | |
contract LibraryUser2 { | |
LibraryExample2.Position public position; | |
using LibraryExample2 for LibraryExample2.Position; | |
function addCollateral(uint256 amount) external { | |
position.addCollateral(amount); | |
} | |
} | |
/** | |
* Mapping | |
*/ | |
contract MappingExample { | |
// Mapping from address to uint | |
mapping(address => uint256) public addressToUint; | |
// Nested mapping | |
mapping(address => mapping(uint256 => bool)) public nestedMapping; | |
// Set the value of the mapping | |
function setMappingValue(uint256 _value) public { | |
addressToUint[msg.sender] = _value; | |
} | |
// Set the value of the nested mapping | |
function setNestedMappingValue(uint256 _value) public { | |
nestedMapping[msg.sender][_value] = true; | |
} | |
} | |
/** | |
* Modifier | |
*/ | |
contract ModifierExample { | |
uint256 public number; | |
modifier addNumber(uint256 _number) { | |
number += _number; | |
_; | |
number += _number; | |
} | |
function addNumberAndReturn(uint256 _number) external addNumber(_number) returns (uint256) { | |
return number; | |
} | |
} | |
/** | |
* Overloading | |
*/ | |
contract OverloadingForDefaultParameters { | |
function foo(uint256 a, uint256 b) public pure returns (uint256) { | |
return a + b; | |
} | |
function foo(uint256 a) external pure returns (uint256) { | |
return foo(a, 0); | |
} | |
} | |
/** | |
* Pure vs View | |
*/ | |
contract PureVsView { | |
uint256 public counter; | |
function increment() external { | |
counter++; | |
} | |
function getCounter() external view returns (uint256) { | |
return counter; | |
} | |
function getCounterPure() external pure returns (uint256) { | |
return 0; | |
} | |
function getCounterAndVariable() external view returns (uint256) { | |
return counter + 0; | |
} | |
} | |
/** | |
* Require | |
*/ | |
contract RequireExample { | |
uint256 public number; | |
constructor(uint256 _number) { | |
require(_number > 0, "number has to be greater than 0"); | |
number = _number; | |
} | |
} | |
/** | |
* Sender | |
*/ | |
contract SenderExample { | |
function isContract() external view returns (bool) { | |
return msg.sender != tx.origin; | |
} | |
} | |
/** | |
* Signature | |
*/ | |
contract Signature { | |
function verify(address signer, bytes32 messageHash, uint8 v, bytes32 r, bytes32 s) external pure returns (bool) { | |
return signer == ecrecover(messageHash, v, r, s); | |
} | |
} | |
/** | |
* Struct | |
*/ | |
library LibraryExample3 { | |
struct Position { | |
uint256 collateral; | |
int256 entryPrice; | |
} | |
} | |
contract StructAsInputParameter { | |
mapping(uint256 => LibraryExample3.Position) positions; | |
uint256 nextId; | |
function openPosition(LibraryExample3.Position memory position) external { | |
positions[++nextId] = position; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment