Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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
88 changes: 88 additions & 0 deletions test/fork/BaseForkTest.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "forge-std/Test.sol";
import "forge-std/Vm.sol";

abstract contract BaseForkTest is Test {
// RPCs of all supported chains.
string public ETHEREUM_RPC_URL = vm.envString("ETHEREUM_RPC_URL");
string public BSC_RPC_URL = vm.envString("BSC_RPC_URL");
string public POLYGON_RPC_URL = vm.envString("POLYGON_RPC_URL");
string public BASE_RPC_URL = vm.envString("BASE_RPC_URL");
string public ARBITRUM_RPC_URL = vm.envString("ARBITRUM_RPC_URL");
string public AVALANCHE_RPC_URL = vm.envString("AVALANCHE_RPC_URL");
string public ZETACHAIN_RPC_URL = vm.envString("ZETACHAIN_RPC_URL");

// The fork identifiers.
uint256 ethereumForkId;
uint256 bscForkId;
uint256 polygonForkId;
uint256 baseForkId;
uint256 arbitrumForkId;
uint256 avalancheForkId;
uint256 zetachainForkId;

// Mainnet admins.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test only work for mainnet? We should precise it somewhere. Otherwise can't we make it dependent on the network used?

address constant ETHEREUM_ADMIN = 0xaeB6dDB7708467814D557e340283248be8E43124;
address constant BSC_ADMIN = 0xaf28a257D292e7f0E531073f70a175b57E0261a8;
address constant POLYGON_ADMIN = 0x7828F92E7d79E141189f24C98aceF71Bc07bad3f;
address constant BASE_ADMIN = 0xF43CF8b3F3D22d4cC33f964c59076eaB2F8A108E;
address constant ARBITRUM_ADMIN = 0xdE3fb63723f0EEed8967ff9124e1c3bA89871b03;
address constant AVALANCHE_ADMIN = 0xdE3fb63723f0EEed8967ff9124e1c3bA89871b03;
address constant ZETACHAIN_ADMIN = 0x01d8207AfF7a7d029114Ee35afc72d0e133B7a0A;

struct ChainConfig {
uint256 forkId;
address contractAddress;
address admin;
string rpcUrl;
string name;
}

ChainConfig[] public chains;

function setUp() public virtual {
ethereumForkId = vm.createFork(ETHEREUM_RPC_URL);
bscForkId = vm.createFork(BSC_RPC_URL);
polygonForkId = vm.createFork(POLYGON_RPC_URL);
baseForkId = vm.createFork(BASE_RPC_URL);
arbitrumForkId = vm.createFork(ARBITRUM_RPC_URL);
avalancheForkId = vm.createFork(AVALANCHE_RPC_URL);
zetachainForkId = vm.createFork(ZETACHAIN_RPC_URL);

_setupChains();
}

function _setupChains() internal virtual;

function testForkIdDiffer() public view {
assert(ethereumForkId != bscForkId);
assert(bscForkId != polygonForkId);
assert(polygonForkId != baseForkId);
assert(baseForkId != arbitrumForkId);
assert(arbitrumForkId != avalancheForkId);
assert(avalancheForkId != zetachainForkId);
}

function testCanSwitchForks() public {
for (uint256 i = 0; i < chains.length; i++) {
vm.selectFork(chains[i].forkId);
assertEq(vm.activeFork(), chains[i].forkId);
}
}

function testUpgradeGatewayOnAllChains() public {
for (uint256 i = 0; i < chains.length; i++) {
console.log("\n=== Testing upgrade on", chains[i].name, "===");
_testUpgradeContract(chains[i]);
}
}

function _testUpgradeContract(ChainConfig memory config) internal virtual;

function _getImplementation(address proxy) internal view returns (address) {
bytes32 slot = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
return address(uint160(uint256(vm.load(proxy, slot))));
}
}
62 changes: 62 additions & 0 deletions test/fork/ERC20CustodyFork.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import { ERC20Custody } from "../../contracts/evm/ERC20Custody.sol";
import "./BaseForkTest.sol";

