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

feat: serialization to and from felts #669

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
16 changes: 14 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ exclude = [".github/**", "images/**"]
[workspace]
members = [
"starknet-core",
"starknet-core-derive",
"starknet-providers",
"starknet-contract",
"starknet-crypto",
Expand All @@ -34,6 +35,7 @@ all-features = true
[dependencies]
starknet-crypto = { version = "0.7.2", path = "./starknet-crypto" }
starknet-core = { version = "0.12.0", path = "./starknet-core", default-features = false }
starknet-core-derive = { version = "0.1.0", path = "./starknet-core-derive", features = ["import_from_starknet"] }
starknet-providers = { version = "0.12.0", path = "./starknet-providers" }
starknet-contract = { version = "0.11.0", path = "./starknet-contract" }
starknet-signers = { version = "0.10.0", path = "./starknet-signers" }
Expand Down
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This workspace contains the following crates:
- `starknet-accounts`: Types for handling Starknet account abstraction
- `starknet-curve`: Starknet curve operations
- `starknet-macros`: Useful macros for using the `starknet` crates
- `starknet-core-derive`: Derive macros for traits in `starknet-core`

## WebAssembly

Expand Down Expand Up @@ -92,21 +93,23 @@ Examples can be found in the [examples folder](./examples):

6. [Query the latest block number with JSON-RPC](./examples/jsonrpc.rs)

7. [Batched JSON-RPC requests](./examples/batch.rs)
7. [Encoding and decoding Cairo types](./examples/serde.rs)

8. [Call a contract view function](./examples/erc20_balance.rs)
8. [Batched JSON-RPC requests](./examples/batch.rs)

9. [Deploy an Argent X account to a pre-funded address](./examples/deploy_argent_account.rs)
9. [Call a contract view function](./examples/erc20_balance.rs)

10. [Inspect public key with Ledger](./examples/ledger_public_key.rs)
10. [Deploy an Argent X account to a pre-funded address](./examples/deploy_argent_account.rs)

11. [Deploy an OpenZeppelin account with Ledger](./examples/deploy_account_with_ledger.rs)
11. [Inspect public key with Ledger](./examples/ledger_public_key.rs)

12. [Transfer ERC20 tokens with Ledger](./examples/transfer_with_ledger.rs)
12. [Deploy an OpenZeppelin account with Ledger](./examples/deploy_account_with_ledger.rs)

13. [Parsing a JSON-RPC request on the server side](./examples/parse_jsonrpc_request.rs)
13. [Transfer ERC20 tokens with Ledger](./examples/transfer_with_ledger.rs)

14. [Inspecting a erased provider-specific error type](./examples/downcast_provider_error.rs)
14. [Parsing a JSON-RPC request on the server side](./examples/parse_jsonrpc_request.rs)

15. [Inspecting a erased provider-specific error type](./examples/downcast_provider_error.rs)

## License

Expand Down
1 change: 1 addition & 0 deletions assets/CORE_DERIVE_README.md
34 changes: 34 additions & 0 deletions examples/serde.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use starknet::{
core::{
codec::{Decode, Encode},
types::Felt,
},
macros::felt,
};

#[derive(Debug, Eq, PartialEq, Encode, Decode)]
struct CairoType {
a: Felt,
b: Option<u32>,
c: bool,
}

fn main() {
let instance = CairoType {
a: felt!("123456789"),
b: Some(100),
c: false,
};

let mut serialized = vec![];
instance.encode(&mut serialized).unwrap();

assert_eq!(
serialized,
[felt!("123456789"), felt!("0"), felt!("100"), felt!("0")]
);

let restored = CairoType::decode(&serialized).unwrap();

assert_eq!(instance, restored);
}
28 changes: 28 additions & 0 deletions starknet-core-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "starknet-core-derive"
version = "0.1.0"
authors = ["Jonathan LEI <[email protected]>"]
license = "MIT OR Apache-2.0"
edition = "2021"
readme = "README.md"
repository = "https://github.com/xJonathanLEI/starknet-rs"
homepage = "https://starknet.rs/"
description = """
Procedural macros for `starknet-core`
"""
keywords = ["ethereum", "starknet", "web3"]

[lib]
proc-macro = true

[dependencies]
proc-macro2 = "1.0.88"
quote = "1.0.37"
syn = "2.0.15"

[features]
default = []
import_from_starknet = []

[lints]
workspace = true
14 changes: 14 additions & 0 deletions starknet-core-derive/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Procedural macros for `starknet-core`

This crate provides procedural macros for deriving the `Encode` and `Decode` traits from `starknet-core`. This allows defining a type like:

```rust
#[derive(Debug, PartialEq, Eq, Decode, Encode)]
struct CairoType {
a: Felt,
b: U256,
c: bool,
}
```

and using the `::encode()` and `::decode()` methods, without manually implementing the corresponding traits.
Loading
Loading