Skip to content

Commit

Permalink
Improve SNIP12 examples (#1036)
Browse files Browse the repository at this point in the history
* docs: improve examples

* refactor: remove empty line

* Update docs/modules/ROOT/pages/guides/snip12.adoc

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

---------

Co-authored-by: Andrew Fleming <[email protected]>
  • Loading branch information
ericnordelo and andrew-fleming authored Jul 5, 2024
1 parent 65894fb commit 4f4e2e4
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions docs/modules/ROOT/pages/guides/snip12.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,25 @@ impl SNIP12MetadataImpl of SNIP12Metadata {
}
----

NOTE: These params could be set in the contract constructor, but then two storage reads would be executed every time
a message hash needs to be generated, and this is unnecessary overhead. When Starknet implements immutable storage
set in constructor, that approach will be more efficient.
In the above example, no storage reads are required which avoids unnecessary extra gas costs, but in
some cases we may need to read from storage to get the domain separator values. This can be accomplished even when
the trait is not bounded to the ContractState, like this:

[,cairo]
----
use openzeppelin::utils::snip12::SNIP12Metadata;
impl SNIP12MetadataImpl of SNIP12Metadata {
fn name() -> felt252 {
let state = unsafe_new_contract_state();
// Some logic to get the name from storage
state.erc20.name().at(0).unwrap().into()
}
fn version() -> felt252 { 'v1' }
}
----

=== 5. Generate the hash.

Expand Down

0 comments on commit 4f4e2e4

Please sign in to comment.