contract ERC20CustodyForkTest is BaseForkTest {
// ERC20Custody proxy addresses.
address constant ETHEREUM_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5;
address constant BSC_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5;
address constant POLYGON_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5;
address constant BASE_ERC20CUSTODY_PROXY = 0x0Bad40D9e9C369f2223c835E108f43a45fd223B5;
address constant ARBITRUM_ERC20CUSTODY_PROXY = 0xECe33274237E6422f2668eD7dEE5901b16336aA0;
address constant AVALANCHE_ERC20CUSTODY_PROXY = 0xECe33274237E6422f2668eD7dEE5901b16336aA0;

function _setupChains() internal override {
chains.push(
ChainConfig(ethereumForkId, ETHEREUM_ERC20CUSTODY_PROXY, ETHEREUM_ADMIN, ETHEREUM_RPC_URL, "Ethereum")
);
chains.push(ChainConfig(bscForkId, BSC_ERC20CUSTODY_PROXY, BSC_ADMIN, BSC_RPC_URL, "BSC"));
chains.push(ChainConfig(polygonForkId, POLYGON_ERC20CUSTODY_PROXY, POLYGON_ADMIN, POLYGON_RPC_URL, "Polygon"));
chains.push(ChainConfig(baseForkId, BASE_ERC20CUSTODY_PROXY, BASE_ADMIN, BASE_RPC_URL, "Base"));
chains.push(
ChainConfig(arbitrumForkId, ARBITRUM_ERC20CUSTODY_PROXY, ARBITRUM_ADMIN, ARBITRUM_RPC_URL, "Arbitrum")
);
chains.push(
ChainConfig(avalancheForkId, AVALANCHE_ERC20CUSTODY_PROXY, AVALANCHE_ADMIN, AVALANCHE_RPC_URL, "Avalanche")
);
}

function _testUpgradeContract(ChainConfig memory config) internal override {
vm.selectFork(config.forkId);

// Get the current proxy contract.
ERC20Custody custody = ERC20Custody(config.contractAddress);

// Storage state before the upgrade.
address gatewayBefore = address(custody.gateway());
address tssAddressBefore = custody.tssAddress();
bool supportsLegacyBefore = custody.supportsLegacy();

// Deploy new implementation.
vm.startPrank(config.admin);
ERC20Custody localNewImplementation = new ERC20Custody();

// Upgrade the proxy.
custody.upgradeToAndCall(address(localNewImplementation), "");

// Verify upgrade.
address onChainNewImplementation = _getImplementation(address(custody));
assertEq(onChainNewImplementation, address(localNewImplementation), "Upgrade failed.");
console.log("Successfully upgraded to:", onChainNewImplementation);

// Verify state preservation.
assertEq(address(custody.gateway()), gatewayBefore, "Gateway address changed.");
assertEq(custody.tssAddress(), tssAddressBefore, "TSS address changed.");
assertEq(custody.supportsLegacy(), supportsLegacyBefore, "Supports legacy changed.");
console.log("State preserved after upgrade");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can also check the the admin is preserved? Same comment for other contracts


vm.stopPrank();
}
}
60 changes: 60 additions & 0 deletions test/fork/GatewayEVMFork.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import { GatewayEVM } from "../../contracts/evm/GatewayEVM.sol";
import "./BaseForkTest.sol";

