Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add chain helpers #36

Merged
merged 2 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 148 additions & 0 deletions src/contracts/utils/ChainHelpers.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Vm} from 'forge-std/Vm.sol';

library ChainIds {
uint256 internal constant MAINNET = 1;
uint256 internal constant ETHEREUM = 1;
uint256 internal constant OPTIMISM = 10;
uint256 internal constant BNB = 56;
uint256 internal constant POLYGON = 137;
uint256 internal constant FANTOM = 250;
uint256 internal constant ZK_SYNC = 324;
uint256 internal constant METIS = 1088;
uint256 internal constant ZK_EVM = 1101;
uint256 internal constant BASE = 8453;
uint256 internal constant ARBITRUM = 42161;
uint256 internal constant AVALANCHE = 43114;
uint256 internal constant GNOSIS = 100;
uint256 internal constant SCROLL = 534352;
uint256 internal constant SEPOLIA = 11155111;
uint256 internal constant HARMONY = 1666600000;
uint256 internal constant CELO = 42220;
uint256 internal constant POLYGON_ZK_EVM = 1101;
}

library TestNetChainIds {
uint256 internal constant ETHEREUM_SEPOLIA = 11155111;
uint256 internal constant POLYGON_AMOY = 80002;
uint256 internal constant AVALANCHE_FUJI = 43113;
uint256 internal constant FANTOM_TESTNET = 4002;
uint256 internal constant HARMONY_TESTNET = 1666700000;
uint256 internal constant METIS_TESTNET = 599;
uint256 internal constant BNB_TESTNET = 97;
uint256 internal constant GNOSIS_CHIADO = 10200;
uint256 internal constant SCROLL_SEPOLIA = 534351;
uint256 internal constant BASE_SEPOLIA = 84532;
uint256 internal constant CELO_ALFAJORES = 44787;
uint256 internal constant OPTIMISM_SEPOLIA = 11155420;
uint256 internal constant ARBITRUM_SEPOLIA = 421614;
uint256 internal constant ZK_SYNC_SEPOLIA = 300;
}

library ChainHelpers {
error UnknownChainId();

function selectChain(Vm vm, uint256 chainId) internal returns (uint256, uint256) {
uint256 previousFork = vm.activeFork();
if (chainId == block.chainid) return (previousFork, previousFork);
uint256 newFork;
if (chainId == ChainIds.MAINNET) {
newFork = vm.createSelectFork(vm.rpcUrl('mainnet'));
} else if (chainId == ChainIds.OPTIMISM) {
newFork = vm.createSelectFork(vm.rpcUrl('optimism'));
} else if (chainId == ChainIds.BNB) {
newFork = vm.createSelectFork(vm.rpcUrl('bnb'));
} else if (chainId == ChainIds.POLYGON) {
newFork = vm.createSelectFork(vm.rpcUrl('polygon'));
} else if (chainId == ChainIds.FANTOM) {
newFork = vm.createSelectFork(vm.rpcUrl('fantom'));
} else if (chainId == ChainIds.ZK_SYNC) {
newFork = vm.createSelectFork(vm.rpcUrl('zkSync'));
} else if (chainId == ChainIds.METIS) {
newFork = vm.createSelectFork(vm.rpcUrl('metis'));
} else if (chainId == ChainIds.ZK_EVM) {
newFork = vm.createSelectFork(vm.rpcUrl('zkEvm'));
} else if (chainId == ChainIds.BASE) {
newFork = vm.createSelectFork(vm.rpcUrl('base'));
} else if (chainId == ChainIds.GNOSIS) {
newFork = vm.createSelectFork(vm.rpcUrl('gnosis'));
} else if (chainId == ChainIds.SCROLL) {
newFork = vm.createSelectFork(vm.rpcUrl('scroll'));
} else if (chainId == ChainIds.ARBITRUM) {
newFork = vm.createSelectFork(vm.rpcUrl('arbitrum'));
} else if (chainId == ChainIds.AVALANCHE) {
newFork = vm.createSelectFork(vm.rpcUrl('avalanche'));
} else if (chainId == ChainIds.SEPOLIA) {
newFork = vm.createSelectFork(vm.rpcUrl('sepolia'));
} else if (chainId == ChainIds.HARMONY) {
newFork = vm.createSelectFork(vm.rpcUrl('harmony'));
} else if (chainId == ChainIds.ZK_SYNC) {
newFork = vm.createSelectFork(vm.rpcUrl('zksync'));
} else {
revert UnknownChainId();
}
return (previousFork, newFork);
}

function getNetworkNameFromId(uint256 chainId) internal pure returns (string memory) {
string memory networkName;
if (chainId == ChainIds.ETHEREUM) {
networkName = 'ethereum';
} else if (chainId == ChainIds.POLYGON) {
networkName = 'polygon';
} else if (chainId == ChainIds.AVALANCHE) {
networkName = 'avalanche';
} else if (chainId == ChainIds.ARBITRUM) {
networkName = 'arbitrum';
} else if (chainId == ChainIds.OPTIMISM) {
networkName = 'optimism';
} else if (chainId == ChainIds.METIS) {
networkName = 'metis';
} else if (chainId == ChainIds.BNB) {
networkName = 'binance';
} else if (chainId == ChainIds.BASE) {
networkName = 'base';
} else if (chainId == ChainIds.POLYGON_ZK_EVM) {
networkName = 'zkevm';
} else if (chainId == ChainIds.GNOSIS) {
networkName = 'gnosis';
} else if (chainId == ChainIds.SCROLL) {
networkName = 'scroll';
} else if (chainId == ChainIds.CELO) {
networkName = 'celo';
} else if (chainId == ChainIds.ZK_SYNC) {
networkName = 'zksync';
}
// testnets
else if (chainId == TestNetChainIds.ETHEREUM_SEPOLIA) {
networkName = 'ethereum_sepolia';
} else if (chainId == TestNetChainIds.POLYGON_AMOY) {
networkName = 'polygon_amoy';
} else if (chainId == TestNetChainIds.AVALANCHE_FUJI) {
networkName = 'avalanche_fuji';
} else if (chainId == TestNetChainIds.ARBITRUM_SEPOLIA) {
networkName = 'arbitrum_sepolia';
} else if (chainId == TestNetChainIds.OPTIMISM_SEPOLIA) {
networkName = 'optimism_sepolia';
} else if (chainId == TestNetChainIds.METIS_TESTNET) {
networkName = 'metis_test';
} else if (chainId == TestNetChainIds.BNB_TESTNET) {
networkName = 'binance_testnet';
} else if (chainId == TestNetChainIds.BASE_SEPOLIA) {
networkName = 'base_sepolia';
} else if (chainId == TestNetChainIds.GNOSIS_CHIADO) {
networkName = 'gno_chiado';
} else if (chainId == TestNetChainIds.SCROLL_SEPOLIA) {
networkName = 'scroll_sepolia';
} else if (chainId == TestNetChainIds.CELO_ALFAJORES) {
networkName = 'celo_alfajores';
} else if (chainId == TestNetChainIds.ZK_SYNC_SEPOLIA) {
networkName = 'zksync_sepolia';
} else {
revert('chain id is not supported');
}

return networkName;
}
}
15 changes: 8 additions & 7 deletions test/Rescuable721.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {Rescuable721} from '../src/contracts/utils/Rescuable721.sol';

