|
| 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 = 10000 ether; |
| 32 | + uint256 private constant LEVEL_SIX_SCR_AMOUNT = 100000 ether; |
| 33 | + |
| 34 | + /// @notice The address of SCR token. |
| 35 | + address public immutable scr; |
| 36 | + |
| 37 | + constructor( |
| 38 | + address resolver_, |
| 39 | + string memory baseTokenURI_, |
| 40 | + address scr_ |
| 41 | + ) ScrollBadge(resolver_) ScrollBadgeDefaultURI(baseTokenURI_) { |
| 42 | + scr = scr_; |
| 43 | + } |
| 44 | + |
| 45 | + /// @notice Update the base token URI. |
| 46 | + /// @param baseTokenURI_ The new base token URI. |
| 47 | + function updateBaseTokenURI(string memory baseTokenURI_) external onlyOwner { |
| 48 | + defaultBadgeURI = baseTokenURI_; |
| 49 | + } |
| 50 | + |
| 51 | + /// @inheritdoc ScrollBadge |
| 52 | + function onIssueBadge( |
| 53 | + Attestation calldata |
| 54 | + ) internal virtual override(ScrollBadge, ScrollBadgeCustomPayload) returns (bool) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + |
| 58 | + /// @inheritdoc ScrollBadge |
| 59 | + function onRevokeBadge( |
| 60 | + Attestation calldata |
| 61 | + ) internal virtual override(ScrollBadge, ScrollBadgeCustomPayload) returns (bool) { |
| 62 | + return false; |
| 63 | + } |
| 64 | + |
| 65 | + /// @inheritdoc ScrollBadge |
| 66 | + function badgeTokenURI( |
| 67 | + bytes32 uid |
| 68 | + ) public view override(IScrollBadge, ScrollBadge, ScrollBadgeDefaultURI) returns (string memory) { |
| 69 | + return ScrollBadgeDefaultURI.badgeTokenURI(uid); |
| 70 | + } |
| 71 | + |
| 72 | + /// @inheritdoc ScrollBadgeDefaultURI |
| 73 | + function getBadgeTokenURI(bytes32 uid) internal view override returns (string memory) { |
| 74 | + Attestation memory attestation = getAndValidateBadge(uid); |
| 75 | + bytes memory payload = getPayload(attestation); |
| 76 | + uint256 year = decodePayloadData(payload); |
| 77 | + |
| 78 | + return string(abi.encodePacked(defaultBadgeURI, Strings.toString(year), ".json")); |
| 79 | + } |
| 80 | + |
| 81 | + /// @inheritdoc ScrollBadgeCustomPayload |
| 82 | + function getSchema() public pure override returns (string memory) { |
| 83 | + return SCR_HOLDING_BADGE_SCHEMA; |
| 84 | + } |
| 85 | + |
| 86 | + /// @inheritdoc IScrollSelfAttestationBadge |
| 87 | + function getBadgeId() external pure returns (uint256) { |
| 88 | + return 0; |
| 89 | + } |
| 90 | + |
| 91 | + /// @inheritdoc IScrollSelfAttestationBadge |
| 92 | + /// |
| 93 | + /// @dev The uid encoding should be |
| 94 | + /// ```text |
| 95 | + /// [ address | badge id | customized data ] |
| 96 | + /// [ 160 bits | 32 bits | 64 bits ] |
| 97 | + /// [LSB MSB] |
| 98 | + /// ``` |
| 99 | + /// The *badge id* and the *customized data* should both be zero. |
| 100 | + function getAttestation(bytes32 uid) external view override returns (Attestation memory attestation) { |
| 101 | + // invalid uid, return empty badge |
| 102 | + if ((uint256(uid) >> 160) > 0) return attestation; |
| 103 | + |
| 104 | + // extract badge recipient from uid |
| 105 | + address recipient; |
| 106 | + assembly { |
| 107 | + recipient := and(uid, 0xffffffffffffffffffffffffffffffffffffffff) |
| 108 | + } |
| 109 | + |
| 110 | + // compute payload |
| 111 | + uint256 level; |
| 112 | + uint256 balance = IERC20(scr).balanceOf(recipient); |
| 113 | + // not hold enough SCR, return empty badge |
| 114 | + if (balance < LEVEL_ONE_SCR_AMOUNT) return attestation; |
| 115 | + else if (balance < LEVEL_TWO_SCR_AMOUNT) level = 1; |
| 116 | + else if (balance < LEVEL_THREE_SCR_AMOUNT) level = 2; |
| 117 | + else if (balance < LEVEL_FOUR_SCR_AMOUNT) level = 3; |
| 118 | + else if (balance < LEVEL_FIVE_SCR_AMOUNT) level = 4; |
| 119 | + else if (balance < LEVEL_SIX_SCR_AMOUNT) level = 5; |
| 120 | + else level = 6; |
| 121 | + bytes memory payload = abi.encode(level); |
| 122 | + |
| 123 | + // fill data in Attestation |
| 124 | + attestation.uid = uid; |
| 125 | + attestation.schema = IScrollBadgeResolver(resolver).schema(); |
| 126 | + attestation.time = uint64(block.timestamp); |
| 127 | + attestation.expirationTime = NO_EXPIRATION_TIME; |
| 128 | + attestation.refUID = bytes32(0); |
| 129 | + attestation.recipient = recipient; |
| 130 | + attestation.attester = address(this); |
| 131 | + attestation.revocable = false; |
| 132 | + attestation.data = encodeBadgeData(address(this), payload); |
| 133 | + |
| 134 | + return attestation; |
| 135 | + } |
| 136 | +} |
0 commit comments