Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: assorted optimizations #69

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions contracts/PrimitiveManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ contract PrimitiveManager is IPrimitiveManager, Multicall, CashManager, SelfPerm
)
{
address engine = EngineAddress.computeAddress(factory, risky, stable);
if (EngineAddress.isContract(engine) == false) revert EngineAddress.EngineNotDeployedError();
if (engine.code.length == 0) revert EngineAddress.EngineNotDeployedError();

if (delLiquidity == 0) revert ZeroLiquidityError();

Expand Down Expand Up @@ -86,7 +86,7 @@ contract PrimitiveManager is IPrimitiveManager, Multicall, CashManager, SelfPerm
uint256 minLiquidityOut
) external payable override lock returns (uint256 delLiquidity) {
_engine = EngineAddress.computeAddress(factory, risky, stable);
if (EngineAddress.isContract(_engine) == false) revert EngineAddress.EngineNotDeployedError();
if (_engine.code.length == 0) revert EngineAddress.EngineNotDeployedError();

if (delRisky == 0 && delStable == 0) revert ZeroLiquidityError();

Expand Down Expand Up @@ -145,8 +145,8 @@ contract PrimitiveManager is IPrimitiveManager, Multicall, CashManager, SelfPerm
address engine = EngineAddress.computeAddress(factory, decoded.risky, decoded.stable);
if (msg.sender != engine) revert NotEngineError();

if (delRisky > 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable > 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
if (delRisky != 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable != 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
}

/// @inheritdoc IPrimitiveLiquidityCallback
Expand All @@ -160,7 +160,7 @@ contract PrimitiveManager is IPrimitiveManager, Multicall, CashManager, SelfPerm
address engine = EngineAddress.computeAddress(factory, decoded.risky, decoded.stable);
if (msg.sender != engine) revert NotEngineError();

if (delRisky > 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable > 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
if (delRisky != 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable != 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
}
}
6 changes: 3 additions & 3 deletions contracts/base/CashManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract contract CashManager is ICashManager, ManagerBase {

if (balance < amountMin) revert BalanceTooLowError(balance, amountMin);

if (balance > 0) {
if (balance != 0) {
IWETH9(WETH9).withdraw(balance);
TransferHelper.safeTransferETH(recipient, balance);
}
Expand All @@ -46,14 +46,14 @@ abstract contract CashManager is ICashManager, ManagerBase {
uint256 balance = IERC20(token).balanceOf(address(this));
if (balance < amountMin) revert BalanceTooLowError(balance, amountMin);

if (balance > 0) {
if (balance != 0) {
TransferHelper.safeTransfer(token, recipient, balance);
}
}

/// @inheritdoc ICashManager
function refundETH() external payable override {
if (address(this).balance > 0) TransferHelper.safeTransferETH(msg.sender, address(this).balance);
if (address(this).balance != 0) TransferHelper.safeTransferETH(msg.sender, address(this).balance);
}

/// @dev Pays {value} of {token] to {recipient} from {payer} wallet
Expand Down
4 changes: 2 additions & 2 deletions contracts/base/ERC1155.sol
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256 amount,
bytes memory data
) private {
if (to.isContract()) {
if (to.code.length != 0) {
try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
if (response != IERC1155Receiver.onERC1155Received.selector) {
revert("ERC1155: ERC1155Receiver rejected tokens");
Expand All @@ -436,7 +436,7 @@ contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
uint256[] memory amounts,
bytes memory data
) private {
if (to.isContract()) {
if (to.code.length != 0) {
try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
bytes4 response
) {
Expand Down
15 changes: 8 additions & 7 deletions contracts/base/ERC1155Permit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,18 @@ contract ERC1155Permit is ERC1155, IERC1155Permit, EIP712 {
) external override {
if (block.timestamp > deadline) revert SigExpiredError();

bytes32 structHash = keccak256(
abi.encode(_PERMIT_TYPEHASH, owner, operator, approved, nonces[owner], deadline)
);
unchecked {
bytes32 structHash = keccak256(
abi.encode(_PERMIT_TYPEHASH, owner, operator, approved, nonces[owner]++, deadline)
);

bytes32 hash = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(hash, v, r, s);
bytes32 hash = _hashTypedDataV4(structHash);
address signer = ECDSA.recover(hash, v, r, s);

if (signer != owner) revert InvalidSigError();
if (signer != owner) revert InvalidSigError();
}

_setApprovalForAll(owner, operator, approved);
nonces[owner] += 1;
}

/// @inheritdoc IERC1155Permit
Expand Down
6 changes: 3 additions & 3 deletions contracts/base/MarginManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ abstract contract MarginManager is IMarginManager, CashManager {
if (delRisky == 0 && delStable == 0) revert ZeroDelError();

address engine = EngineAddress.computeAddress(factory, risky, stable);
if (EngineAddress.isContract(engine) == false) revert EngineAddress.EngineNotDeployedError();
if (engine.code.length == 0) revert EngineAddress.EngineNotDeployedError();

IPrimitiveEngineActions(engine).deposit(
address(this),
Expand Down Expand Up @@ -89,7 +89,7 @@ abstract contract MarginManager is IMarginManager, CashManager {
address engine = EngineAddress.computeAddress(factory, decoded.risky, decoded.stable);
if (msg.sender != engine) revert NotEngineError();

if (delStable > 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
if (delRisky > 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable != 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
if (delRisky != 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
}
}
11 changes: 6 additions & 5 deletions contracts/base/Reentrancy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ contract Reentrancy {
/// @notice Thrown when a call to the contract is made during a locked state
error LockedError();

uint256 private _unlocked = 1;
/// @dev Reentrancy guard initialized to state
uint256 private locked = 1;

/// @notice Locks the contract to prevent reentrancy
modifier lock() {
if (_unlocked != 1) revert LockedError();
if (locked != 1) revert LockedError();

_unlocked = 0;
locked = 2;
_;
_unlocked = 1;
}
locked = 1;

}
6 changes: 3 additions & 3 deletions contracts/base/SwapManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ abstract contract SwapManager is ISwapManager, CashManager, MarginManager {
});

address engine = EngineAddress.computeAddress(factory, params.risky, params.stable);
if (EngineAddress.isContract(engine) == false) revert EngineAddress.EngineNotDeployedError();
if (engine.code.length == 0) revert EngineAddress.EngineNotDeployedError();

IPrimitiveEngineActions(engine).swap(
params.toMargin ? address(this) : params.recipient,
Expand Down Expand Up @@ -87,7 +87,7 @@ abstract contract SwapManager is ISwapManager, CashManager, MarginManager {
address engine = EngineAddress.computeAddress(factory, decoded.risky, decoded.stable);
if (msg.sender != engine) revert NotEngineError();

if (delRisky > 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable > 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
if (delRisky != 0) pay(decoded.risky, decoded.payer, msg.sender, delRisky);
if (delStable != 0) pay(decoded.stable, decoded.payer, msg.sender, delStable);
}
}
16 changes: 0 additions & 16 deletions contracts/libraries/EngineAddress.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,4 @@ library EngineAddress {
)
);
}

/// @notice Checks if the target address is a contract, this function is used
/// to verify if a PrimitiveEngine was deployed before calling it
/// @param target Address of the contract to check
/// @return True if the target is a contract
function isContract(address target) internal view returns (bool) {
// This method relies on extcodesize, which returns 0 for contracts in
// construction, since the code is only stored at the end of the
// constructor execution.

uint256 size;
assembly {
size := extcodesize(target)
}
return size > 0;
}
}
2 changes: 1 addition & 1 deletion contracts/libraries/HexStrings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ library HexStrings {

function toHexStringNoPrefix(uint256 value, uint256 length) internal pure returns (string memory) {
bytes memory buffer = new bytes(2 * length);
for (uint256 i = buffer.length; i > 0; i--) {
for (uint256 i = buffer.length; i != 0; i--) {
buffer[i - 1] = ALPHABET[value & 0xf];
value >>= 4;
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/libraries/Margin.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ library Margin {
uint256 delRisky,
uint256 delStable
) internal {
if (delRisky > 0) margin.balanceRisky += delRisky.toUint128();
if (delStable > 0) margin.balanceStable += delStable.toUint128();
if (delRisky != 0) margin.balanceRisky += delRisky.toUint128();
if (delStable != 0) margin.balanceStable += delStable.toUint128();
}

/// @notice Removes risky and stable token balance from an internal margin account
Expand All @@ -36,7 +36,7 @@ library Margin {
uint256 delRisky,
uint256 delStable
) internal {
if (delRisky > 0) margin.balanceRisky -= delRisky.toUint128();
if (delStable > 0) margin.balanceStable -= delStable.toUint128();
if (delRisky != 0) margin.balanceRisky -= delRisky.toUint128();
if (delStable != 0) margin.balanceStable -= delStable.toUint128();
}
}