Skip to content
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
25 changes: 13 additions & 12 deletions contracts/evm/src/QuorumCustody.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {IDeposit} from "./interfaces/IDeposit.sol";
contract QuorumCustody is IWithdraw, IDeposit, ReentrancyGuard, EIP712 {
using SafeERC20 for IERC20;

// Contract-specific errors
error InvalidSigner();
error NotSigner();
error AlreadySigner();
Expand Down Expand Up @@ -154,12 +155,12 @@ contract QuorumCustody is IWithdraw, IDeposit, ReentrancyGuard, EIP712 {
}

function deposit(address token, uint256 amount) external payable override nonReentrant {
require(amount != 0, ZeroAmount());
require(amount != 0, IDeposit.ZeroAmount());

if (token == address(0)) {
require(msg.value == amount, MsgValueMismatch());
require(msg.value == amount, IDeposit.InvalidMsgValue());
} else {
require(msg.value == 0, NonZeroMsgValueForERC20());
require(msg.value == 0, IDeposit.InvalidMsgValue());
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
}

Expand All @@ -174,10 +175,10 @@ contract QuorumCustody is IWithdraw, IDeposit, ReentrancyGuard, EIP712 {
returns (bytes32)
{
require(user != address(0), InvalidUser());
require(amount != 0, ZeroAmount());
require(amount != 0, IDeposit.ZeroAmount());

bytes32 withdrawalId = _getWithdrawalId(user, token, amount, nonce);
require(withdrawals[withdrawalId].createdAt == 0, WithdrawalAlreadyExists());
require(withdrawals[withdrawalId].createdAt == 0, IWithdraw.WithdrawalAlreadyExists());

withdrawals[withdrawalId] = WithdrawalRequest({
user: user,
Expand All @@ -196,8 +197,8 @@ contract QuorumCustody is IWithdraw, IDeposit, ReentrancyGuard, EIP712 {
WithdrawalRequest storage request = withdrawals[withdrawalId];
address signer = msg.sender;

require(request.createdAt != 0, WithdrawalNotFound());
require(!request.finalized, WithdrawalAlreadyFinalized());
require(request.createdAt != 0, IWithdraw.WithdrawalNotFound());
require(!request.finalized, IWithdraw.WithdrawalAlreadyFinalized());
require(block.timestamp <= request.createdAt + OPERATION_EXPIRY, WithdrawalExpired());
require(!withdrawalApprovals[withdrawalId][signer], SignerAlreadyApproved());

Expand All @@ -215,8 +216,8 @@ contract QuorumCustody is IWithdraw, IDeposit, ReentrancyGuard, EIP712 {
function rejectWithdraw(bytes32 withdrawalId) external override onlySigner nonReentrant {
WithdrawalRequest storage request = withdrawals[withdrawalId];

require(request.createdAt != 0, WithdrawalNotFound());
require(!request.finalized, WithdrawalAlreadyFinalized());
require(request.createdAt != 0, IWithdraw.WithdrawalNotFound());
require(!request.finalized, IWithdraw.WithdrawalAlreadyFinalized());
require(block.timestamp > request.createdAt + OPERATION_EXPIRY, WithdrawalNotExpired());

request.finalized = true;
Expand All @@ -241,11 +242,11 @@ contract QuorumCustody is IWithdraw, IDeposit, ReentrancyGuard, EIP712 {
request.finalized = true;

if (token == address(0)) {
require(address(this).balance >= amount, InsufficientLiquidity());
require(address(this).balance >= amount, IWithdraw.InsufficientLiquidity());
(bool success,) = user.call{value: amount}("");
require(success, ETHTransferFailed());
require(success, IWithdraw.ETHTransferFailed());
} else {
require(IERC20(token).balanceOf(address(this)) >= amount, InsufficientLiquidity());
require(IERC20(token).balanceOf(address(this)) >= amount, IWithdraw.InsufficientLiquidity());
IERC20(token).safeTransfer(user, amount);
}

Expand Down
24 changes: 12 additions & 12 deletions contracts/evm/src/SimpleCustody.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ contract SimpleCustody is IWithdraw, IDeposit, AccessControl, ReentrancyGuard {
}

function deposit(address token, uint256 amount) external payable override nonReentrant {
if (amount == 0) revert ZeroAmount();
if (amount == 0) revert IDeposit.ZeroAmount();
uint256 received = amount;
if (token == address(0)) {
if (msg.value != amount) revert MsgValueMismatch();
if (msg.value != amount) revert IDeposit.InvalidMsgValue();
} else {
if (msg.value != 0) revert NonZeroMsgValueForERC20();
if (msg.value != 0) revert IDeposit.InvalidMsgValue();
uint256 balanceBefore = IERC20(token).balanceOf(address(this));
IERC20(token).safeTransferFrom(msg.sender, address(this), amount);
received = IERC20(token).balanceOf(address(this)) - balanceBefore;
Expand All @@ -52,10 +52,10 @@ contract SimpleCustody is IWithdraw, IDeposit, AccessControl, ReentrancyGuard {
nonReentrant
returns (bytes32 withdrawalId)
{
if (amount == 0) revert ZeroAmount();
if (amount == 0) revert IDeposit.ZeroAmount();
withdrawalId = keccak256(abi.encode(block.chainid, address(this), user, token, amount, nonce));

if (withdrawals[withdrawalId].exists) revert WithdrawalAlreadyExists();
if (withdrawals[withdrawalId].exists) revert IWithdraw.WithdrawalAlreadyExists();

withdrawals[withdrawalId] =
WithdrawalRequest({user: user, token: token, amount: amount, exists: true, finalized: false});
Expand All @@ -65,8 +65,8 @@ contract SimpleCustody is IWithdraw, IDeposit, AccessControl, ReentrancyGuard {

function finalizeWithdraw(bytes32 withdrawalId) external override onlyRole(NITEWATCH_ROLE) nonReentrant {
WithdrawalRequest storage request = withdrawals[withdrawalId];
if (!request.exists) revert WithdrawalNotFound();
if (request.finalized) revert WithdrawalAlreadyFinalized();
if (!request.exists) revert IWithdraw.WithdrawalNotFound();
if (request.finalized) revert IWithdraw.WithdrawalAlreadyFinalized();

request.finalized = true;
address user = request.user;
Expand All @@ -79,11 +79,11 @@ contract SimpleCustody is IWithdraw, IDeposit, AccessControl, ReentrancyGuard {
request.amount = 0;

if (token == address(0)) {
if (address(this).balance < amount) revert InsufficientLiquidity();
if (address(this).balance < amount) revert IWithdraw.InsufficientLiquidity();
(bool success,) = user.call{value: amount}("");
if (!success) revert ETHTransferFailed();
if (!success) revert IWithdraw.ETHTransferFailed();
} else {
if (IERC20(token).balanceOf(address(this)) < amount) revert InsufficientLiquidity();
if (IERC20(token).balanceOf(address(this)) < amount) revert IWithdraw.InsufficientLiquidity();
IERC20(token).safeTransfer(user, amount);
}

Expand All @@ -92,8 +92,8 @@ contract SimpleCustody is IWithdraw, IDeposit, AccessControl, ReentrancyGuard {

function rejectWithdraw(bytes32 withdrawalId) external override onlyRole(NITEWATCH_ROLE) nonReentrant {
WithdrawalRequest storage request = withdrawals[withdrawalId];
if (!request.exists) revert WithdrawalNotFound();
if (request.finalized) revert WithdrawalAlreadyFinalized();
if (!request.exists) revert IWithdraw.WithdrawalNotFound();
if (request.finalized) revert IWithdraw.WithdrawalAlreadyFinalized();

request.finalized = true;

Expand Down
Loading