Skip to content

Commit

Permalink
Merge pull request #153 from hackdays-io/add_upgradable
Browse files Browse the repository at this point in the history
BigBangコントラクトをupgradableに対応させました。
  • Loading branch information
yu23ki14 authored Oct 21, 2024
2 parents 49000ce + 4cc4b7a commit 2517f04
Show file tree
Hide file tree
Showing 15 changed files with 766 additions and 401 deletions.
73 changes: 73 additions & 0 deletions pkgs/contract/contracts/ERC2771ContextUpgradeable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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";

/**
* @dev Context variant with ERC2771 support.
*/
abstract contract ERC2771ContextUpgradeable is
Initializable,
ContextUpgradeable
{
address private _trustedForwarder;

function __ERC2771Context_init(address trustedForwarder)
internal
onlyInitializing
{
__Context_init_unchained();
_trustedForwarder = trustedForwarder;
}

function isTrustedForwarder(address forwarder)
public
view
virtual
returns (bool)
{
return forwarder == _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)))
}
} else {
return super._msgSender();
}
}

function _msgData()
internal
view
virtual
override
returns (bytes calldata)
{
if (isTrustedForwarder(msg.sender)) {
return msg.data[:msg.data.length - 20];
} 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;
}
31 changes: 6 additions & 25 deletions pkgs/contract/contracts/bigbang/BigBang.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { IHats } from "../hats/src/Interfaces/IHats.sol";
import { IHatsModuleFactory } from "./IHatsModuleFactory.sol";
import { ISplitsCreatorFactory } from "../splitscreator/ISplitsCreatorFactory.sol";
import { HatsTimeFrameModule } from "../timeframe/HatsTimeFrameModule.sol";
import "@openzeppelin/contracts/metatx/ERC2771Context.sol";
import "./../ERC2771ContextUpgradeable.sol";

