Skip to content

Commit

Permalink
πŸ‘·πŸ»β€β™‚οΈ Update constants and remove unused contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
JaredBorders committed Dec 14, 2023
1 parent c5f53b9 commit 8c8ce77
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 754 deletions.
51 changes: 21 additions & 30 deletions src/Engine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ import {IERC20} from "src/interfaces/tokens/IERC20.sol";
import {ISpotMarketProxy} from "src/interfaces/synthetix/ISpotMarketProxy.sol";
import {MathLib} from "src/libraries/MathLib.sol";
import {SignatureCheckerLib} from "src/libraries/SignatureCheckerLib.sol";
import {
TrustedForwarder, ERC2771Context
} from "src/utils/TrustedForwarder.sol";

/// @title Kwenta Smart Margin v3: Engine contract
/// @notice Responsible for interacting with Synthetix v3 perps markets
/// @author JaredBorders ([email protected])
contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
contract Engine is IEngine, EIP712, EIP7412 {
using MathLib for int128;
using MathLib for int256;
using MathLib for uint256;
Expand Down Expand Up @@ -61,10 +58,10 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
mapping(uint128 accountId => mapping(uint256 index => uint256 bitmap))
public nonceBitmap;

/// @notice mapping of account id to sUSD balance
/// @notice mapping of account id to sUSD balance (i.e. credit available to pay for fee(s))
/// @dev sUSD can be deposited/withdrawn from the
/// Engine contract to pay for fee(s) (conditional order execution, etc.)
mapping(uint128 accountId => uint256 ethBalance) public susdBalance;
mapping(uint128 accountId => uint256) public credit;

/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
Expand All @@ -79,7 +76,7 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
address _perpsMarketProxy,
address _spotMarketProxy,
address _sUSDProxy
) ERC2771Context(address(new TrustedForwarder())) {
) {
if (_perpsMarketProxy == address(0)) revert ZeroAddress();
if (_spotMarketProxy == address(0)) revert ZeroAddress();
if (_sUSDProxy == address(0)) revert ZeroAddress();
Expand Down Expand Up @@ -137,7 +134,7 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
uint256 _wordPos,
uint256 _mask
) external override {
if (_isAccountOwnerOrDelegate(_accountId, _msgSender())) {
if (_isAccountOwnerOrDelegate(_accountId, msg.sender)) {
/// @dev using bitwise OR to set the bit at the bit position
/// bitmap = .......10001
/// mask = .......00110
Expand Down Expand Up @@ -250,16 +247,14 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
) external override {
IERC20 synth = IERC20(_getSynthAddress(_synthMarketId));

address caller = _msgSender();

if (_amount > 0) {
_depositCollateral(
caller, synth, _accountId, _synthMarketId, _amount
msg.sender, synth, _accountId, _synthMarketId, _amount
);
} else {
if (!isAccountOwner(_accountId, caller)) revert Unauthorized();
if (!isAccountOwner(_accountId, msg.sender)) revert Unauthorized();
_withdrawCollateral(
caller, synth, _accountId, _synthMarketId, _amount
msg.sender, synth, _accountId, _synthMarketId, _amount
);
}
}
Expand Down Expand Up @@ -324,7 +319,7 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
returns (IPerpsMarketProxy.Data memory retOrder, uint256 fees)
{
/// @dev the account owner or the delegate may commit async orders
if (_isAccountOwnerOrDelegate(_accountId, _msgSender())) {
if (_isAccountOwnerOrDelegate(_accountId, msg.sender)) {
(retOrder, fees) = _commitOrder({
_perpsMarketId: _perpsMarketId,
_accountId: _accountId,
Expand Down Expand Up @@ -373,22 +368,19 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
revert AccountDoesNotExist();

Check warning on line 368 in src/Engine.sol

View check run for this annotation

Codecov / codecov/patch

src/Engine.sol#L368

Added line #L368 was not covered by tests
}

if (!SUSD.transferFrom(msg.sender, address(this), _amount)) {
revert DepositFailed();
}
/// @dev sUSD transfers that fail will revert
SUSD.transferFrom(msg.sender, address(this), _amount);

susdBalance[_accountId] += _amount;
credit[_accountId] += _amount;

emit Deposit(_accountId, _amount);
}

/// @inheritdoc IEngine
function withdraw(uint128 _accountId, uint256 _amount) external override {
address caller = _msgSender();
if (!isAccountOwner(_accountId, msg.sender)) revert Unauthorized();

if (!isAccountOwner(_accountId, caller)) revert Unauthorized();

_withdraw(caller, _accountId, _amount);
_withdraw(msg.sender, _accountId, _amount);

Check warning on line 383 in src/Engine.sol

View check run for this annotation

Codecov / codecov/patch

src/Engine.sol#L383

Added line #L383 was not covered by tests

emit Withdraw(_accountId, _amount);

Check warning on line 385 in src/Engine.sol

View check run for this annotation

Codecov / codecov/patch

src/Engine.sol#L385

Added line #L385 was not covered by tests
}
Expand All @@ -400,14 +392,13 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
function _withdraw(address _caller, uint128 _accountId, uint256 _amount)
internal
{
if (_amount > susdBalance[_accountId]) revert InsufficientBalance();
if (_amount > credit[_accountId]) revert InsufficientBalance();

// decrement the sUSD balance of the account prior to transferring sUSD to the caller
susdBalance[_accountId] -= _amount;
credit[_accountId] -= _amount;

if (!SUSD.transfer(_caller, _amount)) {
revert WithdrawalFailed();
}
/// @dev sUSD transfers that fail will revert
SUSD.transfer(_caller, _amount);
}

/*//////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -439,7 +430,7 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
/// @dev the fee is denoted in sUSD and is paid to the caller (conditional order executor)
/// @dev the fee does not exceed the max fee set by the conditional order and
/// this is enforced by the `canExecute` function
_withdraw(_msgSender(), _co.orderDetails.accountId, _fee);
if (_fee > 0) _withdraw(msg.sender, _co.orderDetails.accountId, _fee);

/// @notice get size delta from order details
int128 sizeDelta = _co.orderDetails.sizeDelta;
Expand Down Expand Up @@ -506,7 +497,7 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
if (_fee > _co.maxExecutorFee) return false;

// verify account has enough credit to pay the fee
if (_fee > susdBalance[_co.orderDetails.accountId]) return false;
if (_fee > credit[_co.orderDetails.accountId]) return false;

// verify nonce has not been executed before
if (hasUnorderedNonceBeenUsed(_co.orderDetails.accountId, _co.nonce)) {
Expand All @@ -527,7 +518,7 @@ contract Engine is IEngine, EIP712, EIP7412, ERC2771Context {
} else {
// if the order does not require verification, then the caller
// must be the trusted executor defined by "trustedExecutor"
if (_msgSender() != _co.trustedExecutor) return false;
if (msg.sender != _co.trustedExecutor) return false;
}

return true;
Expand Down
120 changes: 0 additions & 120 deletions src/utils/TrustedForwarder.sol

This file was deleted.

39 changes: 30 additions & 9 deletions test/ConditionalOrder.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ contract CanExecute is ConditionalOrderTest {
_defineConditionalOrder();

// ensure the account has no credit
assertEq(engine.ethBalances(accountId), 0);
assertEq(engine.credit(accountId), 0);

// CO_FEE is non-zero, and the account has no credit
bool canExec = engine.canExecute(co, signature, CO_FEE);
Expand Down Expand Up @@ -178,7 +178,7 @@ contract CanExecute is ConditionalOrderTest {
bytes[] memory conditions = new bytes[](1);
conditions[0] = isTimestampAfter(block.timestamp + 100); // condition not met

IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
accountId: accountId,
sizeDelta: SIZE_DELTA,
Expand All @@ -189,7 +189,7 @@ contract CanExecute is ConditionalOrderTest {
referrer: REFERRER
});

IEngine.ConditionalOrder memory co = IEngine.ConditionalOrder({
co = IEngine.ConditionalOrder({
orderDetails: orderDetails,
signer: signer,
nonce: 0,
Expand All @@ -199,7 +199,7 @@ contract CanExecute is ConditionalOrderTest {
conditions: conditions
});

bytes memory signature = getConditionalOrderSignature({
signature = getConditionalOrderSignature({
co: co,
privateKey: signerPrivateKey,
domainSeparator: engine.DOMAIN_SEPARATOR()
Expand Down Expand Up @@ -647,7 +647,14 @@ contract Execute is ConditionalOrderTest {

contract Fee is ConditionalOrderTest {
function test_fee_imposed() public {
engine.depositEth{value: 1 ether}(accountId);
// prank ACTOR because this address has sUSD
vm.startPrank(ACTOR);

sUSD.approve(address(engine), type(uint256).max);

engine.deposit(accountId, CO_FEE);

vm.stopPrank();

IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
Expand Down Expand Up @@ -676,17 +683,24 @@ contract Fee is ConditionalOrderTest {
domainSeparator: engine.DOMAIN_SEPARATOR()
});

uint256 preExecutorBalance = address(this).balance;
uint256 preExecutorBalance = sUSD.balanceOf(address(this));

engine.execute(co, signature, CO_FEE);

uint256 postExecutorBalance = address(this).balance;
uint256 postExecutorBalance = sUSD.balanceOf(address(this));

assertEq(preExecutorBalance + CO_FEE, postExecutorBalance);
}

function test_fee_exceeds_account_credit() public {
engine.depositEth{value: CO_FEE - 1}(accountId);
// prank ACTOR because this address has sUSD
vm.startPrank(ACTOR);

sUSD.approve(address(engine), type(uint256).max);

engine.deposit(accountId, CO_FEE - 1);

vm.stopPrank();

IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
Expand Down Expand Up @@ -721,7 +735,14 @@ contract Fee is ConditionalOrderTest {
}

function test_fee_exceeds_maxExecutorFee() public {
engine.depositEth{value: 1 ether}(accountId);
// prank ACTOR because this address has sUSD
vm.startPrank(ACTOR);

sUSD.approve(address(engine), type(uint256).max);

engine.deposit(accountId, CO_FEE);

vm.stopPrank();

IEngine.OrderDetails memory orderDetails = IEngine.OrderDetails({
marketId: SETH_PERPS_MARKET_ID,
Expand Down
Loading

0 comments on commit 8c8ce77

Please sign in to comment.