Skip to content

Commit

Permalink
fix: add input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
0xChin committed Nov 19, 2024
1 parent b19bfe6 commit 95d25bc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/contracts/Grateful.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {

/*//////////////////////////////////////////////////////////////
CONSTANTS
//////////////////////////////////////////////////////////////*/
//////////////////////////////////////////////////////////////*/

/// @inheritdoc IGrateful
uint256 public constant MAX_FEE = 10_000; // Max 100% fee (10000 basis points)

/// @inheritdoc IGrateful
uint256 public constant MAX_PERFORMANCE_FEE = 5000; // Max 50% performance fee (5000 basis points)

/*//////////////////////////////////////////////////////////////
STATE VARIABLES
STATE VARIABLES
//////////////////////////////////////////////////////////////*/

/// @inheritdoc IGrateful
Expand Down Expand Up @@ -70,7 +71,7 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
uint256 public performanceFeeRate = 500; // 5% fee

/*//////////////////////////////////////////////////////////////
MODIFIERS
MODIFIERS
//////////////////////////////////////////////////////////////*/

modifier onlyWhenTokenWhitelisted(
Expand All @@ -90,7 +91,7 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
}

/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/

/**
Expand All @@ -110,7 +111,7 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
}

/*//////////////////////////////////////////////////////////////
PUBLIC FUNCTIONS
PUBLIC FUNCTIONS
//////////////////////////////////////////////////////////////*/

/// @inheritdoc IGrateful
Expand Down Expand Up @@ -166,7 +167,7 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
}

/*//////////////////////////////////////////////////////////////
EXTERNAL FUNCTIONS
EXTERNAL FUNCTIONS
//////////////////////////////////////////////////////////////*/

/// @inheritdoc IGrateful
Expand Down Expand Up @@ -350,7 +351,7 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
}

/*//////////////////////////////////////////////////////////////
PRIVATE FUNCTIONS
PRIVATE FUNCTIONS
//////////////////////////////////////////////////////////////*/

/**
Expand All @@ -371,8 +372,8 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
* @param _merchant Address of the merchant.
* @param _token Address of the token.
* @param _amount Amount of the token.
* @param _paymentId ID of the payment
* @param _yieldFunds Whether to yield funds or not
* @param _paymentId ID of the payment.
* @param _yieldFunds Whether to yield funds or not.
*/
function _processPayment(
address _sender,
Expand All @@ -382,6 +383,11 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
uint256 _paymentId,
bool _yieldFunds
) private nonReentrant {
// Validate amount
if (_amount == 0) {
revert Grateful_InvalidAmount();
}

// Check payment id
if (paymentIds[_paymentId]) {
revert Grateful_PaymentIdAlreadyUsed();
Expand Down Expand Up @@ -438,6 +444,10 @@ contract Grateful is IGrateful, Ownable2Step, ReentrancyGuard {
sharesToWithdraw = totalShares;
assetsToWithdraw = vault.previewRedeem(sharesToWithdraw);
} else {
// Validate assets amount
if (_assets == 0) {
revert Grateful_InvalidAmount();
}
sharesToWithdraw = vault.previewWithdraw(_assets);
if (sharesToWithdraw > totalShares) {
revert Grateful_WithdrawExceedsShares();
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/IGrateful.sol
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ interface IGrateful {
/// @notice Thrown when the fee rate is too high.
error Grateful_FeeRateTooHigh();

/// @notice Thrown when the provided amount is invalid.
error Grateful_InvalidAmount();

/*///////////////////////////////////////////////////////////////
VARIABLES
//////////////////////////////////////////////////////////////*/
Expand Down

0 comments on commit 95d25bc

Please sign in to comment.