Skip to content

Commit

Permalink
Add ERC721Enumerable (#391)
Browse files Browse the repository at this point in the history
  • Loading branch information
immrsd authored Sep 26, 2024
1 parent 4dae94d commit 9280664
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 94 deletions.
4 changes: 4 additions & 0 deletions packages/core-cairo/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

- Add ERC721Enumerable. ([#391](https://github.com/OpenZeppelin/contracts-wizard/pull/391))

## 0.15.0 (2024-09-19)

- Add Account and EthAccount. ([#387](https://github.com/OpenZeppelin/contracts-wizard/pull/387))
Expand Down
28 changes: 14 additions & 14 deletions packages/core-cairo/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ export interface Argument {
export class ContractBuilder implements Contract {
readonly name: string;
readonly account: boolean;
license: string = 'MIT';
license = 'MIT';
upgradeable = false;

readonly constructorArgs: Argument[] = [];
readonly constructorCode: string[] = [];

private componentsMap: Map<string, Component> = new Map<string, Component>();
private implementedTraitsMap: Map<string, ImplementedTrait> = new Map<string, ImplementedTrait>();
private superVariablesMap: Map<string, Variable> = new Map<string, Variable>();
private componentsMap: Map<string, Component> = new Map();
private implementedTraitsMap: Map<string, ImplementedTrait> = new Map();
private superVariablesMap: Map<string, Variable> = new Map();
private standaloneImportsSet: Set<string> = new Set();
private interfaceFlagsSet: Set<string> = new Set();

Expand Down Expand Up @@ -126,7 +126,7 @@ export class ContractBuilder implements Contract {
return this.interfaceFlagsSet;
}

addStandaloneImport(fullyQualified: string) {
addStandaloneImport(fullyQualified: string): void {
this.standaloneImportsSet.add(fullyQualified);
}

Expand All @@ -141,7 +141,7 @@ export class ContractBuilder implements Contract {
return !present;
}

addImplToComponent(component: Component, impl: Impl) {
addImplToComponent(component: Component, impl: Impl): void {
this.addComponent(component);
let c = this.componentsMap.get(component.name);
if (c == undefined) {
Expand All @@ -153,7 +153,7 @@ export class ContractBuilder implements Contract {
}
}

addSuperVariable(variable: Variable) {
addSuperVariable(variable: Variable): boolean {
if (this.superVariablesMap.has(variable.name)) {
return false;
} else {
Expand All @@ -162,7 +162,7 @@ export class ContractBuilder implements Contract {
}
}

addImplementedTrait(baseTrait: BaseImplementedTrait) {
addImplementedTrait(baseTrait: BaseImplementedTrait): ImplementedTrait {
const key = baseTrait.name;
const existingTrait = this.implementedTraitsMap.get(key);
if (existingTrait !== undefined) {
Expand All @@ -180,7 +180,7 @@ export class ContractBuilder implements Contract {
}
}

addFunction(baseTrait: BaseImplementedTrait, fn: BaseFunction) {
addFunction(baseTrait: BaseImplementedTrait, fn: BaseFunction): ContractFunction {
const t = this.addImplementedTrait(baseTrait);

const signature = this.getFunctionSignature(fn);
Expand All @@ -203,17 +203,17 @@ export class ContractBuilder implements Contract {
return contractFn;
}

private getFunctionSignature(fn: BaseFunction) {
private getFunctionSignature(fn: BaseFunction): string {
return [fn.name, '(', ...fn.args.map(a => a.name), ')'].join('');
}

addFunctionCodeBefore(baseTrait: BaseImplementedTrait, fn: BaseFunction, codeBefore: string) {
addFunctionCodeBefore(baseTrait: BaseImplementedTrait, fn: BaseFunction, codeBefore: string): void {
this.addImplementedTrait(baseTrait);
const existingFn = this.addFunction(baseTrait, fn);
existingFn.codeBefore = [ ...existingFn.codeBefore ?? [], codeBefore ];
}

addConstructorArgument(arg: Argument) {
addConstructorArgument(arg: Argument): void {
for (const existingArg of this.constructorArgs) {
if (existingArg.name == arg.name) {
return;
Expand All @@ -222,11 +222,11 @@ export class ContractBuilder implements Contract {
this.constructorArgs.push(arg);
}

addConstructorCode(code: string) {
addConstructorCode(code: string): void {
this.constructorCode.push(code);
}

addInterfaceFlag(flag: string) {
addInterfaceFlag(flag: string): void {
this.interfaceFlagsSet.add(flag);
}
}
37 changes: 23 additions & 14 deletions packages/core-cairo/src/erc721.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { printContract } from './print';

import { erc721 } from '.';

const allFeaturesON: Partial<ERC721Options> = {
mintable: true,
burnable: true,
pausable: true,
enumerable: true,
upgradeable: true
} as const;

function testERC721(title: string, opts: Partial<ERC721Options>) {
test(title, t => {
const c = buildERC721({
Expand Down Expand Up @@ -51,36 +59,35 @@ testERC721('mintable', {
mintable: true,
});

testERC721('enumerable', {
enumerable: true,
});

testERC721('pausable + enumerable', {
pausable: true,
enumerable: true,
});

testERC721('mintable + roles', {
mintable: true,
access: 'roles',
});

testERC721('full non-upgradeable', {
mintable: true,
pausable: true,
burnable: true,
...allFeaturesON,
upgradeable: false,
});

testERC721('full upgradeable', {
mintable: true,
pausable: true,
burnable: true,
upgradeable: true,
});
testERC721('full upgradeable', allFeaturesON);

testAPIEquivalence('API default');

testAPIEquivalence('API basic', { name: 'CustomToken', symbol: 'CTK' });

testAPIEquivalence('API full upgradeable', {
...allFeaturesON,
name: 'CustomToken',
symbol: 'CTK',
burnable: true,
mintable: true,
pausable: true,
upgradeable: true,
symbol: 'CTK'
});

test('API assert defaults', async t => {
Expand All @@ -91,4 +98,6 @@ test('API isAccessControlRequired', async t => {
t.is(erc721.isAccessControlRequired({ mintable: true }), true);
t.is(erc721.isAccessControlRequired({ pausable: true }), true);
t.is(erc721.isAccessControlRequired({ upgradeable: true }), true);
t.is(erc721.isAccessControlRequired({ burnable: true }), false);
t.is(erc721.isAccessControlRequired({ enumerable: true }), false);
});
Loading

0 comments on commit 9280664

Please sign in to comment.