Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/cairo-alpha-erc1155-supply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@openzeppelin/wizard-common": patch
---

Cairo: ERC1155 supply tracking description for AI prompts.
1 change: 1 addition & 0 deletions packages/common/src/ai/descriptions/cairo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export const cairoERC1155Descriptions = {
baseUri:
'The location of the metadata for the token. Clients will replace any instance of {id} in this string with the tokenId.',
updatableUri: 'Whether privileged accounts will be able to set a new URI for all token types.',
supply: 'Whether to keep track of total supply of tokens.',
};

export const cairoGovernorDescriptions = {
Expand Down
1 change: 1 addition & 0 deletions packages/core/cairo_alpha/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Add ERC1155 supply tracking extension ([#765](https://github.com/OpenZeppelin/contracts-wizard/pull/765))
- Add ERC721 wrapper extension ([#764](https://github.com/OpenZeppelin/contracts-wizard/pull/764))
- Add ERC721Consecutive extension ([##769](https://github.com/OpenZeppelin/contracts-wizard/pull/#769))
- Add ERC20 wrapper extension ([#763](https://github.com/OpenZeppelin/contracts-wizard/pull/763))
Expand Down
87 changes: 72 additions & 15 deletions packages/core/cairo_alpha/src/erc1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const defaults: Required<ERC1155Options> = {
burnable: false,
pausable: false,
mintable: false,
supply: false,
updatableUri: true,
royaltyInfo: royaltyInfoDefaults,
access: commonDefaults.access,
Expand All @@ -41,6 +42,7 @@ export interface ERC1155Options extends CommonContractOptions {
burnable?: boolean;
pausable?: boolean;
mintable?: boolean;
supply?: boolean;
updatableUri?: boolean;
royaltyInfo?: RoyaltyInfoOptions;
}
Expand All @@ -52,6 +54,7 @@ function withDefaults(opts: ERC1155Options): Required<ERC1155Options> {
burnable: opts.burnable ?? defaults.burnable,
pausable: opts.pausable ?? defaults.pausable,
mintable: opts.mintable ?? defaults.mintable,
supply: opts.supply ?? defaults.supply,
updatableUri: opts.updatableUri ?? defaults.updatableUri,
royaltyInfo: opts.royaltyInfo ?? defaults.royaltyInfo,
};
Expand Down Expand Up @@ -86,6 +89,10 @@ export function buildERC1155(opts: ERC1155Options): Contract {
addMintable(c, allOpts.access);
}

if (allOpts.supply) {
addSupply(c);
}

if (allOpts.updatableUri) {
addSetBaseUri(c, allOpts.access);
}
Expand All @@ -101,7 +108,7 @@ export function buildERC1155(opts: ERC1155Options): Contract {
}

function addHooks(c: ContractBuilder, allOpts: Required<ERC1155Options>) {
const usesCustomHooks = allOpts.pausable;
const usesCustomHooks = allOpts.pausable || allOpts.supply;
if (usesCustomHooks) {
const hooksTrait = {
name: 'ERC1155HooksImpl',
Expand All @@ -112,20 +119,43 @@ function addHooks(c: ContractBuilder, allOpts: Required<ERC1155Options>) {
c.addImplementedTrait(hooksTrait);
c.addUseClause('starknet', 'ContractAddress');

c.addFunction(hooksTrait, {
name: 'before_update',
args: [
{
name: 'ref self',
type: `ERC1155Component::ComponentState<ContractState>`,
},
{ name: 'from', type: 'ContractAddress' },
{ name: 'to', type: 'ContractAddress' },
{ name: 'token_ids', type: 'Span<u256>' },
{ name: 'values', type: 'Span<u256>' },
],
code: ['let contract_state = self.get_contract()', 'contract_state.pausable.assert_not_paused()'],
});
if (allOpts.pausable) {
c.addFunction(hooksTrait, {
name: 'before_update',
args: [
{
name: 'ref self',
type: `ERC1155Component::ComponentState<ContractState>`,
},
{ name: 'from', type: 'ContractAddress' },
{ name: 'to', type: 'ContractAddress' },
{ name: 'token_ids', type: 'Span<u256>' },
{ name: 'values', type: 'Span<u256>' },
],
code: ['let contract_state = self.get_contract()', 'contract_state.pausable.assert_not_paused()'],
});
}

if (allOpts.supply) {
const afterUpdateFn = c.addFunction(hooksTrait, {
name: 'after_update',
args: [
{
name: 'ref self',
type: `ERC1155Component::ComponentState<ContractState>`,
},
{ name: 'from', type: 'ContractAddress' },
{ name: 'to', type: 'ContractAddress' },
{ name: 'token_ids', type: 'Span<u256>' },
{ name: 'values', type: 'Span<u256>' },
],
code: [],
});
afterUpdateFn.code.push(
'let mut contract_state = self.get_contract_mut();',
'contract_state.erc1155_supply.after_update(from, to, token_ids, values);',
);
}
} else {
c.addUseClause('openzeppelin_token::erc1155', 'ERC1155HooksEmptyImpl');
}
Expand Down Expand Up @@ -153,6 +183,10 @@ function addBurnable(c: ContractBuilder) {
c.addFunction(externalTrait, functions.batchBurn);
}

function addSupply(c: ContractBuilder) {
c.addComponent(components.ERC1155SupplyComponent, [], true);
}

function addMintable(c: ContractBuilder, access: Access) {
c.addUseClause('starknet', 'ContractAddress');
requireAccessControl(c, externalTrait, functions.mint, access, 'MINTER', 'minter');
Expand Down Expand Up @@ -188,6 +222,29 @@ const components = defineComponents({
},
],
},
ERC1155SupplyComponent: {
path: 'openzeppelin_token::erc1155::extensions',
substorage: {
name: 'erc1155_supply',
type: 'ERC1155SupplyComponent::Storage',
},
event: {
name: 'ERC1155SupplyEvent',
type: 'ERC1155SupplyComponent::Event',
},
impls: [
{
name: 'ERC1155SupplyImpl',
embed: true,
value: 'ERC1155SupplyComponent::ERC1155SupplyImpl<ContractState>',
},
{
name: 'ERC1155SupplyInternalImpl',
embed: false,
value: 'ERC1155SupplyComponent::InternalImpl<ContractState>',
},
],
},
});

const functions = defineFunctions({
Expand Down
1 change: 1 addition & 0 deletions packages/core/cairo_alpha/src/generate/erc1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function prepareBlueprint(opts: GeneratorOptions) {
burnable: booleans,
pausable: booleans,
mintable: booleans,
supply: booleans,
updatableUri: booleans,
upgradeable: upgradeableOptions,
royaltyInfo: resolveRoyaltyOptionsSubset(opts.royaltyInfo),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const allFeaturesON: Partial<ERC1155Options> = {
mintable: true,
burnable: true,
pausable: true,
supply: true,
royaltyInfo: royaltyInfoOptions.enabledDefault,
upgradeable: true,
} as const;
Expand Down Expand Up @@ -74,6 +75,10 @@ testERC1155('mintable', {
mintable: true,
});

testERC1155('supply tracking', {
supply: true,
});

testERC1155('mintable + roles', {
mintable: true,
access: AccessControl.Roles(),
Expand Down Expand Up @@ -197,6 +202,7 @@ test('API isAccessControlRequired', async t => {
}),
true,
);
t.is(erc1155.isAccessControlRequired({ updatableUri: false, supply: false }), false);
t.is(erc1155.isAccessControlRequired({ updatableUri: false }), false);
t.is(erc1155.isAccessControlRequired({}), true); // updatableUri is true by default
});
Loading
Loading