contract BigBang is ERC2771Context {
contract BigBang is ERC2771ContextUpgradeable {
IHats public Hats;

IHatsModuleFactory public HatsModuleFactory;
Expand All @@ -28,7 +28,7 @@ contract BigBang is ERC2771Context {
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.
Expand All @@ -38,15 +38,16 @@ contract BigBang is ERC2771Context {
* @param _splitFactoryV2 Address of the split factory V2 contract.
* @param _fractionToken Address of the fraction token contract.
*/
constructor(
function initialize (
address _trustedForwarder,
address _hatsAddress,
address _hatsModuleFactory,
address _hatsTimeFrameModule_IMPL,
address _splitsCreatorFactory,
address _splitFactoryV2,
address _fractionToken
) ERC2771Context(_trustedForwarder) {
) initializer public {
__ERC2771Context_init(address(_trustedForwarder));
Hats = IHats(_hatsAddress);
HatsModuleFactory = IHatsModuleFactory(_hatsModuleFactory);
HatsTimeFrameModule_IMPL = _hatsTimeFrameModule_IMPL;
Expand Down Expand Up @@ -123,24 +124,4 @@ contract BigBang is ERC2771Context {

return topHatId;
}

// Override _msgSender to use the context from ERC2771Context.
function _msgSender()
internal
view
override(ERC2771Context)
returns (address)
{
return ERC2771Context._msgSender();
}

// Override _msgData to use the context from ERC2771Context.
function _msgData()
internal
view
override(ERC2771Context)
returns (bytes calldata)
{
return ERC2771Context._msgData();
}
}
29 changes: 16 additions & 13 deletions pkgs/contract/contracts/fractiontoken/FractionToken.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

import { ERC1155 } from "@openzeppelin/contracts/token/ERC1155/ERC1155.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 { ERC2771Context, Context } from "@openzeppelin/contracts/metatx/ERC2771Context.sol";
import "./../ERC2771ContextUpgradeable.sol";

contract FractionToken is ERC1155, ERC2771Context {
uint256 public immutable TOKEN_SUPPLY;
contract FractionToken is ERC1155Upgradeable, ERC2771ContextUpgradeable{
uint256 public TOKEN_SUPPLY;

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

IHats private hatsContract;

constructor(
function initialize(
string memory _uri,
uint256 _tokenSupply,
address _hatsAddress,
address _trustedForwarderAddress
) ERC1155(_uri) ERC2771Context(_trustedForwarderAddress) {
) initializer public {
__ERC1155_init(_uri);
__ERC2771Context_init(address(_trustedForwarderAddress));
hatsContract = IHats(_hatsAddress);
TOKEN_SUPPLY = _tokenSupply;
}
Expand Down Expand Up @@ -144,35 +147,35 @@ contract FractionToken is ERC1155, ERC2771Context {

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

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

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

function _contextSuffixLength()
internal
view
virtual
override(ERC2771Context, Context)
override(ContextUpgradeable)
returns (uint256)
{
return ERC2771Context._contextSuffixLength();
return super._contextSuffixLength();
}
}
70 changes: 39 additions & 31 deletions pkgs/contract/gas-report.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,39 @@
·------------------------|----------------------------|-------------|-----------------------------·
| 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 · 1092017 · 1092029 · 1092023 · 3.6 % · - │
·························|··············|·············|·············|···············|··············
| FractionToken · - · - · 2573797 · 8.6 % · - │
·························|··············|·············|·············|···············|··············
| Hats · - · - · 7032431 · 23.4 % · - │
·························|··············|·············|·············|···············|··············
| HatsModule · - · - · 754132 · 2.5 % · - │
·························|··············|·············|·············|···············|··············
| HatsModuleFactory · - · - · 1101122 · 3.7 % · - │
·························|··············|·············|·············|···············|··············
| HatsTimeFrameModule · - · - · 1287099 · 4.3 % · - │
·························|··············|·············|·············|···············|··············
| PullSplitFactory · - · - · 4535827 · 15.1 % · - │
·························|··············|·············|·············|···············|··············
| PushSplitFactory · - · - · 4483113 · 14.9 % · - │
·························|··············|·············|·············|···············|··············
| SplitsCreator · - · - · 1487532 · 5 % · - │
·························|··············|·············|·············|···············|··············
| SplitsCreatorFactory · - · - · 526836 · 1.8 % · - │
·························|··············|·············|·············|···············|··············
| SplitsWarehouse · - · - · 3934655 · 13.1 % · - │
·------------------------|--------------|-------------|-------------|---------------|-------------·
·--------------------------------------|----------------------------|-------------|-----------------------------·
| Solc version: 0.8.24 · Optimizer enabled: false · Runs: 200 · Block limit: 30000000 gas │
·······································|····························|·············|······························
| Methods │
··················|····················|··············|·············|·············|···············|··············
| Contract · Method · Min · Max · Avg · # calls · usd (avg) │
··················|····················|··············|·············|·············|···············|··············
| BigBang · bigbang · - · - · 603669 · 2 · - │
··················|····················|··············|·············|·············|···············|··············
| FractionToken · burn · 41544 · 55511 · 47442 · 4 · - │
··················|····················|··············|·············|·············|···············|··············
| FractionToken · mint · 135046 · 137558 · 136717 · 9 · - │
··················|····················|··············|·············|·············|···············|··············
| FractionToken · safeTransferFrom · 106335 · 106347 · 106339 · 3 · - │
··················|····················|··············|·············|·············|···············|··············
| Deployments · · % of limit · │
·······································|··············|·············|·············|···············|··············
| BigBang · - · - · 1248128 · 4.2 % · - │
·······································|··············|·············|·············|···············|··············
| FractionToken · - · - · 2859199 · 9.5 % · - │
·······································|··············|·············|·············|···············|··············
| Hats · - · - · 7032431 · 23.4 % · - │
·······································|··············|·············|·············|···············|··············
| HatsModule · - · - · 754132 · 2.5 % · - │
·······································|··············|·············|·············|···············|··············
| HatsModuleFactory · - · - · 1101122 · 3.7 % · - │
·······································|··············|·············|·············|···············|··············
| HatsTimeFrameModule · - · - · 1287099 · 4.3 % · - │
·······································|··············|·············|·············|···············|··············
| PullSplitFactory · - · - · 4535827 · 15.1 % · - │
·······································|··············|·············|·············|···············|··············
| PushSplitFactory · - · - · 4483113 · 14.9 % · - │
·······································|··············|·············|·············|···············|··············
| SplitsCreator · - · - · 1487532 · 5 % · - │
·······································|··············|·············|·············|···············|··············
| SplitsCreatorFactory · - · - · 526836 · 1.8 % · - │
·······································|··············|·············|·············|···············|··············
| SplitsWarehouse · - · - · 3934655 · 13.1 % · - │
·--------------------------------------|--------------|-------------|-------------|---------------|-------------·
5 changes: 5 additions & 0 deletions pkgs/contract/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import "@nomicfoundation/hardhat-ethers";
import "@nomicfoundation/hardhat-toolbox-viem";
import "@nomicfoundation/hardhat-viem";
import "@openzeppelin/hardhat-upgrades";
import * as dotenv from "dotenv";
import fs from "fs";
import "hardhat-gas-reporter";
Expand Down Expand Up @@ -37,6 +39,9 @@ const config: HardhatUserConfig = {
version: "0.8.24",
settings: {
viaIR: true,
optimizer: {
runs: 200,
},
},
},
],
Expand Down
27 changes: 26 additions & 1 deletion pkgs/contract/helpers/deploy/BigBang.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { viem } from "hardhat";
import { ethers, upgrades } from "hardhat";
import { Address } from "viem";

export type BigBang = Awaited<ReturnType<typeof deployBigBang>>["BigBang"];
Expand All @@ -12,6 +12,7 @@ export const deployBigBang = async (params: {
splitsFactoryV2Address: Address;
fractionTokenAddress: Address;
}) => {
/*
const BigBang = await viem.deployContract("BigBang", [
params.trustedForwarder,
params.hatsContractAddress,
Expand All @@ -21,6 +22,30 @@ export const deployBigBang = async (params: {
params.splitsFactoryV2Address,
params.fractionTokenAddress,
]);
*/

const bigBang = await ethers.getContractFactory("BigBang");
const BigBang = await upgrades.deployProxy(
bigBang,
[
params.trustedForwarder,
params.hatsContractAddress,
params.hatsModuleFacotryAddress,
params.hatsTimeFrameModule_impl,
params.splitsCreatorFactoryAddress,
params.splitsFactoryV2Address,
params.fractionTokenAddress,
],
{
initializer: "initialize",
}
);

const tx = await BigBang.deploymentTransaction();
await tx?.wait();

// console.log("BigBang deployed at", tx);
// console.log("BigBang deployed at", BigBang.target);

return { BigBang };
};
19 changes: 12 additions & 7 deletions pkgs/contract/helpers/deploy/FractionToken.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { viem } from "hardhat";
import { ethers, upgrades } from "hardhat";
import { Address } from "viem";

export type FractionToken = Awaited<
Expand All @@ -11,12 +11,17 @@ export const deployFractionToken = async (
hatsContractAddress: Address,
forwarderAddress: Address
) => {
const FractionToken = await viem.deployContract("FractionToken", [
uri,
tokenSupply,
hatsContractAddress,
forwarderAddress,
]);
const fractionToken = await ethers.getContractFactory("FractionToken");
const FractionToken = await upgrades.deployProxy(
fractionToken,
[uri, tokenSupply, hatsContractAddress, forwarderAddress],
{
initializer: "initialize",
}
);

const tx = await FractionToken.deploymentTransaction();
await tx?.wait();

return { FractionToken };
};
Loading

0 comments on commit 2517f04

Please sign in to comment.