Skip to content

Commit

Permalink
[gms-1387] migrate OAL typescript tests to forge (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonzwli authored Jan 25, 2024
1 parent 3001244 commit f415b6d
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 124 deletions.
118 changes: 0 additions & 118 deletions test/allowlist/OperatorAllowlist.test.ts

This file was deleted.

102 changes: 96 additions & 6 deletions test/allowlist/OperatorAllowlistUpgradeable.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,30 @@ import {OperatorAllowlistUpgradeable} from "../../contracts/allowlist/OperatorAl
import {MockOperatorAllowlistUpgradeable} from "./MockOAL.sol";
import {ImmutableERC721} from "../../contracts/token/erc721/preset/ImmutableERC721.sol";
import {DeployOperatorAllowlist} from "../utils/DeployAllowlistProxy.sol";
import {DeploySCWallet} from "../utils/DeploySCW.sol";
import {IWalletProxy} from "../../contracts/allowlist/IWalletProxy.sol";


contract OperatorAllowlistTest is Test {
contract OperatorAllowlistTest is Test, OperatorAllowlistUpgradeable {
OperatorAllowlistUpgradeable public allowlist;
ImmutableERC721 public immutableERC721;
MockOperatorAllowlistUpgradeable public oalV2;
DeploySCWallet public deploySCWScript;

uint256 feeReceiverKey = 1;

address public admin = makeAddr("roleAdmin");
address public upgrader = makeAddr("roleUpgrader");
address public registerar = makeAddr("roleRegisterar");
address public registrar = makeAddr("roleRegisterar");
address public scwOwner = makeAddr("scwOwner");
address feeReceiver = vm.addr(feeReceiverKey);
address proxyAddr;
address nonAuthorizedWallet;

address scwAddr;
address scwModuleAddr;

function setUp() public {
DeployOperatorAllowlist deployScript = new DeployOperatorAllowlist();
proxyAddr = deployScript.run(admin, upgrader, registerar);
proxyAddr = deployScript.run(admin, upgrader, registrar);

allowlist = OperatorAllowlistUpgradeable(proxyAddr);

Expand All @@ -43,11 +47,13 @@ contract OperatorAllowlistTest is Test {
);

nonAuthorizedWallet = address(0x2);

deploySCWScript = new DeploySCWallet();
}

function testDeployment() public {
assertTrue(allowlist.hasRole(allowlist.DEFAULT_ADMIN_ROLE(), admin));
assertTrue(allowlist.hasRole(allowlist.REGISTRAR_ROLE(), registerar));
assertTrue(allowlist.hasRole(allowlist.REGISTRAR_ROLE(), registrar));
assertTrue(allowlist.hasRole(allowlist.UPGRADE_ROLE(), upgrader));
assertEq(address(immutableERC721.operatorAllowlist()), proxyAddr);
}
Expand All @@ -70,4 +76,88 @@ contract OperatorAllowlistTest is Test {
vm.expectRevert("Must have upgrade role to upgrade");
allowlist.upgradeTo(address(oalImplV2));
}

function testShouldSupportIOperatorAllowlistInterface() public {
assertTrue(allowlist.supportsInterface(0x05a3b809));
}

function testShouldLimitAllowlistAddAndRemoveFunctionality() public {
address[] memory addressTargets = new address[](1);
addressTargets[0] = address(0x1);
vm.startPrank(admin);

vm.expectRevert("AccessControl: account 0xe48648ee1c7285ff3ad32fa99c427884666caf17 is missing role 0x5245474953545241525f524f4c45000000000000000000000000000000000000");
allowlist.addAddressesToAllowlist(addressTargets);

vm.expectRevert("AccessControl: account 0xe48648ee1c7285ff3ad32fa99c427884666caf17 is missing role 0x5245474953545241525f524f4c45000000000000000000000000000000000000");
allowlist.removeAddressesFromAllowlist(addressTargets);

vm.expectRevert("AccessControl: account 0xe48648ee1c7285ff3ad32fa99c427884666caf17 is missing role 0x5245474953545241525f524f4c45000000000000000000000000000000000000");
allowlist.addWalletToAllowlist(address(0x3));

vm.expectRevert("AccessControl: account 0xe48648ee1c7285ff3ad32fa99c427884666caf17 is missing role 0x5245474953545241525f524f4c45000000000000000000000000000000000000");
allowlist.removeWalletFromAllowlist(address(0x3));

vm.stopPrank();
}

function testShouldAddAndRemoveSmartContractWalletBytecodeFromAllowlist() public {
bytes32 salt = keccak256(abi.encodePacked("0x1234"));
(scwAddr, scwModuleAddr) = deploySCWScript.run(salt);

IWalletProxy proxy = IWalletProxy(scwAddr);
address implementationAddress = proxy.PROXY_getImplementation();
assertEq(implementationAddress, scwModuleAddr);

bytes memory deployedBytecode = scwAddr.code;

vm.startPrank(registrar);

vm.expectEmit(true, true, true, false, address(allowlist));
emit WalletAllowlistChanged(keccak256(abi.encodePacked(deployedBytecode)), scwAddr, true);
allowlist.addWalletToAllowlist(scwAddr);
assertTrue(allowlist.isAllowlisted(scwAddr));

vm.expectEmit(true, true, true, false, address(allowlist));
emit WalletAllowlistChanged(keccak256(abi.encodePacked(deployedBytecode)), scwAddr, false);
allowlist.removeWalletFromAllowlist(scwAddr);
assertFalse(allowlist.isAllowlisted(scwAddr));

vm.stopPrank();
}

function testShouldAddAndRemoveAnAddressOfAMarketPlaceAndRemoveItFromAllowlist() public {
address[] memory addressTargets = new address[](1);
addressTargets[0] = address(0x1);

vm.startPrank(registrar);

vm.expectEmit(true, true, true, false, address(allowlist));
emit AddressAllowlistChanged(addressTargets[0], true);
allowlist.addAddressesToAllowlist(addressTargets);
assertTrue(allowlist.isAllowlisted(addressTargets[0]));

vm.expectEmit(true, true, true, false, address(allowlist));
emit AddressAllowlistChanged(addressTargets[0], false);
allowlist.removeAddressesFromAllowlist(addressTargets);
assertFalse(allowlist.isAllowlisted(addressTargets[0]));

vm.stopPrank();
}

function testShouldNotAllowlistSCWWithSameBytecodeButDifferentImplementationAddress() public {
bytes32 salt1 = keccak256(abi.encodePacked("0x5678"));
address firstScwAddr;
(firstScwAddr, ) = deploySCWScript.run(salt1);

vm.startPrank(registrar);
allowlist.addWalletToAllowlist(firstScwAddr);
assertTrue(allowlist.isAllowlisted(firstScwAddr));

bytes32 salt2 = keccak256(abi.encodePacked("0x5678"));
address secondScwAddr;
(secondScwAddr, ) = deploySCWScript.run(salt2);
assertFalse(allowlist.isAllowlisted(secondScwAddr));
vm.stopPrank();
}
}
23 changes: 23 additions & 0 deletions test/utils/DeploySCW.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

// SPDX-License-Identifier: Apache 2.0
pragma solidity ^0.8.19;

import "forge-std/Test.sol";
import {MockWallet} from "../../../contracts/mocks/MockWallet.sol";
import {MockWalletFactory} from "../../../contracts/mocks/MockWalletFactory.sol";

contract DeploySCWallet is Test {
MockWallet public mockWalletModule;
MockWallet public scw;
MockWalletFactory public scmf;
address public scwAddress;

function run(bytes32 salt) external returns (address, address) {
scmf = new MockWalletFactory();
mockWalletModule = new MockWallet();
scmf.deploy(address(mockWalletModule), salt);
scwAddress = scmf.getAddress(address(mockWalletModule), salt);
scw = MockWallet(scwAddress);
return (scwAddress, address(mockWalletModule));
}
}

0 comments on commit f415b6d

Please sign in to comment.