diff --git a/v2/test/ZetaConnectorNonNative.t.sol b/v2/test/ZetaConnectorNonNative.t.sol index 2bf2a1de..01d904a3 100644 --- a/v2/test/ZetaConnectorNonNative.t.sol +++ b/v2/test/ZetaConnectorNonNative.t.sol @@ -35,7 +35,7 @@ contract ZetaConnectorNonNativeTest is ReceiverEVM receiver; ERC20Custody custody; ZetaConnectorNonNative zetaConnector; - // ZetaNonEth zetaToken; + ZetaNonEth zetaToken; address owner; address destination; address tssAddress; @@ -45,222 +45,222 @@ contract ZetaConnectorNonNativeTest is destination = address(0x1234); tssAddress = address(0x5678); - // zetaToken = new ZetaNonEth(tssAddress, tssAddress); + zetaToken = new ZetaNonEth(tssAddress, tssAddress); - // proxy = Upgrades.deployUUPSProxy( - // "GatewayEVM.sol", - // abi.encodeCall(GatewayEVM.initialize, (tssAddress, address(zeta))) - // ); - // gateway = GatewayEVM(proxy); + proxy = Upgrades.deployUUPSProxy( + "GatewayEVM.sol", + abi.encodeCall(GatewayEVM.initialize, (tssAddress, address(zetaToken))) + ); + gateway = GatewayEVM(proxy); - // custody = new ERC20Custody(address(gateway), tssAddress); - // zetaConnector = new ZetaConnectorNonNative(address(gateway), address(zetaToken), tssAddress); + custody = new ERC20Custody(address(gateway), tssAddress); + zetaConnector = new ZetaConnectorNonNative(address(gateway), address(zetaToken), tssAddress); - // vm.prank(tssAddress); - // zetaToken.updateTssAndConnectorAddresses(tssAddress, address(zetaConnector)); + vm.prank(tssAddress); + zetaToken.updateTssAndConnectorAddresses(tssAddress, address(zetaConnector)); - // receiver = new ReceiverEVM(); + receiver = new ReceiverEVM(); - // vm.deal(tssAddress, 1 ether); + vm.deal(tssAddress, 1 ether); - // vm.startPrank(tssAddress); - // gateway.setCustody(address(custody)); - // gateway.setConnector(address(zetaConnector)); - // vm.stopPrank(); + vm.startPrank(tssAddress); + gateway.setCustody(address(custody)); + gateway.setConnector(address(zetaConnector)); + vm.stopPrank(); } - // function testWithdraw() public { - // uint256 amount = 100000; - // uint256 balanceBefore = zetaToken.balanceOf(destination); - // assertEq(balanceBefore, 0); - // bytes32 internalSendHash = ""; - - // bytes memory data = abi.encodeWithSignature("mint(address,uint256,bytes32)", destination, amount, - // internalSendHash); - // vm.expectCall(address(zetaToken), 0, data); - // vm.expectEmit(true, true, true, true, address(zetaConnector)); - // emit Withdraw(destination, amount); - // vm.prank(tssAddress); - // zetaConnector.withdraw(destination, amount, internalSendHash); - // uint256 balanceAfter = zetaToken.balanceOf(destination); - // assertEq(balanceAfter, amount); - // } - - // function testWithdrawFailsIfSenderIsNotTSS() public { - // uint256 amount = 100000; - // bytes32 internalSendHash = ""; - - // vm.prank(owner); - // vm.expectRevert(InvalidSender.selector); - // zetaConnector.withdraw(destination, amount, internalSendHash); - // } - - // function testWithdrawAndCallReceiveERC20() public { - // uint256 amount = 100000; - // bytes32 internalSendHash = ""; - // bytes memory data = abi.encodeWithSignature("receiveERC20(uint256,address,address)", amount, - // address(zetaToken), destination); - // uint256 balanceBefore = zetaToken.balanceOf(destination); - // assertEq(balanceBefore, 0); - // uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - // assertEq(balanceBeforeZetaConnector, 0); - - // bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, - // internalSendHash); - // vm.expectCall(address(zetaToken), 0, mintData); - // vm.expectEmit(true, true, true, true, address(receiver)); - // emit ReceivedERC20(address(gateway), amount, address(zetaToken), destination); - // vm.expectEmit(true, true, true, true, address(zetaConnector)); - // emit WithdrawAndCall(address(receiver), amount, data); - // vm.prank(tssAddress); - // zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); - - // // Verify that the tokens were transferred to the destination address - // uint256 balanceAfter = zetaToken.balanceOf(destination); - // assertEq(balanceAfter, amount); - - // // Verify that zeta connector doesn't hold any tokens - // uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - // assertEq(balanceAfterZetaConnector, 0); - - // // Verify that the approval was reset - // uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); - // assertEq(allowance, 0); - - // // Verify that gateway doesn't hold any tokens - // uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); - // assertEq(balanceGateway, 0); - // } - - // function testWithdrawAndCallReceiveERC20FailsIfSenderIsNotTSS() public { - // uint256 amount = 100000; - // bytes32 internalSendHash = ""; - // bytes memory data = abi.encodeWithSignature("receiveERC20(uint256,address,address)", amount, - // address(zetaToken), destination); - - // vm.prank(owner); - // vm.expectRevert(InvalidSender.selector); - // zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); - // } - - // function testWithdrawAndCallReceiveNoParams() public { - // uint256 amount = 100000; - // bytes32 internalSendHash = ""; - // bytes memory data = abi.encodeWithSignature("receiveNoParams()"); - // uint256 balanceBefore = zetaToken.balanceOf(destination); - // assertEq(balanceBefore, 0); - // uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - // assertEq(balanceBeforeZetaConnector, 0); - - // bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, - // internalSendHash); - // vm.expectCall(address(zetaToken), 0, mintData); - // vm.expectEmit(true, true, true, true, address(receiver)); - // emit ReceivedNoParams(address(gateway)); - // vm.expectEmit(true, true, true, true, address(zetaConnector)); - // emit WithdrawAndCall(address(receiver), amount, data); - // vm.prank(tssAddress); - // zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); - - // // Verify that the no tokens were transferred to the destination address - // uint256 balanceAfter = zetaToken.balanceOf(destination); - // assertEq(balanceAfter, 0); - - // // Verify that zeta connector doesn't hold any tokens - // uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - // assertEq(balanceAfterZetaConnector, 0); - - // // Verify that the approval was reset - // uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); - // assertEq(allowance, 0); - - // // Verify that gateway doesn't hold any tokens - // uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); - // assertEq(balanceGateway, 0); - // } - - // function testWithdrawAndCallReceiveERC20Partial() public { - // uint256 amount = 100000; - // bytes32 internalSendHash = ""; - // bytes memory data = abi.encodeWithSignature("receiveERC20Partial(uint256,address,address)", amount, - // address(zetaToken), destination); - // uint256 balanceBefore = zetaToken.balanceOf(destination); - // assertEq(balanceBefore, 0); - // uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - // assertEq(balanceBeforeZetaConnector, 0); - - // bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, - // internalSendHash); - // vm.expectCall(address(zetaToken), 0, mintData); - // vm.expectEmit(true, true, true, true, address(receiver)); - // emit ReceivedERC20(address(gateway), amount / 2, address(zetaToken), destination); - // vm.expectEmit(true, true, true, true, address(zetaConnector)); - // emit WithdrawAndCall(address(receiver), amount, data); - // vm.prank(tssAddress); - // zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); - - // // Verify that the tokens were transferred to the destination address - // uint256 balanceAfter = zetaToken.balanceOf(destination); - // assertEq(balanceAfter, amount / 2); - - // // Verify that zeta connector doesn't hold any tokens - // uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - // assertEq(balanceAfterZetaConnector, 0); - - // // Verify that the approval was reset - // uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); - // assertEq(allowance, 0); - - // // Verify that gateway doesn't hold any tokens - // uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); - // assertEq(balanceGateway, 0); - // } - - // function testWithdrawAndRevert() public { - // uint256 amount = 100000; - // bytes32 internalSendHash = ""; - // bytes memory data = abi.encodePacked("hello"); - // uint256 balanceBefore = zetaToken.balanceOf(address(receiver)); - // assertEq(balanceBefore, 0); - // uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - - // bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, - // internalSendHash); - // vm.expectCall(address(zetaToken), 0, mintData); - // // Verify that onRevert callback was called - // vm.expectEmit(true, true, true, true, address(receiver)); - // emit ReceivedRevert(address(gateway), data); - // vm.expectEmit(true, true, true, true, address(gateway)); - // emit RevertedWithERC20(address(zetaToken), address(receiver), amount, data); - // vm.expectEmit(true, true, true, true, address(zetaConnector)); - // emit WithdrawAndRevert(address(receiver), amount, data); - // vm.prank(tssAddress); - // zetaConnector.withdrawAndRevert(address(receiver), amount, data, internalSendHash); - - // // Verify that the tokens were transferred to the receiver address - // uint256 balanceAfter = zetaToken.balanceOf(address(receiver)); - // assertEq(balanceAfter, amount); - - // // Verify that zeta connector doesn't get more tokens - // uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); - // assertEq(balanceAfterZetaConnector, balanceBeforeZetaConnector); - - // // Verify that the approval was reset - // uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); - // assertEq(allowance, 0); - - // // Verify that gateway doesn't hold any tokens - // uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); - // assertEq(balanceGateway, 0); - // } - - // function testWithdrawAndRevertFailsIfSenderIsNotTSS() public { - // uint256 amount = 100000; - // bytes32 internalSendHash = ""; - // bytes memory data = abi.encodePacked("hello"); - - // vm.prank(owner); - // vm.expectRevert(InvalidSender.selector); - // zetaConnector.withdrawAndRevert(address(receiver), amount, data, internalSendHash); - // } + function testWithdraw() public { + uint256 amount = 100000; + uint256 balanceBefore = zetaToken.balanceOf(destination); + assertEq(balanceBefore, 0); + bytes32 internalSendHash = ""; + + bytes memory data = abi.encodeWithSignature("mint(address,uint256,bytes32)", destination, amount, + internalSendHash); + vm.expectCall(address(zetaToken), 0, data); + vm.expectEmit(true, true, true, true, address(zetaConnector)); + emit Withdraw(destination, amount); + vm.prank(tssAddress); + zetaConnector.withdraw(destination, amount, internalSendHash); + uint256 balanceAfter = zetaToken.balanceOf(destination); + assertEq(balanceAfter, amount); + } + + function testWithdrawFailsIfSenderIsNotTSS() public { + uint256 amount = 100000; + bytes32 internalSendHash = ""; + + vm.prank(owner); + vm.expectRevert(InvalidSender.selector); + zetaConnector.withdraw(destination, amount, internalSendHash); + } + + function testWithdrawAndCallReceiveERC20() public { + uint256 amount = 100000; + bytes32 internalSendHash = ""; + bytes memory data = abi.encodeWithSignature("receiveERC20(uint256,address,address)", amount, + address(zetaToken), destination); + uint256 balanceBefore = zetaToken.balanceOf(destination); + assertEq(balanceBefore, 0); + uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + assertEq(balanceBeforeZetaConnector, 0); + + bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, + internalSendHash); + vm.expectCall(address(zetaToken), 0, mintData); + vm.expectEmit(true, true, true, true, address(receiver)); + emit ReceivedERC20(address(gateway), amount, address(zetaToken), destination); + vm.expectEmit(true, true, true, true, address(zetaConnector)); + emit WithdrawAndCall(address(receiver), amount, data); + vm.prank(tssAddress); + zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); + + // Verify that the tokens were transferred to the destination address + uint256 balanceAfter = zetaToken.balanceOf(destination); + assertEq(balanceAfter, amount); + + // Verify that zeta connector doesn't hold any tokens + uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + assertEq(balanceAfterZetaConnector, 0); + + // Verify that the approval was reset + uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); + assertEq(allowance, 0); + + // Verify that gateway doesn't hold any tokens + uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); + assertEq(balanceGateway, 0); + } + + function testWithdrawAndCallReceiveERC20FailsIfSenderIsNotTSS() public { + uint256 amount = 100000; + bytes32 internalSendHash = ""; + bytes memory data = abi.encodeWithSignature("receiveERC20(uint256,address,address)", amount, + address(zetaToken), destination); + + vm.prank(owner); + vm.expectRevert(InvalidSender.selector); + zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); + } + + function testWithdrawAndCallReceiveNoParams() public { + uint256 amount = 100000; + bytes32 internalSendHash = ""; + bytes memory data = abi.encodeWithSignature("receiveNoParams()"); + uint256 balanceBefore = zetaToken.balanceOf(destination); + assertEq(balanceBefore, 0); + uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + assertEq(balanceBeforeZetaConnector, 0); + + bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, + internalSendHash); + vm.expectCall(address(zetaToken), 0, mintData); + vm.expectEmit(true, true, true, true, address(receiver)); + emit ReceivedNoParams(address(gateway)); + vm.expectEmit(true, true, true, true, address(zetaConnector)); + emit WithdrawAndCall(address(receiver), amount, data); + vm.prank(tssAddress); + zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); + + // Verify that the no tokens were transferred to the destination address + uint256 balanceAfter = zetaToken.balanceOf(destination); + assertEq(balanceAfter, 0); + + // Verify that zeta connector doesn't hold any tokens + uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + assertEq(balanceAfterZetaConnector, 0); + + // Verify that the approval was reset + uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); + assertEq(allowance, 0); + + // Verify that gateway doesn't hold any tokens + uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); + assertEq(balanceGateway, 0); + } + + function testWithdrawAndCallReceiveERC20Partial() public { + uint256 amount = 100000; + bytes32 internalSendHash = ""; + bytes memory data = abi.encodeWithSignature("receiveERC20Partial(uint256,address,address)", amount, + address(zetaToken), destination); + uint256 balanceBefore = zetaToken.balanceOf(destination); + assertEq(balanceBefore, 0); + uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + assertEq(balanceBeforeZetaConnector, 0); + + bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, + internalSendHash); + vm.expectCall(address(zetaToken), 0, mintData); + vm.expectEmit(true, true, true, true, address(receiver)); + emit ReceivedERC20(address(gateway), amount / 2, address(zetaToken), destination); + vm.expectEmit(true, true, true, true, address(zetaConnector)); + emit WithdrawAndCall(address(receiver), amount, data); + vm.prank(tssAddress); + zetaConnector.withdrawAndCall(address(receiver), amount, data, internalSendHash); + + // Verify that the tokens were transferred to the destination address + uint256 balanceAfter = zetaToken.balanceOf(destination); + assertEq(balanceAfter, amount / 2); + + // Verify that zeta connector doesn't hold any tokens + uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + assertEq(balanceAfterZetaConnector, 0); + + // Verify that the approval was reset + uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); + assertEq(allowance, 0); + + // Verify that gateway doesn't hold any tokens + uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); + assertEq(balanceGateway, 0); + } + + function testWithdrawAndRevert() public { + uint256 amount = 100000; + bytes32 internalSendHash = ""; + bytes memory data = abi.encodePacked("hello"); + uint256 balanceBefore = zetaToken.balanceOf(address(receiver)); + assertEq(balanceBefore, 0); + uint256 balanceBeforeZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + + bytes memory mintData = abi.encodeWithSignature("mint(address,uint256,bytes32)", address(gateway), amount, + internalSendHash); + vm.expectCall(address(zetaToken), 0, mintData); + // Verify that onRevert callback was called + vm.expectEmit(true, true, true, true, address(receiver)); + emit ReceivedRevert(address(gateway), data); + vm.expectEmit(true, true, true, true, address(gateway)); + emit RevertedWithERC20(address(zetaToken), address(receiver), amount, data); + vm.expectEmit(true, true, true, true, address(zetaConnector)); + emit WithdrawAndRevert(address(receiver), amount, data); + vm.prank(tssAddress); + zetaConnector.withdrawAndRevert(address(receiver), amount, data, internalSendHash); + + // Verify that the tokens were transferred to the receiver address + uint256 balanceAfter = zetaToken.balanceOf(address(receiver)); + assertEq(balanceAfter, amount); + + // Verify that zeta connector doesn't get more tokens + uint256 balanceAfterZetaConnector = zetaToken.balanceOf(address(zetaConnector)); + assertEq(balanceAfterZetaConnector, balanceBeforeZetaConnector); + + // Verify that the approval was reset + uint256 allowance = zetaToken.allowance(address(gateway), address(receiver)); + assertEq(allowance, 0); + + // Verify that gateway doesn't hold any tokens + uint256 balanceGateway = zetaToken.balanceOf(address(gateway)); + assertEq(balanceGateway, 0); + } + + function testWithdrawAndRevertFailsIfSenderIsNotTSS() public { + uint256 amount = 100000; + bytes32 internalSendHash = ""; + bytes memory data = abi.encodePacked("hello"); + + vm.prank(owner); + vm.expectRevert(InvalidSender.selector); + zetaConnector.withdrawAndRevert(address(receiver), amount, data, internalSendHash); + } }