Skip to content

Commit c4f3208

Browse files
committed
init via carbyne
1 parent bebc4ff commit c4f3208

21 files changed

+11746
-0
lines changed

.env.example

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
RINKEBY_URL="https://rinkeby.infura.io/v3/<YOUR INFURA KEY>"
2+
ACCOUNTS=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb

.eslintignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules
2+
artifacts
3+
cache
4+
coverage

.eslintrc.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
env: {
3+
browser: false,
4+
es2021: true,
5+
mocha: true,
6+
node: true,
7+
},
8+
plugins: ['@typescript-eslint'],
9+
extends: ['standard', 'plugin:prettier/recommended', 'plugin:node/recommended'],
10+
parser: '@typescript-eslint/parser',
11+
parserOptions: {
12+
ecmaVersion: 12,
13+
},
14+
rules: {
15+
'node/no-unsupported-features/es-syntax': ['error', { ignores: ['modules'] }],
16+
'node/no-missing-import': [
17+
'error',
18+
{
19+
allowModules: [],
20+
resolvePaths: ['./typechain'],
21+
tryExtensions: ['.js', '.json', '.node', '.ts'],
22+
},
23+
],
24+
},
25+
};

.gitignore

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
node_modules
2+
.env
3+
coverage
4+
coverage.json
5+
typechain
6+
7+
#Hardhat files
8+
cache
9+
artifacts
10+
.openzeppelin
11+
flatten

.husky/pre-commit

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/sh
2+
. "$(dirname "$0")/_/husky.sh"
3+
4+
npm run lint
5+
npm run test

.prettierignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules
2+
artifacts
3+
cache
4+
coverage*
5+
gasReporterOutput.json
6+
typechain

.prettierrc.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = {
2+
// 128 character each line
3+
printWidth: 128,
4+
// use 2 Spaces for indentation
5+
tabWidth: 2,
6+
// don't use tab
7+
useTabs: false,
8+
// semicolon required at end of line
9+
semi: true,
10+
// use single quote
11+
singleQuote: true,
12+
// The object's key is quoted only when necessary
13+
quoteProps: 'as-needed',
14+
// comma is required at the end
15+
trailingComma: 'all',
16+
// spaces are required at the beginning and end of the curly brackets
17+
bracketSpacing: true,
18+
// Arrow functions, when there is only one parameter, also need parentheses
19+
arrowParens: 'always',
20+
// The scope of each file format is the entire contents of the file
21+
rangeStart: 0,
22+
rangeEnd: Infinity,
23+
// No need to write @prettier at the beginning of the file
24+
requirePragma: false,
25+
// No need to automatically insert @prettier at the beginning of the file
26+
insertPragma: false,
27+
// Use default wrapping standard
28+
proseWrap: 'preserve',
29+
// Decide whether to wrap html according to the display style
30+
htmlWhitespaceSensitivity: 'css',
31+
// Line breaks use lf
32+
endOfLine: 'lf',
33+
// Format embedded content
34+
embeddedLanguageFormatting: 'auto',
35+
};

.solhint.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"extends": "solhint:recommended",
3+
"rules": {
4+
"compiler-version": ["error", "^0.8.0"],
5+
"func-visibility": [
6+
"warn",
7+
{
8+
"ignoreConstructors": true
9+
}
10+
],
11+
"quotes": ["error", "single"]
12+
}
13+
}

.solhintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
# Carbyne General Template
2+
3+
## Set up
4+
5+
```
6+
$ npm install prepare
7+
```
8+
9+
Copy `.env.example` to `.env`
10+
11+
```shell
12+
$ cp .env.example .env
13+
```
14+
15+
Config environment variables. By default, there is a configuration for rinkeby networks RPC url and private key config. You can fill in multiple private keys and separate them with commas.
16+
17+
```
18+
RINKEBY_URL="https://rinkeby.infura.io/v3/<YOUR INFURA KEY>"
19+
ACCOUNTS=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
20+
```
21+
22+
## Compile contract
23+
24+
Compile contracts
25+
26+
```shell
27+
$ npm run compile
28+
```
29+
30+
## Lint
31+
32+
Use `cspell`, `prettier`, `eslint` to format and check code format.
33+
34+
```shell
35+
$ npm run lint
36+
```
37+
38+
## Test
39+
40+
Run all tests
41+
42+
```shell
43+
$ npm run test
44+
```
45+
46+
Run all tests and get estimating gas used in each contract
47+
48+
```shell
49+
$ npm run test:analysis
50+
```
51+
52+
Get Test coverage
53+
54+
```shell
55+
$ npm run coverage
56+
```
57+
58+
Test coverage will show in the terminal(stdout) and HTML files are generated in the `coverage` folder so that you can also open them in the browser to view.
59+
60+
## Smart flatten
61+
62+
Flatten all smart contracts in the `contracts` folder and generate files to the `flatten` folder according to their relative path, and all flattened contracts are ready for being verified. It helps a lot usually.
63+
64+
```shell
65+
$ npm run flat
66+
```
67+
68+
## Run some script
69+
70+
Check before you run it!
71+
72+
```
73+
$ npm hardhat run scripts/xxxx.ts
74+
```
75+
76+
# Directory Structure
77+
78+
```
79+
.
80+
├── contracts/
81+
├── cspell.json
82+
├── .eslintignore
83+
├── .eslintrc.js
84+
├── flat.ts
85+
├── .git
86+
├── .gitignore
87+
├── hardhat.config.ts
88+
├── .husky/
89+
├── node_modules
90+
├── package.json
91+
├── package-lock.json
92+
├── .prettierignore
93+
├── .prettierrc.js
94+
├── scripts/
95+
├── .solhintignore
96+
├── .solhint.json
97+
├── test/
98+
└── tsconfig.json
99+
100+
```
101+
102+
`contracts` is where you write your own smart contracts, there are some common subfolders you can refer to, such as `libraries` for shared libraries, and `tests` for contracts not written by you but used in test scripts. You can also make some subfolders for different features.
103+
104+
`scripts` is where you write some one-time scripts, such as deploying contracts to a specific chain or calling a contract to change some data. As it may write to the real blockchain, be careful before running it!
105+
106+
`tests` is where you write test scripts to test your contracts thoroughly. It uses hardhat framework to run tests and there is a temporary chain for you to read and write arbitrarily. For more, you can refer to hardhat [docs](https://hardhat.org/getting-started/)
107+
108+
`.husky` is a folder for git hook. By default, there is a pre-commit hook that runs `npm run lint` and `npm run test` before you make a git commit.
109+
110+
`flat.ts` is a custom file to run smart flatten as hardhat's origin command `flatten` doesn't work well.
111+
112+
`hardhat.config.ts` is the hardhat config file, you can use more environment variables after configuring them in `.env`.
113+
114+
`tsconfig.json` is the file for configuring typescript compiling options.
115+
116+
`.eslintrc.js` and `.eslintignore` for eslint config.
117+
118+
`.prettierignore` and `.prettierrc.js` for prettier config.
119+
120+
`.solhint.json` and `.solhintignore` for solhint config.

contracts/Campaign.sol

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//SPDX-License-Identifier: GPL-3.0-or-later
2+
3+
pragma solidity 0.8.15;
4+
5+
import '@openzeppelin/contracts/token/ERC721/IERC721.sol';
6+
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
7+
import '@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol';
8+
import '@openzeppelin/contracts/access/Ownable.sol';
9+
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
10+
import './interface/ICampaign.sol';
11+
12+
contract Campaign is ICampaign, Ownable, ERC721 {
13+
using SafeERC20 for IERC20;
14+
15+
IERC20 public immutable targetToken;
16+
uint256 public immutable requiredAmount;
17+
18+
mapping(address => bool) public registry;
19+
20+
constructor(
21+
IERC20 token_,
22+
uint256 amount_,
23+
string memory name_,
24+
string memory symbol_
25+
) ERC721(name_, symbol_) {
26+
require(address(token_) != address(0), 'Campaign: invalid token');
27+
require(amount_ != 0, 'Campaign: invalid amount');
28+
targetToken = token_;
29+
requiredAmount = amount_;
30+
}
31+
32+
function register() public override returns (bool) {
33+
IERC20(targetToken).safeTransferFrom(msg.sender, address(this), requiredAmount);
34+
35+
registry[msg.sender] = true;
36+
37+
return true;
38+
}
39+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//SPDX-License-Identifier: GPL-3.0-or-later
2+
pragma solidity 0.8.15;
3+
4+
import '@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol';
5+
import '@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol';
6+
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
7+
8+
import './Campaign.sol';
9+
10+
import 'hardhat/console.sol';
11+
import './interface/ICampaignFactory.sol';
12+
13+
contract CampaignFactoryUpgradable is ICampaignFactory, UUPSUpgradeable, OwnableUpgradeable {
14+
// White list mapping
15+
mapping(address => bool) public whiteUsers;
16+
17+
// White list token mapping
18+
mapping(IERC20 => bool) public whiteTokens;
19+
20+
function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
21+
22+
function initialize() public initializer {
23+
__Ownable_init_unchained();
24+
}
25+
26+
function modifyWhiteUser(address user, bool status) public onlyOwner {
27+
whiteUsers[user] = status;
28+
emit EvWhiteUserSet(user, status);
29+
}
30+
31+
function modifyWhiteToken(IERC20 token, bool status) public onlyOwner {
32+
whiteTokens[token] = status;
33+
emit EvWhiteTokenSet(token, status);
34+
}
35+
36+
function createCampaign(
37+
IERC20 token,
38+
uint256 amount,
39+
string memory name,
40+
string memory symbol
41+
) public override onlyWhiteUser onlyWhiteToken(token) returns (bool) {
42+
Campaign cam = new Campaign(token, amount, name, symbol);
43+
emit EvCampaignCreated(msg.sender, address(cam));
44+
return true;
45+
}
46+
47+
modifier onlyWhiteUser() {
48+
require(whiteUsers[msg.sender], 'CampaignFactory: not whitelist');
49+
_;
50+
}
51+
52+
modifier onlyWhiteToken(IERC20 token) {
53+
require(whiteTokens[token], 'CampaignFactory: not whitelist');
54+
_;
55+
}
56+
}

contracts/interface/ICampaign.sol

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//SPDX-License-Identifier: GPL-3.0-or-later
2+
pragma solidity 0.8.15;
3+
4+
interface ICampaign {
5+
function register() external returns (bool);
6+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//SPDX-License-Identifier: GPL-3.0-or-later
2+
pragma solidity 0.8.15;
3+
4+
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';
5+
6+
interface ICampaignFactory {
7+
function createCampaign(
8+
IERC20 token,
9+
uint256 amount,
10+
string memory name,
11+
string memory symbol
12+
) external returns (bool success);
13+
14+
event EvCampaignCreated(address indexed promoter, address indexed campaignAddress);
15+
event EvWhiteUserSet(address indexed user, bool status);
16+
event EvWhiteTokenSet(IERC20 indexed token, bool status);
17+
}

0 commit comments

Comments
 (0)