contract GatewayEVMForkTest is BaseForkTest {
// GatewayEVM proxy addresses.
address constant ETHEREUM_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed;
address constant BSC_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed;
address constant POLYGON_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed;
address constant BASE_GATEWAY_PROXY = 0x48B9AACC350b20147001f88821d31731Ba4C30ed;
address constant ARBITRUM_GATEWAY_PROXY = 0x1C53e188Bc2E471f9D4A4762CFf843d32C2C8549;
address constant AVALANCHE_GATEWAY_PROXY = 0x1C53e188Bc2E471f9D4A4762CFf843d32C2C8549;

function _setupChains() internal override {
chains.push(ChainConfig(ethereumForkId, ETHEREUM_GATEWAY_PROXY, ETHEREUM_ADMIN, ETHEREUM_RPC_URL, "Ethereum"));
chains.push(ChainConfig(bscForkId, BSC_GATEWAY_PROXY, BSC_ADMIN, BSC_RPC_URL, "BSC"));
chains.push(ChainConfig(polygonForkId, POLYGON_GATEWAY_PROXY, POLYGON_ADMIN, POLYGON_RPC_URL, "Polygon"));
chains.push(ChainConfig(baseForkId, BASE_GATEWAY_PROXY, BASE_ADMIN, BASE_RPC_URL, "Base"));
chains.push(ChainConfig(arbitrumForkId, ARBITRUM_GATEWAY_PROXY, ARBITRUM_ADMIN, ARBITRUM_RPC_URL, "Arbitrum"));
chains.push(
ChainConfig(avalancheForkId, AVALANCHE_GATEWAY_PROXY, AVALANCHE_ADMIN, AVALANCHE_RPC_URL, "Avalanche")
);
}

function _testUpgradeContract(ChainConfig memory config) internal override {
vm.selectFork(config.forkId);

// Get the current proxy contract.
GatewayEVM gateway = GatewayEVM(config.contractAddress);

// Storage state before the upgrade.
address custodyBefore = gateway.custody();
address tssAddressBefore = gateway.tssAddress();
address zetaConnectorBefore = gateway.zetaConnector();
address zetaTokenBefore = gateway.zetaToken();

// Deploy new implementation.
vm.startPrank(config.admin);
GatewayEVM localNewImplementation = new GatewayEVM();

// Upgrade the proxy.
gateway.upgradeToAndCall(address(localNewImplementation), "");

// Verify upgrade.
address onChainNewImplementation = _getImplementation(address(gateway));
assertEq(onChainNewImplementation, address(localNewImplementation), "Upgrade failed.");
console.log("Successfully upgraded to:", onChainNewImplementation);

// Verify state preservation.
assertEq(gateway.custody(), custodyBefore, "Custody address changed.");
assertEq(gateway.tssAddress(), tssAddressBefore, "TSS address changed.");
assertEq(gateway.zetaConnector(), zetaConnectorBefore, "Connector address changed.");
assertEq(gateway.zetaToken(), zetaTokenBefore, "Zeta token address changed.");
console.log("State preserved after upgrade");

vm.stopPrank();
}
}
44 changes: 44 additions & 0 deletions test/fork/GatewayZEVMFork.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import { GatewayZEVM } from "../../contracts/zevm/GatewayZEVM.sol";
import "./BaseForkTest.sol";

contract GatewayZEVMForkTest is BaseForkTest {
// GatewayZEVM proxy address.
address constant ZETACHAIN_GATEWAY_PROXY = 0xfEDD7A6e3Ef1cC470fbfbF955a22D793dDC0F44E;

function _setupChains() internal override {
chains.push(
ChainConfig(zetachainForkId, ZETACHAIN_GATEWAY_PROXY, ZETACHAIN_ADMIN, ZETACHAIN_RPC_URL, "ZetaChain")
);
}

function _testUpgradeContract(ChainConfig memory config) internal override {
vm.selectFork(config.forkId);

// Get the current proxy contract.
GatewayZEVM gateway = GatewayZEVM(payable(config.contractAddress));

// Storage state before the upgrade.
address zetaTokenBefore = gateway.zetaToken();

// Deploy new implementation.
vm.startPrank(config.admin);
GatewayZEVM localNewImplementation = new GatewayZEVM();

// Upgrade the proxy.
gateway.upgradeToAndCall(address(localNewImplementation), "");

// Verify upgrade.
address onChainNewImplementation = _getImplementation(address(gateway));
assertEq(onChainNewImplementation, address(localNewImplementation), "Upgrade failed.");
console.log("Successfully upgraded to:", onChainNewImplementation);

// Verify state preservation.
assertEq(gateway.zetaToken(), zetaTokenBefore, "Zeta token address changed.");
console.log("State preserved after upgrade");

vm.stopPrank();
}
}
Loading