Skip to content

Commit

Permalink
feat: move JS scripts to subdir and add helper (Uniswap#405)
Browse files Browse the repository at this point in the history
This commit moves our JS helper scripts into a test subdirectory and
adds a helper abstract contract to more easily build the ffi commands to
interop with javascript testers
  • Loading branch information
marktoda authored and hyunchel committed Feb 21, 2024
1 parent f0bb93d commit e06524a
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion .nvmrc

This file was deleted.

2 changes: 0 additions & 2 deletions .prettierignore

This file was deleted.

5 changes: 0 additions & 5 deletions .prettierrc

This file was deleted.

24 changes: 4 additions & 20 deletions test/TickMath.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand All @@ -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++) {
Expand All @@ -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 {
Expand All @@ -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++) {
Expand Down
File renamed without changes.
8 changes: 4 additions & 4 deletions package.json β†’ test/js-scripts/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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"
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions test/utils/JavascriptFfi.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit e06524a

Please sign in to comment.