Skip to content

Commit e3a5b85

Browse files
author
Thai Xuan Dang
committed
test: core libraries Hardhat
1 parent b62a51c commit e3a5b85

35 files changed

+13631
-4565
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ yarn-error.log
1717
.yarn
1818
.yarnrc.yml
1919

20-
.vscode/
20+
.vscode/
21+
22+
cache_hardhat/
23+
artifacts/
24+
typechain/

hardhat.config.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import "hardhat-deploy";
1+
import "@nomicfoundation/hardhat-foundry";
2+
import 'hardhat-typechain'
3+
import '@nomiclabs/hardhat-ethers'
4+
import '@nomiclabs/hardhat-waffle'
5+
import '@nomiclabs/hardhat-etherscan'
26
import {
37
HardhatUserConfig,
48
NetworkUserConfig,
@@ -28,6 +32,16 @@ const config: HardhatUserConfig = {
2832
"ronin-testnet": testnet,
2933
"ronin-mainnet": mainnet,
3034
},
35+
36+
solidity: {
37+
version: '0.7.6',
38+
settings: {
39+
optimizer: {
40+
enabled: true,
41+
runs: 250,
42+
},
43+
},
44+
},
3145
};
3246

3347
export default config;

package.json

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,41 @@
33
"src/**/*.sol"
44
],
55
"devDependencies": {
6+
"@nomicfoundation/hardhat-foundry": "^1.1.2",
7+
"@nomiclabs/hardhat-ethers": "^2.0.2",
8+
"@nomiclabs/hardhat-etherscan": "^2.1.8",
9+
"@nomiclabs/hardhat-waffle": "^2.0.1",
10+
"@typechain/ethers-v5": "^4.0.0",
11+
"@types/chai": "^4.2.6",
12+
"@types/mocha": "^5.2.7",
13+
"chai": "^4.2.0",
14+
"decimal.js": "^10.2.1",
615
"dotenv": "^16.3.1",
7-
"hardhat": "^2.12.7",
8-
"hardhat-deploy": "0.11.29",
16+
"ethereum-waffle": "^3.0.2",
17+
"ethers": "^5.0.8",
18+
"hardhat": "^2.2.0",
19+
"hardhat-typechain": "^0.3.5",
920
"husky": "^8.0.3",
1021
"lint-staged": "^14.0.1",
11-
"ts-node": "^10.4.0",
12-
"typescript": "^4.5.4"
22+
"mocha": "^6.2.2",
23+
"mocha-chai-jest-snapshot": "^1.1.0",
24+
"prettier": "^2.0.5",
25+
"prettier-plugin-solidity": "^1.0.0-alpha.59",
26+
"solhint": "^3.2.1",
27+
"solhint-plugin-prettier": "^0.0.5",
28+
"ts-generator": "^0.1.1",
29+
"ts-node": "^8.5.4",
30+
"typechain": "^4.0.0",
31+
"typescript": "^3.7.3"
1332
},
1433
"lint-staged": {
1534
"{src,script,test}/**/*.sol": [
1635
"forge fmt"
1736
]
1837
},
1938
"scripts": {
20-
"prepare": "husky install"
39+
"prepare": "husky install",
40+
"compile": "hardhat compile",
41+
"test": "hardhat test"
2142
}
2243
}

src/core/test/BitMathTest.sol

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity =0.7.6;
3+
4+
import "../libraries/BitMath.sol";
5+
6+
contract BitMathTest {
7+
function mostSignificantBit(uint256 x) external pure returns (uint8 r) {
8+
return BitMath.mostSignificantBit(x);
9+
}
10+
11+
function getGasCostOfMostSignificantBit(uint256 x) external view returns (uint256) {
12+
uint256 gasBefore = gasleft();
13+
BitMath.mostSignificantBit(x);
14+
return gasBefore - gasleft();
15+
}
16+
17+
function leastSignificantBit(uint256 x) external pure returns (uint8 r) {
18+
return BitMath.leastSignificantBit(x);
19+
}
20+
21+
function getGasCostOfLeastSignificantBit(uint256 x) external view returns (uint256) {
22+
uint256 gasBefore = gasleft();
23+
BitMath.leastSignificantBit(x);
24+
return gasBefore - gasleft();
25+
}
26+
}

