Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
12 changes: 6 additions & 6 deletions packages/core-cairo/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ const components = defineComponents( {
name: 'AccountEvent',
type: 'AccountComponent::Event',
},
impls: [],
internalImpl: {
impls: [{
name: 'AccountInternalImpl',
embed: false,
value: 'AccountComponent::InternalImpl<ContractState>',
},
}],
},
EthAccountComponent: {
path: 'openzeppelin::account::eth_account',
Expand All @@ -176,10 +176,10 @@ const components = defineComponents( {
name: 'EthAccountEvent',
type: 'EthAccountComponent::Event',
},
impls: [],
internalImpl: {
impls: [{
name: 'EthAccountInternalImpl',
embed: false,
value: 'EthAccountComponent::InternalImpl<ContractState>',
},
}]
},
});
13 changes: 6 additions & 7 deletions packages/core-cairo/src/add-pausable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,15 @@ const components = defineComponents( {
name: 'PausableEvent',
type: 'PausableComponent::Event',
},
impls: [
{
impls: [{
name: 'PausableImpl',
value: 'PausableComponent::PausableImpl<ContractState>',
},
}, {
name: 'PausableInternalImpl',
embed: false,
value: 'PausableComponent::InternalImpl<ContractState>',
}
],
internalImpl: {
name: 'PausableInternalImpl',
value: 'PausableComponent::InternalImpl<ContractState>',
},
},
});

