Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgradable #18

Merged
merged 5 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ coverage.json
typechain
.DS_Store
dist
.openzeppelin

# Hardhat files
cache
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

The main storage contract. Heavily influenced by the [Big Whale Labs repos](https://github.com/BigWhaleLabs).

# Deployments

| Contract | Address |
| --------------------------- | -------------------------------------------- |
| Proxy | `0xff0bd519DED90acd89B5d27Bb6Db722C0b696016` |
| Implementation(OBSSStorage) | `0xe56EcbBB9DcCFf16EAC2057Bc5A6472a696c1008` |
| Proxy Admin | `0x50B76E93c14F7C2DBc50608d458Bb5B5fBf3eb8E` |

## Usage

1. Clone the repository with `git clone [email protected]:backmeupplz/obss-storage-contract.git`
Expand Down
12 changes: 7 additions & 5 deletions contracts/OBSSStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/utils/Counters.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@opengsn/contracts/src/ERC2771Recipient.sol";
import "@big-whale-labs/versioned-contract/contracts/Versioned.sol";
import "@big-whale-labs/ketl-allow-map-contract/contracts/KetlAllowMap.sol";
import "@openzeppelin/contracts/utils/Context.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

/**
* @title OBSSStorage
* @dev This contract is used to store the data of the OBSS contract
*/
contract OBSSStorage is Ownable, ERC2771Recipient, Versioned {
contract OBSSStorage is Initializable, Context, ERC2771Recipient {
using Counters for Counters.Counter;

// IPFS cid represented in a more efficient way
Expand All @@ -35,6 +35,7 @@ contract OBSSStorage is Ownable, ERC2771Recipient, Versioned {
}

/* State */
string public version;
// Posts
mapping(uint256 => Post) public posts;
// Ketl allow map
Expand Down Expand Up @@ -95,12 +96,13 @@ contract OBSSStorage is Ownable, ERC2771Recipient, Versioned {
_;
}

constructor(
// Constructor
function initialize(
address _forwarder,
string memory _version,
address _vcAllowMap,
address _founderAllowMap
) Versioned(_version) {
) public initializer {
vcAllowMap = KetlAllowMap(_vcAllowMap);
founderAllowMap = KetlAllowMap(_founderAllowMap);
_setTrustedForwarder(_forwarder);
Expand Down
3 changes: 2 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import '@nomiclabs/hardhat-etherscan'
import '@nomiclabs/hardhat-waffle'
import '@openzeppelin/hardhat-upgrades'
import '@typechain/hardhat'
import 'hardhat-gas-reporter'
import 'solidity-coverage'
import * as dotenv from 'dotenv'
import { ETH_RPC as FALLBACK_ETH_RPC } from '@big-whale-labs/constants'
import { HardhatUserConfig } from 'hardhat/config'
import { cleanEnv, str, testOnly } from 'envalid'
import { ETH_RPC as FALLBACK_ETH_RPC } from '@big-whale-labs/constants'

dotenv.config()

Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"test": "NODE_ENV=test yarn hardhat test",
"coverage": "yarn hardhat compile --force && node --max_old_space_size=6144 node_modules/.bin/hardhat coverage --testfiles 'test/**/*.ts' --show-stack-traces",
"deploy": "yarn hardhat run --network deploy scripts/deploy.ts",
"update": "yarn hardhat run --network deploy scripts/update.ts",
"eth-lint": "yarn solhint 'contracts/**/*.sol'",
"lint": "prettier --check . && eslint --max-warnings 0 --ext ts scripts test && yarn eth-lint",
"prettify": "prettier --write .",
Expand Down Expand Up @@ -77,6 +78,8 @@
"dependencies": {
"@big-whale-labs/constants": "^0.1.39",
"@big-whale-labs/ketl-allow-map-contract": "^0.0.3",
"@openzeppelin/contracts-upgradeable": "^4.8.2",
"@openzeppelin/hardhat-upgrades": "^1.22.1",
"@zk-kit/incremental-merkle-tree": "^1.0.0",
"@zk-kit/incremental-merkle-tree.sol": "^1.3.3",
"circomlibjs": "^0.1.7"
Expand Down
30 changes: 20 additions & 10 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GSN_MUMBAI_FORWARDER_CONTRACT_ADDRESS } from '@big-whale-labs/constants'
import { ethers, run } from 'hardhat'
import { ethers, run, upgrades } from 'hardhat'
import { utils } from 'ethers'
import { version } from '../package.json'
import prompt from 'prompt'
Expand Down Expand Up @@ -28,10 +28,12 @@ async function main() {
vcAllowMap: {
required: true,
pattern: regexes.ethereumAddress,
default: '0xe8c7754340b9f0efe49dfe0f9a47f8f137f70477',
},
founderAllowMap: {
required: true,
pattern: regexes.ethereumAddress,
default: '0x91002bd44b9620866693fd8e03438e69e01563ee',
},
},
})
Expand All @@ -57,8 +59,10 @@ async function main() {

const contractName = 'OBSSStorage'
console.log(`Deploying ${contractName}...`)
const Contract = await ethers.getContractFactory(contractName)
const contract = await Contract.deploy(...constructorArguments)
const factory = await ethers.getContractFactory(contractName)
const contract = await upgrades.deployProxy(factory, constructorArguments, {
initializer: 'initialize',
})

console.log(
'Deploy tx gas price:',
Expand All @@ -69,18 +73,24 @@ async function main() {
utils.formatEther(contract.deployTransaction.gasLimit)
)
await contract.deployed()
const address = contract.address

console.log('Contract deployed to:', address)
const implementationAddress = await upgrades.erc1967.getImplementationAddress(
contract.address
)
const adminAddress = await upgrades.erc1967.getAdminAddress(contract.address)

console.log('OBSSStorage Proxy address:', contract.address)
console.log('Implementation address:', implementationAddress)
console.log('Admin address:', adminAddress)

console.log('Wait for 1 minute to make sure blockchain is updated')
await new Promise((resolve) => setTimeout(resolve, 60 * 1000))
await new Promise((resolve) => setTimeout(resolve, 15 * 1000))

// Try to verify the contract on Etherscan
console.log('Verifying contract on Etherscan')
try {
await run('verify:verify', {
address,
constructorArguments,
address: implementationAddress,
})
} catch (err) {
console.log(
Expand All @@ -91,12 +101,12 @@ async function main() {

// Print out the information
console.log(`${contractName} deployed and verified on Etherscan!`)
console.log('Contract address:', address)
console.log('Contract address:', contract.address)
console.log(
'Etherscan URL:',
`https://${
chainName !== 'polygon' ? `${chainName}.` : ''
}polygonscan.com/address/${address}`
}polygonscan.com/address/${contract.address}`
)
}

Expand Down
27 changes: 27 additions & 0 deletions scripts/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ethers, upgrades } from 'hardhat'
import prompt from 'prompt'

const regexes = {
ethereumAddress: /^0x[a-fA-F0-9]{40}$/,
}

async function main() {
const OBSSStorage = await ethers.getContractFactory('OBSSStorage')
const { proxyAddress } = await prompt.get({
properties: {
proxyAddress: {
required: true,
message: 'Proxy address',
pattern: regexes.ethereumAddress,
},
},
})
console.log('Upgrading OBSSStorage...')
await upgrades.upgradeProxy(proxyAddress as string, OBSSStorage)
console.log('OBSSStorage upgraded')
}

main().catch((error) => {
console.error(error)
process.exitCode = 1
})
36 changes: 24 additions & 12 deletions test/OBSSStorage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigNumber } from 'ethers'
import { ethers } from 'hardhat'
import { ethers, upgrades } from 'hardhat'
import { expect } from 'chai'
import { getFakeAllowMapContract } from './utils'

Expand All @@ -22,23 +22,35 @@ describe('OBSSStorage contract tests', () => {
describe('Constructor', function () {
it('should deploy the contract with the correct fields', async function () {
const version = 'v0.0.1'
const contract = await this.factory.deploy(
zeroAddress,
version,
zeroAddress,
zeroAddress
const contract = await upgrades.deployProxy(
this.factory,
[
zeroAddress,
version,
this.fakeAllowMapContract.address,
this.fakeAllowMapContract.address,
],
{
initializer: 'initialize',
}
)
expect(await contract.version()).to.equal(version)
})
})
describe('OBSSStorage', function () {
beforeEach(async function () {
const version = 'v0.0.1'
this.contract = await this.factory.deploy(
zeroAddress,
version,
this.fakeAllowMapContract.address,
this.fakeAllowMapContract.address
this.contract = await upgrades.deployProxy(
this.factory,
[
zeroAddress,
version,
this.fakeAllowMapContract.address,
this.fakeAllowMapContract.address,
],
{
initializer: 'initialize',
}
)
await this.fakeAllowMapContract.mock.isAddressAllowed.returns(true)
})
Expand Down Expand Up @@ -68,7 +80,7 @@ describe('OBSSStorage contract tests', () => {
await this.contract.addFeedPost(0, MOCK_CID)
// Add reaction
await this.contract.addReaction(0, 1)
expect(await this.contract.removeReaction(0, 0))
expect(await this.contract.removeReaction(0, 1))
})
})
})
3 changes: 2 additions & 1 deletion test/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Contract } from 'ethers'
import { MockContract } from 'ethereum-waffle'
import type { OBSSStorage, OBSSStorage__factory } from '../typechain'
import type { SignerWithAddress } from '@nomiclabs/hardhat-ethers/dist/src/signer-with-address'
Expand All @@ -6,7 +7,7 @@ declare module 'mocha' {
export interface Context {
// Facoriries for contracts
factory: OBSSStorage__factory
contract: OBSSStorage
contract: OBSSStorage | Contract
// Mock contracts
fakeAllowMapContract: MockContract
// Signers
Expand Down
Loading