contract MockReceiver721TokensContract is Rescuable721 {
address public immutable ALLOWED;
constructor (address allowedAddress) {

constructor(address allowedAddress) {
ALLOWED = allowedAddress;
}

Expand All @@ -35,12 +36,9 @@ contract Rescue721Test is Test {
tokensReceiver = new MockReceiver721TokensContract(ALLOWED);
}

function testFuzzEmergencyTokenTransfer(address randomWallet, address recipient) public {
vm.assume(randomWallet != address(0));
function testFuzzEmergencyTokenTransfer(address recipient) public {
vm.assume(recipient != address(0));
testToken.mint(randomWallet, 1);
hoax(randomWallet);
testToken.transferFrom(randomWallet, address(tokensReceiver), 1);
testToken.mint(address(tokensReceiver), 1);

assertEq(testToken.balanceOf(address(tokensReceiver)), 1);

Expand All @@ -53,7 +51,10 @@ contract Rescue721Test is Test {
assertEq(testToken.balanceOf(address(recipient)), 1);
}

function testFuzzEmergencyTokenTransferWhenNotOwner(address randomWallet, address recipient) public {
function testFuzzEmergencyTokenTransferWhenNotOwner(
address randomWallet,
address recipient
) public {
vm.assume(randomWallet != address(0));
vm.assume(recipient != address(0));
testToken.mint(randomWallet, 1);
Expand Down
16 changes: 9 additions & 7 deletions test/TransparentProxyFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract TestTransparentProxyFactory is Test {

function testCreateDeterministic(address admin, bytes32 salt) public {
// we know that this is covered at the ERC1967Upgrade
vm.assume(admin != address(0));
vm.assume(admin != address(0) && admin != address(this));

uint256 FOO = 2;
bytes memory data = abi.encodeWithSelector(mockImpl.initialize.selector, FOO);
Expand All @@ -36,9 +36,10 @@ contract TestTransparentProxyFactory is Test {
assertEq(MockImpl(proxy1).getFoo(), FOO);
}

function testCreateDeterministicWithDeterministicProxy(bytes32 proxyAdminSalt, bytes32 proxySalt)
public
{
function testCreateDeterministicWithDeterministicProxy(
bytes32 proxyAdminSalt,
bytes32 proxySalt
) public {
address deterministicProxyAdmin = factory.predictCreateDeterministicProxyAdmin(proxyAdminSalt);

uint256 FOO = 2;
Expand All @@ -63,9 +64,10 @@ contract TestTransparentProxyFactory is Test {
assertEq(MockImpl(proxy1).getFoo(), FOO);
}

function testCreateDeterministicProxyAdmin(address proxyAdminOwner, bytes32 proxyAdminSalt)
public
{
function testCreateDeterministicProxyAdmin(
address proxyAdminOwner,
bytes32 proxyAdminSalt
) public {
// we know that this is covered at the ProxyAdmin contract
vm.assume(proxyAdminOwner != address(0));

Expand Down
Loading