Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Interface & Dispatchers docs #730

Merged
merged 65 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 64 commits
Commits
Show all changes
65 commits
Select commit Hold shift + click to select a range
49baf23
feat: update format and add api
ericnordelo Aug 23, 2023
6ba12a9
fix: typo
ericnordelo Aug 23, 2023
38a7363
feat: add counterfactual deployment doc
ericnordelo Aug 24, 2023
06d3384
feat: add API entries
ericnordelo Aug 24, 2023
48e77cf
feat: add events
ericnordelo Aug 24, 2023
2cce06a
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 25, 2023
71cc37d
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 25, 2023
a34020e
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 25, 2023
60305e7
Update docs/modules/ROOT/pages/guides/deployment.adoc
ericnordelo Aug 25, 2023
2223482
Update docs/modules/ROOT/pages/guides/deployment.adoc
ericnordelo Aug 25, 2023
1ae6d18
feat: update from reviews
ericnordelo Aug 25, 2023
849739d
Merge branch 'feat/update-account-docs-#560' of github.com:ericnordel…
ericnordelo Aug 25, 2023
7a9f38e
feat: apply review updates
ericnordelo Aug 25, 2023
2c3b734
feat: update docs
ericnordelo Aug 30, 2023
fbc18fb
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
b235619
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
13248e4
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
cdf7bd2
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
bea7e8d
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
437b196
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
d3ffdf4
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
0d8bdce
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
964e58b
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
99fa76f
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
e5afaeb
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
56715e7
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
a596957
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Aug 31, 2023
c5d1466
Update docs/modules/ROOT/pages/guides/deployment.adoc
ericnordelo Aug 31, 2023
022d8bd
Update docs/modules/ROOT/pages/guides/deployment.adoc
ericnordelo Aug 31, 2023
4ede5cc
Update docs/modules/ROOT/pages/guides/deployment.adoc
ericnordelo Aug 31, 2023
f007a1f
Update docs/modules/ROOT/pages/guides/deployment.adoc
ericnordelo Aug 31, 2023
76ce366
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
207662c
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
699a27a
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
29b23f4
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
19b8372
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
d909516
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
8c23b4c
feat: apply review updates
ericnordelo Aug 31, 2023
8741eea
Merge branch 'feat/update-account-docs-#560' of github.com:ericnordel…
ericnordelo Aug 31, 2023
501b241
fix: account casing
ericnordelo Aug 31, 2023
5b33694
feat: add headers
ericnordelo Aug 31, 2023
e781bfd
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Aug 31, 2023
7fdf47b
feat: add link
ericnordelo Aug 31, 2023
448a015
Merge branch 'feat/update-account-docs-#560' of github.com:ericnordel…
ericnordelo Aug 31, 2023
77da367
feat: move API
ericnordelo Sep 1, 2023
76fd058
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Sep 11, 2023
0342dfc
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Sep 11, 2023
7e5a3ec
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Sep 11, 2023
f3cccac
Update docs/modules/ROOT/pages/accounts.adoc
ericnordelo Sep 11, 2023
0f4583d
refactor: update wording
ericnordelo Sep 11, 2023
7a2db69
Merge branch 'feat/update-account-docs-#560' of github.com:ericnordel…
ericnordelo Sep 11, 2023
55287f4
Merge branch 'cairo-2' of github.com:OpenZeppelin/cairo-contracts int…
ericnordelo Sep 11, 2023
039fb1f
Update docs/antora.yml
ericnordelo Sep 11, 2023
2f51db7
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Sep 11, 2023
bf2543c
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Sep 11, 2023
eba2992
feat: apply update reviews
ericnordelo Sep 11, 2023
0fe2d86
Merge branch 'feat/update-account-docs-#560' of github.com:ericnordel…
ericnordelo Sep 11, 2023
694b3a2
Update docs/modules/ROOT/pages/api/account.adoc
ericnordelo Sep 11, 2023
5de23f2
refactor: UI
ericnordelo Sep 11, 2023
717f548
fix: UI
ericnordelo Sep 11, 2023
a5d6f94
feat: focus on SRC6
ericnordelo Sep 11, 2023
c35d6cf
add interface & dispatchers docs
martriay Sep 13, 2023
85649e6
Merge branch 'cairo-2' into dualcase-docs
martriay Sep 15, 2023
1f0983f
apply review feedback
martriay Sep 15, 2023
d1175b6
address feedback comments
martriay Sep 19, 2023
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
25 changes: 14 additions & 11 deletions docs/modules/ROOT/nav.adoc
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
* xref:index.adoc[Overview]
* xref:wizard.adoc[Wizard]
* xref:extensibility.adoc[Extensibility]
* xref:proxies.adoc[Proxies and Upgrades]
//* xref:wizard.adoc[Wizard]
//* xref:extensibility.adoc[Extensibility]
//* xref:proxies.adoc[Proxies and Upgrades]
* xref:interfaces.adoc[Interfaces and Dispatchers]

