|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity 0.8.19; |
| 4 | + |
| 5 | +import {Attestation} from "@eas/contracts/IEAS.sol"; |
| 6 | +import {NO_EXPIRATION_TIME} from "@eas/contracts/Common.sol"; |
| 7 | +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; |
| 8 | +import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; |
| 9 | +import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; |
| 10 | + |
| 11 | +import {IScrollBadgeResolver} from "../../interfaces/IScrollBadgeResolver.sol"; |
| 12 | +import {IScrollBadge, IScrollSelfAttestationBadge} from "../../interfaces/IScrollSelfAttestationBadge.sol"; |
| 13 | +import {encodeBadgeData} from "../../Common.sol"; |
| 14 | +import {ScrollBadge} from "../ScrollBadge.sol"; |
| 15 | +import {ScrollBadgeCustomPayload} from "../extensions/ScrollBadgeCustomPayload.sol"; |
| 16 | +import {ScrollBadgeDefaultURI} from "../extensions/ScrollBadgeDefaultURI.sol"; |
| 17 | + |
| 18 | +string constant SCR_HOLDING_BADGE_SCHEMA = "uint256 level"; |
| 19 | + |
| 20 | +function decodePayloadData(bytes memory data) pure returns (uint256) { |
| 21 | + return abi.decode(data, (uint256)); |
| 22 | +} |
| 23 | + |
| 24 | +/// @title SCRHoldingBadge |
| 25 | +/// @notice A badge that represents user's SCR holding amount. |
| 26 | +contract SCRHoldingBadge is ScrollBadgeCustomPayload, ScrollBadgeDefaultURI, Ownable, IScrollSelfAttestationBadge { |
| 27 | + uint256 private constant LEVEL_ONE_SCR_AMOUNT = 1 ether; |
| 28 | + uint256 private constant LEVEL_TWO_SCR_AMOUNT = 10 ether; |
| 29 | + uint256 private constant LEVEL_THREE_SCR_AMOUNT = 100 ether; |
| 30 | + uint256 private constant LEVEL_FOUR_SCR_AMOUNT = 1000 ether; |
| 31 | + uint256 private constant LEVEL_FIVE_SCR_AMOUNT = 10_000 ether; |
| 32 | + uint256 private constant LEVEL_SIX_SCR_AMOUNT = 100_000 ether; |
| 33 | + |
| 34 | + /// @notice The address of SCR token. |
| 35 | + address public immutable scr; |
| 36 | + |
| 37 | + constructor(address resolver_, string memory baseTokenURI_, address scr_) |
| 38 | + ScrollBadge(resolver_) |
| 39 | + ScrollBadgeDefaultURI(baseTokenURI_) |
| 40 | + { |
| 41 | + scr = scr_; |
| 42 | + } |
| 43 | + |
| 44 | + /// @notice Update the base token URI. |
| 45 | + /// @param baseTokenURI_ The new base token URI. |
| 46 | + function updateBaseTokenURI(string memory baseTokenURI_) external onlyOwner { |
| 47 | + defaultBadgeURI = baseTokenURI_; |
| 48 | + } |
| 49 | + |
| 50 | + /// @inheritdoc ScrollBadge |
| 51 | + function onIssueBadge(Attestation calldata) |
| 52 | + internal |
| 53 | + virtual |
| 54 | + override (ScrollBadge, ScrollBadgeCustomPayload) |
| 55 | + returns (bool) |
| 56 | + { |
| 57 | + return false; |
| 58 | + } |
| 59 | + |
| 60 | + /// @inheritdoc ScrollBadge |
| 61 | + function onRevokeBadge(Attestation calldata) |
| 62 | + internal |
| 63 | + virtual |
| 64 | + override (ScrollBadge, ScrollBadgeCustomPayload) |
| 65 | + returns (bool) |
| 66 | + { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + /// @inheritdoc ScrollBadge |
| 71 | + function badgeTokenURI(bytes32 uid) |
| 72 | + public |
| 73 | + view |
| 74 | + override (IScrollBadge, ScrollBadge, ScrollBadgeDefaultURI) |
| 75 | + returns (string memory) |
| 76 | + { |
| 77 | + return ScrollBadgeDefaultURI.badgeTokenURI(uid); |
| 78 | + } |
| 79 | + |
| 80 | + /// @inheritdoc IScrollBadge |
| 81 | + function hasBadge(address user) public view virtual override (IScrollBadge, ScrollBadge) returns (bool) { |
| 82 | + uint256 balance = IERC20(scr).balanceOf(user); |
| 83 | + return balance >= LEVEL_ONE_SCR_AMOUNT; |
| 84 | + } |
| 85 | + |
| 86 | + /// @inheritdoc ScrollBadgeDefaultURI |
| 87 | + function getBadgeTokenURI(bytes32 uid) internal view override returns (string memory) { |
| 88 | + Attestation memory attestation = getAndValidateBadge(uid); |
| 89 | + bytes memory payload = getPayload(attestation); |
| 90 | + uint256 level = decodePayloadData(payload); |
| 91 | + |
| 92 | + return string(abi.encodePacked(defaultBadgeURI, Strings.toString(level), ".json")); |
| 93 | + } |
| 94 | + |
| 95 | + /// @inheritdoc ScrollBadgeCustomPayload |
| 96 | + function getSchema() public pure override returns (string memory) { |
| 97 | + return SCR_HOLDING_BADGE_SCHEMA; |
| 98 | + } |
| 99 | + |
| 100 | + /// @inheritdoc IScrollSelfAttestationBadge |
| 101 | + function getBadgeId() external pure returns (uint256) { |
| 102 | + return 0; |
| 103 | + } |
| 104 | + |
| 105 | + /// @inheritdoc IScrollSelfAttestationBadge |
| 106 | + /// |
| 107 | + /// @dev The uid encoding should be |
| 108 | + /// ```text |
| 109 | + /// [ address | badge id | customized data ] |
| 110 | + /// [ 160 bits | 32 bits | 64 bits ] |
| 111 | + /// [LSB MSB] |
| 112 | + /// ``` |
| 113 | + /// The *badge id* and the *customized data* should both be zero. |
| 114 | + function getAttestation(bytes32 uid) external view override returns (Attestation memory attestation) { |
| 115 | + // invalid uid, return empty badge |
| 116 | + if ((uint256(uid) >> 160) > 0) return attestation; |
| 117 | + |
| 118 | + // extract badge recipient from uid |
| 119 | + address recipient; |
| 120 | + assembly { |
| 121 | + recipient := and(uid, 0xffffffffffffffffffffffffffffffffffffffff) |
| 122 | + } |
| 123 | + |
| 124 | + // compute payload |
| 125 | + uint256 level; |
| 126 | + uint256 balance = IERC20(scr).balanceOf(recipient); |
| 127 | + // not hold enough SCR, return empty badge |
| 128 | + if (balance < LEVEL_ONE_SCR_AMOUNT) return attestation; |
| 129 | + else if (balance < LEVEL_TWO_SCR_AMOUNT) level = 1; |
| 130 | + else if (balance < LEVEL_THREE_SCR_AMOUNT) level = 2; |
| 131 | + else if (balance < LEVEL_FOUR_SCR_AMOUNT) level = 3; |
| 132 | + else if (balance < LEVEL_FIVE_SCR_AMOUNT) level = 4; |
| 133 | + else if (balance < LEVEL_SIX_SCR_AMOUNT) level = 5; |
| 134 | + else level = 6; |
| 135 | + bytes memory payload = abi.encode(level); |
| 136 | + |
| 137 | + // fill data in Attestation |
| 138 | + attestation.uid = uid; |
| 139 | + attestation.schema = IScrollBadgeResolver(resolver).schema(); |
| 140 | + attestation.time = uint64(block.timestamp); |
| 141 | + attestation.expirationTime = NO_EXPIRATION_TIME; |
| 142 | + attestation.refUID = bytes32(0); |
| 143 | + attestation.recipient = recipient; |
| 144 | + attestation.attester = address(this); |
| 145 | + attestation.revocable = false; |
| 146 | + attestation.data = encodeBadgeData(address(this), payload); |
| 147 | + |
| 148 | + return attestation; |
| 149 | + } |
| 150 | +} |
0 commit comments