diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8e7594194..8ea3c73a4 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -19,9 +19,11 @@ jobs: - uses: actions/setup-node@v3 with: node-version: 16 + cache-dependency-path: './test/js-scripts' cache: 'yarn' - run: yarn + working-directory: ./test/js-scripts - name: Install Foundry uses: foundry-rs/foundry-toolchain@v1 diff --git a/.nvmrc b/.nvmrc deleted file mode 100644 index 5edcff036..000000000 --- a/.nvmrc +++ /dev/null @@ -1 +0,0 @@ -v16 \ No newline at end of file diff --git a/.prettierignore b/.prettierignore deleted file mode 100644 index bff58ff66..000000000 --- a/.prettierignore +++ /dev/null @@ -1,2 +0,0 @@ -typechain/ -lib/forge-std/ diff --git a/.prettierrc b/.prettierrc deleted file mode 100644 index 31ba22d84..000000000 --- a/.prettierrc +++ /dev/null @@ -1,5 +0,0 @@ -{ - "semi": false, - "singleQuote": true, - "printWidth": 120 -} diff --git a/test/TickMath.t.sol b/test/TickMath.t.sol index c669ec788..c19702727 100644 --- a/test/TickMath.t.sol +++ b/test/TickMath.t.sol @@ -5,8 +5,9 @@ import {Test} from "forge-std/Test.sol"; import {Vm} from "forge-std/Vm.sol"; import {TickMathTest} from "../src/test/TickMathTest.sol"; import {TickMath} from "../src/libraries/TickMath.sol"; +import {JavascriptFfi} from "./utils/JavascriptFfi.sol"; -contract TickMathTestTest is Test { +contract TickMathTestTest is Test, JavascriptFfi { int24 constant MIN_TICK = -887272; int24 constant MAX_TICK = -MIN_TICK; @@ -113,14 +114,6 @@ contract TickMathTestTest is Test { function test_getSqrtRatioAtTick_matchesJavaScriptImplByOneHundrethOfABip() public { string memory jsParameters = ""; - string[] memory runJsInputs = new string[](6); - - // build ffi command string - runJsInputs[0] = "npm"; - runJsInputs[1] = "--silent"; - runJsInputs[2] = "run"; - runJsInputs[3] = "forge-test-getSqrtRatioAtTick"; - runJsInputs[4] = "--"; int24 tick = 50; @@ -138,8 +131,7 @@ contract TickMathTestTest is Test { tick = tick * 2; } - runJsInputs[5] = jsParameters; - bytes memory jsResult = vm.ffi(runJsInputs); + bytes memory jsResult = runScript("forge-test-getSqrtRatioAtTick", jsParameters); uint160[] memory jsSqrtRatios = abi.decode(jsResult, (uint160[])); for (uint256 i = 0; i < jsSqrtRatios.length; i++) { @@ -156,13 +148,6 @@ contract TickMathTestTest is Test { function test_getTickAtSqrtRatio_matchesJavascriptImplWithin1() public { string memory jsParameters = ""; - string[] memory runJsInputs = new string[](5); - - // build ffi command string - runJsInputs[0] = "npm"; - runJsInputs[1] = "--silent"; - runJsInputs[2] = "run"; - runJsInputs[3] = "forge-test-getTickAtSqrtRatio"; uint160 sqrtRatio = MIN_SQRT_RATIO; unchecked { @@ -176,8 +161,7 @@ contract TickMathTestTest is Test { } } - runJsInputs[4] = jsParameters; - bytes memory jsResult = vm.ffi(runJsInputs); + bytes memory jsResult = runScript("forge-test-getTickAtSqrtRatio", jsParameters); int24[] memory jsTicks = abi.decode(jsResult, (int24[])); for (uint256 i = 0; i < jsTicks.length; i++) { diff --git a/.yarnrc b/test/js-scripts/.yarnrc similarity index 100% rename from .yarnrc rename to test/js-scripts/.yarnrc diff --git a/package.json b/test/js-scripts/package.json similarity index 65% rename from package.json rename to test/js-scripts/package.json index b2474f943..7fe3bc9c0 100644 --- a/package.json +++ b/test/js-scripts/package.json @@ -1,6 +1,6 @@ { - "name": "@uniswap/v4-core", - "description": "🦄 Uniswap Protocol smart contracts", + "name": "v4-js-scripts", + "description": "Scripts for v4 tests", "license": "BUSL-1.1", "publishConfig": { "access": "restricted" @@ -27,7 +27,7 @@ "typescript": "^3.7.3" }, "scripts": { - "forge-test-getSqrtRatioAtTick": "npx ts-node test/js-scripts/getSqrtRatioAtTick.ts", - "forge-test-getTickAtSqrtRatio": "npx ts-node test/js-scripts/getTickAtSqrtRatio.ts" + "forge-test-getSqrtRatioAtTick": "npx ts-node src/getSqrtRatioAtTick.ts", + "forge-test-getTickAtSqrtRatio": "npx ts-node src/getTickAtSqrtRatio.ts" } } diff --git a/test/js-scripts/getSqrtRatioAtTick.ts b/test/js-scripts/src/getSqrtRatioAtTick.ts similarity index 100% rename from test/js-scripts/getSqrtRatioAtTick.ts rename to test/js-scripts/src/getSqrtRatioAtTick.ts diff --git a/test/js-scripts/getTickAtSqrtRatio.ts b/test/js-scripts/src/getTickAtSqrtRatio.ts similarity index 100% rename from test/js-scripts/getTickAtSqrtRatio.ts rename to test/js-scripts/src/getTickAtSqrtRatio.ts diff --git a/tsconfig.json b/test/js-scripts/tsconfig.json similarity index 100% rename from tsconfig.json rename to test/js-scripts/tsconfig.json diff --git a/yarn.lock b/test/js-scripts/yarn.lock similarity index 100% rename from yarn.lock rename to test/js-scripts/yarn.lock diff --git a/test/utils/JavascriptFfi.sol b/test/utils/JavascriptFfi.sol new file mode 100644 index 000000000..373b316b5 --- /dev/null +++ b/test/utils/JavascriptFfi.sol @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.20; + +import {CommonBase} from "forge-std/Base.sol"; + +abstract contract JavascriptFfi is CommonBase { + function runScript(string memory scriptName, string memory args) internal returns (bytes memory result) { + string[] memory inputs = new string[](8); + + // build ffi command string + inputs[0] = "npm"; + inputs[1] = "--silent"; + inputs[2] = "--prefix"; + inputs[3] = "./test/js-scripts"; + inputs[4] = "run"; + inputs[5] = scriptName; + inputs[6] = "--"; + inputs[7] = args; + result = vm.ffi(inputs); + } +}