Skip to content

Commit

Permalink
🦄 v2.0.3 -> v2.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredBorders authored Jul 17, 2023
2 parents 86efc1f + b743857 commit 4a7dae5
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 40 deletions.
13 changes: 6 additions & 7 deletions src/Account.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1013,7 +1013,7 @@ contract Account is IAccount, Auth, OpsReady {
/// @dev verify direction and validity of swap (i.e. sUSD <-> whitelisted token)
if (
tokenIn == address(MARGIN_ASSET)
&& SETTINGS.whitelistedTokens(tokenOut)
&& SETTINGS.isWhitelistedTokens(tokenOut)
) {
// if swapping sUSD for another token, ensure sufficient margin
/// @dev margin is being transferred out of this contract
Expand All @@ -1022,7 +1022,7 @@ contract Account is IAccount, Auth, OpsReady {
recipient = msg.sender;
} else if (
tokenOut == address(MARGIN_ASSET)
&& SETTINGS.whitelistedTokens(tokenIn)
&& SETTINGS.isWhitelistedTokens(tokenIn)
) {
// if swapping another token for sUSD, token must be transferred to this contract
/// @dev msg.sender must have approved Permit2 to spend at least the amountIn
Expand All @@ -1047,9 +1047,8 @@ contract Account is IAccount, Auth, OpsReady {
token: tokenIn,
spender: address(UNISWAP_UNIVERSAL_ROUTER),
amount: _amountIn.toUint160(),
// Use an unlimited expiration because it most
// closely mimics how a standard approval works
expiration: type(uint48).max
/// @dev timstamp will never overflow (i.e. maximum value of uint48 is year 8 million 921 thousand 556)
expiration: uint48(block.timestamp)
});

_universalRouterExecute(recipient, _amountIn, _amountOutMin, _path);
Expand Down Expand Up @@ -1104,8 +1103,8 @@ contract Account is IAccount, Auth, OpsReady {

UNISWAP_UNIVERSAL_ROUTER.execute({
commands: abi.encodePacked(bytes1(uint8(V3_SWAP_EXACT_IN))),
inputs: inputs,
deadline: block.timestamp
inputs: inputs
/// @custom:auditor removed deadline here and want increased scrutiny
});
}

Expand Down
4 changes: 2 additions & 2 deletions src/Settings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ contract Settings is ISettings, Owned {
//////////////////////////////////////////////////////////////*/

/// @inheritdoc ISettings
function whitelistedTokens(address _token)
function isWhitelistedTokens(address _token)
external
view
override
Expand Down Expand Up @@ -76,6 +76,6 @@ contract Settings is ISettings, Owned {
{
_whitelistedTokens[_token] = _isWhitelisted;

emit TokenWhitelistStatusUpdated(_token);
emit TokenWhitelistStatusUpdated(_token, _isWhitelisted);
}
}
21 changes: 11 additions & 10 deletions src/interfaces/IAccount.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,17 @@ interface IAccount {
CHAINLINK
}

/// @param _factory: address of the Smart Margin Account Factory
/// @param _events: address of the contract used by all accounts for emitting events
/// @param _marginAsset: address of the Synthetix ProxyERC20sUSD contract used as the margin asset
/// @param _perpsV2ExchangeRate: address of the Synthetix PerpsV2ExchangeRate
/// @param _futuresMarketManager: address of the Synthetix FuturesMarketManager
/// @param _gelato: address of Gelato
/// @param _ops: address of Ops
/// @param _settings: address of contract used to store global settings
/// @param _universalRouter: address of Uniswap's Universal Router
/// @param _permit2: address of Uniswap's Permit2
/// @param factory: address of the Smart Margin Account Factory
/// @param events: address of the contract used by all accounts for emitting events
/// @param marginAsset: address of the Synthetix ProxyERC20sUSD contract used as the margin asset
/// @param perpsV2ExchangeRate: address of the Synthetix PerpsV2ExchangeRate
/// @param futuresMarketManager: address of the Synthetix FuturesMarketManager
/// @param systemStatus: address of the Synthetix SystemStatus
/// @param gelato: address of Gelato
/// @param ops: address of Ops
/// @param settings: address of contract used to store global settings
/// @param universalRouter: address of Uniswap's Universal Router
/// @param permit2: address of Uniswap's Permit2
struct AccountConstructorParams {
address factory;
address events;
Expand Down
5 changes: 3 additions & 2 deletions src/interfaces/ISettings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ interface ISettings {

/// @notice emitted when a token is added to or removed from the whitelist
/// @param token: address of the token
event TokenWhitelistStatusUpdated(address token);
/// @param isWhitelisted: true if token is whitelisted, false if not
event TokenWhitelistStatusUpdated(address token, bool isWhitelisted);

/*//////////////////////////////////////////////////////////////
VIEWS
Expand All @@ -35,7 +36,7 @@ interface ISettings {
/// @notice checks if token is whitelisted
/// @param _token: address of the token to check
/// @return true if token is whitelisted, false if not
function whitelistedTokens(address _token) external view returns (bool);
function isWhitelistedTokens(address _token) external view returns (bool);

/*//////////////////////////////////////////////////////////////
SETTERS
Expand Down
7 changes: 7 additions & 0 deletions src/interfaces/uniswap/IUniversalRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ interface IUniversalRouter {
bytes[] calldata inputs,
uint256 deadline
) external payable;

/// @notice Executes encoded commands along with provided inputs
/// @param commands A set of concatenated commands, each 1 byte in length
/// @param inputs An array of byte strings containing abi encoded inputs for each command
function execute(bytes calldata commands, bytes[] calldata inputs)
external
payable;
}
36 changes: 24 additions & 12 deletions test/integration/swap.behavior.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,14 @@ contract SwapBehaviorTest is Test, ConsolidatedEvents {
);
inputs[0] = abi.encode(amountIn, amountOutMin, path);

uint256 preBalance = sUSD.balanceOf(address(account));
uint256 preSUSDBalance = sUSD.balanceOf(address(account));
uint256 preDAIBalance = dai.balanceOf(address(this));
account.execute(commands, inputs);
uint256 postBalance = sUSD.balanceOf(address(account));
uint256 postSUSDBalance = sUSD.balanceOf(address(account));
uint256 postDAIBalance = dai.balanceOf(address(this));

assertGt(postBalance, preBalance);
assertGt(postSUSDBalance, preSUSDBalance);
assertLt(postDAIBalance, preDAIBalance);
}

function test_UniswapV3Swap_SUSD_DAI() public {
Expand All @@ -150,11 +153,14 @@ contract SwapBehaviorTest is Test, ConsolidatedEvents {
);
inputs[0] = abi.encode(amountIn, amountOutMin, path);

uint256 preBalance = dai.balanceOf(address(this));
uint256 preSUSDBalance = sUSD.balanceOf(address(account));
uint256 preDAIBalance = dai.balanceOf(address(this));
account.execute(commands, inputs);
uint256 postBalance = dai.balanceOf(address(this));
uint256 postSUSDBalance = sUSD.balanceOf(address(account));
uint256 postDAIBalance = dai.balanceOf(address(this));

assertGt(postBalance, preBalance);
assertGt(postDAIBalance, preDAIBalance);
assertLt(postSUSDBalance, preSUSDBalance);
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -185,11 +191,14 @@ contract SwapBehaviorTest is Test, ConsolidatedEvents {
);
inputs[0] = abi.encode(amountIn, amountOutMin, path);

uint256 preBalance = usdc.balanceOf(address(this));
uint256 preSUSDBalance = sUSD.balanceOf(address(account));
uint256 preUSDCBalance = usdc.balanceOf(address(this));
account.execute(commands, inputs);
uint256 postBalance = usdc.balanceOf(address(this));
uint256 postSUSDBalance = sUSD.balanceOf(address(account));
uint256 postUSDCBalance = usdc.balanceOf(address(this));

assertGt(postBalance, preBalance);
assertGt(postUSDCBalance, preUSDCBalance);
assertLt(postSUSDBalance, preSUSDBalance);
}

function test_UniswapV3Swap_DAI_USDC_SUSD() public {
Expand All @@ -213,11 +222,14 @@ contract SwapBehaviorTest is Test, ConsolidatedEvents {
);
inputs[0] = abi.encode(amountIn, amountOutMin, path);

uint256 preBalance = sUSD.balanceOf(address(account));
uint256 preSUSDBalance = sUSD.balanceOf(address(account));
uint256 preDAIBalance = dai.balanceOf(address(this));
account.execute(commands, inputs);
uint256 postBalance = sUSD.balanceOf(address(account));
uint256 postSUSDBalance = sUSD.balanceOf(address(account));
uint256 postDAIBalance = dai.balanceOf(address(this));

assertGt(postBalance, preBalance);
assertGt(postSUSDBalance, preSUSDBalance);
assertLt(postDAIBalance, preDAIBalance);
}

/*//////////////////////////////////////////////////////////////
Expand Down
17 changes: 10 additions & 7 deletions test/unit/Settings.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ contract SettingsTest is Test, ConsolidatedEvents {
//////////////////////////////////////////////////////////////*/

function test_whitelistedTokens() public {
assertEq(settings.whitelistedTokens(MARGIN_ASSET), false);
assertEq(settings.isWhitelistedTokens(MARGIN_ASSET), false);
settings.setTokenWhitelistStatus(MARGIN_ASSET, true);
assertEq(settings.whitelistedTokens(MARGIN_ASSET), true);
assertEq(settings.isWhitelistedTokens(MARGIN_ASSET), true);
settings.setTokenWhitelistStatus(MARGIN_ASSET, false);
assertEq(settings.whitelistedTokens(MARGIN_ASSET), false);
assertEq(settings.isWhitelistedTokens(MARGIN_ASSET), false);
}

function test_setTokenWhitelistStatus_OnlyOwner() public {
Expand All @@ -96,16 +96,19 @@ contract SettingsTest is Test, ConsolidatedEvents {
}

function test_setTokenWhitelistStatus(address token) public {
assertEq(settings.whitelistedTokens(token), false);
assertEq(settings.isWhitelistedTokens(token), false);
settings.setTokenWhitelistStatus(token, true);
assertEq(settings.whitelistedTokens(token), true);
assertEq(settings.isWhitelistedTokens(token), true);
settings.setTokenWhitelistStatus(token, false);
assertEq(settings.whitelistedTokens(token), false);
assertEq(settings.isWhitelistedTokens(token), false);
}

function test_setTokenWhitelistStatus_Event() public {
vm.expectEmit(true, true, true, true);
emit TokenWhitelistStatusUpdated(MARGIN_ASSET);
emit TokenWhitelistStatusUpdated(MARGIN_ASSET, true);
settings.setTokenWhitelistStatus(MARGIN_ASSET, true);
vm.expectEmit(true, true, true, true);
emit TokenWhitelistStatusUpdated(MARGIN_ASSET, false);
settings.setTokenWhitelistStatus(MARGIN_ASSET, false);
}
}

0 comments on commit 4a7dae5

Please sign in to comment.