@@ -23,6 +23,7 @@ export const defaults: Required<ERC1155Options> = {
2323 burnable : false ,
2424 pausable : false ,
2525 mintable : false ,
26+ supply : false ,
2627 updatableUri : true ,
2728 royaltyInfo : royaltyInfoDefaults ,
2829 access : commonDefaults . access ,
@@ -41,6 +42,7 @@ export interface ERC1155Options extends CommonContractOptions {
4142 burnable ?: boolean ;
4243 pausable ?: boolean ;
4344 mintable ?: boolean ;
45+ supply ?: boolean ;
4446 updatableUri ?: boolean ;
4547 royaltyInfo ?: RoyaltyInfoOptions ;
4648}
@@ -52,6 +54,7 @@ function withDefaults(opts: ERC1155Options): Required<ERC1155Options> {
5254 burnable : opts . burnable ?? defaults . burnable ,
5355 pausable : opts . pausable ?? defaults . pausable ,
5456 mintable : opts . mintable ?? defaults . mintable ,
57+ supply : opts . supply ?? defaults . supply ,
5558 updatableUri : opts . updatableUri ?? defaults . updatableUri ,
5659 royaltyInfo : opts . royaltyInfo ?? defaults . royaltyInfo ,
5760 } ;
@@ -86,6 +89,10 @@ export function buildERC1155(opts: ERC1155Options): Contract {
8689 addMintable ( c , allOpts . access ) ;
8790 }
8891
92+ if ( allOpts . supply ) {
93+ addSupply ( c ) ;
94+ }
95+
8996 if ( allOpts . updatableUri ) {
9097 addSetBaseUri ( c , allOpts . access ) ;
9198 }
@@ -101,7 +108,7 @@ export function buildERC1155(opts: ERC1155Options): Contract {
101108}
102109
103110function addHooks ( c : ContractBuilder , allOpts : Required < ERC1155Options > ) {
104- const usesCustomHooks = allOpts . pausable ;
111+ const usesCustomHooks = allOpts . pausable || allOpts . supply ;
105112 if ( usesCustomHooks ) {
106113 const hooksTrait = {
107114 name : 'ERC1155HooksImpl' ,
@@ -112,20 +119,43 @@ function addHooks(c: ContractBuilder, allOpts: Required<ERC1155Options>) {
112119 c . addImplementedTrait ( hooksTrait ) ;
113120 c . addUseClause ( 'starknet' , 'ContractAddress' ) ;
114121
115- c . addFunction ( hooksTrait , {
116- name : 'before_update' ,
117- args : [
118- {
119- name : 'ref self' ,
120- type : `ERC1155Component::ComponentState<ContractState>` ,
121- } ,
122- { name : 'from' , type : 'ContractAddress' } ,
123- { name : 'to' , type : 'ContractAddress' } ,
124- { name : 'token_ids' , type : 'Span<u256>' } ,
125- { name : 'values' , type : 'Span<u256>' } ,
126- ] ,
127- code : [ 'let contract_state = self.get_contract()' , 'contract_state.pausable.assert_not_paused()' ] ,
128- } ) ;
122+ if ( allOpts . pausable ) {
123+ c . addFunction ( hooksTrait , {
124+ name : 'before_update' ,
125+ args : [
126+ {
127+ name : 'ref self' ,
128+ type : `ERC1155Component::ComponentState<ContractState>` ,
129+ } ,
130+ { name : 'from' , type : 'ContractAddress' } ,
131+ { name : 'to' , type : 'ContractAddress' } ,
132+ { name : 'token_ids' , type : 'Span<u256>' } ,
133+ { name : 'values' , type : 'Span<u256>' } ,
134+ ] ,
135+ code : [ 'let contract_state = self.get_contract()' , 'contract_state.pausable.assert_not_paused()' ] ,
136+ } ) ;
137+ }
138+
139+ if ( allOpts . supply ) {
140+ const afterUpdateFn = c . addFunction ( hooksTrait , {
141+ name : 'after_update' ,
142+ args : [
143+ {
144+ name : 'ref self' ,
145+ type : `ERC1155Component::ComponentState<ContractState>` ,
146+ } ,
147+ { name : 'from' , type : 'ContractAddress' } ,
148+ { name : 'to' , type : 'ContractAddress' } ,
149+ { name : 'token_ids' , type : 'Span<u256>' } ,
150+ { name : 'values' , type : 'Span<u256>' } ,
151+ ] ,
152+ code : [ ] ,
153+ } ) ;
154+ afterUpdateFn . code . push (
155+ 'let mut contract_state = self.get_contract_mut();' ,
156+ 'contract_state.erc1155_supply.after_update(from, to, token_ids, values);' ,
157+ ) ;
158+ }
129159 } else {
130160 c . addUseClause ( 'openzeppelin_token::erc1155' , 'ERC1155HooksEmptyImpl' ) ;
131161 }
@@ -153,6 +183,10 @@ function addBurnable(c: ContractBuilder) {
153183 c . addFunction ( externalTrait , functions . batchBurn ) ;
154184}
155185
186+ function addSupply ( c : ContractBuilder ) {
187+ c . addComponent ( components . ERC1155SupplyComponent , [ ] , true ) ;
188+ }
189+
156190function addMintable ( c : ContractBuilder , access : Access ) {
157191 c . addUseClause ( 'starknet' , 'ContractAddress' ) ;
158192 requireAccessControl ( c , externalTrait , functions . mint , access , 'MINTER' , 'minter' ) ;
@@ -188,6 +222,29 @@ const components = defineComponents({
188222 } ,
189223 ] ,
190224 } ,
225+ ERC1155SupplyComponent : {
226+ path : 'openzeppelin_token::erc1155::extensions' ,
227+ substorage : {
228+ name : 'erc1155_supply' ,
229+ type : 'ERC1155SupplyComponent::Storage' ,
230+ } ,
231+ event : {
232+ name : 'ERC1155SupplyEvent' ,
233+ type : 'ERC1155SupplyComponent::Event' ,
234+ } ,
235+ impls : [
236+ {
237+ name : 'ERC1155SupplyImpl' ,
238+ embed : true ,
239+ value : 'ERC1155SupplyComponent::ERC1155SupplyImpl<ContractState>' ,
240+ } ,
241+ {
242+ name : 'ERC1155SupplyInternalImpl' ,
243+ embed : false ,
244+ value : 'ERC1155SupplyComponent::InternalImpl<ContractState>' ,
245+ } ,
246+ ] ,
247+ } ,
191248} ) ;
192249
193250const functions = defineFunctions ( {
0 commit comments