Skip to content

Commit

Permalink
FractionTokenの権限管理の修正、テストコードの追加および修正
Browse files Browse the repository at this point in the history
  • Loading branch information
aowheel committed Oct 28, 2024
1 parent d7c7a09 commit 153802a
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 79 deletions.
16 changes: 14 additions & 2 deletions pkgs/contract/contracts/fractiontoken/FractionToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable {
uint256 hatId,
address account
) public {
require(
_hasHatRole(account, hatId),
"This account does not have the role"
);

require(
_hasHatAuthority(hatId),
"Not authorized"
"This msg.sender does not have the authority"
);

uint256 tokenId = getTokenId(hatId, account);
Expand All @@ -53,8 +58,15 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable {
uint256 amount
) public {
uint256 tokenId = getTokenId(hatId, account);

require(
tokenRecipients[tokenId].length > 0,
"This account has not received the initial supply"
);

require(_msgSender() == tokenRecipients[tokenId][0], "Only the first recipient can additionally mint");
require(
_msgSender() == tokenRecipients[tokenId][0],
"Only the first recipient can additionally mint");

_mint(account, tokenId, amount, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,44 @@ import "./../../ERC2771ContextUpgradeable.sol";


contract FractionToken_Mock_v2 is ERC1155Upgradeable, ERC2771ContextUpgradeable {
uint256 public TOKEN_SUPPLY;
uint256 public TOKEN_SUPPLY;

mapping(uint256 => address[]) private tokenRecipients;

IHats public hatsContract;
IHats private hatsContract;

function initialize(
string memory _uri,
uint256 _tokenSupply,
address _hatsAddress,
address _trustedForwarderAddress
) initializer public {
) public initializer {
__ERC1155_init(_uri);
__ERC2771Context_init(address(_trustedForwarderAddress));
hatsContract = IHats(_hatsAddress);
TOKEN_SUPPLY = _tokenSupply;
}

function mint(uint256 hatId, address account) public {
require(_hasHatRole(account, hatId), "not authorized");
function mintInitialSupply(
uint256 hatId,
address account
) public {
require(
_hasHatRole(account, hatId),
"This account does not have the role"
);

require(
_hasHatAuthority(hatId),
"This msg.sender does not have the authority"
);

uint256 tokenId = getTokenId(hatId, account);

require(!_containsRecipient(tokenId, account), "already received");
require(
!_containsRecipient(tokenId, account),
"This account has already received"
);

_mint(account, tokenId, TOKEN_SUPPLY, "");

Expand All @@ -40,6 +54,25 @@ uint256 public TOKEN_SUPPLY;
}
}

function mint(
uint256 hatId,
address account,
uint256 amount
) public {
uint256 tokenId = getTokenId(hatId, account);

require(
tokenRecipients[tokenId].length > 0,
"This account has not received the initial supply"
);

require(
_msgSender() == tokenRecipients[tokenId][0],
"Only the first recipient can additionally mint");

_mint(account, tokenId, amount, "");
}

function burn(
address from,
address wearer,
Expand All @@ -48,7 +81,10 @@ uint256 public TOKEN_SUPPLY;
) public {
uint256 tokenId = getTokenId(hatId, wearer);

require(_msgSender() == from || _containsRecipient(tokenId, _msgSender()), "not authorized");
require(
_msgSender() == from || _hasHatAuthority(hatId),
"Not authorized"
);

_burn(from, tokenId, value);
}
Expand Down Expand Up @@ -117,14 +153,30 @@ uint256 public TOKEN_SUPPLY;
return balance > 0;
}

function _hasHatAuthority(
uint256 hatId
) private view returns (bool) {
uint32 hatLevel = hatsContract.getHatLevel(hatId);

uint256 parentHatId = hatsContract.getAdminAtLevel(hatId, hatLevel - 1);
if (_hasHatRole(_msgSender(), parentHatId)) return true;

uint256 topHatId = hatsContract.getAdminAtLevel(hatId, 0);
if (_hasHatRole(_msgSender(), topHatId)) return true;

return false;
}

function balanceOf(
address account,
address wearer,
uint256 hatId
) public view returns (uint256) {
uint256 tokenId = getTokenId(hatId, wearer);

if (_hasHatRole(account, hatId) && !_containsRecipient(tokenId, account)) {
if (
_hasHatRole(account, hatId) && !_containsRecipient(tokenId, account)
) {
return TOKEN_SUPPLY;
}

Expand Down Expand Up @@ -181,9 +233,9 @@ uint256 public TOKEN_SUPPLY;
}

/**
* 検証用に追加した関数
*/
function testUpgradeFunction() external pure returns (string memory) {
return "testUpgradeFunction";
}
* 検証用に追加した関数
*/
function testUpgradeFunction() external pure returns (string memory) {
return "testUpgradeFunction";
}
}
Loading

0 comments on commit 153802a

Please sign in to comment.