Skip to content

Commit

Permalink
chore: forge fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
odyslam committed Dec 21, 2023
1 parent 5fdcc88 commit c3fb4d1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 82 deletions.
25 changes: 13 additions & 12 deletions src/Phylax.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@
pragma solidity >=0.6.2 <0.9.0;
pragma experimental ABIEncoderV2;

import { console } from "forge-std/console.sol";
import { CommonBase } from "forge-std/Base.sol";
import {console} from "forge-std/console.sol";
import {CommonBase} from "forge-std/Base.sol";

/// @title Vm-like interface for Phylax
/// @dev It includes cheatcodes that are available in the phylax fork of foundry, but have not been upstreamed
contract Phylax is CommonBase {
/// @dev The internal cheatcode address that is used by forge
Phylax internal constant _ph = Phylax(VM_ADDRESS);
/// @dev The internal cheatcode address that is used by forge
Phylax internal constant _ph = Phylax(VM_ADDRESS);

/// @notice Export a name/value pair to Phylax
/// @dev It's exposed via the API and if it's numerical, via the prometheus endpoint as well.
/// @param name The name of the value to export
/// @param value The value to export
function export(string memory name, string memory value) external {
try _ph.export(name, value) {} catch {
console.log("'ph.export(string,string)' reverted. This is normal if the code was run by vanilla 'forge'");
/// @notice Export a name/value pair to Phylax
/// @dev It's exposed via the API and if it's numerical, via the prometheus endpoint as well.
/// @param name The name of the value to export
/// @param value The value to export
function export(string memory name, string memory value) external {
try _ph.export(name, value) {}
catch {
console.log("'ph.export(string,string)' reverted. This is normal if the code was run by vanilla 'forge'");
}
}
}
}
100 changes: 50 additions & 50 deletions src/PhylaxBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,59 @@
pragma solidity >=0.6.2 <0.9.0;
pragma experimental ABIEncoderV2;

import { CommonBase } from "forge-std/Base.sol";
import { Phylax } from "./Phylax.sol";
import {CommonBase} from "forge-std/Base.sol";
import {Phylax} from "./Phylax.sol";

/// @title Alert
/// @dev Base contract for all Phylax alert contracts.
abstract contract PhylaxBase is CommonBase {
/// @dev Instance of Phylax contract
Phylax internal ph;
/// @dev Array of active chains
uint256[] internal $activeChains;

/// @notice Sets up the Phylax contract
/// @dev Instantiates a new Phylax contract and assigns it to the ph variable
function setupPhylax() internal {
ph = new Phylax();
// This is required so that the contract is persistent across forks
vm.makePersistent(address(ph));
}

/// @notice Enables a new chain
/// @param aliasOrUrl The alias or URL of the chain to enable. The alias is used
/// when the RPC is defined in `foundry.toml`, as in Forge fork tests.
/// @return The index of the newly enabled forked chain
function enableChain(string memory aliasOrUrl) internal ensurePhylaxSetup returns (uint256) {
$activeChains.push(vm.createFork(aliasOrUrl));
return $activeChains.length - 1;
}

/// @notice Enables a new chain

/// @param aliasOrUrl The alias or URL of the chain to enable. The alias is used
/// when the RPC is defined in `foundry.toml`, as in Forge fork tests.
/// @param blockNumber The block number at which the chain should be forked
/// @return The index of the newly enabled forked chain
function enableChain(string memory aliasOrUrl, uint256 blockNumber) internal ensurePhylaxSetup returns (uint256) {
$activeChains.push(vm.createFork(aliasOrUrl, blockNumber));
return $activeChains.length - 1;
}

/// @notice Modifier to select a chain
/// @dev Exports "fork_activated" and selects the fork at the given index
/// @param index The index of the chain to select
modifier chain(uint256 index) {
vm.selectFork($activeChains[index]);
_;
}

/// @notice Modifier to ensure Phylax is setup
/// @dev Instantiates Phylax if not already setup
modifier ensurePhylaxSetup() {
if (address(ph) == address(0)) {
setupPhylax();
/// @dev Instance of Phylax contract
Phylax internal ph;
/// @dev Array of active chains
uint256[] internal $activeChains;

/// @notice Sets up the Phylax contract
/// @dev Instantiates a new Phylax contract and assigns it to the ph variable
function setupPhylax() internal {
ph = new Phylax();
// This is required so that the contract is persistent across forks
vm.makePersistent(address(ph));
}

/// @notice Enables a new chain
/// @param aliasOrUrl The alias or URL of the chain to enable. The alias is used
/// when the RPC is defined in `foundry.toml`, as in Forge fork tests.
/// @return The index of the newly enabled forked chain
function enableChain(string memory aliasOrUrl) internal ensurePhylaxSetup returns (uint256) {
$activeChains.push(vm.createFork(aliasOrUrl));
return $activeChains.length - 1;
}

/// @notice Enables a new chain

/// @param aliasOrUrl The alias or URL of the chain to enable. The alias is used
/// when the RPC is defined in `foundry.toml`, as in Forge fork tests.
/// @param blockNumber The block number at which the chain should be forked
/// @return The index of the newly enabled forked chain
function enableChain(string memory aliasOrUrl, uint256 blockNumber) internal ensurePhylaxSetup returns (uint256) {
$activeChains.push(vm.createFork(aliasOrUrl, blockNumber));
return $activeChains.length - 1;
}

/// @notice Modifier to select a chain
/// @dev Exports "fork_activated" and selects the fork at the given index
/// @param index The index of the chain to select
modifier chain(uint256 index) {
vm.selectFork($activeChains[index]);
_;
}

/// @notice Modifier to ensure Phylax is setup
/// @dev Instantiates Phylax if not already setup
modifier ensurePhylaxSetup() {
if (address(ph) == address(0)) {
setupPhylax();
}
_;
}
_;
}
}
40 changes: 20 additions & 20 deletions test/PhylaxBase.t.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.7.0 <0.9.0;

import { PhylaxBase } from "../src/PhylaxBase.sol";
import { console } from "forge-std/console.sol";
import {PhylaxBase} from "../src/PhylaxBase.sol";
import {console} from "forge-std/console.sol";

contract PhylaxBaseTest is PhylaxBase {
uint256 sepolia;
uint256 sepolia;

function setUp() public {
assert(address(ph) == address(0));
sepolia = enableChain("sepolia");
}
function setUp() public {
assert(address(ph) == address(0));
sepolia = enableChain("sepolia");
}

function test_modifierForks() public chain(sepolia) {
assert(block.number != 0);
}
function test_modifierForks() public chain(sepolia) {
assert(block.number != 0);
}

function test_phylaxContractIsSetup() public view {
assert(address(ph) != address(0));
}
function test_phylaxContractIsSetup() public view {
assert(address(ph) != address(0));
}

function test_chainsArrayCorrect() public view {
assert($activeChains.length == 1);
assert($activeChains[0] == sepolia);
}
function test_chainsArrayCorrect() public view {
assert($activeChains.length == 1);
assert($activeChains[0] == sepolia);
}

function test_exportNotRevert() public {
ph.export("test_key", "test_value");
}
function test_exportNotRevert() public {
ph.export("test_key", "test_value");
}
}

0 comments on commit c3fb4d1

Please sign in to comment.