src/core/test/FullMathTest.sol

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity =0.7.6;
3+
4+
import "../libraries/FullMath.sol";
5+
6+
contract FullMathTest {
7+
function mulDiv(uint256 x, uint256 y, uint256 z) external pure returns (uint256) {
8+
return FullMath.mulDiv(x, y, z);
9+
}
10+
11+
function mulDivRoundingUp(uint256 x, uint256 y, uint256 z) external pure returns (uint256) {
12+
return FullMath.mulDivRoundingUp(x, y, z);
13+
}
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity =0.7.6;
3+
4+
import "../libraries/LiquidityMath.sol";
5+
6+
contract LiquidityMathTest {
7+
function addDelta(uint128 x, int128 y) external pure returns (uint128 z) {
8+
return LiquidityMath.addDelta(x, y);
9+
}
10+
11+
function getGasCostOfAddDelta(uint128 x, int128 y) external view returns (uint256) {
12+
uint256 gasBefore = gasleft();
13+
LiquidityMath.addDelta(x, y);
14+
return gasBefore - gasleft();
15+
}
16+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity =0.7.6;
3+
4+
import "../libraries/SqrtPriceMath.sol";
5+
6+
contract SqrtPriceMathTest {
7+
function getNextSqrtPriceFromInput(uint160 sqrtP, uint128 liquidity, uint256 amountIn, bool zeroForOne)
8+
external
9+
pure
10+
returns (uint160 sqrtQ)
11+
{
12+
return SqrtPriceMath.getNextSqrtPriceFromInput(sqrtP, liquidity, amountIn, zeroForOne);
13+
}
14+
15+
function getGasCostOfGetNextSqrtPriceFromInput(uint160 sqrtP, uint128 liquidity, uint256 amountIn, bool zeroForOne)
16+
external
17+
view
18+
returns (uint256)
19+
{
20+
uint256 gasBefore = gasleft();
21+
SqrtPriceMath.getNextSqrtPriceFromInput(sqrtP, liquidity, amountIn, zeroForOne);
22+
return gasBefore - gasleft();
23+
}
24+
25+
function getNextSqrtPriceFromOutput(uint160 sqrtP, uint128 liquidity, uint256 amountOut, bool zeroForOne)
26+
external
27+
pure
28+
returns (uint160 sqrtQ)
29+
{
30+
return SqrtPriceMath.getNextSqrtPriceFromOutput(sqrtP, liquidity, amountOut, zeroForOne);
31+
}
32+
33+
function getGasCostOfGetNextSqrtPriceFromOutput(uint160 sqrtP, uint128 liquidity, uint256 amountOut, bool zeroForOne)
34+
external
35+
view
36+
returns (uint256)
37+
{
38+
uint256 gasBefore = gasleft();
39+
SqrtPriceMath.getNextSqrtPriceFromOutput(sqrtP, liquidity, amountOut, zeroForOne);
40+
return gasBefore - gasleft();
41+
}
42+
43+
function getAmount0Delta(uint160 sqrtLower, uint160 sqrtUpper, uint128 liquidity, bool roundUp)
44+
external
45+
pure
46+
returns (uint256 amount0)
47+
{
48+
return SqrtPriceMath.getAmount0Delta(sqrtLower, sqrtUpper, liquidity, roundUp);
49+
}
50+
51+
function getAmount1Delta(uint160 sqrtLower, uint160 sqrtUpper, uint128 liquidity, bool roundUp)
52+
external
53+
pure
54+
returns (uint256 amount1)
55+
{
56+
return SqrtPriceMath.getAmount1Delta(sqrtLower, sqrtUpper, liquidity, roundUp);
57+
}
58+
59+
function getGasCostOfGetAmount0Delta(uint160 sqrtLower, uint160 sqrtUpper, uint128 liquidity, bool roundUp)
60+
external
61+
view
62+
returns (uint256)
63+
{
64+
uint256 gasBefore = gasleft();
65+
SqrtPriceMath.getAmount0Delta(sqrtLower, sqrtUpper, liquidity, roundUp);
66+
return gasBefore - gasleft();
67+
}
68+
69+
function getGasCostOfGetAmount1Delta(uint160 sqrtLower, uint160 sqrtUpper, uint128 liquidity, bool roundUp)
70+
external
71+
view
72+
returns (uint256)
73+
{
74+
uint256 gasBefore = gasleft();
75+
SqrtPriceMath.getAmount1Delta(sqrtLower, sqrtUpper, liquidity, roundUp);
76+
return gasBefore - gasleft();
77+
}
78+
}

src/core/test/SwapMathTest.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity =0.7.6;
3+
4+
import "../libraries/SwapMath.sol";
5+
6+
contract SwapMathTest {
7+
function computeSwapStep(
8+
uint160 sqrtP,
9+
uint160 sqrtPTarget,
10+
uint128 liquidity,
11+
int256 amountRemaining,
12+
uint24 feePips
13+
) external pure returns (uint160 sqrtQ, uint256 amountIn, uint256 amountOut, uint256 feeAmount) {
14+
return SwapMath.computeSwapStep(sqrtP, sqrtPTarget, liquidity, amountRemaining, feePips);
15+
}
16+
17+
function getGasCostOfComputeSwapStep(
18+
uint160 sqrtP,
19+
uint160 sqrtPTarget,
20+
uint128 liquidity,
21+
int256 amountRemaining,
22+
uint24 feePips
23+
) external view returns (uint256) {
24+
uint256 gasBefore = gasleft();
25+
SwapMath.computeSwapStep(sqrtP, sqrtPTarget, liquidity, amountRemaining, feePips);
26+
return gasBefore - gasleft();
27+
}
28+
}

src/core/test/TickBitmapTest.sol

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity =0.7.6;
3+
4+
import "../libraries/TickBitmap.sol";
5+
6+
contract TickBitmapTest {
7+
using TickBitmap for mapping(int16 => uint256);
8+
9+
mapping(int16 => uint256) public bitmap;
10+
11+
function flipTick(int24 tick) external {
12+
bitmap.flipTick(tick, 1);
13+
}
14+
15+
function getGasCostOfFlipTick(int24 tick) external returns (uint256) {
16+
uint256 gasBefore = gasleft();
17+
bitmap.flipTick(tick, 1);
18+
return gasBefore - gasleft();
19+
}
20+
21+
function nextInitializedTickWithinOneWord(int24 tick, bool lte) external view returns (int24 next, bool initialized) {
22+
return bitmap.nextInitializedTickWithinOneWord(tick, 1, lte);
23+
}
24+
25+
function getGasCostOfNextInitializedTickWithinOneWord(int24 tick, bool lte) external view returns (uint256) {
26+
uint256 gasBefore = gasleft();
27+
bitmap.nextInitializedTickWithinOneWord(tick, 1, lte);
28+
return gasBefore - gasleft();
29+
}
30+
31+
// returns whether the given tick is initialized
32+
function isInitialized(int24 tick) external view returns (bool) {
33+
(int24 next, bool initialized) = bitmap.nextInitializedTickWithinOneWord(tick, 1, true);
34+
return next == tick ? initialized : false;
35+
}
36+
}

src/core/test/TickMathTest.sol

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-License-Identifier: UNLICENSED
2+
pragma solidity =0.7.6;
3+
4+
import "../libraries/TickMath.sol";
5+
6+
contract TickMathTest {
7+
function getSqrtRatioAtTick(int24 tick) external pure returns (uint160) {
8+
return TickMath.getSqrtRatioAtTick(tick);
9+
}
10+
11+
function getGasCostOfGetSqrtRatioAtTick(int24 tick) external view returns (uint256) {
12+
uint256 gasBefore = gasleft();
13+
TickMath.getSqrtRatioAtTick(tick);
14+
return gasBefore - gasleft();
15+
}
16+
17+
function getTickAtSqrtRatio(uint160 sqrtPriceX96) external pure returns (int24) {
18+
return TickMath.getTickAtSqrtRatio(sqrtPriceX96);
19+
}
20+
21+
function getGasCostOfGetTickAtSqrtRatio(uint160 sqrtPriceX96) external view returns (uint256) {
22+
uint256 gasBefore = gasleft();
23+
TickMath.getTickAtSqrtRatio(sqrtPriceX96);
24+
return gasBefore - gasleft();
25+
}
26+
27+
function MIN_SQRT_RATIO() external pure returns (uint160) {
28+
return TickMath.MIN_SQRT_RATIO;
29+
}
30+
31+
function MAX_SQRT_RATIO() external pure returns (uint160) {
32+
return TickMath.MAX_SQRT_RATIO;
33+
}
34+
}

0 commit comments

Comments
 (0)