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

[FEA-792] added multiplier #127

Merged
merged 4 commits into from
Dec 19, 2024
Merged
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
67 changes: 63 additions & 4 deletions plume/src/Faucet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ contract Faucet is Initializable, UUPSUpgradeable {
/// @notice Indicates a failure because the address is invalid
error InvalidAddress();

/// @notice Indicates a failure because the flightClass is invalid
error InvalidFlightClass(uint256 flightClass);

/**
* @notice Indicates a failure because the sender is not authorized to perform the action
* @param sender Address of the sender that is not authorized
Expand Down Expand Up @@ -187,6 +190,7 @@ contract Faucet is Initializable, UUPSUpgradeable {
/**
* @notice Get tokens from the faucet
* @param token Name of the token requested
* @param flightClass User's flight class
* @param salt Random value to prevent replay attacks
* @param signature Signature of the message signed by the owner
*/
Expand All @@ -198,11 +202,14 @@ contract Faucet is Initializable, UUPSUpgradeable {
) external onlySignedByOwner(token, flightClass, salt, signature) {
FaucetStorage storage $ = _getFaucetStorage();
address tokenAddress = $.tokens[token];
uint256 amount = $.dripAmounts[tokenAddress];
if (tokenAddress == address(0) || amount == 0) {
uint256 baseAmount = $.dripAmounts[tokenAddress];

if (tokenAddress == address(0) || baseAmount == 0) {
revert InvalidToken();
}

uint256 amount = _calculateDripAmount(baseAmount, flightClass);

if (tokenAddress == ETH_ADDRESS) {
if (address(this).balance < amount) {
revert InsufficientBalance(amount, token);
Expand Down Expand Up @@ -253,6 +260,40 @@ contract Faucet is Initializable, UUPSUpgradeable {
emit Withdrawn(recipient, amount, token);
}

/**
* @notice Calculate the amount of tokens to mint based on the base amount and flight class
* @dev Internal function to calculate the drip amount based on the flight class multiplier
* Flight classes correspond to:
* - Class 1: Economy
* - Class 2: Plus
* - Class 3: Premium
* - Class 4: Business
* - Class 5: First
* - Class 6: Private
* @param baseAmount Base amount of tokens to mint
* @param flightClass User flight class
*/
function _calculateDripAmount(uint256 baseAmount, uint256 flightClass) internal pure returns (uint256) {
uint256 multiplier;
if (flightClass == 1) {
multiplier = 1; // 1x
} else if (flightClass == 2) {
multiplier = 11; // 1.1x (scaled by 10)
} else if (flightClass == 3) {
multiplier = 125; // 1.25x (scaled by 100)
} else if (flightClass == 4) {
multiplier = 200; // 2x (scaled by 100)
} else if (flightClass == 5) {
multiplier = 300; // 3x (scaled by 100)
} else if (flightClass == 6) {
multiplier = 500; // 5x (scaled by 100)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add comments on what each flight class is (Economy, Plus, etc.)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a more efficient way of doing this? e.g. uint256[] constant multipliers = [0, 1, 11, 125, 200, 300, 500]; - check if this actually saves gas

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe something like this.

uint256[6] memory multipliers = [100, 110, 125, 200, 300, 500];
    
if (flightClass == 0 || flightClass > 6) {
    revert InvalidFlightClass(flightClass);
}
    
return (baseAmount * multipliers[flightClass - 1]) / 100;```

} else {
revert InvalidFlightClass(flightClass);
}

return (baseAmount * multiplier) / 100; // Normalize for scaling
}

/**
* @notice Set ownership of the faucet contract to the given address
* @dev Only the owner can call this function
Expand Down Expand Up @@ -302,9 +343,9 @@ contract Faucet is Initializable, UUPSUpgradeable {
}

/**
* @notice Get the amount of tokens to mint per faucet call for the given token
* @notice Get the base amount of tokens to mint per faucet call for the given token
* @param token Name of the token to get the amount for
* @return dripAmount Amount of tokens to mint per faucet call
* @return dripAmount Base amount of tokens to mint per faucet call
*/
function getDripAmount(
string calldata token
Expand All @@ -313,6 +354,24 @@ contract Faucet is Initializable, UUPSUpgradeable {
return $.dripAmounts[$.tokens[token]];
}

/**
* @notice Get the amount of tokens to mint per user call for the given token
* @param token Name of the token to get the amount for
* @param flightClass User's flight class
* @return dripAmount Amount of tokens to mint per faucet call
*/
function getDripAmount(string calldata token, uint256 flightClass) public view returns (uint256 dripAmount) {
FaucetStorage storage $ = _getFaucetStorage();
address tokenAddress = $.tokens[token];
uint256 baseAmount = $.dripAmounts[tokenAddress];

if (tokenAddress == address(0) || baseAmount == 0) {
revert InvalidToken();
}

return _calculateDripAmount(baseAmount, flightClass);
}

/**
* @notice Get the address of the given token
* @param token Name of the token to get the address for
Expand Down
Loading