Skip to content

Commit

Permalink
Add Account preset (#803)
Browse files Browse the repository at this point in the history
* fix: link (#545)

* feat: update logic

* feat: add mocks

* docs: update

* feat: update docs

* Update src/account/account.cairo

Co-authored-by: Andrew Fleming <[email protected]>

* feat: apply review updates

* feat: update overview

* Update docs/modules/ROOT/pages/api/account.adoc

Co-authored-by: Martín Triay <[email protected]>

* feat: apply review updates

* feat: flatten events

* feat: add account preset

* feat: apply review updates

* feat: apply review updates

* feat: remove preset

* Revert "feat: remove preset"

This reverts commit 472b04c.

* fix: merge unresolved conflicts

* docs: update

* Update src/presets/account.cairo

Co-authored-by: Andrew Fleming <[email protected]>

* feat: apply review updates

* feat: add account preset section

* Update docs/modules/ROOT/pages/api/account.adoc

Co-authored-by: Martín Triay <[email protected]>

* Update src/presets/account.cairo

Co-authored-by: Martín Triay <[email protected]>

* Update src/presets/account.cairo

Co-authored-by: Martín Triay <[email protected]>

* feat: apply review updates

---------

Co-authored-by: Andrew Fleming <[email protected]>
Co-authored-by: Martín Triay <[email protected]>
  • Loading branch information
3 people authored Nov 10, 2023
1 parent 4eafccf commit 7a8231a
Show file tree
Hide file tree
Showing 9 changed files with 564 additions and 5 deletions.
50 changes: 47 additions & 3 deletions docs/modules/ROOT/pages/api/account.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Reference of interfaces, presets, and utilities related to account contracts.

[.contract]
[[ISRC6]]
=== `++ISRC6++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-v0.8.0-beta.0/src/account/interface.cairo#L12[{github-icon},role=heading-link]
=== `++ISRC6++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-v0.8.0-beta.0/src/account/interface.cairo[{github-icon},role=heading-link]

```javascript
use openzeppelin::account::interface::ISRC6;
Expand Down Expand Up @@ -75,7 +75,7 @@ Account component implementing xref:ISRC6[`ISRC6`].

NOTE: Implementing xref:api/introspection.adoc#SRC5Component[SRC5Component] is a requirement for this component to be implemented.

[.contract-index]
[.contract-index#AccountComponent-Embeddable-Impls]
.Embeddable Implementations
--
.SRC6Impl
Expand All @@ -98,7 +98,7 @@ NOTE: Implementing xref:api/introspection.adoc#SRC5Component[SRC5Component] is a
* xref:#AccountComponent-set_public_key[`++set_public_key(self, new_public_key)++`]
--

[.contract-index]
[.contract-index#AccountComponent-Embeddable-Impls-camelCase]
.Embeddable Implementations (camelCase)
--
.SRC6CamelOnlyImpl
Expand Down Expand Up @@ -259,3 +259,47 @@ Emitted when a `public_key` is added.
==== `[.contract-item-name]#++OwnerRemoved++#++(removed_owner_guid: felt252)++` [.item-kind]#event#

Emitted when a `public_key` is removed.

== Presets

[.contract]
[[Account]]
=== `++Account++` link:https://github.com/OpenZeppelin/cairo-contracts/blob/release-v0.8.0-beta.0/src/presets/account.cairo[{github-icon},role=heading-link]

```javascript
use openzeppelin::presets::Account;
```

Basic account contract leveraging xref:#AccountComponent[AccountComponent].

[.contract-index]
.Constructor
--
* xref:#Account-constructor[`++constructor(self, public_key)++`]
--

[.contract-index]
.Embedded Implementations
--
.AccountComponent

* xref:#AccountComponent-Embeddable-Impls[`++SRC6Impl++`]
* xref:#AccountComponent-Embeddable-Impls[`++PublicKeyImpl++`]
* xref:#AccountComponent-Embeddable-Impls[`++DeclarerImpl++`]
* xref:#AccountComponent-Embeddable-Impls[`++DeployableImpl++`]
* xref:#AccountComponent-Embeddable-Impls-camelCase[`++SRC6CamelOnlyImpl++`]
* xref:#AccountComponent-Embeddable-Impls-camelCase[`++PublicKeyCamelImpl++`]

.SRC5Component

* xref:api/introspection.adoc#SRC5Component-Embeddable-Impls[`++SRC5Impl++`]
--

[#Account-constructor-section]
==== Constructor

[.contract-item]
[[Account-constructor]]
==== `[.contract-item-name]#++constructor++#++(ref self: ContractState, public_key: felt252)++` [.item-kind]#constructor#

Sets the account `public_key` and registers the interfaces the contract supports.
2 changes: 1 addition & 1 deletion docs/modules/ROOT/pages/api/introspection.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use openzeppelin::introspection::src5::SRC5Component;

SRC5 component extending xref:ISRC5[`ISRC5`].

[.contract-index]
[.contract-index#SRC5Component-Embeddable-Impls]
.Embeddable Implementations
--
.SRC5Impl
Expand Down
1 change: 1 addition & 0 deletions src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod access;
mod account;
mod introspection;
mod presets;
mod security;
#[cfg(test)]
mod tests;
Expand Down
3 changes: 3 additions & 0 deletions src/presets.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod account;

use account::Account;
55 changes: 55 additions & 0 deletions src/presets/account.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.8.0-beta.0 (presets/account.cairo)

/// # Account Preset
///
/// OpenZeppelin's basic account which can change its public key and declare, deploy, or call contracts.
#[starknet::contract]
mod Account {
use openzeppelin::account::AccountComponent;
use openzeppelin::introspection::src5::SRC5Component;

component!(path: AccountComponent, storage: account, event: AccountEvent);
component!(path: SRC5Component, storage: src5, event: SRC5Event);

// Account
#[abi(embed_v0)]
impl SRC6Impl = AccountComponent::SRC6Impl<ContractState>;
#[abi(embed_v0)]
impl SRC6CamelOnlyImpl = AccountComponent::SRC6CamelOnlyImpl<ContractState>;
#[abi(embed_v0)]
impl PublicKeyImpl = AccountComponent::PublicKeyImpl<ContractState>;
#[abi(embed_v0)]
impl PublicKeyCamelImpl = AccountComponent::PublicKeyCamelImpl<ContractState>;
#[abi(embed_v0)]
impl DeclarerImpl = AccountComponent::DeclarerImpl<ContractState>;
#[abi(embed_v0)]
impl DeployableImpl = AccountComponent::DeployableImpl<ContractState>;
impl AccountInternalImpl = AccountComponent::InternalImpl<ContractState>;

// SRC5
#[abi(embed_v0)]
impl SRC5Impl = SRC5Component::SRC5Impl<ContractState>;

#[storage]
struct Storage {
#[substorage(v0)]
account: AccountComponent::Storage,
#[substorage(v0)]
src5: SRC5Component::Storage
}

#[event]
#[derive(Drop, starknet::Event)]
enum Event {
#[flat]
AccountEvent: AccountComponent::Event,
#[flat]
SRC5Event: SRC5Component::Event
}

#[constructor]
fn constructor(ref self: ContractState, public_key: felt252) {
self.account.initializer(public_key);
}
}
1 change: 1 addition & 0 deletions src/tests.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod access;
mod account;
mod introspection;
mod mocks;
mod presets;
mod security;
mod token;
mod upgrades;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/account/test_account.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ fn test_initializer() {
assert(event.new_owner_guid == PUBKEY, 'Invalid owner key');
utils::assert_no_events_left(ZERO());

assert(state.account.get_public_key() == PUBKEY, 'Should return public key');
assert(state.account.get_public_key() == PUBKEY, 'Should return PUBKEY');
}

#[test]
Expand Down
1 change: 1 addition & 0 deletions src/tests/presets.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mod test_account;
Loading

0 comments on commit 7a8231a

Please sign in to comment.