* xref:accounts.adoc[Accounts]
** xref:/guides/deployment.adoc[Counterfactual deployments]
** xref:/api/account.adoc[API Reference]

* xref:access.adoc[Access Control]

* Tokens
** xref:erc20.adoc[ERC20]
** xref:erc721.adoc[ERC721]
** xref:erc1155.adoc[ERC1155]
//* xref:access.adoc[Access Control]

* xref:security.adoc[Security]
* xref:introspection.adoc[Introspection]
* xref:udc.adoc[Universal Deployer Contract]
* xref:utilities.adoc[Utilities]
//* Tokens
//** xref:erc20.adoc[ERC20]
//** xref:erc721.adoc[ERC721]
//** xref:erc1155.adoc[ERC1155]

//* xref:security.adoc[Security]
//* xref:introspection.adoc[Introspection]
//* xref:udc.adoc[Universal Deployer Contract]
//* xref:utilities.adoc[Utilities]

* xref:contracts::index.adoc[Contracts for Solidity]
10 changes: 10 additions & 0 deletions docs/modules/ROOT/pages/accounts.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@ In this section we will describe the methods that the protocol uses for abstract
are required for enabling accounts to be used for executing transactions. The rest are optional:

1. `\\__validate__` verifies the validity of the transaction to be executed. This is usually used to validate signatures,
<<<<<<< HEAD
but following the account abstraction design, the entrypoint implementation can be customized to feature any
validation mechanism.
=======
but the entrypoint implementation can be customized to feature any validation mechanism https://docs.starknet.io/documentation/architecture_and_concepts/Accounts/validate_and_execute/#validate_limitations[with some limitations].
>>>>>>> cairo-2
martriay marked this conversation as resolved.
Show resolved Hide resolved

2. `\\__execute__` executes the transaction if the validation is successful.

Expand All @@ -87,7 +92,12 @@ meant to declare other contracts.

4. `\\__validate_deploy__` optional entrypoint similar to `\\__validate__` but meant for {counterfactual}.

<<<<<<< HEAD
NOTE: Even though these entrypoints _can_ be called directly at the contract level, they are not designed for that,
but to be called by the Starknet OS in the transaction execution flow.
=======
NOTE: Although these entrypoints are available to the protocol for its regular transaction flow, they can also be called like any other method.
>>>>>>> cairo-2

== Deploying an account

Expand Down
181 changes: 181 additions & 0 deletions docs/modules/ROOT/pages/interfaces.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
= Interfaces and Dispatchers

This section describes the interfaces OpenZeppelin Contracts for Cairo offer, and explains the design choices behind them.

Interfaces can be found in the module tree under the `interface` submodule, such as `token::erc20::interface`. For example:

```javascript
use openzeppelin::token::erc20::interface::IERC20;
```

or
martriay marked this conversation as resolved.
Show resolved Hide resolved

```javascript
use openzeppelin::token::erc20::dual20::DualCaseERC20;
```

NOTE: For simplicity, we'll use ERC20 as example but the same concepts apply to other modules.

== Interface traits
The library offers three types of traits to implement or interact with contracts:

=== Standard traits

These are associated with a predefined interface such as a standard.
This includes only the functions defined in the interface, and is the standard way to interact with a compliant contract.

```javascript
#[starknet::interface]
trait IERC20<TState> {
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn decimals(self: @TState) -> u8;
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}
```

=== ABI traits

They describe a contract's complete interface. This is useful to interface with a preset contract offered by this library, such as the ERC20 preset that includes non-standard functions like `increase_allowance`.

```javascript
#[starknet::interface]
trait ERC20ABI<TState> {
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn decimals(self: @TState) -> u8;
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
fn increase_allowance(ref self: TState, spender: ContractAddress, added_value: u256) -> bool;
fn decrease_allowance(
ref self: TState, spender: ContractAddress, subtracted_value: u256
) -> bool;
}
```

