Skip to content

Commit

Permalink
Add Governor (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericnordelo authored Dec 12, 2024
1 parent 8e8f10e commit 355d098
Show file tree
Hide file tree
Showing 39 changed files with 3,729 additions and 747 deletions.
2 changes: 2 additions & 0 deletions packages/core-cairo/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## 0.20.0 (2024-12-10)

- Add Governor tab. ([#417](https://github.com/OpenZeppelin/contracts-wizard/pull/417))

- **Breaking changes**:
- Use OpenZeppelin Contracts for Cairo v0.20.0. ([#419](https://github.com/OpenZeppelin/contracts-wizard/pull/419))

Expand Down
156 changes: 138 additions & 18 deletions packages/core-cairo/src/account.test.ts.md

Large diffs are not rendered by default.

Binary file modified packages/core-cairo/src/account.test.ts.snap
Binary file not shown.
14 changes: 7 additions & 7 deletions packages/core-cairo/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export function buildAccount(opts: AccountOptions): Contract {
c.addComponent(components.AccountComponent, [{ lit: 'public_key' }], true);
break;
case 'eth':
c.addStandaloneImport('openzeppelin::account::interface::EthPublicKey;');
c.addUseClause('openzeppelin::account::interface', 'EthPublicKey');
c.addConstructorArgument({ name: 'public_key', type: 'EthPublicKey' });
c.addComponent(components.EthAccountComponent, [{ lit: 'public_key' }], true);
break;
Expand Down 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
18 changes: 12 additions & 6 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');
export function addVotesComponent(c: ContractBuilder, name: string, version: string, section?: string) {
addSNIP12Metadata(c, name, version, section);
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.addUseClause('openzeppelin::utils::cryptography::snip12', 'SNIP12Metadata');

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

Expand Down
16 changes: 8 additions & 8 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 Expand Up @@ -107,6 +106,7 @@ test('contract with standalone import', t => {
Foo.addComponent(
FOO_COMPONENT,
);
Foo.addStandaloneImport('some::library::SomeLibrary');
Foo.addUseClause('some::library', 'SomeLibrary');
t.snapshot(printContract(Foo));
});

9 changes: 9 additions & 0 deletions packages/core-cairo/src/contract.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,11 @@ Generated by [AVA](https://avajs.dev).
component!(path: FooComponent, storage: foo, event: FooEvent);␊
// External␊
#[abi(embed_v0)]␊
impl FooImpl = FooComponent::FooImpl<ContractState>;␊
// Internal␊
impl FooInternalImpl = FooComponent::InternalImpl<ContractState>;␊
#[storage]␊
Expand Down Expand Up @@ -161,9 +163,11 @@ Generated by [AVA](https://avajs.dev).
component!(path: FooComponent, storage: foo, event: FooEvent);␊
// External␊
#[abi(embed_v0)]␊
impl FooImpl = FooComponent::FooImpl<ContractState>;␊
// Internal␊
impl FooInternalImpl = FooComponent::InternalImpl<ContractState>;␊
#[storage]␊
Expand All @@ -178,5 +182,10 @@ Generated by [AVA](https://avajs.dev).
#[flat]␊
FooEvent: FooComponent::Event,␊
}␊
#[constructor]␊
fn constructor(ref self: ContractState) {␊
self.foo.initializer();␊
}␊
}␊
`
Binary file modified packages/core-cairo/src/contract.test.ts.snap
Binary file not shown.
Loading

0 comments on commit 355d098

Please sign in to comment.