Skip to content

Commit

Permalink
feat(contracts): add IScrollBadgeUpgradeable
Browse files Browse the repository at this point in the history
  • Loading branch information
lucianHymer committed Jun 26, 2024
1 parent 1782b72 commit 41cc7da
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity 0.8.19;

error Unauthorized();
error CannotUpgrade(bytes32 uid);

// attestation errors
// note: these don't include the uid since it is not known prior to the attestation.
Expand Down
23 changes: 18 additions & 5 deletions src/badge/examples/ScrollBadgePowerRank.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {ScrollBadgeCustomPayload} from "../extensions/ScrollBadgeCustomPayload.s
import {ScrollBadgeNoExpiry} from "../extensions/ScrollBadgeNoExpiry.sol";
import {ScrollBadgeNonRevocable} from "../extensions/ScrollBadgeNonRevocable.sol";
import {ScrollBadgeSingleton} from "../extensions/ScrollBadgeSingleton.sol";
import {Unauthorized} from "../../Errors.sol";
import {IScrollBadgeUpgradeable} from "../extensions/IScrollBadgeUpgradeable.sol";
import {Unauthorized, CannotUpgrade} from "../../Errors.sol";

string constant SCROLL_BADGE_POWER_RANK_SCHEMA = "uint256 firstTxTimestamp";

Expand All @@ -28,10 +29,9 @@ contract ScrollBadgePowerRank is
ScrollBadgeCustomPayload,
ScrollBadgeNoExpiry,
ScrollBadgeNonRevocable,
ScrollBadgeSingleton
ScrollBadgeSingleton,
IScrollBadgeUpgradeable
{
error CannotUpgrade();

event Upgrade(uint256 oldRank, uint256 newRank);

// badge UID => current rank
Expand Down Expand Up @@ -92,6 +92,19 @@ contract ScrollBadgePowerRank is
return SCROLL_BADGE_POWER_RANK_SCHEMA;
}

/// @inheritdoc IScrollBadgeUpgradeable
function canUpgrade(bytes32 uid) external view returns (bool) {
Attestation memory badge = getAndValidateBadge(uid);

bytes memory payload = getPayload(badge);
(uint256 firstTxTimestamp) = decodePayloadData(payload);
uint256 newRank = timestampToRank(firstTxTimestamp);

uint256 oldRank = badgeRank[uid];
return newRank > oldRank;
}

/// @inheritdoc IScrollBadgeUpgradeable
function upgrade(bytes32 uid) external {
Attestation memory badge = getAndValidateBadge(uid);

Expand All @@ -105,7 +118,7 @@ contract ScrollBadgePowerRank is

uint256 oldRank = badgeRank[uid];
if (newRank <= oldRank) {
revert CannotUpgrade();
revert CannotUpgrade(uid);
}

badgeRank[uid] = newRank;
Expand Down
18 changes: 18 additions & 0 deletions src/badge/extensions/IScrollBadgeUpgradeable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.19;

/// @title IScrollBadgeUpgradeable
/// @notice This interface defines functions to facilitate badge upgrades.
interface IScrollBadgeUpgradeable {
/// @notice Checks if a badge can be upgraded.
/// @param uid The unique identifier of the badge.
/// @return True if the badge can be upgraded, false otherwise.
function canUpgrade(bytes32 uid) external view returns (bool);

/// @notice Upgrades a badge.
/// @param uid The unique identifier of the badge.
/// @dev Should revert with CannotUpgrade (from Errors.sol) if the badge cannot be upgraded.
/// @dev Should emit an Upgrade event (custom defined) if the upgrade is successful.
function upgrade(bytes32 uid) external;
}

0 comments on commit 41cc7da

Please sign in to comment.