generated from Kwenta/foundry-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix encoding & hardhat test #18
Merged
Merged
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
558f4a7
⚙️ Update .gitignore for hardhat
tommyrharper 8f841af
⚙️ Integrate and install hardhat
tommyrharper 5bdfbbc
🗑️ Delete Lock contract
tommyrharper 1b36a92
✅ 🪖 Add hardhat Signature test
tommyrharper ec42d2f
✅ Add signature scaffold
tommyrharper e7fa90a
✅ Update to use signature
tommyrharper 17d844c
✅ 🔧 Remove owner from test use signer
tommyrharper 9145753
✅ Complete hardhat signer test
tommyrharper 28099ca
👷🏻 🪲 Fix conditional order hash bug
tommyrharper 760a9a0
✅ 🪲 Update getConditionalOrderSignatureRaw with encodePacked
tommyrharper 9ec92ed
🚀 Add hardhat test to ci
tommyrharper df98ede
🚀 🪲 Remove hardhat compile
tommyrharper 28d8ef1
🚀 🪲 Remove weird package.json thing
tommyrharper d2db657
🚀 🪲 Change node and npm versions in ci
tommyrharper ffc21c9
🚀 Update ci npm & node versions
tommyrharper 1a0c4f1
🗑️ Delete package.json
tommyrharper 20a23f0
🗑️ commented compile step to CI
tommyrharper 8761f57
✅ test values in conditions hardhat
tommyrharper 7928df5
👷🏻 🪲 Fix out-of-bounds error
tommyrharper 38347dc
Merge pull request #19 from Kwenta/fix-encoding-hardhat-ci
tommyrharper 6f97fbd
⚙️ Add package-lock.json to .gitignore
tommyrharper c142518
✅ 🧹 Add correct conditions type
tommyrharper e20490a
✅ 🧹 Add conditions.length to hashedConditions in test utils
tommyrharper File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,31 @@ jobs: | |
run: | | ||
forge test --fork-url ${{ secrets.OPTIMISM_GOERLI_RPC_URL }} --etherscan-api-key ${{ secrets.ETHERSCAN_API_KEY }} -vvv | ||
id: test | ||
|
||
hardhat_test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
|
||
- name: Check out repository code | ||
uses: actions/checkout@v2 | ||
|
||
- name: Building on Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: '18.12.0' | ||
|
||
- name: Update NPM | ||
run: npm install -g [email protected] | ||
- uses: actions/cache@v2 | ||
with: | ||
path: '**/node_modules' | ||
key: ${{ runner.os }}-modules-${{ hashFiles('**/package-lock.json') }} | ||
|
||
- name: Install dependencies | ||
run: npm i --no-audit | ||
|
||
- name: Execute contract tests | ||
run: npx hardhat test |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { HardhatUserConfig } from "hardhat/config"; | ||
import "@nomicfoundation/hardhat-toolbox"; | ||
import "hardhat-preprocessor"; | ||
import fs from "fs"; | ||
|
||
function getRemappings() { | ||
return fs | ||
.readFileSync("remappings.txt", "utf8") | ||
.split("\n") | ||
.filter(Boolean) // remove empty lines | ||
.map((line) => line.trim().split("=")); | ||
} | ||
|
||
const config: HardhatUserConfig = { | ||
solidity: "0.8.20", | ||
preprocess: { | ||
eachLine: (hre) => ({ | ||
transform: (line: string) => { | ||
if (line.match(/^\s*import /i)) { | ||
for (const [from, to] of getRemappings()) { | ||
if (line.includes(from)) { | ||
line = line.replace(from, to); | ||
break; | ||
} | ||
} | ||
} | ||
return line; | ||
}, | ||
}), | ||
}, | ||
paths: { | ||
sources: "./src", | ||
cache: "./cache_hardhat", | ||
}, | ||
}; | ||
|
||
export default config; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
@openzeppelin/contracts/=lib/trusted-multicall-forwarder/lib/openzeppelin-contracts/contracts/ | ||
ds-test/=lib/forge-std/lib/ds-test/src/ | ||
erc4626-tests/=lib/trusted-multicall-forwarder/lib/openzeppelin-contracts/lib/erc4626-tests/ | ||
forge-std/=lib/forge-std/src/ | ||
openzeppelin-contracts/=lib/trusted-multicall-forwarder/lib/openzeppelin-contracts/ | ||
synthetix-v3/=lib/synthetix-v3/ | ||
trusted-multicall-forwarder/=lib/trusted-multicall-forwarder/src/ |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { loadFixture } from "@nomicfoundation/hardhat-toolbox/network-helpers"; | ||
import { expect } from "chai"; | ||
import { ethers } from "hardhat"; | ||
|
||
const ONE_ADDRESS = "0x0000000000000000000000000000000000000001"; | ||
|
||
// example data | ||
const signerPrivateKey = | ||
"0xe690d00bd51f5343c6999d8e88328e3dfa0111b65f2a8790d48f89fe43ad07c0"; | ||
const marketId = "200"; | ||
const accountId = "170141183460469231731687303715884105756"; | ||
const sizeDelta = "1000000000000000000"; | ||
const settlementStrategyId = "0"; | ||
const acceptablePrice = | ||
"115792089237316195423570985008687907853269984665640564039457584007913129639935"; | ||
const isReduceOnly = false; | ||
const trackingCode = | ||
"0x4b57454e54410000000000000000000000000000000000000000000000000000"; | ||
const referrer = "0xF510a2Ff7e9DD7e18629137adA4eb56B9c13E885"; | ||
const nonce = "0"; | ||
const requireVerified = false; | ||
const trustedExecutor = "0x7FA9385bE102ac3EAc297483Dd6233D62b3e1496"; | ||
const maxExecutorFee = | ||
"115792089237316195423570985008687907853269984665640564039457584007913129639935"; | ||
// 0x6162630000000000000000000000000000000000000000000000000000000000 = abc | ||
// 0x6465660000000000000000000000000000000000000000000000000000000000 = def | ||
// 0x6768690000000000000000000000000000000000000000000000000000000000 = ghi | ||
const conditions: string[] = [ | ||
"0x6162630000000000000000000000000000000000000000000000000000000000", | ||
"0x6465660000000000000000000000000000000000000000000000000000000000", | ||
"0x6768690000000000000000000000000000000000000000000000000000000000", | ||
]; | ||
|
||
describe("Signature", function () { | ||
// We define a fixture to reuse the same setup in every test. | ||
// We use loadFixture to run this setup once, snapshot that state, | ||
// and reset Hardhat Network to that snapshot in every test. | ||
async function bootstrapSystem() { | ||
// Contracts are deployed using the first signer/account by default | ||
const [owner, otherAccount] = await ethers.getSigners(); | ||
|
||
const Engine = await ethers.getContractFactory("Engine"); | ||
const engine = await Engine.deploy( | ||
ONE_ADDRESS, | ||
ONE_ADDRESS, | ||
ONE_ADDRESS, | ||
ONE_ADDRESS, | ||
ONE_ADDRESS | ||
); | ||
await engine.waitForDeployment(); | ||
|
||
return { engine, owner, otherAccount }; | ||
} | ||
|
||
describe("Signature checks", function () { | ||
it("The engine deployed successfully", async function () { | ||
const { engine } = await loadFixture(bootstrapSystem); | ||
|
||
expect(await engine.getAddress()).to.exist; | ||
}); | ||
|
||
it("Signature is verified", async () => { | ||
const { engine } = await loadFixture(bootstrapSystem); | ||
const wallet = new ethers.Wallet(signerPrivateKey); | ||
const signer = wallet.address; | ||
const engineAddress = await engine.getAddress(); | ||
|
||
const domain = { | ||
name: "SMv3: OrderBook", | ||
version: "1", | ||
chainId: 31337, | ||
verifyingContract: engineAddress, | ||
}; | ||
|
||
const types = { | ||
OrderDetails: [ | ||
{ name: "marketId", type: "uint128" }, | ||
{ name: "accountId", type: "uint128" }, | ||
{ name: "sizeDelta", type: "int128" }, | ||
{ name: "settlementStrategyId", type: "uint128" }, | ||
{ name: "acceptablePrice", type: "uint256" }, | ||
{ name: "isReduceOnly", type: "bool" }, | ||
{ name: "trackingCode", type: "bytes32" }, | ||
{ name: "referrer", type: "address" }, | ||
], | ||
ConditionalOrder: [ | ||
{ name: "orderDetails", type: "OrderDetails" }, | ||
{ name: "signer", type: "address" }, | ||
{ name: "nonce", type: "uint256" }, | ||
{ name: "requireVerified", type: "bool" }, | ||
{ name: "trustedExecutor", type: "address" }, | ||
{ name: "maxExecutorFee", type: "uint256" }, | ||
{ name: "conditions", type: "bytes[]" }, | ||
], | ||
}; | ||
|
||
let orderDetails = { | ||
marketId: BigInt(marketId), | ||
accountId: BigInt(accountId), | ||
sizeDelta: BigInt(sizeDelta), | ||
settlementStrategyId: BigInt(settlementStrategyId), | ||
acceptablePrice: BigInt(acceptablePrice), | ||
isReduceOnly: isReduceOnly, | ||
trackingCode: trackingCode, | ||
referrer: referrer, | ||
}; | ||
|
||
// define the conditional order struct | ||
let conditionalOrder = { | ||
orderDetails: orderDetails, | ||
signer: signer, | ||
nonce: BigInt(nonce), | ||
requireVerified: requireVerified, | ||
trustedExecutor: trustedExecutor, | ||
maxExecutorFee: BigInt(maxExecutorFee), | ||
conditions: conditions, | ||
}; | ||
|
||
const signature = await wallet.signTypedData( | ||
domain, | ||
types, | ||
conditionalOrder | ||
); | ||
|
||
const recoveredAddress = ethers.verifyTypedData( | ||
domain, | ||
types, | ||
conditionalOrder, | ||
signature | ||
); | ||
|
||
expect(recoveredAddress).to.equal(signer); | ||
|
||
const res = await engine.verifySignature(conditionalOrder, signature); | ||
expect(res).to.be.true; | ||
}); | ||
}); | ||
}); |
tommyrharper marked this conversation as resolved.
Show resolved
Hide resolved
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2020", | ||
"module": "commonjs", | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"strict": true, | ||
"skipLibCheck": true, | ||
"resolveJsonModule": true | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
very nicely done ☑️