-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement precompile mock contracts and foundry mock util test contra…
…cts (#462) - Add foundry test support files - Add husky commit-msg to help devs not forget signing off - Add foundry testing readme file - Add npm package script for easy runs of foundry, hardhat and localnode - Add HTS mock and test contracts and use for reference foundry tests - Add github CI action to run foundry tests --------- Signed-off-by: Mo Shaikjee <[email protected]>
- Loading branch information
Showing
32 changed files
with
4,779 additions
and
4 deletions.
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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: Foundry Tests | ||
|
||
on: | ||
workflow_dispatch: | ||
pull_request: | ||
branches: [main] | ||
|
||
jobs: | ||
check: | ||
strategy: | ||
fail-fast: true | ||
|
||
name: Foundry project | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v3 | ||
with: | ||
submodules: recursive | ||
|
||
- name: Use Node.js [18.15] | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 18.15 | ||
cache: npm | ||
|
||
- name: Create .env file | ||
run: cp local.env .env | ||
|
||
- name: Install dependencies | ||
run: npm ci | ||
|
||
- name: Install Foundry | ||
uses: foundry-rs/foundry-toolchain@v1 | ||
with: | ||
version: nightly | ||
|
||
- name: Run Forge build | ||
run: | | ||
forge --version | ||
forge build | ||
id: build | ||
|
||
- name: Run Forge tests | ||
run: | | ||
forge test -vvv | ||
id: 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
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,3 @@ | ||
[submodule "lib/forge-std"] | ||
path = lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std |
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,4 @@ | ||
#!/usr/bin/env sh | ||
. "$(dirname -- "$0")/_/husky.sh" | ||
|
||
grep -q "Signed-off-by" $1 || (echo "No Signed-off-by found. Run git commit --signoff" && false) |
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,66 @@ | ||
## Foundry | ||
|
||
**Foundry is a blazing fast, portable and modular toolkit for Ethereum application development written in Rust.** | ||
|
||
Foundry consists of: | ||
|
||
- **Forge**: Ethereum testing framework (like Truffle, Hardhat and DappTools). | ||
- **Cast**: Swiss army knife for interacting with EVM smart contracts, sending transactions and getting chain data. | ||
- **Anvil**: Local Ethereum node, akin to Ganache, Hardhat Network. | ||
- **Chisel**: Fast, utilitarian, and verbose solidity REPL. | ||
|
||
## Documentation | ||
|
||
https://book.getfoundry.sh/ | ||
|
||
## Usage | ||
|
||
### Build | ||
|
||
```shell | ||
$ forge build | ||
``` | ||
|
||
### Test | ||
|
||
```shell | ||
$ forge test | ||
``` | ||
|
||
### Format | ||
|
||
```shell | ||
$ forge fmt | ||
``` | ||
|
||
### Gas Snapshots | ||
|
||
```shell | ||
$ forge snapshot | ||
``` | ||
|
||
### Anvil | ||
|
||
```shell | ||
$ anvil | ||
``` | ||
|
||
### Deploy | ||
|
||
```shell | ||
$ forge script script/Counter.s.sol:CounterScript --rpc-url <your_rpc_url> --private-key <your_private_key> | ||
``` | ||
|
||
### Cast | ||
|
||
```shell | ||
$ cast <subcommand> | ||
``` | ||
|
||
### Help | ||
|
||
```shell | ||
$ forge --help | ||
$ anvil --help | ||
$ cast --help | ||
``` |
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 |
---|---|---|
|
@@ -38,6 +38,8 @@ For further details on methods, hashes and availability please refer to [PRNG Pr | |
|
||
This project is set up using the Hardhat development environment. To get started, please follow this [test setup guide](./TEST_SETUP.md). | ||
|
||
For using this project as a library in a Foundry project see [Foundry Testing](FOUNDRY_TESTING.md) | ||
|
||
## Support | ||
|
||
If you have a question on how to use the product, please see our | ||
|
@@ -63,4 +65,3 @@ to [[email protected]](mailto:[email protected]). | |
## Smart contracts - testing | ||
|
||
[Smart contracts tests - documentation](https://raw.githubusercontent.com/hashgraph/hedera-smart-contracts/main/test/README.md) | ||
|
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,31 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity >=0.8.0; | ||
|
||
import "../libraries/Constants.sol"; | ||
|
||
/// @title Prevents delegatecall to a contract | ||
/// @notice Base contract that provides a modifier for preventing delegatecall to methods in a child contract | ||
abstract contract NoDelegateCall is Constants { | ||
/// @dev The original address of this contract | ||
address private immutable original; | ||
|
||
/// @dev slightly modified as in context of constructor address(this) is the address of the deployed contract and not the etched contract address | ||
/// hence _original allows passing the address to which a contract is etched to; for normal uses pass ADDRESS_ZERO | ||
constructor(address _original) { | ||
// Immutables are computed in the init code of the contract, and then inlined into the deployed bytecode. | ||
// In other words, this variable won't change when it's checked at runtime. | ||
original = _original == ADDRESS_ZERO ? address(this) : _original; | ||
} | ||
|
||
/// @dev Private method is used instead of inlining into modifier because modifiers are copied into each method, | ||
/// and the use of immutable means the address bytes are copied in every place the modifier is used. | ||
function checkNotDelegateCall() private view { | ||
require(address(this) == original, "NO_DELEGATECALL"); | ||
} | ||
|
||
/// @notice Prevents delegatecall into the modified method | ||
modifier noDelegateCall() { | ||
checkNotDelegateCall(); | ||
_; | ||
} | ||
} |
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,10 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.9; | ||
|
||
abstract contract Constants { | ||
address internal constant HTS_PRECOMPILE = address(0x167); | ||
address internal constant EXCHANGE_RATE_PRECOMPILE = address(0x168); | ||
address internal constant UTIL_PRECOMPILE = address(0x168); | ||
|
||
address internal constant ADDRESS_ZERO = address(0); | ||
} |
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,12 @@ | ||
[profile.default] | ||
src = 'contracts' | ||
out = 'out' | ||
libs = ['node_modules', 'lib'] | ||
test = 'test/foundry' | ||
cache_path = 'forge-cache' | ||
remappings = [ | ||
'@openzeppelin/=node_modules/@openzeppelin/', | ||
'hardhat/=node_modules/hardhat/', | ||
] | ||
|
||
# See more config options https://github.com/foundry-rs/foundry/tree/master/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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,2 @@ | ||
ds-test/=lib/forge-std/lib/ds-test/src/ | ||
forge-std/=lib/forge-std/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
pragma solidity ^0.8.9; | ||
|
||
import './utils/ExchangeRateUtils.sol'; | ||
|
||
contract ExchangeRatePrecompileMockTest is ExchangeRateUtils { | ||
|
||
// setUp is executed before each and every test function | ||
function setUp() public { | ||
_setUpExchangeRatePrecompileMock(); | ||
_setUpAccounts(); | ||
} | ||
|
||
function test_CanCorrectlyConvertTinycentsToTinybars() public { | ||
uint256 tinycents = 1e8; | ||
uint256 tinybars = _doConvertTinycentsToTinybars(tinycents); | ||
assertEq(tinybars, 1e7, "expected 1 cent to equal 1e7 tinybar(0.1 HBAR) at $0.1/HBAR"); | ||
} | ||
|
||
function test_CanCorrectlyConvertTinybarsToTinyCents() public { | ||
uint256 tinybars = 1e8; | ||
uint256 tinycents = _doConvertTinybarsToTinycents(tinybars); | ||
assertEq(tinycents, 1e9, "expected 1 HBAR to equal 10 cents(1e9 tinycents) at $0.1/HBAR"); | ||
} | ||
|
||
} | ||
|
||
// forge test --match-contract ExchangeRatePrecompileMockTest -vv |
Oops, something went wrong.