Expand Down
7 changes: 7 additions & 0 deletions packages/core-cairo/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { printERC20, defaults as erc20defaults, isAccessControlRequired as erc20
import { printERC721, defaults as erc721defaults, isAccessControlRequired as erc721IsAccessControlRequired, ERC721Options } from './erc721';
import { printERC1155, defaults as erc1155defaults, isAccessControlRequired as erc1155IsAccessControlRequired, ERC1155Options } from './erc1155';
import { printAccount, defaults as accountDefaults, AccountOptions } from './account';
import { printGovernor, defaults as governorDefaults, isAccessControlRequired as governorIsAccessControlRequired, GovernorOptions } from './governor';
import { printCustom, defaults as customDefaults, isAccessControlRequired as customIsAccessControlRequired, CustomOptions } from './custom';

export interface WizardAccountAPI<Options extends CommonOptions>{
Expand Down Expand Up @@ -39,6 +40,7 @@ export type ERC20 = WizardContractAPI<ERC20Options>;
export type ERC721 = WizardContractAPI<ERC721Options>;
export type ERC1155 = WizardContractAPI<ERC1155Options>;
export type Account = WizardAccountAPI<AccountOptions>;
export type Governor = WizardContractAPI<GovernorOptions>;
export type Custom = WizardContractAPI<CustomOptions>;

export const erc20: ERC20 = {
Expand All @@ -60,6 +62,11 @@ export const account: Account = {
print: printAccount,
defaults: accountDefaults,
}
export const governor: Governor = {
print: printGovernor,
defaults: governorDefaults,
isAccessControlRequired: governorIsAccessControlRequired
}
export const custom: Custom = {
print: printCustom,
defaults: customDefaults,
Expand Down
6 changes: 5 additions & 1 deletion packages/core-cairo/src/build-generic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { ERC721Options, buildERC721 } from './erc721';
import { ERC1155Options, buildERC1155 } from './erc1155';
import { CustomOptions, buildCustom } from './custom';
import { AccountOptions, buildAccount } from './account';

import { GovernorOptions, buildGovernor } from './governor';
export interface KindedOptions {
ERC20: { kind: 'ERC20' } & ERC20Options;
ERC721: { kind: 'ERC721' } & ERC721Options;
ERC1155: { kind: 'ERC1155' } & ERC1155Options;
Account: { kind: 'Account' } & AccountOptions;
Governor: { kind: 'Governor' } & GovernorOptions;
Custom: { kind: 'Custom' } & CustomOptions;
}

Expand All @@ -28,6 +29,9 @@ export function buildGeneric(opts: GenericOptions) {
case 'Account':
return buildAccount(opts);

case 'Governor':
return buildGovernor(opts);

case 'Custom':
return buildCustom(opts);

Expand Down
16 changes: 11 additions & 5 deletions packages/core-cairo/src/common-components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const components = defineComponents( {
name: 'VotesEvent',
type: 'VotesComponent::Event',
},
impls: [],
internalImpl: {
impls: [{
name: 'VotesInternalImpl',
embed: false,
value: 'VotesComponent::InternalImpl<ContractState>',
},
}],
},

NoncesComponent: {
Expand All @@ -54,32 +54,38 @@ const components = defineComponents( {
},
})

export function addSRC5Component(c: ContractBuilder) {
export function addSRC5Component(c: ContractBuilder, section?: string) {
c.addComponent(components.SRC5Component, [], false);

if (!c.interfaceFlags.has('ISRC5')) {
c.addImplToComponent(components.SRC5Component, {
name: 'SRC5Impl',
value: 'SRC5Component::SRC5Impl<ContractState>',
section,
});
c.addInterfaceFlag('ISRC5');
}
}

export function addVotesComponent(c: ContractBuilder, name: string, version: string) {
c.addStandaloneImport('openzeppelin::utils::cryptography::snip12::SNIP12Metadata');
addSNIP12Metadata(c, name, version);
c.addComponent(components.NoncesComponent, [], false);
c.addComponent(components.VotesComponent, [], false);
c.addImplToComponent(components.VotesComponent, {
name: 'VotesImpl',
value: `VotesComponent::VotesImpl<ContractState>`,
});
}

export function addSNIP12Metadata(c: ContractBuilder, name: string, version: string, section?: string) {
c.addStandaloneImport('openzeppelin::utils::cryptography::snip12::SNIP12Metadata');

const SNIP12Metadata: BaseImplementedTrait = {
name: 'SNIP12MetadataImpl',
of: 'SNIP12Metadata',
tags: [],
priority: 0,
section,
};
c.addImplementedTrait(SNIP12Metadata);

Expand Down
13 changes: 6 additions & 7 deletions packages/core-cairo/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ const FOO_COMPONENT: Component = {
name: 'FooEvent',
type: 'FooComponent::Event',
},
impls: [
{
impls: [{
name: 'FooImpl',
value: 'FooComponent::FooImpl<ContractState>',
},
}, {
name: 'FooInternalImpl',
embed: false,
value: 'FooComponent::InternalImpl<ContractState>',
}
],
internalImpl: {
name: 'FooInternalImpl',
value: 'FooComponent::InternalImpl<ContractState>',
},
};

test('contract basics', t => {
Expand Down
23 changes: 22 additions & 1 deletion packages/core-cairo/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Contract {
account: boolean;
standaloneImports: string[];
components: Component[];
constants: Variable[];
constructorCode: string[];
constructorArgs: Argument[];
upgradeable: boolean;
Expand All @@ -21,7 +22,6 @@ export interface Component {
substorage: Substorage;
event: Event;
impls: Impl[];
internalImpl?: Impl;
initializer?: Initializer;
}

Expand All @@ -38,6 +38,8 @@ export interface Event {
export interface Impl {
name: string;
value: string;
embed?: boolean;
section?: string;
}

export interface Initializer {
Expand All @@ -49,6 +51,7 @@ export interface BaseImplementedTrait {
of: string;
tags: string[];
perItemTag?: string;
section?: string;
/**
* Priority for which trait to print first.
* Lower numbers are higher priority, undefined is lowest priority.
Expand All @@ -59,6 +62,7 @@ export interface BaseImplementedTrait {
export interface ImplementedTrait extends BaseImplementedTrait {
superVariables: Variable[];
functions: ContractFunction[];
section?: string;
}

export interface BaseFunction {
Expand All @@ -77,6 +81,8 @@ export interface Variable {
name: string;
type: string;
value: string;
comment?: string;
inlineComment?: boolean;
}

export interface Argument {
Expand All @@ -96,6 +102,7 @@ export class ContractBuilder implements Contract {
private componentsMap: Map<string, Component> = new Map();
private implementedTraitsMap: Map<string, ImplementedTrait> = new Map();
private superVariablesMap: Map<string, Variable> = new Map();
private constantsMap: Map<string, Variable> = new Map();
private standaloneImportsSet: Set<string> = new Set();
private interfaceFlagsSet: Set<string> = new Set();

Expand All @@ -116,6 +123,10 @@ export class ContractBuilder implements Contract {
return [...this.superVariablesMap.values()];
}

get constants(): Variable[] {
return [...this.constantsMap.values()];
}

get standaloneImports(): string[] {
return [...this.standaloneImportsSet];
}
Expand Down Expand Up @@ -154,6 +165,15 @@ export class ContractBuilder implements Contract {
}
}

addConstant(constant: Variable): boolean {
if (this.constantsMap.has(constant.name)) {
return false;
} else {
this.constantsMap.set(constant.name, constant);
return true;
}
}

addSuperVariable(variable: Variable): boolean {
if (this.superVariablesMap.has(variable.name)) {
return false;
Expand All @@ -175,6 +195,7 @@ export class ContractBuilder implements Contract {
tags: [ ...baseTrait.tags ],
superVariables: [],
functions: [],
section: baseTrait.section,
priority: baseTrait.priority,
};
this.implementedTraitsMap.set(key, t);
Expand Down
6 changes: 3 additions & 3 deletions packages/core-cairo/src/erc1155.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ const components = defineComponents( {
name: 'ERC1155Event',
type: 'ERC1155Component::Event',
},
impls: [],
internalImpl: {
impls: [{
name: 'ERC1155InternalImpl',
embed: false,
value: 'ERC1155Component::InternalImpl<ContractState>',
},
}],
},
});

Expand Down
8 changes: 4 additions & 4 deletions packages/core-cairo/src/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ function addPremint(c: ContractBuilder, amount: string) {

/**
* Calculates the initial supply that would be used in an ERC20 contract based on a given premint amount and number of decimals.
*
*
* @param premint Premint amount in token units, may be fractional
* @param decimals The number of decimals in the token
* @returns `premint` with zeros padded or removed based on `decimals`.
Expand Down Expand Up @@ -258,11 +258,11 @@ const components = defineComponents( {
name: 'ERC20Event',
type: 'ERC20Component::Event',
},
impls: [],
internalImpl: {
impls: [{
name: 'ERC20InternalImpl',
embed: false,
value: 'ERC20Component::InternalImpl<ContractState>',
},
}],
},
});

Expand Down
20 changes: 9 additions & 11 deletions packages/core-cairo/src/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,11 @@ const components = defineComponents( {
name: 'ERC721Event',
type: 'ERC721Component::Event',
},
impls: [],
internalImpl: {
impls: [{
name: 'ERC721InternalImpl',
embed: false,
value: 'ERC721Component::InternalImpl<ContractState>',
},
}],
},
ERC721EnumerableComponent: {
path: 'openzeppelin::token::erc721::extensions',
Expand All @@ -223,16 +223,14 @@ const components = defineComponents( {
name: 'ERC721EnumerableEvent',
type: 'ERC721EnumerableComponent::Event',
},
impls: [
{
name: 'ERC721EnumerableImpl',
value: 'ERC721EnumerableComponent::ERC721EnumerableImpl<ContractState>',
},
],
internalImpl: {
impls: [{
name: 'ERC721EnumerableImpl',
value: 'ERC721EnumerableComponent::ERC721EnumerableImpl<ContractState>',
}, {
name: 'ERC721EnumerableInternalImpl',
embed: false,
value: 'ERC721EnumerableComponent::InternalImpl<ContractState>',
},
}],
},
});

Expand Down
Loading
Loading