@openzeppelin/wizard 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +82 -0
- package/dist/add-pausable.d.ts +4 -0
- package/dist/add-pausable.d.ts.map +1 -0
- package/dist/add-pausable.js +30 -0
- package/dist/add-pausable.js.map +1 -0
- package/dist/api.d.ts +24 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +24 -0
- package/dist/api.js.map +1 -0
- package/dist/build-generic.d.ts +21 -0
- package/dist/build-generic.d.ts.map +1 -0
- package/dist/build-generic.js +24 -0
- package/dist/build-generic.js.map +1 -0
- package/dist/common-functions.d.ts +3 -0
- package/dist/common-functions.d.ts.map +1 -0
- package/dist/common-functions.js +13 -0
- package/dist/common-functions.js.map +1 -0
- package/dist/common-options.d.ts +11 -0
- package/dist/common-options.d.ts.map +1 -0
- package/dist/common-options.js +19 -0
- package/dist/common-options.js.map +1 -0
- package/dist/contract.d.ts +86 -0
- package/dist/contract.d.ts.map +1 -0
- package/dist/contract.js +125 -0
- package/dist/contract.js.map +1 -0
- package/dist/contract.test.d.ts +2 -0
- package/dist/contract.test.d.ts.map +1 -0
- package/dist/contract.test.js +147 -0
- package/dist/contract.test.js.map +1 -0
- package/dist/erc1155.d.ts +14 -0
- package/dist/erc1155.d.ts.map +1 -0
- package/dist/erc1155.js +130 -0
- package/dist/erc1155.js.map +1 -0
- package/dist/erc1155.test.d.ts +2 -0
- package/dist/erc1155.test.d.ts.map +1 -0
- package/dist/erc1155.test.js +80 -0
- package/dist/erc1155.test.js.map +1 -0
- package/dist/erc20.d.ts +19 -0
- package/dist/erc20.d.ts.map +1 -0
- package/dist/erc20.js +202 -0
- package/dist/erc20.js.map +1 -0
- package/dist/erc20.test.d.ts +2 -0
- package/dist/erc20.test.d.ts.map +1 -0
- package/dist/erc20.test.js +126 -0
- package/dist/erc20.test.js.map +1 -0
- package/dist/erc721.d.ts +18 -0
- package/dist/erc721.d.ts.map +1 -0
- package/dist/erc721.js +200 -0
- package/dist/erc721.js.map +1 -0
- package/dist/erc721.test.d.ts +2 -0
- package/dist/erc721.test.d.ts.map +1 -0
- package/dist/erc721.test.js +106 -0
- package/dist/erc721.test.js.map +1 -0
- package/dist/error.d.ts +8 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +11 -0
- package/dist/error.js.map +1 -0
- package/dist/general.d.ts +8 -0
- package/dist/general.d.ts.map +1 -0
- package/dist/general.js +22 -0
- package/dist/general.js.map +1 -0
- package/dist/general.test.d.ts +2 -0
- package/dist/general.test.d.ts.map +1 -0
- package/dist/general.test.js +42 -0
- package/dist/general.test.js.map +1 -0
- package/dist/generate/alternatives.d.ts +7 -0
- package/dist/generate/alternatives.d.ts.map +1 -0
- package/dist/generate/alternatives.js +29 -0
- package/dist/generate/alternatives.js.map +1 -0
- package/dist/generate/erc1155.d.ts +3 -0
- package/dist/generate/erc1155.d.ts.map +1 -0
- package/dist/generate/erc1155.js +24 -0
- package/dist/generate/erc1155.js.map +1 -0
- package/dist/generate/erc20.d.ts +3 -0
- package/dist/generate/erc20.d.ts.map +1 -0
- package/dist/generate/erc20.js +28 -0
- package/dist/generate/erc20.js.map +1 -0
- package/dist/generate/erc721.d.ts +3 -0
- package/dist/generate/erc721.d.ts.map +1 -0
- package/dist/generate/erc721.js +28 -0
- package/dist/generate/erc721.js.map +1 -0
- package/dist/generate/governor.d.ts +3 -0
- package/dist/generate/governor.d.ts.map +1 -0
- package/dist/generate/governor.js +32 -0
- package/dist/generate/governor.js.map +1 -0
- package/dist/generate/sources.d.ts +16 -0
- package/dist/generate/sources.d.ts.map +1 -0
- package/dist/generate/sources.js +79 -0
- package/dist/generate/sources.js.map +1 -0
- package/dist/governor.d.ts +26 -0
- package/dist/governor.d.ts.map +1 -0
- package/dist/governor.js +386 -0
- package/dist/governor.js.map +1 -0
- package/dist/governor.test.d.ts +2 -0
- package/dist/governor.test.d.ts.map +1 -0
- package/dist/governor.test.js +104 -0
- package/dist/governor.test.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/kind.d.ts +4 -0
- package/dist/kind.d.ts.map +1 -0
- package/dist/kind.js +28 -0
- package/dist/kind.js.map +1 -0
- package/dist/options.d.ts +11 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +40 -0
- package/dist/options.js.map +1 -0
- package/dist/print-versioned.d.ts +3 -0
- package/dist/print-versioned.d.ts.map +1 -0
- package/dist/print-versioned.js +12 -0
- package/dist/print-versioned.js.map +1 -0
- package/dist/print.d.ts +6 -0
- package/dist/print.d.ts.map +1 -0
- package/dist/print.js +182 -0
- package/dist/print.js.map +1 -0
- package/dist/scripts/prepare.d.ts +2 -0
- package/dist/scripts/prepare.d.ts.map +1 -0
- package/dist/scripts/prepare.js +52 -0
- package/dist/scripts/prepare.js.map +1 -0
- package/dist/set-access-control.d.ts +5 -0
- package/dist/set-access-control.d.ts.map +1 -0
- package/dist/set-access-control.js +38 -0
- package/dist/set-access-control.js.map +1 -0
- package/dist/set-info.d.ts +13 -0
- package/dist/set-info.d.ts.map +1 -0
- package/dist/set-info.js +17 -0
- package/dist/set-info.js.map +1 -0
- package/dist/set-upgradeable.d.ts +6 -0
- package/dist/set-upgradeable.d.ts.map +1 -0
- package/dist/set-upgradeable.js +43 -0
- package/dist/set-upgradeable.js.map +1 -0
- package/dist/test.d.ts +2 -0
- package/dist/test.d.ts.map +1 -0
- package/dist/test.js +23 -0
- package/dist/test.js.map +1 -0
- package/dist/utils/define-functions.d.ts +5 -0
- package/dist/utils/define-functions.d.ts.map +1 -0
- package/dist/utils/define-functions.js +11 -0
- package/dist/utils/define-functions.js.map +1 -0
- package/dist/utils/duration.d.ts +3 -0
- package/dist/utils/duration.d.ts.map +1 -0
- package/dist/utils/duration.js +31 -0
- package/dist/utils/duration.js.map +1 -0
- package/dist/utils/find-cover.d.ts +2 -0
- package/dist/utils/find-cover.d.ts.map +1 -0
- package/dist/utils/find-cover.js +23 -0
- package/dist/utils/find-cover.js.map +1 -0
- package/dist/utils/format-lines.d.ts +7 -0
- package/dist/utils/format-lines.d.ts.map +1 -0
- package/dist/utils/format-lines.js +30 -0
- package/dist/utils/format-lines.js.map +1 -0
- package/dist/utils/map-values.d.ts +2 -0
- package/dist/utils/map-values.d.ts.map +1 -0
- package/dist/utils/map-values.js +12 -0
- package/dist/utils/map-values.js.map +1 -0
- package/dist/utils/sorted-by.d.ts +2 -0
- package/dist/utils/sorted-by.d.ts.map +1 -0
- package/dist/utils/sorted-by.js +8 -0
- package/dist/utils/sorted-by.js.map +1 -0
- package/dist/utils/to-identifier.d.ts +2 -0
- package/dist/utils/to-identifier.d.ts.map +1 -0
- package/dist/utils/to-identifier.js +12 -0
- package/dist/utils/to-identifier.js.map +1 -0
- package/dist/utils/to-identifier.test.d.ts +2 -0
- package/dist/utils/to-identifier.test.d.ts.map +1 -0
- package/dist/utils/to-identifier.test.js +21 -0
- package/dist/utils/to-identifier.test.js.map +1 -0
- package/dist/utils/transitive-closure.d.ts +5 -0
- package/dist/utils/transitive-closure.d.ts.map +1 -0
- package/dist/utils/transitive-closure.js +27 -0
- package/dist/utils/transitive-closure.js.map +1 -0
- package/dist/zip.d.ts +4 -0
- package/dist/zip.d.ts.map +1 -0
- package/dist/zip.js +48 -0
- package/dist/zip.js.map +1 -0
- package/dist/zip.test.d.ts +2 -0
- package/dist/zip.test.d.ts.map +1 -0
- package/dist/zip.test.js +37 -0
- package/dist/zip.test.js.map +1 -0
- package/package.json +37 -0
- package/src/add-pausable.ts +32 -0
- package/src/api.ts +39 -0
- package/src/build-generic.ts +33 -0
- package/src/common-functions.ts +11 -0
- package/src/common-options.ts +24 -0
- package/src/contract.test.ts +164 -0
- package/src/contract.test.ts.md +272 -0
- package/src/contract.test.ts.snap +0 -0
- package/src/contract.ts +201 -0
- package/src/erc1155.test.ts +90 -0
- package/src/erc1155.test.ts.md +416 -0
- package/src/erc1155.test.ts.snap +0 -0
- package/src/erc1155.ts +160 -0
- package/src/erc20.test.ts +144 -0
- package/src/erc20.test.ts.md +571 -0
- package/src/erc20.test.ts.snap +0 -0
- package/src/erc20.ts +250 -0
- package/src/erc721.test.ts +122 -0
- package/src/erc721.test.ts.md +517 -0
- package/src/erc721.test.ts.snap +0 -0
- package/src/erc721.ts +250 -0
- package/src/error.ts +7 -0
- package/src/generate/alternatives.ts +38 -0
- package/src/generate/erc1155.ts +23 -0
- package/src/generate/erc20.ts +27 -0
- package/src/generate/erc721.ts +27 -0
- package/src/generate/governor.ts +30 -0
- package/src/generate/sources.ts +91 -0
- package/src/governor.test.ts +120 -0
- package/src/governor.test.ts.md +1419 -0
- package/src/governor.test.ts.snap +0 -0
- package/src/governor.ts +445 -0
- package/src/index.ts +23 -0
- package/src/kind.ts +30 -0
- package/src/options.ts +45 -0
- package/src/print-versioned.ts +12 -0
- package/src/print.ts +236 -0
- package/src/scripts/prepare.ts +60 -0
- package/src/set-access-control.ts +39 -0
- package/src/set-info.ts +24 -0
- package/src/set-upgradeable.ts +49 -0
- package/src/test.ts +21 -0
- package/src/utils/define-functions.ts +18 -0
- package/src/utils/duration.ts +34 -0
- package/src/utils/find-cover.ts +26 -0
- package/src/utils/format-lines.ts +31 -0
- package/src/utils/map-values.ts +10 -0
- package/src/utils/sorted-by.ts +3 -0
- package/src/utils/to-identifier.test.ts +20 -0
- package/src/utils/to-identifier.ts +7 -0
- package/src/utils/transitive-closure.ts +27 -0
- package/src/zip.test.ts +35 -0
- package/src/zip.ts +53 -0
package/src/erc721.ts
ADDED
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
import { Contract, ContractBuilder } from './contract';
|
|
2
|
+
import { Access, setAccessControl } from './set-access-control';
|
|
3
|
+
import { addPausable } from './add-pausable';
|
|
4
|
+
import { supportsInterface } from './common-functions';
|
|
5
|
+
import { defineFunctions } from './utils/define-functions';
|
|
6
|
+
import { CommonOptions, withCommonDefaults, defaults as commonDefaults } from './common-options';
|
|
7
|
+
import { setUpgradeable } from './set-upgradeable';
|
|
8
|
+
import { setInfo } from './set-info';
|
|
9
|
+
import { printContract } from './print';
|
|
10
|
+
|
|
11
|
+
export interface ERC721Options extends CommonOptions {
|
|
12
|
+
name: string;
|
|
13
|
+
symbol: string;
|
|
14
|
+
baseUri?: string;
|
|
15
|
+
enumerable?: boolean;
|
|
16
|
+
uriStorage?: boolean;
|
|
17
|
+
burnable?: boolean;
|
|
18
|
+
pausable?: boolean;
|
|
19
|
+
mintable?: boolean;
|
|
20
|
+
incremental?: boolean;
|
|
21
|
+
votes?: boolean;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export const defaults: Required<ERC721Options> = {
|
|
25
|
+
name: 'MyToken',
|
|
26
|
+
symbol: 'MTK',
|
|
27
|
+
baseUri: '',
|
|
28
|
+
enumerable: false,
|
|
29
|
+
uriStorage: false,
|
|
30
|
+
burnable: false,
|
|
31
|
+
pausable: false,
|
|
32
|
+
mintable: false,
|
|
33
|
+
incremental: false,
|
|
34
|
+
votes: false,
|
|
35
|
+
access: commonDefaults.access,
|
|
36
|
+
upgradeable: commonDefaults.upgradeable,
|
|
37
|
+
info: commonDefaults.info
|
|
38
|
+
} as const;
|
|
39
|
+
|
|
40
|
+
function withDefaults(opts: ERC721Options): Required<ERC721Options> {
|
|
41
|
+
return {
|
|
42
|
+
...opts,
|
|
43
|
+
...withCommonDefaults(opts),
|
|
44
|
+
baseUri: opts.baseUri ?? defaults.baseUri,
|
|
45
|
+
enumerable: opts.enumerable ?? defaults.enumerable,
|
|
46
|
+
uriStorage: opts.uriStorage ?? defaults.uriStorage,
|
|
47
|
+
burnable: opts.burnable ?? defaults.burnable,
|
|
48
|
+
pausable: opts.pausable ?? defaults.pausable,
|
|
49
|
+
mintable: opts.mintable ?? defaults.mintable,
|
|
50
|
+
incremental: opts.incremental ?? defaults.incremental,
|
|
51
|
+
votes: opts.votes ?? defaults.votes,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function printERC721(opts: ERC721Options = defaults): string {
|
|
56
|
+
return printContract(buildERC721(opts));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function buildERC721(opts: ERC721Options): Contract {
|
|
60
|
+
const allOpts = withDefaults(opts);
|
|
61
|
+
|
|
62
|
+
const c = new ContractBuilder(allOpts.name);
|
|
63
|
+
|
|
64
|
+
const { access, upgradeable, info } = allOpts;
|
|
65
|
+
|
|
66
|
+
addBase(c, allOpts.name, allOpts.symbol);
|
|
67
|
+
|
|
68
|
+
if (allOpts.baseUri) {
|
|
69
|
+
addBaseURI(c, allOpts.baseUri);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (allOpts.enumerable) {
|
|
73
|
+
addEnumerable(c);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (allOpts.uriStorage) {
|
|
77
|
+
addURIStorage(c);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if (allOpts.pausable) {
|
|
81
|
+
addPausable(c, access, [functions._beforeTokenTransfer]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (allOpts.burnable) {
|
|
85
|
+
addBurnable(c);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (allOpts.mintable) {
|
|
89
|
+
addMintable(c, access, allOpts.incremental, allOpts.uriStorage);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (allOpts.votes) {
|
|
93
|
+
addVotes(c, allOpts.name);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
setUpgradeable(c, upgradeable, access);
|
|
97
|
+
|
|
98
|
+
setInfo(c, info);
|
|
99
|
+
|
|
100
|
+
return c;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function addBase(c: ContractBuilder, name: string, symbol: string) {
|
|
104
|
+
c.addParent(
|
|
105
|
+
{
|
|
106
|
+
name: 'ERC721',
|
|
107
|
+
path: '@openzeppelin/contracts/token/ERC721/ERC721.sol',
|
|
108
|
+
},
|
|
109
|
+
[name, symbol],
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
c.addOverride('ERC721', functions._beforeTokenTransfer);
|
|
113
|
+
c.addOverride('ERC721', functions._afterTokenTransfer);
|
|
114
|
+
c.addOverride('ERC721', functions._burn);
|
|
115
|
+
c.addOverride('ERC721', functions.tokenURI);
|
|
116
|
+
c.addOverride('ERC721', supportsInterface);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function addBaseURI(c: ContractBuilder, baseUri: string) {
|
|
120
|
+
c.addOverride('ERC721', functions._baseURI);
|
|
121
|
+
c.setFunctionBody([`return ${JSON.stringify(baseUri)};`], functions._baseURI);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function addEnumerable(c: ContractBuilder) {
|
|
125
|
+
c.addParent({
|
|
126
|
+
name: 'ERC721Enumerable',
|
|
127
|
+
path: '@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol',
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
c.addOverride('ERC721Enumerable', functions._beforeTokenTransfer);
|
|
131
|
+
c.addOverride('ERC721Enumerable', supportsInterface);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function addURIStorage(c: ContractBuilder) {
|
|
135
|
+
c.addParent({
|
|
136
|
+
name: 'ERC721URIStorage',
|
|
137
|
+
path: '@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol',
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
c.addOverride('ERC721URIStorage', functions._burn);
|
|
141
|
+
c.addOverride('ERC721URIStorage', functions.tokenURI);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function addBurnable(c: ContractBuilder) {
|
|
145
|
+
c.addParent({
|
|
146
|
+
name: 'ERC721Burnable',
|
|
147
|
+
path: '@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol',
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function addMintable(c: ContractBuilder, access: Access, incremental = false, uriStorage = false) {
|
|
152
|
+
const fn = getMintFunction(incremental, uriStorage);
|
|
153
|
+
setAccessControl(c, fn, access, 'MINTER');
|
|
154
|
+
|
|
155
|
+
if (incremental) {
|
|
156
|
+
c.addUsing({
|
|
157
|
+
name: 'Counters',
|
|
158
|
+
path: '@openzeppelin/contracts/utils/Counters.sol',
|
|
159
|
+
}, 'Counters.Counter');
|
|
160
|
+
c.addVariable('Counters.Counter private _tokenIdCounter;');
|
|
161
|
+
c.addFunctionCode('uint256 tokenId = _tokenIdCounter.current();', fn);
|
|
162
|
+
c.addFunctionCode('_tokenIdCounter.increment();', fn);
|
|
163
|
+
c.addFunctionCode('_safeMint(to, tokenId);', fn);
|
|
164
|
+
} else {
|
|
165
|
+
c.addFunctionCode('_safeMint(to, tokenId);', fn);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (uriStorage) {
|
|
169
|
+
c.addFunctionCode('_setTokenURI(tokenId, uri);', fn);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function addVotes(c: ContractBuilder, name: string) {
|
|
174
|
+
c.addParent(
|
|
175
|
+
{
|
|
176
|
+
name: 'EIP712',
|
|
177
|
+
path: '@openzeppelin/contracts/utils/cryptography/draft-EIP712.sol',
|
|
178
|
+
},
|
|
179
|
+
[name, "1"]
|
|
180
|
+
);
|
|
181
|
+
c.addParent(
|
|
182
|
+
{
|
|
183
|
+
name: 'ERC721Votes',
|
|
184
|
+
path: '@openzeppelin/contracts/token/ERC721/extensions/draft-ERC721Votes.sol',
|
|
185
|
+
});
|
|
186
|
+
c.addOverride('ERC721Votes', functions._afterTokenTransfer);
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
const functions = defineFunctions({
|
|
190
|
+
_beforeTokenTransfer: {
|
|
191
|
+
kind: 'internal' as const,
|
|
192
|
+
args: [
|
|
193
|
+
{ name: 'from', type: 'address' },
|
|
194
|
+
{ name: 'to', type: 'address' },
|
|
195
|
+
{ name: 'tokenId', type: 'uint256' },
|
|
196
|
+
],
|
|
197
|
+
},
|
|
198
|
+
|
|
199
|
+
_afterTokenTransfer: {
|
|
200
|
+
kind: 'internal' as const,
|
|
201
|
+
args: [
|
|
202
|
+
{ name: 'from', type: 'address' },
|
|
203
|
+
{ name: 'to', type: 'address' },
|
|
204
|
+
{ name: 'tokenId', type: 'uint256' },
|
|
205
|
+
],
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
_burn: {
|
|
209
|
+
kind: 'internal' as const,
|
|
210
|
+
args: [
|
|
211
|
+
{ name: 'tokenId', type: 'uint256' },
|
|
212
|
+
],
|
|
213
|
+
},
|
|
214
|
+
|
|
215
|
+
tokenURI: {
|
|
216
|
+
kind: 'public' as const,
|
|
217
|
+
args: [
|
|
218
|
+
{ name: 'tokenId', type: 'uint256' },
|
|
219
|
+
],
|
|
220
|
+
returns: ['string memory'],
|
|
221
|
+
mutability: 'view' as const,
|
|
222
|
+
},
|
|
223
|
+
|
|
224
|
+
_baseURI: {
|
|
225
|
+
kind: 'internal' as const,
|
|
226
|
+
args: [],
|
|
227
|
+
returns: ['string memory'],
|
|
228
|
+
mutability: 'pure' as const,
|
|
229
|
+
},
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
function getMintFunction(incremental: boolean, uriStorage: boolean) {
|
|
233
|
+
const fn = {
|
|
234
|
+
name: 'safeMint',
|
|
235
|
+
kind: 'public' as const,
|
|
236
|
+
args: [
|
|
237
|
+
{ name: 'to', type: 'address' },
|
|
238
|
+
],
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
if (!incremental) {
|
|
242
|
+
fn.args.push({ name: 'tokenId', type: 'uint256' });
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (uriStorage) {
|
|
246
|
+
fn.args.push({ name: 'uri', type: 'string memory' });
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return fn;
|
|
250
|
+
}
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { mapValues } from "../utils/map-values";
|
|
2
|
+
|
|
3
|
+
type Blueprint = Record<string, readonly unknown[]>;
|
|
4
|
+
|
|
5
|
+
type Alternatives<B extends Blueprint> = {
|
|
6
|
+
[k in keyof B]: B[k][number];
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export function* generateAlternatives<B extends Blueprint>(
|
|
10
|
+
blueprint: B,
|
|
11
|
+
): Generator<Alternatives<B>> {
|
|
12
|
+
const entries = Object.entries(blueprint).map(([key, values]) => ({
|
|
13
|
+
key,
|
|
14
|
+
values,
|
|
15
|
+
current: 0,
|
|
16
|
+
limit: values.length,
|
|
17
|
+
}));
|
|
18
|
+
|
|
19
|
+
for (; !done(); advance()) {
|
|
20
|
+
yield Object.fromEntries(
|
|
21
|
+
entries.map(e => [e.key, e.values[e.current % e.limit]]),
|
|
22
|
+
) as Alternatives<B>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function done() {
|
|
26
|
+
const last = entries[entries.length - 1];
|
|
27
|
+
return last?.current === last?.limit;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function advance() {
|
|
31
|
+
for (const e of entries) {
|
|
32
|
+
e.current = (e.current % e.limit) + 1;
|
|
33
|
+
if (e.current < e.limit) {
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ERC1155Options } from '../erc1155';
|
|
2
|
+
import { accessOptions } from '../set-access-control';
|
|
3
|
+
import { infoOptions } from '../set-info';
|
|
4
|
+
import { upgradeableOptions } from '../set-upgradeable';
|
|
5
|
+
import { generateAlternatives } from './alternatives';
|
|
6
|
+
|
|
7
|
+
const booleans = [true, false];
|
|
8
|
+
|
|
9
|
+
const blueprint = {
|
|
10
|
+
name: ['MyToken'],
|
|
11
|
+
uri: ['https://example.com/'],
|
|
12
|
+
burnable: booleans,
|
|
13
|
+
pausable: booleans,
|
|
14
|
+
mintable: booleans,
|
|
15
|
+
supply: booleans,
|
|
16
|
+
access: accessOptions,
|
|
17
|
+
upgradeable: upgradeableOptions,
|
|
18
|
+
info: infoOptions,
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export function* generateERC1155Options(): Generator<Required<ERC1155Options>> {
|
|
22
|
+
yield* generateAlternatives(blueprint);
|
|
23
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ERC20Options } from '../erc20';
|
|
2
|
+
import { accessOptions } from '../set-access-control';
|
|
3
|
+
import { infoOptions } from '../set-info';
|
|
4
|
+
import { upgradeableOptions } from '../set-upgradeable';
|
|
5
|
+
import { generateAlternatives } from './alternatives';
|
|
6
|
+
|
|
7
|
+
const booleans = [true, false];
|
|
8
|
+
|
|
9
|
+
const blueprint = {
|
|
10
|
+
name: ['MyToken'],
|
|
11
|
+
symbol: ['MTK'],
|
|
12
|
+
burnable: booleans,
|
|
13
|
+
snapshots: booleans,
|
|
14
|
+
pausable: booleans,
|
|
15
|
+
mintable: booleans,
|
|
16
|
+
permit: booleans,
|
|
17
|
+
votes: booleans,
|
|
18
|
+
flashmint: booleans,
|
|
19
|
+
premint: ['1'],
|
|
20
|
+
access: accessOptions,
|
|
21
|
+
upgradeable: upgradeableOptions,
|
|
22
|
+
info: infoOptions,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function* generateERC20Options(): Generator<Required<ERC20Options>> {
|
|
26
|
+
yield* generateAlternatives(blueprint);
|
|
27
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ERC721Options } from '../erc721';
|
|
2
|
+
import { accessOptions } from '../set-access-control';
|
|
3
|
+
import { infoOptions } from '../set-info';
|
|
4
|
+
import { upgradeableOptions } from '../set-upgradeable';
|
|
5
|
+
import { generateAlternatives } from './alternatives';
|
|
6
|
+
|
|
7
|
+
const booleans = [true, false];
|
|
8
|
+
|
|
9
|
+
const blueprint = {
|
|
10
|
+
name: ['MyToken'],
|
|
11
|
+
symbol: ['MTK'],
|
|
12
|
+
baseUri: ['https://example.com/'],
|
|
13
|
+
enumerable: booleans,
|
|
14
|
+
uriStorage: booleans,
|
|
15
|
+
burnable: booleans,
|
|
16
|
+
pausable: booleans,
|
|
17
|
+
mintable: booleans,
|
|
18
|
+
incremental: booleans,
|
|
19
|
+
access: accessOptions,
|
|
20
|
+
upgradeable: upgradeableOptions,
|
|
21
|
+
info: infoOptions,
|
|
22
|
+
votes: booleans,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export function* generateERC721Options(): Generator<Required<ERC721Options>> {
|
|
26
|
+
yield* generateAlternatives(blueprint);
|
|
27
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { defaults, GovernorOptions, timelockOptions, votesOptions } from '../governor';
|
|
2
|
+
import { accessOptions } from '../set-access-control';
|
|
3
|
+
import { infoOptions } from '../set-info';
|
|
4
|
+
import { upgradeableOptions } from '../set-upgradeable';
|
|
5
|
+
import { generateAlternatives } from './alternatives';
|
|
6
|
+
|
|
7
|
+
const booleans = [true, false];
|
|
8
|
+
|
|
9
|
+
const blueprint = {
|
|
10
|
+
name: ['MyGovernor'],
|
|
11
|
+
delay: ['1 week'],
|
|
12
|
+
period: ['1 week'],
|
|
13
|
+
blockTime: [defaults.blockTime],
|
|
14
|
+
proposalThreshold: ['0', '1000'],
|
|
15
|
+
decimals: [18],
|
|
16
|
+
quorumMode: ['percent', 'absolute'] as const,
|
|
17
|
+
quorumPercent: [4],
|
|
18
|
+
quorumAbsolute: ['1000'],
|
|
19
|
+
votes: votesOptions,
|
|
20
|
+
timelock: timelockOptions,
|
|
21
|
+
bravo: booleans,
|
|
22
|
+
settings: booleans,
|
|
23
|
+
upgradeable: upgradeableOptions,
|
|
24
|
+
access: accessOptions,
|
|
25
|
+
info: infoOptions,
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export function* generateGovernorOptions(): Generator<Required<GovernorOptions>> {
|
|
29
|
+
yield* generateAlternatives(blueprint);
|
|
30
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { promises as fs } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import crypto from 'crypto';
|
|
4
|
+
|
|
5
|
+
import { generateERC20Options } from './erc20';
|
|
6
|
+
import { generateERC721Options } from './erc721';
|
|
7
|
+
import { generateERC1155Options } from './erc1155';
|
|
8
|
+
import { buildGeneric, GenericOptions } from '../build-generic';
|
|
9
|
+
import { printContract } from '../print';
|
|
10
|
+
import { generateGovernorOptions } from './governor';
|
|
11
|
+
import { OptionsError } from '../error';
|
|
12
|
+
import { findCover } from '../utils/find-cover';
|
|
13
|
+
import type { Contract } from '../contract';
|
|
14
|
+
|
|
15
|
+
type Subset = 'all' | 'minimal-cover';
|
|
16
|
+
|
|
17
|
+
export function* generateOptions(): Generator<GenericOptions> {
|
|
18
|
+
for (const kindOpts of generateERC20Options()) {
|
|
19
|
+
yield { kind: 'ERC20', ...kindOpts };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
for (const kindOpts of generateERC721Options()) {
|
|
23
|
+
yield { kind: 'ERC721', ...kindOpts };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
for (const kindOpts of generateERC1155Options()) {
|
|
27
|
+
yield { kind: 'ERC1155', ...kindOpts };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
for (const kindOpts of generateGovernorOptions()) {
|
|
31
|
+
yield { kind: 'Governor', ...kindOpts };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface GeneratedContract {
|
|
36
|
+
id: string;
|
|
37
|
+
options: GenericOptions;
|
|
38
|
+
contract: Contract;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface GeneratedSource extends GeneratedContract {
|
|
42
|
+
source: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function generateContractSubset(subset: Subset): GeneratedContract[] {
|
|
46
|
+
const contracts = [];
|
|
47
|
+
|
|
48
|
+
for (const options of generateOptions()) {
|
|
49
|
+
const id = crypto
|
|
50
|
+
.createHash('sha1')
|
|
51
|
+
.update(JSON.stringify(options))
|
|
52
|
+
.digest()
|
|
53
|
+
.toString('hex');
|
|
54
|
+
|
|
55
|
+
try {
|
|
56
|
+
const contract = buildGeneric(options);
|
|
57
|
+
contracts.push({ id, options, contract });
|
|
58
|
+
} catch (e: unknown) {
|
|
59
|
+
if (e instanceof OptionsError) {
|
|
60
|
+
continue;
|
|
61
|
+
} else {
|
|
62
|
+
throw e;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (subset === 'all') {
|
|
68
|
+
return contracts;
|
|
69
|
+
} else {
|
|
70
|
+
const getParents = (c: GeneratedContract) => c.contract.parents.map(p => p.contract.path);
|
|
71
|
+
return [
|
|
72
|
+
...findCover(contracts.filter(c => c.options.upgradeable), getParents),
|
|
73
|
+
...findCover(contracts.filter(c => !c.options.upgradeable), getParents),
|
|
74
|
+
];
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export function* generateSources(subset: Subset): Generator<GeneratedSource> {
|
|
79
|
+
for (const c of generateContractSubset(subset)) {
|
|
80
|
+
const source = printContract(c.contract);
|
|
81
|
+
yield { ...c, source };
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export async function writeGeneratedSources(dir: string, subset: Subset): Promise<void> {
|
|
86
|
+
await fs.mkdir(dir, { recursive: true });
|
|
87
|
+
|
|
88
|
+
for (const { id, source } of generateSources(subset)) {
|
|
89
|
+
await fs.writeFile(path.format({ dir, name: id, ext: '.sol' }), source);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { governor } from '.';
|
|
3
|
+
|
|
4
|
+
import { buildGovernor, GovernorOptions } from './governor';
|
|
5
|
+
import { printContract } from './print';
|
|
6
|
+
|
|
7
|
+
function testGovernor(title: string, opts: Partial<GovernorOptions>) {
|
|
8
|
+
test(title, t => {
|
|
9
|
+
const c = buildGovernor({
|
|
10
|
+
name: 'MyGovernor',
|
|
11
|
+
delay: '1 week',
|
|
12
|
+
period: '1 week',
|
|
13
|
+
settings: false,
|
|
14
|
+
...opts,
|
|
15
|
+
});
|
|
16
|
+
t.snapshot(printContract(c));
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Tests external API for equivalence with internal API
|
|
22
|
+
*/
|
|
23
|
+
function testAPIEquivalence(title: string, opts?: GovernorOptions) {
|
|
24
|
+
test(title, t => {
|
|
25
|
+
t.is(governor.print(opts), printContract(buildGovernor({
|
|
26
|
+
name: 'MyGovernor',
|
|
27
|
+
delay: '1 block',
|
|
28
|
+
period: '1 week',
|
|
29
|
+
...opts,
|
|
30
|
+
})));
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
testGovernor('governor with proposal threshold', {
|
|
35
|
+
proposalThreshold: '1',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
testGovernor('governor with custom block time', {
|
|
39
|
+
blockTime: 6,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
testGovernor('governor with custom decimals', {
|
|
43
|
+
decimals: 6,
|
|
44
|
+
proposalThreshold: '1',
|
|
45
|
+
quorumMode: 'absolute',
|
|
46
|
+
quorumAbsolute: '1',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
testGovernor('governor with 0 decimals', {
|
|
50
|
+
decimals: 0,
|
|
51
|
+
proposalThreshold: '1',
|
|
52
|
+
quorumMode: 'absolute',
|
|
53
|
+
quorumAbsolute: '1',
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
testGovernor('governor with settings', {
|
|
57
|
+
settings: true,
|
|
58
|
+
proposalThreshold: '1',
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
testGovernor('governor with bravo', {
|
|
62
|
+
bravo: true,
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
testGovernor('governor with erc20votes', {
|
|
66
|
+
votes: 'erc20votes',
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
testGovernor('governor with erc721votes', {
|
|
70
|
+
votes: 'erc721votes',
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
testGovernor('governor with erc721votes omit decimals', {
|
|
74
|
+
votes: 'erc721votes',
|
|
75
|
+
decimals: 6,
|
|
76
|
+
proposalThreshold: '1',
|
|
77
|
+
quorumMode: 'absolute',
|
|
78
|
+
quorumAbsolute: '5',
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
testGovernor('governor with erc721votes settings omit decimals', {
|
|
82
|
+
votes: 'erc721votes',
|
|
83
|
+
decimals: 6,
|
|
84
|
+
proposalThreshold: '10',
|
|
85
|
+
settings: true,
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
testGovernor('governor with comp', {
|
|
89
|
+
votes: 'comp',
|
|
90
|
+
quorumMode: 'absolute',
|
|
91
|
+
quorumAbsolute: '1',
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
testGovernor('governor with percent quorum', {
|
|
95
|
+
quorumMode: 'percent',
|
|
96
|
+
quorumPercent: 6,
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
testGovernor('governor with fractional percent quorum', {
|
|
100
|
+
quorumMode: 'percent',
|
|
101
|
+
quorumPercent: 0.5,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
testGovernor('governor with openzeppelin timelock', {
|
|
105
|
+
timelock: 'openzeppelin',
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
testGovernor('governor with compound timelock', {
|
|
109
|
+
timelock: 'compound',
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
testAPIEquivalence('API default');
|
|
113
|
+
|
|
114
|
+
testAPIEquivalence('API basic', { name: 'CustomGovernor', delay: '2 weeks', period: '2 week' });
|
|
115
|
+
|
|
116
|
+
testAPIEquivalence('API basic upgradeable', { name: 'CustomGovernor', delay: '2 weeks', period: '2 week', upgradeable: 'uups' });
|
|
117
|
+
|
|
118
|
+
test('API assert defaults', async t => {
|
|
119
|
+
t.is(governor.print(governor.defaults), governor.print());
|
|
120
|
+
});
|