Skip to content

Commit

Permalink
migrate upgrade test
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jul 15, 2024
1 parent 6fff8af commit 3c10a32
Show file tree
Hide file tree
Showing 12 changed files with 108 additions and 83 deletions.
2 changes: 1 addition & 1 deletion contracts/prototypes/evm/ERC20CustodyNew.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./interfaces.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/prototypes/evm/GatewayEVM.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
Expand Down
1 change: 1 addition & 0 deletions contracts/prototypes/evm/GatewayEVMUpgradeTest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
// NOTE: Purpose of this contract is to test upgrade process, the only difference should be name of Executed event
// The Gateway contract is the endpoint to call smart contracts on external chains
// The contract doesn't hold any funds and should never have active allowances
/// @custom:oz-upgrades-from GatewayEVM
contract GatewayEVMUpgradeTest is Initializable, OwnableUpgradeable, UUPSUpgradeable {
using SafeERC20 for IERC20;

Expand Down
2 changes: 1 addition & 1 deletion contracts/prototypes/evm/ReceiverEVM.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
Expand Down
2 changes: 1 addition & 1 deletion contracts/prototypes/evm/TestERC20.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

Expand Down
2 changes: 1 addition & 1 deletion contracts/prototypes/evm/interfaces.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
pragma solidity ^0.8.0;

interface IGatewayEVMEvents {
event Executed(address indexed destination, uint256 value, bytes data);
Expand Down
11 changes: 8 additions & 3 deletions contracts/prototypes/test/GatewayEVM.t.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.7;
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import "forge-std/Vm.sol";
Expand All @@ -10,7 +10,9 @@ import "contracts/prototypes/evm/ERC20CustodyNew.sol";
import "contracts/prototypes/evm/TestERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "../evm/interfaces.sol";
import {Upgrades} from "openzeppelin-foundry-upgrades/LegacyUpgrades.sol";

contract GatewayEVMTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IReceiverEVMEvents {
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -157,10 +159,13 @@ contract GatewayEVMInboundTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IR
tssAddress = address(0x5678);

token = new TestERC20("test", "TTK");
gateway = new GatewayEVM();
address proxy = address(new ERC1967Proxy(
address(new GatewayEVM()),
abi.encodeWithSelector(GatewayEVM.initialize.selector, (tssAddress))
));
gateway = GatewayEVM(proxy);
custody = new ERC20CustodyNew(address(gateway));

gateway.initialize(tssAddress);
gateway.setCustody(address(custody));

// Mint initial supply to the owner
Expand Down
86 changes: 86 additions & 0 deletions contracts/prototypes/test/GatewayEVMUpgrade.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

import "contracts/prototypes/evm/GatewayEVM.sol";
import "contracts/prototypes/evm/GatewayEVMUpgradeTest.sol";
import "contracts/prototypes/evm/ReceiverEVM.sol";
import "contracts/prototypes/evm/ERC20CustodyNew.sol";
import "contracts/prototypes/evm/TestERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {ERC1967Proxy} from "@openzeppelin/contracts/proxy/ERC1967/ERC1967Proxy.sol";
import "../evm/interfaces.sol";
import {Upgrades} from "openzeppelin-foundry-upgrades/LegacyUpgrades.sol";

contract GatewayEVMUUPSUpgradeTest is Test, IGatewayEVMErrors, IGatewayEVMEvents, IReceiverEVMEvents {
using SafeERC20 for IERC20;
event ExecutedV2(address indexed destination, uint256 value, bytes data);

address proxy;
GatewayEVM gateway;
ReceiverEVM receiver;
ERC20CustodyNew custody;
TestERC20 token;
address owner;
address destination;
address tssAddress;

function setUp() public {
owner = address(this);
destination = address(0x1234);
tssAddress = address(0x5678);

token = new TestERC20("test", "TTK");

proxy = address(new ERC1967Proxy(
address(new GatewayEVM()),
abi.encodeWithSelector(GatewayEVM.initialize.selector, (tssAddress))
));
gateway = GatewayEVM(proxy);

custody = new ERC20CustodyNew(address(gateway));
receiver = new ReceiverEVM();

gateway.setCustody(address(custody));

// Mint initial supply to the owner
token.mint(owner, 1000000);

// Transfer some tokens to the custody contract
token.transfer(address(custody), 500000);
}

function testUpgradeAndForwardCallToReceivePayable() public {
address custodyBeforeUpgrade = gateway.custody();
address tssBeforeUpgrade = gateway.tssAddress();

string memory str = "Hello, Foundry!";
uint256 num = 42;
bool flag = true;
uint256 value = 1 ether;

vm.prank(owner);
Upgrades.upgradeProxy(
proxy,
"GatewayEVMUpgradeTest.sol",
"",
owner
);

bytes memory data = abi.encodeWithSignature("receivePayable(string,uint256,bool)", str, num, flag);
GatewayEVMUpgradeTest gatewayUpgradeTest = GatewayEVMUpgradeTest(proxy);

vm.expectCall(address(receiver), value, data);
vm.expectEmit(true, true, true, true, address(receiver));
emit ReceivedPayable(address(gateway), value, str, num, flag);
vm.expectEmit(true, true, true, true, address(gateway));
emit ExecutedV2(address(receiver), value, data);
gateway.execute{value: value}(address(receiver), data);

assertEq(custodyBeforeUpgrade, gateway.custody());
assertEq(tssBeforeUpgrade, gateway.tssAddress());
}
}
7 changes: 6 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ test = 'test'
cache_path = 'cache_forge'
no-match-contract = '.*EchidnaTest$'
optimizer = true
optimizer_runs = 10_000
optimizer_runs = 10_000
ffi = true
ast = true
build_info = true
extra_output = ["storageLayout"]
fs_permissions = [{ access = "read", path = "out"}]
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
"localnet": "concurrently --names \"NODE,WORKER\" --prefix-colors \"blue.bold,green.bold\" \"npx hardhat node\" \"wait-on tcp:8545 && yarn worker\"",
"prepublishOnly": "yarn build",
"test": "npx hardhat test",
"test:forge": "forge test -vvv",
"test:forge": "forge clean && forge test -vvv",
"test:prototypes": "yarn compile && npx hardhat test test/prototypes/*",
"tsc:watch": "npx tsc --watch",
"worker": "npx hardhat run scripts/worker.ts --network localhost"
Expand Down
1 change: 1 addition & 0 deletions remappings.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
@openzeppelin/=node_modules/@openzeppelin/
@openzeppelin/contracts-upgradeable/=node_modules/@openzeppelin/contracts-upgradeable/
forge-std/=lib/forge-std/src/
73 changes: 0 additions & 73 deletions test/prototypes/GatewayEVMUpgrade.spec.ts

This file was deleted.

0 comments on commit 3c10a32

Please sign in to comment.