diff --git a/test/Zap.t.sol b/test/Zap.t.sol index e7a689f..201098d 100644 --- a/test/Zap.t.sol +++ b/test/Zap.t.sol @@ -1,7 +1,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.20; -import {Bootstrap, Zap} from "test/utils/Bootstrap.sol"; +import {Bootstrap} from "test/utils/Bootstrap.sol"; import {IERC20} from "../src/interfaces/IERC20.sol"; import {ZapExposed} from "test/utils/exposed/ZapExposed.sol"; @@ -9,8 +9,6 @@ contract ZapTest is Bootstrap { IERC20 internal SUSD; IERC20 internal USDC; - Zap.ZapDetails internal details; - function setUp() public { vm.rollFork(BASE_BLOCK_NUMBER); initializeBase(); @@ -22,13 +20,18 @@ contract ZapTest is Bootstrap { deal(address(USDC), ACTOR, UINT_ONE_HUNDRED); } + function test_deploy() public view { + assert(address(zap) != address(0)); + } + function test_usdc_zero_address() public { vm.expectRevert(); new ZapExposed({ _usdc: address(0), _susd: address(0x1), - _spotMarketProxy: address(0x1) + _spotMarketProxy: address(0x1), + _sUSDCId: type(uint128).max }); } @@ -38,7 +41,8 @@ contract ZapTest is Bootstrap { new ZapExposed({ _usdc: address(0x1), _susd: address(0), - _spotMarketProxy: address(0x1) + _spotMarketProxy: address(0x1), + _sUSDCId: type(uint128).max }); } @@ -48,51 +52,56 @@ contract ZapTest is Bootstrap { new ZapExposed({ _usdc: address(0x1), _susd: address(0x1), - _spotMarketProxy: address(0) + _spotMarketProxy: address(0), + _sUSDCId: type(uint128).max }); } -} -contract VerifySynthMarketId is ZapTest { - function test_verifySynthMarketId_true() public { - // correctly verfies + function test_sUSDCId_invalid() public { + vm.expectRevert(); + + new ZapExposed({ + _usdc: address(0x1), + _susd: address(0x1), + _spotMarketProxy: address(0x1), + _sUSDCId: type(uint128).max + }); } } contract ZapIn is ZapTest { function test_zap_in() public { - details = Zap.ZapDetails({ - sUSDCMarketId: sUSDCMarketId, - amount: INT_ONE_HUNDRED, - referrer: REFERRER - }); - vm.startPrank(ACTOR); USDC.approve(address(zap), UINT_ONE_HUNDRED); - zap.zap(details); + zap.zap(INT_ONE_HUNDRED, REFERRER); assertEq(SUSD.balanceOf(address(zap)), UINT_ONE_HUNDRED); vm.stopPrank(); } + /// @dev test assumes the wrapper cap is always < 2^255 - 1 (max int128) function test_zap_in_exceeds_cap() public { - // determine if there is a cap and how to find it - // deal x USDC to ACTOR where x > cap - // attempt to zap + // convert to uint256 for deal/approval + uint256 cap = type(uint128).max; + + deal(address(USDC), ACTOR, cap); + + vm.startPrank(ACTOR); + + USDC.approve(address(zap), cap); + + try zap.zap(type(int128).max, REFERRER) {} + catch (bytes memory reason) { + assertEq(bytes4(reason), WrapperExceedsMaxAmount.selector); + } - assertEq(true, false); + vm.stopPrank(); } function test_zap_in_event() public { - details = Zap.ZapDetails({ - sUSDCMarketId: sUSDCMarketId, - amount: INT_ONE_HUNDRED, - referrer: REFERRER - }); - vm.startPrank(ACTOR); USDC.approve(address(zap), UINT_ONE_HUNDRED); @@ -100,54 +109,31 @@ contract ZapIn is ZapTest { vm.expectEmit(true, true, true, false); emit ZappedIn(UINT_ONE_HUNDRED); - zap.zap(details); + zap.zap(INT_ONE_HUNDRED, REFERRER); vm.stopPrank(); } } contract ZapOut is ZapTest { - function test_zap_out() public { - details = Zap.ZapDetails({ - sUSDCMarketId: sUSDCMarketId, - amount: INT_NEGATIVE_ONE_HUNDRED, - referrer: REFERRER - }); + // function test_zap_out() public { + // vm.startPrank(ACTOR); - vm.startPrank(ACTOR); - - SUSD.transfer(address(zap), UINT_ONE_HUNDRED); + // SUSD.transfer(address(zap), UINT_ONE_HUNDRED); - zap.zap(details); + // zap.zap(INT_NEGATIVE_ONE_HUNDRED, REFERRER); - assertEq(USDC.balanceOf(address(zap)), UINT_ONE_HUNDRED); + // assertEq(USDC.balanceOf(address(zap)), UINT_ONE_HUNDRED); - vm.stopPrank(); - } + // vm.stopPrank(); + // } function test_zap_out_zero() public { - assertEq(true, false); - } - - function test_zap_out_exceeds_cap() public { - assertEq(true, false); - } - - function test_zap_out_event() public { - details = Zap.ZapDetails({ - sUSDCMarketId: sUSDCMarketId, - amount: INT_NEGATIVE_ONE_HUNDRED, - referrer: REFERRER - }); - vm.startPrank(ACTOR); - SUSD.transfer(address(zap), UINT_ONE_HUNDRED); - - vm.expectEmit(true, true, true, false); - emit ZappedOut(UINT_ONE_HUNDRED); + vm.expectRevert(); // fails at buy() - zap.zap(details); + zap.zap(0, REFERRER); vm.stopPrank(); } diff --git a/test/utils/Bootstrap.sol b/test/utils/Bootstrap.sol index 368edbb..4bfd53f 100644 --- a/test/utils/Bootstrap.sol +++ b/test/utils/Bootstrap.sol @@ -13,16 +13,14 @@ import {Constants} from "./Constants.sol"; import {MockSpotMarketProxy} from "test/utils/mocks/MockSpotMarketProxy.sol"; import {MockUSDC} from "test/utils/mocks/MockUSDC.sol"; import {MockSUSD} from "test/utils/mocks/MockSUSD.sol"; +import {SynthetixV3Errors} from "./errors/SynthetixV3Errors.sol"; import {Test} from "lib/forge-std/src/Test.sol"; -contract Bootstrap is Test, ZapEvents, Constants { +contract Bootstrap is Test, ZapEvents, SynthetixV3Errors, Constants { using console2 for *; Zap internal zap; - /// @custom:todo make dynamic depending on fork - uint128 internal sUSDCMarketId = 1; - function initializeLocal() internal { BootstrapLocal bootstrap = new BootstrapLocal(); (address zapAddress) = bootstrap.init(); @@ -45,24 +43,29 @@ contract Bootstrap is Test, ZapEvents, Constants { } } -contract BootstrapLocal is Setup { +contract BootstrapLocal is Setup, Constants { function init() public returns (address zapAddress) { zapAddress = Setup.deploySystem( address(new MockUSDC()), address(new MockSUSD()), - address(new MockSpotMarketProxy()) + address(new MockSpotMarketProxy()), + LOCAL_SUSDC_SPOT_MARKET_ID ); } } contract BootstrapBase is Setup, BaseParameters { function init() public returns (address zapAddress) { - zapAddress = Setup.deploySystem(USDC, USD_PROXY, SPOT_MARKET_PROXY); + zapAddress = Setup.deploySystem( + USDC, USD_PROXY, SPOT_MARKET_PROXY, SUSDC_SPOT_MARKET_ID + ); } } contract BootstrapBaseGoerli is Setup, BaseGoerliParameters { function init() public returns (address zapAddress) { - zapAddress = Setup.deploySystem(USDC, USD_PROXY, SPOT_MARKET_PROXY); + zapAddress = Setup.deploySystem( + USDC, USD_PROXY, SPOT_MARKET_PROXY, SUSDC_SPOT_MARKET_ID + ); } } diff --git a/test/utils/Constants.sol b/test/utils/Constants.sol index df87fb1..f8d05be 100644 --- a/test/utils/Constants.sol +++ b/test/utils/Constants.sol @@ -4,11 +4,12 @@ pragma solidity 0.8.20; /// @title Contract for defining constants used in testing /// @author JaredBorders (jaredborders@pm.me) contract Constants { - uint256 public constant BASE_BLOCK_NUMBER = 8_163_000; + uint256 public constant BASE_BLOCK_NUMBER = 8_163_300; address public constant ACTOR = address(0xAC7AC7AC7); address public constant BAD_ACTOR = address(0xBACBACBAC); address public constant REFERRER = address(0xFEEEEEEEE); - uint256 public constant UINT_ONE_HUNDRED = 100 ether; - int256 public constant INT_ONE_HUNDRED = 100 ether; - int256 public constant INT_NEGATIVE_ONE_HUNDRED = -100 ether; + uint256 public constant UINT_ONE_HUNDRED = 100; + int256 public constant INT_ONE_HUNDRED = 100; + int256 public constant INT_NEGATIVE_ONE_HUNDRED = -100; + uint128 public constant LOCAL_SUSDC_SPOT_MARKET_ID = 1; } diff --git a/test/utils/errors/SynthetixV3Errors.sol b/test/utils/errors/SynthetixV3Errors.sol new file mode 100644 index 0000000..3410803 --- /dev/null +++ b/test/utils/errors/SynthetixV3Errors.sol @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity 0.8.20; + +/// @title Cosolidated Errors from Synthetix v3 contracts +/// @notice This contract consolidates all necessary errors from Synthetix v3 contracts +/// and is used for testing purposes +/// @author JaredBorders (jaredborders@pm.me) +contract SynthetixV3Errors { + error WrapperExceedsMaxAmount( + uint256 maxWrappableAmount, uint256 currentSupply, uint256 amountToWrap + ); +} diff --git a/test/utils/exposed/ZapExposed.sol b/test/utils/exposed/ZapExposed.sol index 6b54296..f6976f3 100644 --- a/test/utils/exposed/ZapExposed.sol +++ b/test/utils/exposed/ZapExposed.sol @@ -2,15 +2,13 @@ pragma solidity 0.8.20; import {Zap} from "src/Zap.sol"; +import {ZapExposedEvents} from "./ZapExposedEvents.sol"; /// @title Exposed Zap contract used for testing and in the deploy script /// @dev although it is used in the deploy script, it is /// *not* safe and *not* meant for mainnet /// @author JaredBorders (jaredborders@pm.me) -contract ZapExposed is Zap { - event PreZap(); - event PostZap(); - +contract ZapExposed is Zap, ZapExposedEvents { function expose_HASHED_USDC_NAME() public pure returns (bytes32) { return HASHED_USDC_NAME; } @@ -23,13 +21,24 @@ contract ZapExposed is Zap { return address(SUSD); } + function expose_SUSDC() public view returns (address) { + return address(SUSDC); + } + + function expose_SUSDC_SPOT_MARKET_ID() public view returns (uint128) { + return SUSDC_SPOT_MARKET_ID; + } + function expose_SPOT_MARKET_PROXY() public view returns (address) { return address(SPOT_MARKET_PROXY); } - constructor(address _usdc, address _susd, address _spotMarketProxy) - Zap(_usdc, _susd, _spotMarketProxy) - {} + constructor( + address _usdc, + address _susd, + address _spotMarketProxy, + uint128 _sUSDCId + ) Zap(_usdc, _susd, _spotMarketProxy, _sUSDCId) {} function _preZap() internal override { emit PreZap(); @@ -38,10 +47,4 @@ contract ZapExposed is Zap { function _postZap() internal override { emit PostZap(); } - - function expose_verifySynthMarketId(uint128 _synthMarketId, bytes32 _hash) - public view - { - super._verifySynthMarketId(_synthMarketId, _hash); - } } diff --git a/test/utils/exposed/ZapExposedEvents.sol b/test/utils/exposed/ZapExposedEvents.sol new file mode 100644 index 0000000..c9f2863 --- /dev/null +++ b/test/utils/exposed/ZapExposedEvents.sol @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: GPL-3.0-or-later +pragma solidity 0.8.20; + +/// @title Exposed Zap contract events +/// @author JaredBorders (jaredborders@pm.me) +contract ZapExposedEvents { + event PreZap(); + event PostZap(); +}