forked from scroll-tech/canvas-contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrollBadgeLevels.sol
72 lines (60 loc) · 2.67 KB
/
ScrollBadgeLevels.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
import {Attestation} from "@eas/contracts/IEAS.sol";
import {Base64} from "@openzeppelin/contracts/utils/Base64.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {ScrollBadgeAccessControl} from "../extensions/ScrollBadgeAccessControl.sol";
import {ScrollBadgeCustomPayload} from "../extensions/ScrollBadgeCustomPayload.sol";
import {ScrollBadgeDefaultURI} from "../extensions/ScrollBadgeDefaultURI.sol";
import {ScrollBadge} from "../ScrollBadge.sol";
string constant SCROLL_BADGE_LEVELS_SCHEMA = "uint8 scrollLevel";
function decodePayloadData(bytes memory data) pure returns (uint8) {
return abi.decode(data, (uint8));
}
/// @title ScrollBadgeLevels
/// @notice A simple badge that represents the user's level.
contract ScrollBadgeLevels is ScrollBadgeAccessControl, ScrollBadgeCustomPayload, ScrollBadgeDefaultURI {
constructor(address resolver_, string memory _defaultBadgeURI)
ScrollBadge(resolver_)
ScrollBadgeDefaultURI(_defaultBadgeURI)
{
// empty
}
/// @inheritdoc ScrollBadge
function onIssueBadge(Attestation calldata attestation)
internal
override (ScrollBadge, ScrollBadgeAccessControl, ScrollBadgeCustomPayload)
returns (bool)
{
return super.onIssueBadge(attestation);
}
/// @inheritdoc ScrollBadge
function onRevokeBadge(Attestation calldata attestation)
internal
override (ScrollBadge, ScrollBadgeAccessControl, ScrollBadgeCustomPayload)
returns (bool)
{
return super.onRevokeBadge(attestation);
}
/// @inheritdoc ScrollBadgeDefaultURI
function getBadgeTokenURI(bytes32 uid) internal view override returns (string memory) {
uint8 level = getCurrentLevel(uid);
string memory name = string(abi.encode("Scroll Level #", Strings.toString(level)));
string memory description = "Scroll Level Badge";
string memory image = ""; // IPFS, HTTP, or data URL
string memory tokenUriJson = Base64.encode(
abi.encodePacked('{"name":"', name, '", "description":"', description, ', "image": "', image, '"}')
);
return string(abi.encodePacked("data:application/json;base64,", tokenUriJson));
}
/// @inheritdoc ScrollBadgeCustomPayload
function getSchema() public pure override returns (string memory) {
return SCROLL_BADGE_LEVELS_SCHEMA;
}
function getCurrentLevel(bytes32 uid) public view returns (uint8) {
Attestation memory badge = getAndValidateBadge(uid);
bytes memory payload = getPayload(badge);
(uint8 level) = decodePayloadData(payload);
return level;
}
}