Skip to content

Commit

Permalink
refac: replace ERC20 LegacyMap with Map
Browse files Browse the repository at this point in the history
  • Loading branch information
sprtd committed Oct 24, 2024
1 parent a671793 commit 732741d
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 76 deletions.
2 changes: 1 addition & 1 deletion crates/cheatnet/tests/cheatcodes/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ fn store_state_map_simple_value() {
test_env.call_contract(&contract_address, "read", &[map_key]),
&[inserted_value.into()],
);
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
#[starknet::contract]
mod MapSimpleValueSimpleKey {
use starknet::{
storage::{StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess, StoragePathEntry, Map}};
#[storage]
struct Storage {
values: LegacyMap<felt252, felt252>,
values: Map<felt252, felt252>,
}

#[external(v0)]
fn insert(ref self: ContractState, key: felt252, value: felt252) {
self.values.write(key, value);
self.values.entry(key).write(value);
}

#[external(v0)]
fn read(self: @ContractState, key: felt252) -> felt252 {
self.values.read(key)
self.values.entry(key).read()
}
}
24 changes: 12 additions & 12 deletions crates/forge/tests/data/contracts/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ trait IERC20<TContractState> {

#[starknet::contract]
mod ERC20 {
use starknet::{get_caller_address, contract_address_const, ContractAddress,
storage::{StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess, StoragePathEntry, Map},
};
use zeroable::Zeroable;
use starknet::get_caller_address;
use starknet::contract_address_const;
use starknet::ContractAddress;

#[storage]
struct Storage {
name: felt252,
symbol: felt252,
decimals: u8,
total_supply: u256,
balances: LegacyMap::<ContractAddress, u256>,
allowances: LegacyMap::<(ContractAddress, ContractAddress), u256>,
balances: Map<ContractAddress, u256>,
allowances: Map<(ContractAddress, ContractAddress), u256>,
}

#[event]
Expand Down Expand Up @@ -69,7 +69,7 @@ mod ERC20 {
self.decimals.write(decimals_);
assert(!recipient.is_zero(), 'ERC20: mint to the 0 address');
self.total_supply.write(initial_supply);
self.balances.write(recipient, initial_supply);
self.balances.entry(recipient).write(initial_supply);
self
.emit(
Event::Transfer(
Expand Down Expand Up @@ -99,13 +99,13 @@ mod ERC20 {
}

fn balance_of(self: @ContractState, account: ContractAddress) -> u256 {
self.balances.read(account)
self.balances.entry(account).read()
}

fn allowance(
self: @ContractState, owner: ContractAddress, spender: ContractAddress
) -> u256 {
self.allowances.read((owner, spender))
self.allowances.entry((owner, spender)).read()
}

fn transfer(ref self: ContractState, recipient: ContractAddress, amount: u256) {
Expand Down Expand Up @@ -160,8 +160,8 @@ mod ERC20 {
) {
assert(!sender.is_zero(), 'ERC20: transfer from 0');
assert(!recipient.is_zero(), 'ERC20: transfer to 0');
self.balances.write(sender, self.balances.read(sender) - amount);
self.balances.write(recipient, self.balances.read(recipient) + amount);
self.balances.entry(sender).write(self.balances.entry(sender).read() - amount);
self.balances.entry(recipient).write(self.balances.entry(recipient).read() + amount);
self.emit(Event::Transfer(Transfer { from: sender, to: recipient, value: amount }));
}

Expand All @@ -181,8 +181,8 @@ mod ERC20 {
ref self: ContractState, owner: ContractAddress, spender: ContractAddress, amount: u256
) {
assert(!spender.is_zero(), 'ERC20: approve from 0');
self.allowances.write((owner, spender), amount);
self.allowances.entry((owner, spender)).write(amount);
self.emit(Event::Approval(Approval { owner, spender, value: amount }));
}
}
}
}
16 changes: 7 additions & 9 deletions crates/forge/tests/data/erc20_package/src/erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,17 @@ trait IERC20<TContractState> {

#[starknet::contract]
mod ERC20 {
use starknet::{ contract_address_const, get_caller_address, ContractAddress,
storage::{StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess, StoragePathEntry, Map}, };
use zeroable::Zeroable;
use starknet::get_caller_address;
use starknet::contract_address_const;
use starknet::ContractAddress;

#[storage]
struct Storage {
name: felt252,
symbol: felt252,
decimals: u8,
total_supply: u256,
balances: LegacyMap::<ContractAddress, u256>,
allowances: LegacyMap::<(ContractAddress, ContractAddress), u256>,
balances: Map<ContractAddress, u256>,
allowances: Map<(ContractAddress, ContractAddress), u256>,
}

#[event]
Expand Down Expand Up @@ -69,7 +67,7 @@ mod ERC20 {
self.decimals.write(decimals_);
assert(!recipient.is_zero(), 'ERC20: mint to the 0 address');
self.total_supply.write(initial_supply);
self.balances.write(recipient, initial_supply);
self.balances.entry(recipient).write(initial_supply);
self
.emit(
Event::Transfer(
Expand Down Expand Up @@ -160,8 +158,8 @@ mod ERC20 {
) {
assert(!sender.is_zero(), 'ERC20: transfer from 0');
assert(!recipient.is_zero(), 'ERC20: transfer to 0');
self.balances.write(sender, self.balances.read(sender) - amount);
self.balances.write(recipient, self.balances.read(recipient) + amount);
self.balances.entry(sender).write(self.balances.read(sender) - amount);
self.balances.entry(recipient).write(self.balances.read(recipient) + amount);
self.emit(Event::Transfer(Transfer { from: sender, to: recipient, value: amount }));
}

Expand Down
6 changes: 4 additions & 2 deletions crates/forge/tests/integration/test_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ fn storage_access_default_values() {
#[storage]
struct Storage {
balance: felt252,
legacy_map: LegacyMap<felt252, felt252>,
legacy_map: Map<felt252, felt252>,
custom_struct: CustomStruct,
}
}
Expand All @@ -427,7 +427,7 @@ fn storage_access_default_values() {
let default_felt252 = state.balance.read();
assert(default_felt252 == 0, 'Incorrect storage value');
let default_map_value = state.legacy_map.read(22);
let default_map_value = state.legacy_map.entry(22).read();
assert(default_map_value == 0, 'Incorrect map value');
let default_custom_struct = state.custom_struct.read();
Expand Down Expand Up @@ -859,3 +859,5 @@ fn felt252_dict_usage() {

assert_passed(&result);
}


25 changes: 10 additions & 15 deletions crates/sncast/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ Note, that `sncast` only officially supports contracts written in Cairo 2.
## Table of contents

<!-- TOC -->
* [Installation](#installation)
* [Documentation](#documentation)
* [Example usages](#example-usages)
* [Declaring contracts](#declare-a-contract)
* [Deploying contracts](#deploy-a-contract)
* [Invoking contracts](#invoke-a-contract)
* [Calling contracts](#call-a-contract)
* [Development](#development)

- [Installation](#installation)
- [Documentation](#documentation)
- [Example usages](#example-usages)
- [Declaring contracts](#declare-a-contract)
- [Deploying contracts](#deploy-a-contract)
- [Invoking contracts](#invoke-a-contract)
- [Calling contracts](#call-a-contract)
- [Development](#development)
<!-- TOC -->

## Installation
Expand All @@ -23,7 +24,7 @@ You can download latest version of `sncast` [here](https://github.com/foundry-rs

## Documentation

For more details on Starknet Foundry `sncast`, please visit [our docs](https://foundry-rs.github.io/starknet-foundry/starknet/index.html)
For more details on Starknet Foundry `sncast`, please visit [our docs](https://foundry-rs.github.io/starknet-foundry/starknet/index.html)

## Example usages

Expand All @@ -42,7 +43,6 @@ class_hash: 0x8448a68b5ea1affc45e3fd4b8b480ea36a51dc34e337a16d2567d32d0c6f8a
transaction_hash: 0x7ad0d6e449e33b6581a4bb8df866c0fce3919a5ee05a30840ba521dafee217f
```


With arguments taken from `snfoundry.toml` file (default profile name):

```shell
Expand All @@ -66,7 +66,6 @@ contract_address: 0x301316d47a81b39c5e27cca4a7b8ca4773edbf1103218588d6da4d3ed530
transaction_hash: 0x64a62a000240e034d1862c2bbfa154aac6a8195b4b2e570f38bf4fd47a5ab1e
```


With arguments taken from `snfoundry.toml` file (default profile name):

```shell
Expand All @@ -77,7 +76,6 @@ contract_address: 0x301316d47a81b39c5e27cca4a7b8ca4773edbf1103218588d6da4d3ed530
transaction_hash: 0x64a62a000240e034d1862c2bbfa154aac6a8195b4b2e570f38bf4fd47a5ab1e
```


### Invoke a contract

```shell
Expand All @@ -92,7 +90,6 @@ command: Invoke
transaction_hash: 0x7ad0d6e449e33b6581a4bb8df866c0fce3919a5ee05a30840ba521dafee217f
```


With arguments taken from `snfoundry.toml` file (default profile name):

```shell
Expand All @@ -118,7 +115,6 @@ command: call
response: [0x0]
```


With arguments taken from `snfoundry.toml` file (default profile name):

```shell
Expand All @@ -131,7 +127,6 @@ command: call
response: [0x0]
```


## Development

Refer to [documentation](https://foundry-rs.github.io/starknet-foundry/development/environment-setup.html) to make sure you have all the pre-requisites, and to obtain an information on how to help to develop `sncast`.
Expand Down
14 changes: 10 additions & 4 deletions crates/sncast/tests/data/contracts/map/src/lib.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ trait IMap<TMapState> {

#[starknet::contract]
mod Map {
use starknet::{
storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess,
StoragePathEntry, Map
}
};
#[storage]
struct Storage {
storage: LegacyMap::<felt252, felt252>,
storage: Map<felt252, felt252>,
}

#[abi(embed_v0)]
impl Map of super::IMap<ContractState> {
impl MapImpl of super::IMap<ContractState> {
fn put(ref self: ContractState, key: felt252, value: felt252) {
self.storage.write(key, value);
self.storage.entry(key).write(value);
}

fn get(self: @ContractState, key: felt252) -> felt252 {
self.storage.read(key)
self.storage.entry(key).read()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ trait IMap<TMapState> {
#[starknet::contract]
mod Mapa {
use starknet::{
storage::{StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess, StoragePathEntry, Map}
storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess,
StoragePathEntry, Map
}
};

#[storage]
Expand All @@ -36,7 +39,10 @@ mod Mapa {
#[starknet::contract]
mod Mapa2 {
use starknet::{
storage::{StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess, StoragePathEntry, Map}
storage::{
StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess,
StoragePathEntry, Map
}
};

#[storage]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ trait IState<TState> {

#[starknet::contract]
mod State {
use starknet::{
storage::{StoragePointerReadAccess, StoragePointerWriteAccess, StorageMapReadAccess, StoragePathEntry, Map}};

#[storage]
struct Storage {
storage: LegacyMap::<felt252, felt252>,
storage: Map<felt252, felt252>,
}

#[abi(embed_v0)]
impl State of super::IState<ContractState> {
fn put(ref self: ContractState, key: felt252, value: felt252) {
self.storage.write(key, value);
self.storage.entry(key).write(value);
}

fn get(self: @ContractState, key: felt252) -> felt252 {
self.storage.read(key)
self.storage.entry(key).read()
}

fn dummy(self: @ContractState) -> felt252 {
Expand Down
8 changes: 4 additions & 4 deletions design_documents/stark_curve_signatures.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
## Context

Some users would like to have a way of generating and signing with the `stark curve`. It would be useful for testing
custom account implementations.
custom account implementations.

## Existing solutions

Expand All @@ -17,7 +17,7 @@ My proposal would be to introduce a `StarkCurveKeyPair` struct which would imple
```cairo
struct StarkCurveKeyPair {
private_key: felt252,
public_key: felt252
public_key: felt252
}
trait StarkCurveKeyPairTrait {
Expand All @@ -36,9 +36,9 @@ use snforge_std::{StarkCurveKeyPair, StarkCurveKeyPairTrait};
fn test_stark_curve() {
let mut key_pair = StarkCurveKeyPairTrait::generate();
let message_hash = 12345;
let signature = key_pair.sign(message_hash);
assert(key_pair.verify(message_hash, signature), 'Signature is incorrect');
}
```
Loading

0 comments on commit 732741d

Please sign in to comment.