forked from OpenZeppelin/contracts-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherc721.ts
More file actions
271 lines (243 loc) · 8.19 KB
/
erc721.ts
File metadata and controls
271 lines (243 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { BaseImplementedTrait, Contract, ContractBuilder } from './contract';
import { Access, requireAccessControl, setAccessControl } from './set-access-control';
import { addPausable } from './add-pausable';
import { defineFunctions } from './utils/define-functions';
import { CommonContractOptions, withCommonContractDefaults, getSelfArg } from './common-options';
import { setUpgradeable } from './set-upgradeable';
import { setInfo } from './set-info';
import { defineComponents } from './utils/define-components';
import { contractDefaults as commonDefaults } from './common-options';
import { printContract } from './print';
import { addSRC5Component, addVotesComponent } from './common-components';
import { externalTrait } from './external-trait';
import { toByteArray, toFelt252 } from './utils/convert-strings';
import { OptionsError } from './error';
import { RoyaltyInfoOptions, setRoyaltyInfo, defaults as royaltyInfoDefaults } from './set-royalty-info';
export const defaults: Required<ERC721Options> = {
name: 'MyToken',
symbol: 'MTK',
baseUri: '',
burnable: false,
pausable: false,
mintable: false,
enumerable: false,
votes: false,
royaltyInfo: royaltyInfoDefaults,
appName: '', // Defaults to empty string, but user must provide a non-empty value if votes are enabled
appVersion: 'v1',
access: commonDefaults.access,
upgradeable: commonDefaults.upgradeable,
info: commonDefaults.info
} as const;
export function printERC721(opts: ERC721Options = defaults): string {
return printContract(buildERC721(opts));
}
export interface ERC721Options extends CommonContractOptions {
name: string;
symbol: string;
baseUri?: string;
burnable?: boolean;
pausable?: boolean;
mintable?: boolean;
enumerable?: boolean;
votes?: boolean;
royaltyInfo?: RoyaltyInfoOptions;
appName?: string;
appVersion?: string;
}
function withDefaults(opts: ERC721Options): Required<ERC721Options> {
return {
...opts,
...withCommonContractDefaults(opts),
baseUri: opts.baseUri ?? defaults.baseUri,
burnable: opts.burnable ?? defaults.burnable,
pausable: opts.pausable ?? defaults.pausable,
mintable: opts.mintable ?? defaults.mintable,
enumerable: opts.enumerable ?? defaults.enumerable,
royaltyInfo: opts.royaltyInfo ?? defaults.royaltyInfo,
votes: opts.votes ?? defaults.votes,
appName: opts.appName ?? defaults.appName,
appVersion: opts.appVersion ?? defaults.appVersion
};
}
export function isAccessControlRequired(opts: Partial<ERC721Options>): boolean {
return opts.mintable === true || opts.pausable === true || opts.upgradeable === true || opts.royaltyInfo?.enabled === true;
}
export function buildERC721(opts: ERC721Options): Contract {
const c = new ContractBuilder(opts.name);
const allOpts = withDefaults(opts);
addBase(c, toByteArray(allOpts.name), toByteArray(allOpts.symbol), toByteArray(allOpts.baseUri));
addERC721Mixin(c);
if (allOpts.pausable) {
addPausable(c, allOpts.access);
}
if (allOpts.burnable) {
addBurnable(c);
}
if (allOpts.mintable) {
addMintable(c, allOpts.access);
}
if (allOpts.enumerable) {
addEnumerable(c);
}
setAccessControl(c, allOpts.access);
setUpgradeable(c, allOpts.upgradeable, allOpts.access);
setInfo(c, allOpts.info);
setRoyaltyInfo(c, allOpts.royaltyInfo, allOpts.access);
addHooks(c, allOpts);
return c;
}
function addHooks(c: ContractBuilder, opts: Required<ERC721Options>) {
const usesCustomHooks = opts.pausable || opts.enumerable || opts.votes;
if (usesCustomHooks) {
const ERC721HooksTrait: BaseImplementedTrait = {
name: `ERC721HooksImpl`,
of: 'ERC721Component::ERC721HooksTrait<ContractState>',
tags: [],
priority: 0,
};
c.addImplementedTrait(ERC721HooksTrait);
c.addStandaloneImport('starknet::ContractAddress');
const requiresMutState = opts.enumerable || opts.votes;
const initStateLine = requiresMutState
? 'let mut contract_state = self.get_contract_mut()'
: 'let contract_state = self.get_contract()';
const beforeUpdateCode = [initStateLine];
if (opts.pausable) {
beforeUpdateCode.push('contract_state.pausable.assert_not_paused()');
}
if (opts.enumerable) {
beforeUpdateCode.push('contract_state.erc721_enumerable.before_update(to, token_id)');
}
if (opts.votes) {
if (!opts.appName) {
throw new OptionsError({
appName: 'Application Name is required when Votes are enabled',
});
}
if (!opts.appVersion) {
throw new OptionsError({
appVersion: 'Application Version is required when Votes are enabled',
});
}
addVotesComponent(c, toFelt252(opts.appName, 'appName'), toFelt252(opts.appVersion, 'appVersion'));
beforeUpdateCode.push('let previous_owner = self._owner_of(token_id);');
beforeUpdateCode.push('contract_state.votes.transfer_voting_units(previous_owner, to, 1);');
}
c.addFunction(ERC721HooksTrait, {
name: 'before_update',
args: [
{ name: 'ref self', type: `ERC721Component::ComponentState<ContractState>` },
{ name: 'to', type: 'ContractAddress' },
{ name: 'token_id', type: 'u256' },
{ name: 'auth', type: 'ContractAddress' },
],
code: beforeUpdateCode,
});
} else {
c.addStandaloneImport('openzeppelin::token::erc721::ERC721HooksEmptyImpl');
}
}
function addERC721Mixin(c: ContractBuilder) {
c.addImplToComponent(components.ERC721Component, {
name: 'ERC721MixinImpl',
value: 'ERC721Component::ERC721MixinImpl<ContractState>',
});
c.addInterfaceFlag('ISRC5');
addSRC5Component(c);
}
function addBase(c: ContractBuilder, name: string, symbol: string, baseUri: string) {
c.addComponent(
components.ERC721Component,
[
name, symbol, baseUri,
],
true,
);
}
function addEnumerable(c: ContractBuilder) {
c.addComponent(components.ERC721EnumerableComponent, [], true);
}
function addBurnable(c: ContractBuilder) {
c.addStandaloneImport('core::num::traits::Zero');
c.addStandaloneImport('starknet::get_caller_address');
c.addFunction(externalTrait, functions.burn);
}
function addMintable(c: ContractBuilder, access: Access) {
c.addStandaloneImport('starknet::ContractAddress');
requireAccessControl(c, externalTrait, functions.safe_mint, access, 'MINTER', 'minter');
// Camel case version of safe_mint. Access control and pausable are already set on safe_mint.
c.addFunction(externalTrait, functions.safeMint);
}
const components = defineComponents( {
ERC721Component: {
path: 'openzeppelin::token::erc721',
substorage: {
name: 'erc721',
type: 'ERC721Component::Storage',
},
event: {
name: 'ERC721Event',
type: 'ERC721Component::Event',
},
impls: [],
internalImpl: {
name: 'ERC721InternalImpl',
value: 'ERC721Component::InternalImpl<ContractState>',
},
},
ERC721EnumerableComponent: {
path: 'openzeppelin::token::erc721::extensions',
substorage: {
name: 'erc721_enumerable',
type: 'ERC721EnumerableComponent::Storage',
},
event: {
name: 'ERC721EnumerableEvent',
type: 'ERC721EnumerableComponent::Event',
},
impls: [
{
name: 'ERC721EnumerableImpl',
value: 'ERC721EnumerableComponent::ERC721EnumerableImpl<ContractState>',
},
],
internalImpl: {
name: 'ERC721EnumerableInternalImpl',
value: 'ERC721EnumerableComponent::InternalImpl<ContractState>',
},
},
});
const functions = defineFunctions({
burn: {
args: [
getSelfArg(),
{ name: 'token_id', type: 'u256' }
],
code: [
'self.erc721.update(Zero::zero(), token_id, get_caller_address());',
]
},
safe_mint: {
args: [
getSelfArg(),
{ name: 'recipient', type: 'ContractAddress' },
{ name: 'token_id', type: 'u256' },
{ name: 'data', type: 'Span<felt252>' },
],
code: [
'self.erc721.safe_mint(recipient, token_id, data);',
]
},
safeMint: {
args: [
getSelfArg(),
{ name: 'recipient', type: 'ContractAddress' },
{ name: 'tokenId', type: 'u256' },
{ name: 'data', type: 'Span<felt252>' },
],
code: [
'self.safe_mint(recipient, tokenId, data);',
]
},
});