Skip to content

Commit

Permalink
πŸ‘·πŸ»β€β™‚οΈ Temporarily pass $USDC as a param
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredBorders committed Nov 15, 2023
1 parent 2e20494 commit 148c0a0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/Engine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
uint128 internal constant USD_SYNTH_ID = 0;

/// @notice spot market name of $sUSDC in Synthetix v3
string internal constant SUSDC_SPOT_MARKET_NAME = "sUSDC Spot Market";
bytes32 internal constant SUSDC_SPOT_MARKET_NAME =
keccak256(abi.encodePacked("sUSDC Spot Market"));

/// @notice max number of conditions that can be defined for a conditional order
uint256 internal constant MAX_CONDITIONS = 8;
Expand Down Expand Up @@ -78,9 +79,6 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
/// @notice Synthetix v3 $sUSD contract
IERC20 internal immutable SUSD;

/// @notice $USDC token contract
IERC20 internal immutable USDC;

/*//////////////////////////////////////////////////////////////
STATE
//////////////////////////////////////////////////////////////*/
Expand All @@ -105,27 +103,23 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
/// @param _sUSDProxy Synthetix v3 $sUSD contract
/// @param _oracle pyth oracle contract used to get asset prices
/// @param _trustedForwarder trusted forwarder contract used for meta transactions
/// @param _usdc address of the $USDC contract
constructor(
address _perpsMarketProxy,
address _spotMarketProxy,
address _sUSDProxy,
address _oracle,
address _trustedForwarder,
address _usdc
address _trustedForwarder
) ERC2771Context(_trustedForwarder) {
if (_perpsMarketProxy == address(0)) revert ZeroAddress();
if (_spotMarketProxy == address(0)) revert ZeroAddress();
if (_sUSDProxy == address(0)) revert ZeroAddress();
if (_oracle == address(0)) revert ZeroAddress();
if (_trustedForwarder == address(0)) revert ZeroAddress();
if (_usdc == address(0)) revert ZeroAddress();

PERPS_MARKET_PROXY = IPerpsMarketProxy(_perpsMarketProxy);
SPOT_MARKET_PROXY = ISpotMarketProxy(_spotMarketProxy);
SUSD = IERC20(_sUSDProxy);
ORACLE = IPyth(_oracle);
USDC = IERC20(_usdc);
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -326,17 +320,24 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
/// @inheritdoc IEngine
function zap(
uint128 _accountId,
address _usdc,
uint128 _synthMarketId,
int256 _amount,
address _referrer
) external override {
address caller = _msgSender();

/// @dev ensure specified synth market id is for $sUSDC
if (SPOT_MARKET_PROXY.name(_synthMarketId) != SUSDC_SPOT_MARKET_NAME) {
if (
keccak256(abi.encodePacked(SPOT_MARKET_PROXY.name(_synthMarketId)))
!= SUSDC_SPOT_MARKET_NAME
) {
revert ZapFailed();
}

// define $USDC contract
IERC20 USDC = IERC20(_usdc);

// define $sUSDC synth contract
IERC20 sUSDC = IERC20(_getSynthAddress(_synthMarketId));

Expand All @@ -345,7 +346,9 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {

if (_amount > 0) {
/// @dev given the amount is positive, simply casting (int -> uint) is safe
USDC.transferFrom(caller, address(this), uint256(_amount));
if (!USDC.transferFrom(caller, address(this), uint256(_amount))) {
revert ZapFailed();
}

// approve the spot market proxy to spend the $USDC
USDC.approve(address(SPOT_MARKET_PROXY), uint256(_amount));
Expand Down Expand Up @@ -391,8 +394,8 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
// sell the $sUSD for $sUSDC
SPOT_MARKET_PROXY.buy({
marketId: _synthMarketId,
synthAmount: _amount.abs256(),
minUsdAmount: _amount.abs256(),
usdAmount: _amount.abs256(),
minAmountReceived: _amount.abs256(),
referrer: _referrer
});

Expand All @@ -408,7 +411,9 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
});

// transfer the $USDC to the caller
USDC.transfer(caller, _amount.abs256());
if (!USDC.transfer(caller, _amount.abs256())) {
revert ZapFailed();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/interfaces/IEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -180,11 +180,13 @@ interface IEngine {
/// @notice "zap" $USDC for $sUSD and deposit the $sUSD into the perps market proxy
/// or withdraw the $sUSD from the perps market proxy and "zap" $sUSD for $USDC
/// @param _accountId the account id to modify collateral for
/// @param _usdc the address of the $USDC contract
/// @param _synthMarketId the id of the synth market (i.e. $sUSDC)
/// @param _amount the amount of $USDC to deposit/withdraw
/// @param _referrer the address of the referrer
function zap(
uint128 _accountId,
address _usdc,
uint128 _synthMarketId,
int256 _amount,
address _referrer
Expand Down

0 comments on commit 148c0a0

Please sign in to comment.