Skip to content

Commit

Permalink
feat: introduce htsSetup wrapper function (#138)
Browse files Browse the repository at this point in the history
Signed-off-by: Luis Mastrangelo <luis@swirldslabs.com>
  • Loading branch information
acuarica authored Nov 19, 2024
1 parent e114fa7 commit 5d09b38
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 18 deletions.
55 changes: 55 additions & 0 deletions src/htsSetup.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.0;

import {Vm} from "forge-std/Vm.sol";

import {HtsSystemContractJson, HTS_ADDRESS} from "./HtsSystemContractJson.sol";
import {MirrorNode} from "./MirrorNode.sol";
import {MirrorNodeFFI} from "./MirrorNodeFFI.sol";

Vm constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

/**
* @notice Sets up the HTS emulation contract.
* The HTS emulation contract will be deployed at `0x167` address using `vm.etch`.
*
* Remember to enable [ffi](https://book.getfoundry.sh/cheatcodes/ffi)
* in your Foundry project to use HTS contract in your tests.
* You can do this by adding the following lines to your `.toml` file
*
* ```toml
* [profile.default]
* ffi = true
* ```
*
* Alternatively, you can include the `--ffi` flag when running `forge`.
*
* This is necessary because our library uses `ffi` to fetch remote state from the Mirror Node.
*
* > DISCLAIMER
* > The HTS emulation contract **SHOULD BE ONLY** used to ease development workflow when working with Hedera Tokens.
* > The HTS emulation contract **DOES NOT** replicate Hedera Token Services fully.
* > That is, behavior might differ when switching from local development to a real Hedera network.
* > Always test your contracts against a real Hedera network before launching your contracts.
*/
function htsSetup() {
htsSetup(new MirrorNodeFFI());
}

function htsSetup(MirrorNode mirrorNode) {
__deployCodeTo("HtsSystemContractJson.sol", HTS_ADDRESS);
HtsSystemContractJson(HTS_ADDRESS).setMirrorNodeProvider(mirrorNode);
vm.allowCheatcodes(HTS_ADDRESS);
}

/**
* @dev Taken from `StdCheats.sol:deployCodeTo`.
* This is to avoid inheriting from `StdCheats`, and such simplifying usage.
*/
function __deployCodeTo(string memory what, address where) {
bytes memory creationCode = vm.getCode(what);
vm.etch(where, creationCode);
(bool success, bytes memory runtimeBytecode) = where.call("");
require(success, "__deployCodeTo(string,address): Failed to create runtime bytecode.");
vm.etch(where, runtimeBytecode);
}
23 changes: 5 additions & 18 deletions test/lib/TestSetup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
pragma solidity ^0.8.0;

import {console} from "forge-std/console.sol";
import {StdCheats} from "forge-std/StdCheats.sol";
import {Vm} from "forge-std/Vm.sol";

import {HtsSystemContractJson, HTS_ADDRESS} from "../../src/HtsSystemContractJson.sol";
import {MirrorNodeFFI} from "../../src/MirrorNodeFFI.sol";
import {htsSetup} from "../../src/htsSetup.sol";
import {HTS_ADDRESS} from "../../src/HtsSystemContractJson.sol";
import {MirrorNodeMock} from "./MirrorNodeMock.sol";

abstract contract TestSetup is StdCheats {
// `vm` is private in StdCheats, so we duplicate it here
Vm private constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

abstract contract TestSetup {
/**
* https://hashscan.io/testnet/token/0.0.429274
* https://testnet.mirrornode.hedera.com/api/v1/tokens/0.0.429274
Expand Down Expand Up @@ -59,25 +54,17 @@ abstract contract TestSetup is StdCheats {
function setUpMockStorageForNonFork() internal {
if (HTS_ADDRESS.code.length == 0) {
console.log("HTS code length is 0, non-fork test, code and data provided locally");

deployCodeTo("HtsSystemContractJson.sol", HTS_ADDRESS);
MirrorNodeMock mirrorNode = new MirrorNodeMock();
mirrorNode.deployHIP719Proxy(USDC, "USDC");
mirrorNode.deployHIP719Proxy(MFCT, "MFCT");
HtsSystemContractJson(HTS_ADDRESS).setMirrorNodeProvider(mirrorNode);
vm.allowCheatcodes(HTS_ADDRESS);

htsSetup(mirrorNode);
testMode = TestMode.NonFork;
} else if (HTS_ADDRESS.code.length == 1) {
console.log("HTS code length is 1, forking from a remote Hedera network, HTS/FFI code with Mirror Node backend is deployed here");
deployCodeTo("HtsSystemContractJson.sol", HTS_ADDRESS);
HtsSystemContractJson(HTS_ADDRESS).setMirrorNodeProvider(new MirrorNodeFFI());
vm.allowCheatcodes(HTS_ADDRESS);

htsSetup();
testMode = TestMode.FFI;
} else {
console.log("HTS code length is greater than 1 (%d), HTS code comes from forked network", HTS_ADDRESS.code.length);

testMode = TestMode.JSON_RPC;
}
}
Expand Down

0 comments on commit 5d09b38

Please sign in to comment.