=== Dispatcher traits
This is a utility trait to interface with contracts whose interface is unknown. Read more in the xref:#dualcase_dispatchers[DualCase Dispatchers] section.

```javascript
#[derive(Copy, Drop)]
struct DualCaseERC20 {
contract_address: ContractAddress
}

trait DualCaseERC20Trait {
fn name(self: @DualCaseERC20) -> felt252;
fn symbol(self: @DualCaseERC20) -> felt252;
fn decimals(self: @DualCaseERC20) -> u8;
fn total_supply(self: @DualCaseERC20) -> u256;
fn balance_of(self: @DualCaseERC20, account: ContractAddress) -> u256;
fn allowance(self: @DualCaseERC20, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(self: @DualCaseERC20, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(
self: @DualCaseERC20, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(self: @DualCaseERC20, spender: ContractAddress, amount: u256) -> bool;
}
```

== Dual interfaces

Following the https://community.starknet.io/t/the-great-interface-migration/92107[Great Interface Migration] plan, we added `snake_case` functions to all of our preexisting `camelCase` contracts with the goal of eventually dropping support for the latter.

In short, we offer two types of interfaces and utilities to handle them:

1. `camelCase` interfaces, which are the ones we've been using so far.
2. `snake_case` interfaces, which are the ones we're migrating to.

This means that currently most of our contracts implement _dual interfaces_. For example, the ERC20 preset contract exposes `transferFrom`, `transfer_from`, `balanceOf`, `balance_of`, etc.
ericnordelo marked this conversation as resolved.
Show resolved Hide resolved

NOTE: Dual interfaces are available for all external functions present in previous versions of OpenZeppelin Contracts for Cairo (https://github.com/OpenZeppelin/cairo-contracts/releases/tag/v0.6.1[v0.6.1] and below).

=== `IERC20`

The default version of the ERC20 interface trait exposes `snake_case` functions:

```javascript
#[starknet::interface]
trait IERC20<TState> {
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn decimals(self: @TState) -> u8;
fn total_supply(self: @TState) -> u256;
fn balance_of(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transfer_from(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}
```

=== `IERC20Camel`

On top of that, we also offer a `camelCase` version of the same interface:

```javascript
#[starknet::interface]
trait IERC20Camel<TState> {
fn name(self: @TState) -> felt252;
fn symbol(self: @TState) -> felt252;
fn decimals(self: @TState) -> u8;
fn totalSupply(self: @TState) -> u256;
fn balanceOf(self: @TState, account: ContractAddress) -> u256;
fn allowance(self: @TState, owner: ContractAddress, spender: ContractAddress) -> u256;
fn transfer(ref self: TState, recipient: ContractAddress, amount: u256) -> bool;
fn transferFrom(
ref self: TState, sender: ContractAddress, recipient: ContractAddress, amount: u256
) -> bool;
fn approve(ref self: TState, spender: ContractAddress, amount: u256) -> bool;
}
```

== `DualCase` dispatchers

WARNING: `DualCase` dispatchers won't work on live chains (`mainnet` or testnets) until they implement panic handling in their runtime. Dispatchers work fine in testing environments.

In order to ease this transition, OpenZeppelin Contracts for Cairo offer what we call `DualCase` dispatchers such as `DualCaseERC721` or `DualCaseAccount`.

These modules wrap a target contract with a compatibility layer to expose a `snake_case` interface no matter what casing the underlying contract uses.
This way, an AMM wouldn't have problems integrating tokens independently of their interface.

For example:

```javascript
let token = DualCaseERC20 { contract_address: target };
token.transfer_from(OWNER(), RECIPIENT(), VALUE);
```

This is done by simply executing the `snake_case` version of the function (e.g. `transfer_from`) and falling back to the `camelCase` one (e.g. `transferFrom`) in case it reverts with `ENTRYPOINT_NOT_FOUND`, like this:
martriay marked this conversation as resolved.
Show resolved Hide resolved

```javascript
fn try_selector_with_fallback(
target: ContractAddress, snake_selector: felt252, camel_selector: felt252, args: Span<felt252>
) -> SyscallResult<Span<felt252>> {
match call_contract_syscall(target, snake_selector, args) {
Result::Ok(ret) => Result::Ok(ret),
Result::Err(errors) => {
if *errors.at(0) == 'ENTRYPOINT_NOT_FOUND' {
return call_contract_syscall(target, camel_selector, args);
} else {
Result::Err(errors)
}
}
}
}
```
martriay marked this conversation as resolved.
Show resolved Hide resolved