From 2cc32d000adf9435c7b823f51ebe65bdf5e2657f Mon Sep 17 00:00:00 2001 From: rymnc <43716372+rymnc@users.noreply.github.com> Date: Wed, 29 Mar 2023 12:28:42 +0530 Subject: [PATCH] feat: foundry integration --- .env.example | 2 +- .gitignore | 4 ++++ .gitmodules | 4 ++++ contracts/PoseidonHasher.sol | 2 +- contracts/Rln.sol | 2 +- foundry.toml | 6 ++++++ hardhat.config.ts | 11 ++++++++-- lib/forge-std | 1 + package.json | 11 +++++++--- test/PoseidonHasher.t.sol | 28 ++++++++++++++++++++++++++ test/{poseidon.ts => poseidon.test.ts} | 0 test/{rln.ts => rln.test.ts} | 0 yarn.lock | 19 ++++++++++++++++- 13 files changed, 81 insertions(+), 9 deletions(-) create mode 100644 .gitmodules create mode 100644 foundry.toml create mode 160000 lib/forge-std create mode 100644 test/PoseidonHasher.t.sol rename test/{poseidon.ts => poseidon.test.ts} (100%) rename test/{rln.ts => rln.test.ts} (100%) diff --git a/.env.example b/.env.example index 861d3b1..83dc51a 100644 --- a/.env.example +++ b/.env.example @@ -1,3 +1,3 @@ ETHERSCAN_API_KEY=ABC123ABC123ABC123ABC123ABC123ABC1 -ROPSTEN_URL=https://eth-ropsten.alchemyapi.io/v2/ +SEPOLIA_URL=https://eth-sepolia.alchemyapi.io/v2/ PRIVATE_KEY=0xabc123abc123abc123abc123abc123abc123abc123abc123abc123abc123abc1 diff --git a/.gitignore b/.gitignore index 36077f2..9bd0508 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,7 @@ typechain #Hardhat files cache artifacts + +#Foundry files +cache_forge +out diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..80b9773 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,4 @@ +[submodule "lib/forge-std"] + path = lib/forge-std + url = https://github.com/foundry-rs/forge-std + branch = v1.5.2 diff --git a/contracts/PoseidonHasher.sol b/contracts/PoseidonHasher.sol index c1de721..8b441a9 100644 --- a/contracts/PoseidonHasher.sol +++ b/contracts/PoseidonHasher.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: (MIT OR Apache-2.0) +// SPDX-License-Identifier: MIT // Forked from https://github.com/kilic/rlnapp/ diff --git a/contracts/Rln.sol b/contracts/Rln.sol index e70085b..48b5362 100644 --- a/contracts/Rln.sol +++ b/contracts/Rln.sol @@ -1,4 +1,4 @@ -// SPDX-License-Identifier: (MIT OR Apache-2.0) +// SPDX-License-Identifier: MIT pragma solidity 0.8.15; diff --git a/foundry.toml b/foundry.toml new file mode 100644 index 0000000..4f3bf9f --- /dev/null +++ b/foundry.toml @@ -0,0 +1,6 @@ +[profile.default] +src = 'contracts' +out = 'out' +libs = ['node_modules', 'lib'] +test = 'test' +cache_path = 'cache_forge' \ No newline at end of file diff --git a/hardhat.config.ts b/hardhat.config.ts index 8318da8..49cd3db 100644 --- a/hardhat.config.ts +++ b/hardhat.config.ts @@ -3,24 +3,31 @@ import * as dotenv from "dotenv"; import { HardhatUserConfig } from "hardhat/config"; import { NetworksUserConfig } from "hardhat/types"; +import "@nomicfoundation/hardhat-foundry"; import "hardhat-deploy"; +import "@nomiclabs/hardhat-etherscan"; import "@nomiclabs/hardhat-ethers"; import "@nomiclabs/hardhat-waffle"; import "hardhat-gas-reporter"; import "solidity-coverage"; dotenv.config(); -const { SEPOLIA_URL, PRIVATE_KEY } = process.env; +const { SEPOLIA_URL, PRIVATE_KEY, ETHERSCAN_API_KEY } = process.env; const getNetworkConfig = (): NetworksUserConfig | undefined => { if (SEPOLIA_URL && PRIVATE_KEY) { return { - goerli: { + sepolia: { url: SEPOLIA_URL, accounts: [PRIVATE_KEY], forking: { url: SEPOLIA_URL, }, + verify: { + etherscan: { + apiKey: ETHERSCAN_API_KEY, + } + } }, localhost_integration: { url: "http://localhost:8545", diff --git a/lib/forge-std b/lib/forge-std new file mode 160000 index 0000000..2b58ecb --- /dev/null +++ b/lib/forge-std @@ -0,0 +1 @@ +Subproject commit 2b58ecbcf3dfde7a75959dc7b4eb3d0670278de6 diff --git a/package.json b/package.json index 7030a33..2c595d0 100644 --- a/package.json +++ b/package.json @@ -2,25 +2,30 @@ "name": "rln-contract", "license": "(MIT OR Apache-2.0)", "scripts": { - "start": "hardhat node", + "start": "hardhat node --export-all=deployments/allDeployments.json", "compile": "hardhat compile", + "test": "yarn test:foundry && yarn test:hardhat", "test:hardhat": "hardhat test", "test:hardhat:localhost": "yarn test:hardhat --network localhost", "test:hardhat:sepolia": "yarn test:hardhat --network sepolia", "test:foundry": "forge test", - "deploy": "hardhat run scripts/deploy.ts --network", + "deploy": "hardhat deploy --export-all=deployments/allDeployments.json --network", "deploy:sepolia": "yarn deploy sepolia", "deploy:localhost": "yarn deploy localhost", + "verify:sepolia": "hardhat --network sepolia etherscan-verify", "coverage": "hardhat coverage", "fmt": "prettier --write \"**/*.{js,ts}\"", "lint": "prettier --check \"**/*.{js,ts}\"", "prepare": "husky install" }, "devDependencies": { + "@nomicfoundation/hardhat-foundry": "^1.0.0", "@nomiclabs/hardhat-ethers": "npm:hardhat-deploy-ethers", - "@nomiclabs/hardhat-etherscan": "^3.1.0", + "@nomiclabs/hardhat-etherscan": "^3.1.7", "@nomiclabs/hardhat-waffle": "^2.0.3", + "@types/chai": "^4.3.6", "@types/mocha": "^9.1.1", + "@types/node": "^16.11.6", "chai": "^4.3.6", "ethereum-waffle": "^3.4.4", "ethers": "^5.7.2", diff --git a/test/PoseidonHasher.t.sol b/test/PoseidonHasher.t.sol new file mode 100644 index 0000000..7a24fc4 --- /dev/null +++ b/test/PoseidonHasher.t.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: Unlicense +pragma solidity ^0.8.15; + +import "../contracts/PoseidonHasher.sol"; +import "forge-std/Test.sol"; + +contract PoseidonHasherTest is Test { + PoseidonHasher public poseidon; + + /// @dev Setup the testing environment. + function setUp() public { + poseidon = new PoseidonHasher(); + } + + /// @dev Ensure that you can hash a value. + function testHasher(uint256 value) public { + assertEq(poseidon.hash(value), poseidon.hash(value)); + } + + function testHasher() public { + assertEq( + poseidon.hash( + 19014214495641488759237505126948346942972912379615652741039992445865937985820 + ), + 0x0c3ac305f6a4fe9bfeb3eba978bc876e2a99208b8b56c80160cfb54ba8f02368 + ); + } +} diff --git a/test/poseidon.ts b/test/poseidon.test.ts similarity index 100% rename from test/poseidon.ts rename to test/poseidon.test.ts diff --git a/test/rln.ts b/test/rln.test.ts similarity index 100% rename from test/rln.ts rename to test/rln.test.ts diff --git a/yarn.lock b/yarn.lock index e8ba1a7..987239b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -659,6 +659,13 @@ mcl-wasm "^0.7.1" rustbn.js "~0.2.0" +"@nomicfoundation/hardhat-foundry@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@nomicfoundation/hardhat-foundry/-/hardhat-foundry-1.0.0.tgz#14eba643138b09e4a8f2fed2a99bc9c96c487b0a" + integrity sha512-/2cmtIZPnsQj/SRIu9idbBan5j19RD35MECAGmZCcuXX4AO6Wn0nOnpUwpcvGomKW403h4+rXh8AHMWC4Vvw0Q== + dependencies: + chalk "^2.4.2" + "@nomicfoundation/solidity-analyzer-darwin-arm64@0.1.1": version "0.1.1" resolved "https://registry.yarnpkg.com/@nomicfoundation/solidity-analyzer-darwin-arm64/-/solidity-analyzer-darwin-arm64-0.1.1.tgz#4c858096b1c17fe58a474fe81b46815f93645c15" @@ -730,7 +737,7 @@ resolved "https://registry.yarnpkg.com/hardhat-deploy-ethers/-/hardhat-deploy-ethers-0.3.0-beta.13.tgz#b96086ff768ddf69928984d5eb0a8d78cfca9366" integrity sha512-PdWVcKB9coqWV1L7JTpfXRCI91Cgwsm7KLmBcwZ8f0COSm1xtABHZTyz3fvF6p42cTnz1VM0QnfDvMFlIRkSNw== -"@nomiclabs/hardhat-etherscan@^3.1.0": +"@nomiclabs/hardhat-etherscan@^3.1.7": version "3.1.7" resolved "https://registry.yarnpkg.com/@nomiclabs/hardhat-etherscan/-/hardhat-etherscan-3.1.7.tgz#72e3d5bd5d0ceb695e097a7f6f5ff6fcbf062b9a" integrity sha512-tZ3TvSgpvsQ6B6OGmo1/Au6u8BrAkvs1mIC/eURA3xgIfznUZBhmpne8hv7BXUzw9xNL3fXdpOYgOQlVMTcoHQ== @@ -996,6 +1003,11 @@ "@types/node" "*" "@types/responselike" "^1.0.0" +"@types/chai@^4.3.6": + version "4.3.4" + resolved "https://registry.yarnpkg.com/@types/chai/-/chai-4.3.4.tgz#e913e8175db8307d78b4e8fa690408ba6b65dee4" + integrity sha512-KnRanxnpfpjUTqTCXslZSEdLfXExwgNxYPdiO2WGUj8+HDjFi8R3k5RVKPeSCzLjCcshCAtVO2QBbVuAV4kTnw== + "@types/concat-stream@^1.6.0": version "1.6.1" resolved "https://registry.yarnpkg.com/@types/concat-stream/-/concat-stream-1.6.1.tgz#24bcfc101ecf68e886aaedce60dfd74b632a1b74" @@ -1075,6 +1087,11 @@ resolved "https://registry.yarnpkg.com/@types/node/-/node-12.20.55.tgz#c329cbd434c42164f846b909bd6f85b5537f6240" integrity sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ== +"@types/node@^16.11.6": + version "16.18.22" + resolved "https://registry.yarnpkg.com/@types/node/-/node-16.18.22.tgz#a6505a5da1387aae03fddfb93591118f27b4c0ea" + integrity sha512-LJSIirgASa1LicFGTUFwDY7BfKDtLIbijqDLkH47LxEo/jtdrtiZ4/kLPD99bEQhTcPcuh6KhDllHqRxygJD2w== + "@types/node@^8.0.0": version "8.10.66" resolved "https://registry.yarnpkg.com/@types/node/-/node-8.10.66.tgz#dd035d409df322acc83dff62a602f12a5783bbb3"