Skip to content

Commit

Permalink
Merkle proof contract
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Jan 1, 2022
1 parent 1c43f12 commit 9bab232
Show file tree
Hide file tree
Showing 10 changed files with 44,992 additions and 0 deletions.
2 changes: 2 additions & 0 deletions merkle_tree/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALCHEMY_API_KEY=
ROPSTEN_PRIVATE_KEY=
8 changes: 8 additions & 0 deletions merkle_tree/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
node_modules
.env

#Hardhat files
cache
artifacts
dist
typechain
16 changes: 16 additions & 0 deletions merkle_tree/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false,
"explicitTypes": "always",
"semi": true
}
}
]
}
53 changes: 53 additions & 0 deletions merkle_tree/contracts/MerkleProof.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
pragma solidity ^0.8.7;

import "hardhat/console.sol";

contract MerkleProof {
function verify(
bytes32[] memory proof,
bytes32 root,
bytes32 leaf,
uint256 index
) public pure returns (bool) {
bytes32 hash = leaf;
for (uint256 i = 0; i < proof.length; i++) {
bytes32 proofElement = proof[i];
if (index % 2 == 0) {
hash = keccak256(abi.encodePacked(hash, proofElement));
} else {
hash = keccak256(abi.encodePacked(proofElement, hash));
}

index = index / 2;
}

return hash == root;
}
}

contract TestMerkleProof is MerkleProof {
bytes32[] public hashes;

constructor() {
string[4] memory transactions = ["transaction 1", "transaction 2", "transaction 3", "transaction 4"];

for (uint256 i = 0; i < transactions.length; i++) {
hashes.push(keccak256(abi.encodePacked(transactions[i])));
}

uint256 n = transactions.length;
uint256 offset = 0;

while (n > 0) {
for (uint256 i = 0; i < n - 1; i += 2) {
hashes.push(keccak256(abi.encodePacked(hashes[offset + i], hashes[offset + i + 1])));
}
offset += n;
n = n / 2;
}
}

function getRoot() public view returns (bytes32) {
return hashes[hashes.length - 1];
}
}
56 changes: 56 additions & 0 deletions merkle_tree/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { config as dotenvConfig } from "dotenv";
import "@typechain/hardhat";
import "@nomiclabs/hardhat-ethers";
import "@nomiclabs/hardhat-waffle";
import "@nomiclabs/hardhat-etherscan";
import "solidity-coverage";
import "hardhat-gas-reporter";
import { HardhatUserConfig } from "hardhat/types/config";

dotenvConfig();

const gasPrice = parseInt(process.env.GAS_PRICE || "1000000000");

const config: HardhatUserConfig = {
solidity: {
compilers: [
{
version: "0.8.10",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
],
},
networks: {
hardhat: {
initialBaseFeePerGas: 0,
},
rinkeby: {
url: process.env.RINKEBY_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
gasPrice,
},
mainnet: {
url: process.env.MAINNET_URL || "",
accounts:
process.env.PRIVATE_KEY !== undefined ? [process.env.PRIVATE_KEY] : [],
gasPrice,
},
},
gasReporter: {
enabled: process.env.REPORT_GAS !== undefined,
currency: "USD",
gasPrice: 120,
coinmarketcap: process.env.COINMARKETCAP_API_KEY,
},
etherscan: {
apiKey: process.env.ETHERSCAN_API_KEY,
},
};

export default config;
Loading

0 comments on commit 9bab232

Please sign in to comment.