Skip to content

Commit

Permalink
✅ Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredBorders committed Dec 22, 2023
1 parent 6767c04 commit 0ed7218
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 85 deletions.
106 changes: 46 additions & 60 deletions test/Zap.t.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
// 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";

contract ZapTest is Bootstrap {
IERC20 internal SUSD;
IERC20 internal USDC;

Zap.ZapDetails internal details;

function setUp() public {
vm.rollFork(BASE_BLOCK_NUMBER);
initializeBase();
Expand All @@ -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
});
}

Expand All @@ -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
});
}

Expand All @@ -48,106 +52,88 @@ 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);

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();
}
Expand Down
19 changes: 11 additions & 8 deletions test/utils/Bootstrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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
);
}
}
9 changes: 5 additions & 4 deletions test/utils/Constants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
12 changes: 12 additions & 0 deletions test/utils/errors/SynthetixV3Errors.sol
Original file line number Diff line number Diff line change
@@ -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
);
}
29 changes: 16 additions & 13 deletions test/utils/exposed/ZapExposed.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand All @@ -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);
}
}
9 changes: 9 additions & 0 deletions test/utils/exposed/ZapExposedEvents.sol
Original file line number Diff line number Diff line change
@@ -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();
}

0 comments on commit 0ed7218

Please sign in to comment.