Skip to content

Commit

Permalink
Merge pull request #159 from hackdays-io/feature/fractiontoken
Browse files Browse the repository at this point in the history
Feature/fractiontoken
  • Loading branch information
yu23ki14 authored Nov 22, 2024
2 parents cb6992c + b25d2ab commit b990ddf
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 268 deletions.
58 changes: 53 additions & 5 deletions pkgs/contract/contracts/fractiontoken/FractionToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable {
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 @@ -38,6 +52,26 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable {
}
}

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 @@ -47,8 +81,8 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable {
uint256 tokenId = getTokenId(hatId, wearer);

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

_burn(from, tokenId, value);
Expand Down Expand Up @@ -118,6 +152,20 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable {
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,
Expand Down
6 changes: 5 additions & 1 deletion pkgs/contract/contracts/fractiontoken/IFractionToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";

interface IFractionToken is IERC1155 {
function mint(string memory hatId, address account) external;
function mintInitialSupply(
string memory hatId,
address account,
uint256 amount
) external;

function burn(
address from,
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,26 @@ 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 +82,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 +154,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 +234,9 @@ uint256 public TOKEN_SUPPLY;
}

/**
* 検証用に追加した関数
*/
function testUpgradeFunction() external pure returns (string memory) {
return "testUpgradeFunction";
}
* 検証用に追加した関数
*/
function testUpgradeFunction() external pure returns (string memory) {
return "testUpgradeFunction";
}
}
31 changes: 31 additions & 0 deletions pkgs/contract/gas-report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
·------------------------|----------------------------|-------------|-----------------------------·
| Solc version: 0.8.24 · Optimizer enabled: false · Runs: 200 · Block limit: 30000000 gas │
·························|····························|·············|······························
| Methods │
··············|··········|··············|·············|·············|···············|··············
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
··············|··········|··············|·············|·············|···············|··············
| Deployments · · % of limit · │
·························|··············|·············|·············|···············|··············
| BigBang · - · - · 1248128 · 4.2 % · - │
·························|··············|·············|·············|···············|··············
| FractionToken · - · - · 3131808 · 10.4 % · - │
·························|··············|·············|·············|···············|··············
| Hats · - · - · 7032431 · 23.4 % · - │
·························|··············|·············|·············|···············|··············
| HatsModule · - · - · 754132 · 2.5 % · - │
·························|··············|·············|·············|···············|··············
| HatsModuleFactory · - · - · 1101122 · 3.7 % · - │
·························|··············|·············|·············|···············|··············
| HatsTimeFrameModule · - · - · 1287099 · 4.3 % · - │
·························|··············|·············|·············|···············|··············
| PullSplitFactory · 4535815 · 4535827 · 4535825 · 15.1 % · - │
·························|··············|·············|·············|···············|··············
| PushSplitFactory · 4483101 · 4483113 · 4483111 · 14.9 % · - │
·························|··············|·············|·············|···············|··············
| SplitsCreator · - · - · 1487532 · 5 % · - │
·························|··············|·············|·············|···············|··············
| SplitsCreatorFactory · - · - · 526836 · 1.8 % · - │
·························|··············|·············|·············|···············|··············
| SplitsWarehouse · - · - · 3934655 · 13.1 % · - │
·------------------------|--------------|-------------|-------------|---------------|-------------·
Loading

0 comments on commit b990ddf

Please sign in to comment.