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 3 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
86 changes: 82 additions & 4 deletions plume/src/Faucet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ contract Faucet is Initializable, UUPSUpgradeable {
mapping(string tokenName => address tokenAddress) tokens;
/// @dev True if the nonce has been used; false otherwise
mapping(bytes32 nonce => bool used) usedNonces;
/// @dev Multipliers for each flight class
uint16[] multipliers;
}

// keccak256(abi.encode(uint256(keccak256("plume.storage.Faucet")) - 1)) & ~bytes32(uint256(0xff))
Expand Down Expand Up @@ -86,6 +88,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 +192,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 +204,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 +262,32 @@ 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 view returns (uint256) {
FaucetStorage storage $ = _getFaucetStorage();

if (flightClass < 1 || flightClass > 6) {
revert InvalidFlightClass(flightClass);
}

// Retrieve the multiplier from the storage array
uint256 multiplier = $.multipliers[flightClass - 1]; // Array index starts at 0

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 @@ -294,6 +329,31 @@ contract Faucet is Initializable, UUPSUpgradeable {
$.dripAmounts[tokenAddress] = amount;
}

/**
* @notice Update multipliers for flight classes
* @dev Only the owner can call this function
* @param newMultipliers Array of multipliers for flight classes
*/
function setMultipliers(
uint8[6] calldata newMultipliers
) external onlyOwner {
FaucetStorage storage $ = _getFaucetStorage();

// Update multipliers
$.multipliers = newMultipliers;
}

/**
* @notice Reset multipliers for flight classes to default values
* @dev Only the owner can call this function
*/
function setMultipliers() external onlyOwner {
FaucetStorage storage $ = _getFaucetStorage();

// Set to default multipliers
$.multipliers = [1, 11, 125, 200, 300, 500];
}

// Getter View Functions

/// @notice Get the owner of the faucet
Expand All @@ -302,9 +362,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 +373,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