From c1bb5fca1dc8e07cf43af3501e5b873d4f10e013 Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 21 Oct 2024 21:08:20 +0900 Subject: [PATCH 1/7] chore: Rename vars for SplitsFactoryV2 and FractionToken --- pkgs/contract/contracts/bigbang/BigBang.sol | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pkgs/contract/contracts/bigbang/BigBang.sol b/pkgs/contract/contracts/bigbang/BigBang.sol index 84c1caa..715ea97 100644 --- a/pkgs/contract/contracts/bigbang/BigBang.sol +++ b/pkgs/contract/contracts/bigbang/BigBang.sol @@ -16,9 +16,9 @@ contract BigBang is ERC2771ContextUpgradeable { address public HatsTimeFrameModule_IMPL; - address public splitFactoryV2; + address public SplitsFactoryV2; - address public fractionToken; + address public FractionToken; event Executed( address indexed owner, @@ -52,8 +52,8 @@ contract BigBang is ERC2771ContextUpgradeable { HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory); HatsTimeFrameModule_IMPL = _hatsTimeFrameModule_IMPL; SplitsCreatorFactory = ISplitsCreatorFactory(_splitsCreatorFactory); - splitFactoryV2 = _splitFactoryV2; - fractionToken = _fractionToken; + SplitsFactoryV2 = _splitFactoryV2; + FractionToken = _fractionToken; } /** @@ -114,9 +114,9 @@ contract BigBang is ERC2771ContextUpgradeable { .createSplitCreatorDeterministic( topHatId, _trustedForwarder, - splitFactoryV2, + SplitsFactoryV2, hatsTimeFrameModule, - fractionToken, + FractionToken, keccak256(abi.encodePacked(topHatId)) ); From 4662113c1b8a6635945ecb300ddf402be34cd2ae Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 21 Oct 2024 21:31:48 +0900 Subject: [PATCH 2/7] feat: set functions with onlyOwner in BigBang.sol --- pkgs/contract/contracts/bigbang/BigBang.sol | 36 ++++++++++++++++++++- pkgs/contract/gas-report.txt | 4 +-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pkgs/contract/contracts/bigbang/BigBang.sol b/pkgs/contract/contracts/bigbang/BigBang.sol index 715ea97..6ddeec5 100644 --- a/pkgs/contract/contracts/bigbang/BigBang.sol +++ b/pkgs/contract/contracts/bigbang/BigBang.sol @@ -6,8 +6,9 @@ import { IHatsModuleFactory } from "./IHatsModuleFactory.sol"; import { ISplitsCreatorFactory } from "../splitscreator/ISplitsCreatorFactory.sol"; import { HatsTimeFrameModule } from "../timeframe/HatsTimeFrameModule.sol"; import "./../ERC2771ContextUpgradeable.sol"; +import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; -contract BigBang is ERC2771ContextUpgradeable { +contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { IHats public Hats; IHatsModuleFactory public HatsModuleFactory; @@ -48,6 +49,7 @@ contract BigBang is ERC2771ContextUpgradeable { address _fractionToken ) initializer public { __ERC2771Context_init(address(_trustedForwarder)); + __Ownable_init(_msgSender()); Hats = IHats(_hatsAddress); HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory); HatsTimeFrameModule_IMPL = _hatsTimeFrameModule_IMPL; @@ -124,4 +126,36 @@ contract BigBang is ERC2771ContextUpgradeable { return topHatId; } + + function setHats(address _hats) external { + Hats = IHats(_hats); + } + + function setHatsModuleFactory(address _hatsModuleFactory) external onlyOwner { + HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory); + } + + function setSplitsCreatorFactory(address _splitsCreatorFactory) external onlyOwner { + SplitsCreatorFactory = ISplitsCreatorFactory(_splitsCreatorFactory); + } + + function setHatsTimeFrameModuleImpl(address _hatsTimeFrameModuleImpl) external onlyOwner { + HatsTimeFrameModule_IMPL = _hatsTimeFrameModuleImpl; + } + + function setSplitsFactoryV2(address _splitsFactoryV2) external onlyOwner { + SplitsFactoryV2 = _splitsFactoryV2; + } + + function setFractionToken(address _fractionToken) external onlyOwner { + FractionToken = _fractionToken; + } + + 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(); + } } diff --git a/pkgs/contract/gas-report.txt b/pkgs/contract/gas-report.txt index 4565993..bd5643e 100644 --- a/pkgs/contract/gas-report.txt +++ b/pkgs/contract/gas-report.txt @@ -5,7 +5,7 @@ ··················|····················|··············|·············|·············|···············|·············· | Contract · Method · Min · Max · Avg · # calls · usd (avg) │ ··················|····················|··············|·············|·············|···············|·············· -| BigBang · bigbang · - · - · 603669 · 2 · - │ +| BigBang · bigbang · - · - · 603827 · 2 · - │ ··················|····················|··············|·············|·············|···············|·············· | FractionToken · burn · 41544 · 55511 · 47442 · 4 · - │ ··················|····················|··············|·············|·············|···············|·············· @@ -15,7 +15,7 @@ ··················|····················|··············|·············|·············|···············|·············· | Deployments · · % of limit · │ ·······································|··············|·············|·············|···············|·············· -| BigBang · - · - · 1248128 · 4.2 % · - │ +| BigBang · - · - · 1592787 · 5.3 % · - │ ·······································|··············|·············|·············|···············|·············· | FractionToken · - · - · 2859199 · 9.5 % · - │ ·······································|··············|·············|·············|···············|·············· From 87a2cb18206e110d0e397e8e95e1eae09677dc12 Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:21:42 +0900 Subject: [PATCH 3/7] WIP: 4662113 feat: set functions with onlyOwner in BigBang.sol --- contracts/fractiontoken/FractionToken.sol | 1 + .../contracts/ERC2771ContextUpgradeable.sol | 86 ++++----- pkgs/contract/contracts/bigbang/BigBang.sol | 14 +- .../contracts/fractiontoken/FractionToken.sol | 2 +- pkgs/contract/gas-report.txt | 6 +- pkgs/contract/test/BigBang.ts | 179 +++++++++++++++++- 6 files changed, 233 insertions(+), 55 deletions(-) create mode 100644 contracts/fractiontoken/FractionToken.sol diff --git a/contracts/fractiontoken/FractionToken.sol b/contracts/fractiontoken/FractionToken.sol new file mode 100644 index 0000000..0519ecb --- /dev/null +++ b/contracts/fractiontoken/FractionToken.sol @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol b/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol index a01b797..2860b4e 100644 --- a/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol +++ b/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol @@ -1,73 +1,71 @@ // SPDX-License-Identifier: MIT -// OpenZeppelin Contracts (last updated v4.7.0) (metatx/ERC2771Context.sol) - pragma solidity ^0.8.24; -import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol"; -import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; - +import {ContextUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol"; +import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; +import "hardhat/console.sol"; /** * @dev Context variant with ERC2771 support. */ -abstract contract ERC2771ContextUpgradeable is - Initializable, - ContextUpgradeable -{ +abstract contract ERC2771ContextUpgradeable is Initializable, ContextUpgradeable { + /// @custom:oz-upgrades-unsafe-allow state-variable-immutable address private _trustedForwarder; function __ERC2771Context_init(address trustedForwarder) internal onlyInitializing { + console.log("__ERC2771Context_init start"); + console.log("trustedForwarder", trustedForwarder); + console.log("_msgSender 1", _msgSender()); + console.log("msg.sender 1", msg.sender); __Context_init_unchained(); + console.log("_msgSender 2", _msgSender()); + console.log("msg.sender 2", msg.sender); _trustedForwarder = trustedForwarder; + console.log("_msgSender 3", _msgSender()); + console.log("msg.sender 3", msg.sender); + console.log("_trustedForwarder", _trustedForwarder); + console.log("__ERC2771Context_init end"); } - function isTrustedForwarder(address forwarder) - public - view - virtual - returns (bool) - { - return forwarder == _trustedForwarder; + function trustedForwarder() public view virtual returns (address) { + return _trustedForwarder; } - function _msgSender() - internal - view - virtual - override - returns (address sender) - { - if (isTrustedForwarder(msg.sender)) { - // The assembly code is more direct than the Solidity version using `abi.decode`. - /// @solidity memory-safe-assembly - assembly { - sender := shr(96, calldataload(sub(calldatasize(), 20))) - } + function isTrustedForwarder(address forwarder) public view virtual returns (bool) { + return forwarder == trustedForwarder(); + } + + + function _msgSender() internal view virtual override returns (address) { + console.log("=== _msgSender() start ==="); + uint256 calldataLength = msg.data.length; + uint256 contextSuffixLength = _contextSuffixLength(); + console.log("isTrustedForwarder", isTrustedForwarder(msg.sender)); + console.log("calldataLength", calldataLength); + console.log("contextSuffixLength", contextSuffixLength); + console.log("msg.data", address(bytes20(msg.data[calldataLength - contextSuffixLength:]))); + if (isTrustedForwarder(msg.sender) && calldataLength >= contextSuffixLength) { + console.log("=== _msgSender() end 1 ==="); + return address(bytes20(msg.data[calldataLength - contextSuffixLength:])); } else { + console.log("=== _msgSender() end 2 ==="); return super._msgSender(); } } - function _msgData() - internal - view - virtual - override - returns (bytes calldata) - { - if (isTrustedForwarder(msg.sender)) { - return msg.data[:msg.data.length - 20]; + function _msgData() internal view virtual override returns (bytes calldata) { + uint256 calldataLength = msg.data.length; + uint256 contextSuffixLength = _contextSuffixLength(); + if (isTrustedForwarder(msg.sender) && calldataLength >= contextSuffixLength) { + return msg.data[:calldataLength - contextSuffixLength]; } else { return super._msgData(); } } - /** - * @dev This empty reserved space is put in place to allow future versions to add new - * variables without shifting down storage in the inheritance chain. - * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps - */ - uint256[50] private __gap; + function _contextSuffixLength() internal view virtual override returns (uint256) { + return 20; + } } \ No newline at end of file diff --git a/pkgs/contract/contracts/bigbang/BigBang.sol b/pkgs/contract/contracts/bigbang/BigBang.sol index 6ddeec5..66463a0 100644 --- a/pkgs/contract/contracts/bigbang/BigBang.sol +++ b/pkgs/contract/contracts/bigbang/BigBang.sol @@ -8,6 +8,8 @@ import { HatsTimeFrameModule } from "../timeframe/HatsTimeFrameModule.sol"; import "./../ERC2771ContextUpgradeable.sol"; import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; +import "hardhat/console.sol"; + contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { IHats public Hats; @@ -48,8 +50,12 @@ contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { address _splitFactoryV2, address _fractionToken ) initializer public { + console.log("msgSender", _msgSender()); + console.log("trustedForwarder", address(_trustedForwarder)); __ERC2771Context_init(address(_trustedForwarder)); __Ownable_init(_msgSender()); + console.log("msgSender", _msgSender()); + console.log("msgSender", _msgSender()); Hats = IHats(_hatsAddress); HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory); HatsTimeFrameModule_IMPL = _hatsTimeFrameModule_IMPL; @@ -127,7 +133,7 @@ contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { return topHatId; } - function setHats(address _hats) external { + function setHats(address _hats) external onlyOwner { Hats = IHats(_hats); } @@ -151,11 +157,15 @@ contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { FractionToken = _fractionToken; } - function _msgSender() internal view override(ERC2771ContextUpgradeable, ContextUpgradeable ) returns (address sender) { + 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(ERC2771ContextUpgradeable, ContextUpgradeable) returns (uint256) { + return super._contextSuffixLength(); + } } diff --git a/pkgs/contract/contracts/fractiontoken/FractionToken.sol b/pkgs/contract/contracts/fractiontoken/FractionToken.sol index c41fa68..630dcd9 100644 --- a/pkgs/contract/contracts/fractiontoken/FractionToken.sol +++ b/pkgs/contract/contracts/fractiontoken/FractionToken.sol @@ -173,7 +173,7 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable{ internal view virtual - override(ContextUpgradeable) + override(ERC2771ContextUpgradeable, ContextUpgradeable) returns (uint256) { return super._contextSuffixLength(); diff --git a/pkgs/contract/gas-report.txt b/pkgs/contract/gas-report.txt index be3d880..96e04e6 100644 --- a/pkgs/contract/gas-report.txt +++ b/pkgs/contract/gas-report.txt @@ -7,14 +7,12 @@ ··············|··········|··············|·············|·············|···············|·············· | Deployments · · % of limit · │ ·························|··············|·············|·············|···············|·············· -| BigBang · - · - · 1248128 · 4.2 % · - │ +| BigBang · - · - · 2245511 · 7.5 % · - │ ·························|··············|·············|·············|···············|·············· -| FractionToken · - · - · 2859199 · 9.5 % · - │ +| FractionToken · - · - · 3422431 · 11.4 % · - │ ·························|··············|·············|·············|···············|·············· | Hats · - · - · 7032431 · 23.4 % · - │ ·························|··············|·············|·············|···············|·············· -| HatsModule · - · - · 754132 · 2.5 % · - │ -·························|··············|·············|·············|···············|·············· | HatsModuleFactory · - · - · 1101122 · 3.7 % · - │ ·························|··············|·············|·············|···············|·············· | HatsTimeFrameModule · - · - · 1287099 · 4.3 % · - │ diff --git a/pkgs/contract/test/BigBang.ts b/pkgs/contract/test/BigBang.ts index 1c87685..215f693 100644 --- a/pkgs/contract/test/BigBang.ts +++ b/pkgs/contract/test/BigBang.ts @@ -38,7 +38,7 @@ describe("BigBang", () => { let BigBang: BigBang; let address1: WalletClient; - + let relayer: WalletClient; let publicClient: PublicClient; before(async () => { @@ -79,13 +79,13 @@ describe("BigBang", () => { SplitsCreatorFactory = _SplitsCreatorFactory; - [address1] = await viem.getWalletClients(); + [address1, relayer] = await viem.getWalletClients(); publicClient = await viem.getPublicClient(); }); it("should deploy BigBang", async () => { const { BigBang: _BigBang } = await deployBigBang({ - trustedForwarder: address1.account?.address!, + trustedForwarder: relayer.account?.address!, hatsContractAddress: Hats.address, hatsModuleFacotryAddress: HatsModuleFactory.address, hatsTimeFrameModule_impl: HatsTimeFrameModule_IMPL.address, @@ -97,6 +97,19 @@ describe("BigBang", () => { expect(_BigBang.address).to.not.be.undefined; BigBang = _BigBang; + + const owner = await BigBang.read.owner(); + console.log("owner of BigBang", owner); + console.log("address1", address1.account?.address); + console.log("relayer", relayer.account?.address); + console.log("Hats", Hats.address); + console.log("HatsModuleFactory", HatsModuleFactory.address); + console.log("HatsTimeFrameModule_IMPL", HatsTimeFrameModule_IMPL.address); + console.log("SplitsCreatorFactory", SplitsCreatorFactory.address); + console.log("PullSplitsFactory", PullSplitsFactory.address); + console.log("FractionToken", FractionToken.address); + + expect(owner.toLowerCase()).to.equal(address1.account?.address); }); it("should execute bigbang", async () => { @@ -107,7 +120,7 @@ describe("BigBang", () => { "tophatURI", "hatterhatDetails", "hatterhatURI", - address1.account?.address!, + relayer.account?.address!, ], { account: address1.account } ); @@ -131,4 +144,162 @@ describe("BigBang", () => { } catch (error) {} } }); + + it("should set new hats address", async () => { + const oldHatsAddress = Hats.address; + const newHatsAddress = address1.account?.address!; + const ownerAccount = address1.account; + + expect((await BigBang.read.Hats()).toLowerCase()).equal(oldHatsAddress); + + await BigBang.write.setHats([newHatsAddress], { + account: ownerAccount, + }); + + expect((await BigBang.read.Hats()).toLowerCase()).equal(newHatsAddress); + + await BigBang.write.setHats([oldHatsAddress], { + account: ownerAccount, + }); + + expect((await BigBang.read.Hats()).toLowerCase()).equal(oldHatsAddress); + }); + + it("should set new hats module factory address", async () => { + const oldHatsModuleFactoryAddress = HatsModuleFactory.address; + const newHatsModuleFactoryAddress = address1.account?.address!; + const ownerAccount = address1.account; + + expect((await BigBang.read.HatsModuleFactory()).toLowerCase()).equal( + oldHatsModuleFactoryAddress + ); + + await BigBang.write.setHatsModuleFactory([newHatsModuleFactoryAddress], { + account: ownerAccount, + }); + + expect((await BigBang.read.HatsModuleFactory()).toLowerCase()).equal( + newHatsModuleFactoryAddress + ); + + await BigBang.write.setHatsModuleFactory([oldHatsModuleFactoryAddress], { + account: ownerAccount, + }); + + expect((await BigBang.read.HatsModuleFactory()).toLowerCase()).equal( + oldHatsModuleFactoryAddress + ); + }); + + it("should set new splits creator factory address", async () => { + const oldSplitsCreatorFactoryAddress = SplitsCreatorFactory.address; + const newSplitsCreatorFactoryAddress = address1.account?.address!; + const ownerAccount = address1.account; + + expect((await BigBang.read.SplitsCreatorFactory()).toLowerCase()).equal( + oldSplitsCreatorFactoryAddress + ); + + await BigBang.write.setSplitsCreatorFactory( + [newSplitsCreatorFactoryAddress], + { + account: ownerAccount, + } + ); + + expect((await BigBang.read.SplitsCreatorFactory()).toLowerCase()).equal( + newSplitsCreatorFactoryAddress + ); + + await BigBang.write.setSplitsCreatorFactory( + [oldSplitsCreatorFactoryAddress], + { + account: ownerAccount, + } + ); + + expect((await BigBang.read.SplitsCreatorFactory()).toLowerCase()).equal( + oldSplitsCreatorFactoryAddress + ); + }); + + it("should set new hats time frame module address", async () => { + const oldHatsTimeFrameModuleAddress = HatsTimeFrameModule_IMPL.address; + const newHatsTimeFrameModuleAddress = address1.account?.address!; + const ownerAccount = address1.account; + + expect((await BigBang.read.HatsTimeFrameModule_IMPL()).toLowerCase()).equal( + oldHatsTimeFrameModuleAddress + ); + + await BigBang.write.setHatsTimeFrameModuleImpl( + [newHatsTimeFrameModuleAddress], + { + account: ownerAccount, + } + ); + + expect((await BigBang.read.HatsTimeFrameModule_IMPL()).toLowerCase()).equal( + newHatsTimeFrameModuleAddress + ); + + await BigBang.write.setHatsTimeFrameModuleImpl( + [oldHatsTimeFrameModuleAddress], + { + account: ownerAccount, + } + ); + + expect((await BigBang.read.HatsTimeFrameModule_IMPL()).toLowerCase()).equal( + oldHatsTimeFrameModuleAddress + ); + }); + + it("should set new splits factory v2 address", async () => { + const oldSplitsFactoryV2Address = PullSplitsFactory.address; + const newSplitsFactoryV2Address = address1.account?.address!; + const ownerAccount = address1.account; + + expect((await BigBang.read.SplitsFactoryV2()).toLowerCase()).equal( + oldSplitsFactoryV2Address + ); + + await BigBang.write.setSplitsFactoryV2([newSplitsFactoryV2Address], { + account: ownerAccount, + }); + + expect((await BigBang.read.SplitsFactoryV2()).toLowerCase()).equal( + newSplitsFactoryV2Address + ); + + await BigBang.write.setSplitsFactoryV2([oldSplitsFactoryV2Address], { + account: ownerAccount, + }); + + expect((await BigBang.read.SplitsFactoryV2()).toLowerCase()).equal( + oldSplitsFactoryV2Address + ); + }); + + it("should set new fraction token address", async () => { + const oldFractionTokenAddress = FractionToken.address; + const newFractionTokenAddress = address1.account?.address!; + const ownerAccount = address1.account; + + expect(await BigBang.read.FractionToken()).equal(oldFractionTokenAddress); + + await BigBang.write.setFractionToken([newFractionTokenAddress], { + account: ownerAccount, + }); + + expect((await BigBang.read.FractionToken()).toLowerCase()).equal( + newFractionTokenAddress + ); + + await BigBang.write.setFractionToken([oldFractionTokenAddress], { + account: ownerAccount, + }); + + expect(await BigBang.read.FractionToken()).equal(oldFractionTokenAddress); + }); }); From 58a2b9bfc9c0be59ca235487d4a86bdd76b52502 Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:41:57 +0900 Subject: [PATCH 4/7] fix: tests and remove unnecessary logs --- .../contracts/ERC2771ContextUpgradeable.sol | 86 ++++++++++--------- pkgs/contract/contracts/bigbang/BigBang.sol | 12 +-- .../contracts/fractiontoken/FractionToken.sol | 10 --- pkgs/contract/gas-report.txt | 4 +- pkgs/contract/test/BigBang.ts | 17 +--- pkgs/contract/test/IntegrationTest.ts | 2 +- 6 files changed, 52 insertions(+), 79 deletions(-) diff --git a/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol b/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol index 2860b4e..a01b797 100644 --- a/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol +++ b/pkgs/contract/contracts/ERC2771ContextUpgradeable.sol @@ -1,71 +1,73 @@ // SPDX-License-Identifier: MIT +// OpenZeppelin Contracts (last updated v4.7.0) (metatx/ERC2771Context.sol) + pragma solidity ^0.8.24; -import {ContextUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol"; -import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; -import "hardhat/console.sol"; +import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol"; +import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; + /** * @dev Context variant with ERC2771 support. */ -abstract contract ERC2771ContextUpgradeable is Initializable, ContextUpgradeable { - /// @custom:oz-upgrades-unsafe-allow state-variable-immutable +abstract contract ERC2771ContextUpgradeable is + Initializable, + ContextUpgradeable +{ address private _trustedForwarder; function __ERC2771Context_init(address trustedForwarder) internal onlyInitializing { - console.log("__ERC2771Context_init start"); - console.log("trustedForwarder", trustedForwarder); - console.log("_msgSender 1", _msgSender()); - console.log("msg.sender 1", msg.sender); __Context_init_unchained(); - console.log("_msgSender 2", _msgSender()); - console.log("msg.sender 2", msg.sender); _trustedForwarder = trustedForwarder; - console.log("_msgSender 3", _msgSender()); - console.log("msg.sender 3", msg.sender); - console.log("_trustedForwarder", _trustedForwarder); - console.log("__ERC2771Context_init end"); - } - - function trustedForwarder() public view virtual returns (address) { - return _trustedForwarder; } - function isTrustedForwarder(address forwarder) public view virtual returns (bool) { - return forwarder == trustedForwarder(); + function isTrustedForwarder(address forwarder) + public + view + virtual + returns (bool) + { + return forwarder == _trustedForwarder; } - - function _msgSender() internal view virtual override returns (address) { - console.log("=== _msgSender() start ==="); - uint256 calldataLength = msg.data.length; - uint256 contextSuffixLength = _contextSuffixLength(); - console.log("isTrustedForwarder", isTrustedForwarder(msg.sender)); - console.log("calldataLength", calldataLength); - console.log("contextSuffixLength", contextSuffixLength); - console.log("msg.data", address(bytes20(msg.data[calldataLength - contextSuffixLength:]))); - if (isTrustedForwarder(msg.sender) && calldataLength >= contextSuffixLength) { - console.log("=== _msgSender() end 1 ==="); - return address(bytes20(msg.data[calldataLength - contextSuffixLength:])); + function _msgSender() + internal + view + virtual + override + returns (address sender) + { + if (isTrustedForwarder(msg.sender)) { + // The assembly code is more direct than the Solidity version using `abi.decode`. + /// @solidity memory-safe-assembly + assembly { + sender := shr(96, calldataload(sub(calldatasize(), 20))) + } } else { - console.log("=== _msgSender() end 2 ==="); return super._msgSender(); } } - function _msgData() internal view virtual override returns (bytes calldata) { - uint256 calldataLength = msg.data.length; - uint256 contextSuffixLength = _contextSuffixLength(); - if (isTrustedForwarder(msg.sender) && calldataLength >= contextSuffixLength) { - return msg.data[:calldataLength - contextSuffixLength]; + function _msgData() + internal + view + virtual + override + returns (bytes calldata) + { + if (isTrustedForwarder(msg.sender)) { + return msg.data[:msg.data.length - 20]; } else { return super._msgData(); } } - function _contextSuffixLength() internal view virtual override returns (uint256) { - return 20; - } + /** + * @dev This empty reserved space is put in place to allow future versions to add new + * variables without shifting down storage in the inheritance chain. + * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps + */ + uint256[50] private __gap; } \ No newline at end of file diff --git a/pkgs/contract/contracts/bigbang/BigBang.sol b/pkgs/contract/contracts/bigbang/BigBang.sol index 66463a0..053c2b5 100644 --- a/pkgs/contract/contracts/bigbang/BigBang.sol +++ b/pkgs/contract/contracts/bigbang/BigBang.sol @@ -8,8 +8,6 @@ import { HatsTimeFrameModule } from "../timeframe/HatsTimeFrameModule.sol"; import "./../ERC2771ContextUpgradeable.sol"; import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; -import "hardhat/console.sol"; - contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { IHats public Hats; @@ -50,12 +48,8 @@ contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { address _splitFactoryV2, address _fractionToken ) initializer public { - console.log("msgSender", _msgSender()); - console.log("trustedForwarder", address(_trustedForwarder)); - __ERC2771Context_init(address(_trustedForwarder)); __Ownable_init(_msgSender()); - console.log("msgSender", _msgSender()); - console.log("msgSender", _msgSender()); + __ERC2771Context_init(address(_trustedForwarder)); Hats = IHats(_hatsAddress); HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory); HatsTimeFrameModule_IMPL = _hatsTimeFrameModule_IMPL; @@ -164,8 +158,4 @@ contract BigBang is ERC2771ContextUpgradeable, OwnableUpgradeable { function _msgData() internal view override(ERC2771ContextUpgradeable, ContextUpgradeable) returns (bytes calldata) { return super._msgData(); } - - function _contextSuffixLength() internal view virtual override(ERC2771ContextUpgradeable, ContextUpgradeable) returns (uint256) { - return super._contextSuffixLength(); - } } diff --git a/pkgs/contract/contracts/fractiontoken/FractionToken.sol b/pkgs/contract/contracts/fractiontoken/FractionToken.sol index 630dcd9..05ca03f 100644 --- a/pkgs/contract/contracts/fractiontoken/FractionToken.sol +++ b/pkgs/contract/contracts/fractiontoken/FractionToken.sol @@ -168,14 +168,4 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable{ { return super._msgData(); } - - function _contextSuffixLength() - internal - view - virtual - override(ERC2771ContextUpgradeable, ContextUpgradeable) - returns (uint256) - { - return super._contextSuffixLength(); - } } diff --git a/pkgs/contract/gas-report.txt b/pkgs/contract/gas-report.txt index 96e04e6..904ca8f 100644 --- a/pkgs/contract/gas-report.txt +++ b/pkgs/contract/gas-report.txt @@ -7,9 +7,9 @@ ··············|··········|··············|·············|·············|···············|·············· | Deployments · · % of limit · │ ·························|··············|·············|·············|···············|·············· -| BigBang · - · - · 2245511 · 7.5 % · - │ +| BigBang · - · - · 1599262 · 5.3 % · - │ ·························|··············|·············|·············|···············|·············· -| FractionToken · - · - · 3422431 · 11.4 % · - │ +| FractionToken · - · - · 2859199 · 9.5 % · - │ ·························|··············|·············|·············|···············|·············· | Hats · - · - · 7032431 · 23.4 % · - │ ·························|··············|·············|·············|···············|·············· diff --git a/pkgs/contract/test/BigBang.ts b/pkgs/contract/test/BigBang.ts index 215f693..26c3b5c 100644 --- a/pkgs/contract/test/BigBang.ts +++ b/pkgs/contract/test/BigBang.ts @@ -85,7 +85,7 @@ describe("BigBang", () => { it("should deploy BigBang", async () => { const { BigBang: _BigBang } = await deployBigBang({ - trustedForwarder: relayer.account?.address!, + trustedForwarder: zeroAddress, hatsContractAddress: Hats.address, hatsModuleFacotryAddress: HatsModuleFactory.address, hatsTimeFrameModule_impl: HatsTimeFrameModule_IMPL.address, @@ -98,18 +98,9 @@ describe("BigBang", () => { BigBang = _BigBang; - const owner = await BigBang.read.owner(); - console.log("owner of BigBang", owner); - console.log("address1", address1.account?.address); - console.log("relayer", relayer.account?.address); - console.log("Hats", Hats.address); - console.log("HatsModuleFactory", HatsModuleFactory.address); - console.log("HatsTimeFrameModule_IMPL", HatsTimeFrameModule_IMPL.address); - console.log("SplitsCreatorFactory", SplitsCreatorFactory.address); - console.log("PullSplitsFactory", PullSplitsFactory.address); - console.log("FractionToken", FractionToken.address); - - expect(owner.toLowerCase()).to.equal(address1.account?.address); + expect((await BigBang.read.owner()).toLowerCase()).to.equal( + address1.account?.address + ); }); it("should execute bigbang", async () => { diff --git a/pkgs/contract/test/IntegrationTest.ts b/pkgs/contract/test/IntegrationTest.ts index b758274..973277d 100644 --- a/pkgs/contract/test/IntegrationTest.ts +++ b/pkgs/contract/test/IntegrationTest.ts @@ -109,7 +109,7 @@ describe("IntegrationTest", () => { it("should deploy BigBang", async () => { const { BigBang: _BigBang } = await deployBigBang({ - trustedForwarder: deployer.account?.address!, + trustedForwarder: zeroAddress, hatsContractAddress: Hats.address, hatsModuleFacotryAddress: HatsModuleFactory.address, hatsTimeFrameModule_impl: HatsTimeFrameModule_IMPL.address, From dfed9a88c97bf5c5644d6e8f324c3642a04eb78a Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 28 Oct 2024 20:42:29 +0900 Subject: [PATCH 5/7] chore: update test --- pkgs/contract/gas-report.txt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkgs/contract/gas-report.txt b/pkgs/contract/gas-report.txt index 904ca8f..add104c 100644 --- a/pkgs/contract/gas-report.txt +++ b/pkgs/contract/gas-report.txt @@ -13,13 +13,15 @@ ·························|··············|·············|·············|···············|·············· | Hats · - · - · 7032431 · 23.4 % · - │ ·························|··············|·············|·············|···············|·············· +| HatsModule · - · - · 754132 · 2.5 % · - │ +·························|··············|·············|·············|···············|·············· | HatsModuleFactory · - · - · 1101122 · 3.7 % · - │ ·························|··············|·············|·············|···············|·············· | HatsTimeFrameModule · - · - · 1287099 · 4.3 % · - │ ·························|··············|·············|·············|···············|·············· -| PullSplitFactory · - · - · 4535827 · 15.1 % · - │ +| PullSplitFactory · 4535815 · 4535827 · 4535825 · 15.1 % · - │ ·························|··············|·············|·············|···············|·············· -| PushSplitFactory · - · - · 4483113 · 14.9 % · - │ +| PushSplitFactory · 4483101 · 4483113 · 4483111 · 14.9 % · - │ ·························|··············|·············|·············|···············|·············· | SplitsCreator · - · - · 1487532 · 5 % · - │ ·························|··············|·············|·············|···············|·············· From 9d3423fc142022f08c4ce3745c6969a8505ed930 Mon Sep 17 00:00:00 2001 From: yawn <69970183+yawn-c111@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:29:57 +0900 Subject: [PATCH 6/7] update: BigBang upgrade test --- .../bigbang/mock/BigBang_Mock_v2.sol | 54 +++++++++++++++---- pkgs/contract/test/BigBang.ts | 4 +- 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/pkgs/contract/contracts/bigbang/mock/BigBang_Mock_v2.sol b/pkgs/contract/contracts/bigbang/mock/BigBang_Mock_v2.sol index 4b6d771..d22de53 100644 --- a/pkgs/contract/contracts/bigbang/mock/BigBang_Mock_v2.sol +++ b/pkgs/contract/contracts/bigbang/mock/BigBang_Mock_v2.sol @@ -2,15 +2,16 @@ pragma solidity ^0.8.24; import { IHats } from "../../hats/src/Interfaces/IHats.sol"; -import { IHatsModuleFactory } from "./../IHatsModuleFactory.sol"; +import { IHatsModuleFactory } from "../IHatsModuleFactory.sol"; import { ISplitsCreatorFactory } from "../../splitscreator/ISplitsCreatorFactory.sol"; import { HatsTimeFrameModule } from "../../timeframe/HatsTimeFrameModule.sol"; -import "./../../ERC2771ContextUpgradeable.sol"; +import "../../ERC2771ContextUpgradeable.sol"; +import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; /** * Upgradableになっている確認するための検証用BigBangコントラクト */ -contract BigBang_Mock_v2 is ERC2771ContextUpgradeable { +contract BigBang_Mock_v2 is ERC2771ContextUpgradeable, OwnableUpgradeable { IHats public Hats; IHatsModuleFactory public HatsModuleFactory; @@ -19,9 +20,9 @@ contract BigBang_Mock_v2 is ERC2771ContextUpgradeable { address public HatsTimeFrameModule_IMPL; - address public splitFactoryV2; + address public SplitsFactoryV2; - address public fractionToken; + address public FractionToken; event Executed( address indexed owner, @@ -31,7 +32,7 @@ contract BigBang_Mock_v2 is ERC2771ContextUpgradeable { address splitCreator ); - /* + /** * @dev Constructor to initialize the trusted forwarder. * @param _trustedForwarder Address of the trusted forwarder contract. * @param _hatsAddress Address of the hats protocol V1 contract. @@ -50,13 +51,14 @@ contract BigBang_Mock_v2 is ERC2771ContextUpgradeable { address _splitFactoryV2, address _fractionToken ) initializer public { + __Ownable_init(_msgSender()); __ERC2771Context_init(address(_trustedForwarder)); Hats = IHats(_hatsAddress); HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory); HatsTimeFrameModule_IMPL = _hatsTimeFrameModule_IMPL; SplitsCreatorFactory = ISplitsCreatorFactory(_splitsCreatorFactory); - splitFactoryV2 = _splitFactoryV2; - fractionToken = _fractionToken; + SplitsFactoryV2 = _splitFactoryV2; + FractionToken = _fractionToken; } /** @@ -117,9 +119,9 @@ contract BigBang_Mock_v2 is ERC2771ContextUpgradeable { .createSplitCreatorDeterministic( topHatId, _trustedForwarder, - splitFactoryV2, + SplitsFactoryV2, hatsTimeFrameModule, - fractionToken, + FractionToken, keccak256(abi.encodePacked(topHatId)) ); @@ -128,6 +130,38 @@ contract BigBang_Mock_v2 is ERC2771ContextUpgradeable { return topHatId; } + function setHats(address _hats) external onlyOwner { + Hats = IHats(_hats); + } + + function setHatsModuleFactory(address _hatsModuleFactory) external onlyOwner { + HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory); + } + + function setSplitsCreatorFactory(address _splitsCreatorFactory) external onlyOwner { + SplitsCreatorFactory = ISplitsCreatorFactory(_splitsCreatorFactory); + } + + function setHatsTimeFrameModuleImpl(address _hatsTimeFrameModuleImpl) external onlyOwner { + HatsTimeFrameModule_IMPL = _hatsTimeFrameModuleImpl; + } + + function setSplitsFactoryV2(address _splitsFactoryV2) external onlyOwner { + SplitsFactoryV2 = _splitsFactoryV2; + } + + function setFractionToken(address _fractionToken) external onlyOwner { + FractionToken = _fractionToken; + } + + 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(); + } + /** * 検証用に追加した関数 */ diff --git a/pkgs/contract/test/BigBang.ts b/pkgs/contract/test/BigBang.ts index 3ac9c08..dc30b5d 100644 --- a/pkgs/contract/test/BigBang.ts +++ b/pkgs/contract/test/BigBang.ts @@ -188,7 +188,7 @@ describe("BigBang", () => { const newSplitsCreatorFactoryAddress = address1.account?.address!; const ownerAccount = address1.account; - expect((await BigBang.read.SplitsCreatorFactory()).toLowerCase()).equal( + expect(await BigBang.read.SplitsCreatorFactory()).equal( oldSplitsCreatorFactoryAddress ); @@ -210,7 +210,7 @@ describe("BigBang", () => { } ); - expect((await BigBang.read.SplitsCreatorFactory()).toLowerCase()).equal( + expect(await BigBang.read.SplitsCreatorFactory()).equal( oldSplitsCreatorFactoryAddress ); }); From dcb5b82e303b204a0ee89314f6a7746275673f46 Mon Sep 17 00:00:00 2001 From: yu23ki14 Date: Fri, 22 Nov 2024 11:26:18 +0900 Subject: [PATCH 7/7] tmp --- .../contracts/fractiontoken/FractionToken.sol | 21 +++++-------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/pkgs/contract/contracts/fractiontoken/FractionToken.sol b/pkgs/contract/contracts/fractiontoken/FractionToken.sol index 86206f5..cd6a282 100644 --- a/pkgs/contract/contracts/fractiontoken/FractionToken.sol +++ b/pkgs/contract/contracts/fractiontoken/FractionToken.sol @@ -24,10 +24,7 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable { TOKEN_SUPPLY = _tokenSupply; } - function mintInitialSupply( - uint256 hatId, - address account - ) public { + function mintInitialSupply(uint256 hatId, address account) public { require( _hasHatRole(account, hatId), "This account does not have the role" @@ -47,23 +44,17 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable { _mint(account, tokenId, TOKEN_SUPPLY, ""); - if (!_containsRecipient(tokenId, account)) { - tokenRecipients[tokenId].push(account); - } + tokenRecipients[tokenId].push(account); } - function mint( - uint256 hatId, - address account, - uint256 amount - ) public { + 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" @@ -152,9 +143,7 @@ contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable { return balance > 0; } - function _hasHatAuthority( - uint256 hatId - ) private view returns (bool) { + function _hasHatAuthority(uint256 hatId) private view returns (bool) { uint32 hatLevel = hatsContract.getHatLevel(hatId); uint256 parentHatId = hatsContract.getAdminAtLevel(hatId, hatLevel - 1);