Skip to content

Instantly share code, notes, and snippets.

/**
* Javascript module to construct and hash EIP-712 typed messages to be signed by private key.
* [EIP712]{@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md} standard
*
* @author Ashwin Yardi
* @module eip712Signature
*/
const { padLeft, sha3 } = require("web3-utils");
const web3EthAbi = require("web3-eth-abi");
@ashwinYardi
ashwinYardi / DemoValueTypes.sol
Created July 13, 2022 10:25
Contract to demo value typed datatypes in solidity
// Solidity program to demonstrate
// value types
pragma solidity ^ 0.8.5;
// Creating a contract
contract DemoValueTypes {
// Initializing Bool variable
bool public boolean;
// Initializing Integer variable
@ashwinYardi
ashwinYardi / DemoReferenceTypes.sol
Created July 13, 2022 10:29
Contract to demo reference typed data types in Solidity
// Solidity program to demonstrate
// reference types
pragma solidity ^ 0.8.5;
// Creating a contract
contract DemoReferenceTypes {
// Defining an array of fixed size
uint[5] public fixedSizeArray = [uint(1), 2, 3, 4, 5];
// Defining a Structure
@ashwinYardi
ashwinYardi / StorageVariableDemo.sol
Created July 19, 2022 06:58
Solidity program to demonstrate how modifying storage items is expensive
// Solidity program to demonstrate
// how modifying storage items is expensive
pragma solidity ^ 0.8.5;
contract StorageVariableDemo
{
uint public temp;
/*
* @description:Below function runs the for loop 100 times and updates
@ashwinYardi
ashwinYardi / contarct-merkle-verification.ts
Created August 4, 2022 08:20
Hardhat script for deploying MerkleVerifier contract, creating merkle tree using the array of address and verify if an address is a part of this merkle root.
import { ethers } from "hardhat";
import keccak256 from "keccak256";
import { MerkleTree } from "merkletreejs";
import { merkleTreeLeaves, validAddress, invalidAddress } from "./utils/mock-data";
import { MerkleVerifier } from "../typechain-types/MerkleVerifier";
let merkleTree: MerkleTree;
let merkleVerifier: MerkleVerifier;
function generateMerkleTree() {
@ashwinYardi
ashwinYardi / MerkleVerifier.sol
Created August 4, 2022 12:28
Simple smart contract to perform merkle verifications on-chain
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
contract MerkleVerifier {
bytes32 public merkleRoot;
constructor(bytes32 _merkleRoot) {
@ashwinYardi
ashwinYardi / InheritedStorage.sol
Created August 19, 2022 15:17
Contract which describes InheritedStorage approach for proxies and diamonds.
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Storage {
uint firstVar;
uint secondVar;
string thirdVar;
string fourthVar;
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract FacetA {
struct DiamondStorage {
uint firstVar;
uint secondVar;
}
@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 {
@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);