Skip to content

Commit

Permalink
修正
Browse files Browse the repository at this point in the history
  • Loading branch information
mashharuki committed Oct 27, 2024
1 parent 676d3a8 commit 61b70a8
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkgs/contract/contracts/fractiontoken/FractionToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable{

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

IHats public hatsContract;
IHats private hatsContract;

function initialize(
string memory _uri,
Expand Down
169 changes: 166 additions & 3 deletions pkgs/contract/contracts/fractiontoken/mock/FractionToken_Mock_v2.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import "./../FractionToken.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
import { IHats } from "../../hats/src/Interfaces/IHats.sol";
import "./../../ERC2771ContextUpgradeable.sol";

contract FractionToken_Mock_v2 is FractionToken {

function initialize2(
contract FractionToken_Mock_v2 is ERC1155Upgradeable, ERC2771ContextUpgradeable {
uint256 public TOKEN_SUPPLY;

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

IHats public hatsContract;

function initialize(
string memory _uri,
uint256 _tokenSupply,
address _hatsAddress,
Expand All @@ -17,6 +26,160 @@ contract FractionToken_Mock_v2 is FractionToken {
TOKEN_SUPPLY = _tokenSupply;
}

function mint(uint256 hatId, address account) public {
require(_hasHatRole(account, hatId), "not authorized");

uint256 tokenId = getTokenId(hatId, account);

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

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

if (!_containsRecipient(tokenId, account)) {
tokenRecipients[tokenId].push(account);
}
}

function burn(
address from,
address wearer,
uint256 hatId,
uint256 value
) public {
uint256 tokenId = getTokenId(hatId, wearer);

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

_burn(from, tokenId, value);
}

function safeTransferFrom(
address from,
address to,
uint256 tokenId,
uint256 amount,
bytes memory data
) public override {
super.safeTransferFrom(from, to, tokenId, amount, data);

if (!_containsRecipient(tokenId, to)) {
tokenRecipients[tokenId].push(to);
}
}

function safeBatchTransferFrom(
address from,
address to,
uint256[] memory tokenIds,
uint256[] memory amounts,
bytes memory data
) public override {
super.safeBatchTransferFrom(from, to, tokenIds, amounts, data);

for (uint256 i = 0; i < tokenIds.length; i++) {
if (!_containsRecipient(tokenIds[i], to)) {
tokenRecipients[tokenIds[i]].push(to);
}
}
}

function getTokenRecipients(
uint256 tokenId
) public view returns (address[] memory) {
return tokenRecipients[tokenId];
}

function getTokenId(
uint256 hatId,
address account
) public pure returns (uint256) {
return uint256(keccak256(abi.encodePacked(hatId, account)));
}

function _containsRecipient(
uint256 tokenId,
address recipient
) private view returns (bool) {
address[] memory recipients = tokenRecipients[tokenId];
for (uint256 i = 0; i < recipients.length; i++) {
if (recipients[i] == recipient) {
return true;
}
}
return false;
}

function _hasHatRole(
address wearer,
uint256 hatId
) private view returns (bool) {
uint256 balance = hatsContract.balanceOf(wearer, hatId);
return balance > 0;
}

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

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

uint256 erc1155Balance = super.balanceOf(account, tokenId);
return erc1155Balance;
}

function balanceOfBatch(
address[] memory accounts,
address[] memory wearers,
uint256[] memory hatIds
) public view returns (uint256[] memory) {
uint256[] memory balances = new uint256[](accounts.length);

for (uint256 i = 0; i < accounts.length; i++) {
balances[i] = balanceOf(accounts[i], wearers[i], hatIds[i]);
}

return balances;
}

function uri(
uint256 tokenId
) public view override(ERC1155Upgradeable) returns (string memory) {
return super.uri(tokenId);
}

function _msgSender()
internal
view
override(ERC2771ContextUpgradeable, ContextUpgradeable)
returns (address sender)
{
return super._msgSender();
}

function _msgData()
internal
view
override(ERC2771ContextUpgradeable, ContextUpgradeable)
returns (bytes calldata)
{
return super._msgData();
}

function _contextSuffixLength()
internal
view
virtual
override(ContextUpgradeable)
returns (uint256)
{
return super._contextSuffixLength();
}

/**
* 検証用に追加した関数
*/
Expand Down
4 changes: 2 additions & 2 deletions pkgs/contract/gas-report.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
··························|··············|·············|·············|···············|··············
| BigBang_Mock_v2 · - · - · 1326614 · 4.4 % · - │
··························|··············|·············|·············|···············|··············
| FractionToken · - · - · 2893315 · 9.6 % · - │
| FractionToken · - · - · 2859199 · 9.5 % · - │
··························|··············|·············|·············|···············|··············
| FractionToken_Mock_v2 · - · - · 3037174 · 10.1 % · - │
| FractionToken_Mock_v2 · - · - · 2928899 · 9.8 % · - │
··························|··············|·············|·············|···············|··············
| Hats · - · - · 7032431 · 23.4 % · - │
··························|··············|·············|·············|···············|··············
Expand Down
2 changes: 1 addition & 1 deletion pkgs/contract/helpers/upgrade.ts/bigbang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const upgradeBigBang = async (

const address = _BigBang.target;

console.log("upgraded address:", address);
//console.log("upgraded address:", address);

// create a new instance of the contract
const newBigBang = await viem.getContractAt(contractName, address as Address);
Expand Down
2 changes: 1 addition & 1 deletion pkgs/contract/helpers/upgrade.ts/fractionToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const upgradeFractionToken = async (

const address = _FractionToken.target;

console.log("upgraded address:", address);
//console.log("upgraded address:", address);

// create a new instance of the contract
const newFractionToken = await viem.getContractAt(
Expand Down
35 changes: 30 additions & 5 deletions pkgs/contract/test/FractionToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,33 @@ describe("FractionToken", () => {
address2.account?.address!,
]);

// transfer と burn前の残高情報を取得する
let balance: bigint;

// 処理前のaddress1のbalance
balance = await FractionToken.read.balanceOf([
address1.account?.address!,
address1.account?.address!,
hatId,
]);
expect(balance).to.equal(10000n);

// 処理前のaddress2のbalance
balance = await FractionToken.read.balanceOf([
address2.account?.address!,
address2.account?.address!,
hatId,
]);
expect(balance).to.equal(10000n);

// 処理前のaddress3のbalance
balance = await FractionToken.read.balanceOf([
address3.account?.address!,
address2.account?.address!,
hatId,
]);
expect(balance).to.equal(0n);

// address2のtokenの半分をaddress3に移動
await FractionToken.write.safeTransferFrom(
[
Expand Down Expand Up @@ -117,25 +144,23 @@ describe("FractionToken", () => {
}
);

let balance: bigint;

// address1のbalance
// 処理後のaddress1のbalance
balance = await FractionToken.read.balanceOf([
address1.account?.address!,
address1.account?.address!,
hatId,
]);
expect(balance).to.equal(5000n);

// address2のbalance
// 処理後のaddress2のbalance
balance = await FractionToken.read.balanceOf([
address2.account?.address!,
address2.account?.address!,
hatId,
]);
expect(balance).to.equal(5000n);

// address3のbalance
// 処理後のaddress3のbalance
balance = await FractionToken.read.balanceOf([
address3.account?.address!,
address2.account?.address!,
Expand Down

0 comments on commit 61b70a8

Please sign in to comment.