Skip to content

Instantly share code, notes, and snippets.

@ashwinYardi
ashwinYardi / EIP1167Factory.sol
Last active January 29, 2024 03:24
EIP-1167 clone method
function clone(address implementation) internal returns (address instance) {
assembly {
let ptr := mload(0x40)
mstore(ptr, 0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000)
mstore(add(ptr, 0x14), shl(0x60, implementation))
mstore(add(ptr, 0x28), 0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000)
instance := create(0, ptr, 0x37)
}
require(instance != address(0), "ERC1167: create failed");
}
@ashwinYardi
ashwinYardi / DemoTryCatch.sol
Last active April 22, 2023 13:51
Solidity contract to demo try / catch statements and error retrieval.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
function showMessage() public pure returns (string memory) {
return "Hello, World!";
}
}
contract HelloWorldFactory {
@ashwinYardi
ashwinYardi / DemoTryCatch.sol
Last active April 21, 2023 14:04
Contract to demo how to use try/catch in Solidity.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
function showMessage() public pure returns (string memory) {
return "Hello, World!";
}
}
contract HelloWorldFactory {
@ashwinYardi
ashwinYardi / LibFactorial.sol
Created March 30, 2023 05:20
Demo Library to calculate the factorial of the given number.
pragma solidity ^0.8.17;
library Factorial {
function factorial(uint n) public pure returns (uint) {
uint result = 1;
for (uint i = 1; i <= n; i++) {
result *= i;
}
return result;
}
@ashwinYardi
ashwinYardi / FactorialCalculator.sol
Created March 30, 2023 05:19
Solidity Contract for demoing how to use libraries. The contract uses Factorial Library to calculate the factorial of the number.
import "./Factorial.sol";
contract FactorialCalculator {
using Factorial for uint;
uint public factorial;
constructor(uint n) public {
factorial = n.factorial();
}
@ashwinYardi
ashwinYardi / DemoFallbackFunctions.sol
Created February 24, 2023 12:51
Contract to demo fallback function and receive function.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract DemoFallbackFunctions {
event Log(string typeOfFunction, uint gas);
// Fallback function must be declared as external.
fallback() external payable {
@ashwinYardi
ashwinYardi / DelegateCall.sol
Created February 1, 2023 04:19
Contract to demonstrate delegate call in solidity.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
// NOTE: Deploy this contract first
contract IronMan {
uint public power;
uint public speed;
uint public impact;
@ashwinYardi
ashwinYardi / PrivateAccessDemo.sol
Last active September 28, 2022 11:28
Sample contract with privare variables
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
contract PrivateAccessDemo {
uint256 private privateVariable;
uint256 public publicVariable;
constructor(uint256 privateValue, uint256 publicValue) {
privateVariable = privateValue;
@ashwinYardi
ashwinYardi / DemoCustomError.sol
Created August 26, 2022 12:44
Demo solidity contract to demo custom errors
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract DemoCustomError {
error Unauthorized();
error myCustomError(string message);
address public owner = address(0x0);
@ashwinYardi
ashwinYardi / DemoErrors.sol
Created August 26, 2022 12:40
Simple solidity contract to demo error throwing mechanisms in solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract DemoErrors {
function demoRequire(uint input) public pure {
require(input > 5, "input must be greater than 5");
}
function demoRevert(uint input) public pure {