Skip to content

Commit

Permalink
cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Sep 24, 2023
1 parent 5c9628e commit d1fefcd
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 65 deletions.
23 changes: 11 additions & 12 deletions examples/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,9 @@ use starknet::{
// Note here, we import an ABI type. This applies for
// ContractAddress, ClassHash, EthAddress only.
accounts::{ExecutionEncoding, SingleOwnerAccount},
core::{
chain_id,
types::FieldElement,
},
contract::abi::ContractAddress,
macros::{felt, abigen},
core::{chain_id, types::FieldElement},
macros::{abigen, felt},
providers::{Provider, SequencerGatewayProvider},
signers::{LocalWallet, SigningKey},
};
Expand All @@ -29,8 +26,8 @@ async fn main() {

// To call a view, there is no need to initialize an account. You can directly
// use the name of the method in the ABI to realize the call.
let balance: u256 = token_contract.balanceOf(
&ContractAddress(felt!("YOUR_ACCOUNT_ADDRESS_HEX_HERE")))
let balance: u256 = token_contract
.balanceOf(&ContractAddress(felt!("YOUR_ACCOUNT_ADDRESS_HEX_HERE")))
.await
.expect("Call to get balance failed");

Expand All @@ -54,11 +51,13 @@ async fn main() {

let token_contract = token_contract.with_account(Arc::new(account));

token_contract.approve(&ContractAddress(felt!("SPENDER_ADDRESS_HEX")), &u256 {
low: 10000,
high: 0
});

token_contract.approve(
&ContractAddress(felt!("SPENDER_ADDRESS_HEX")),
&u256 {
low: 10000,
high: 0,
},
);

println!("Your balance: {:?}", balance);
}
41 changes: 21 additions & 20 deletions examples/abigen_events.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use starknet::{
core::{
types::{FieldElement, EventFilter},
},
accounts::{SingleOwnerAccount, ExecutionEncoding},
macros::{felt, abigen},
providers::{Provider, JsonRpcClient, jsonrpc::HttpTransport},
accounts::{ExecutionEncoding, SingleOwnerAccount},
core::types::{EventFilter, FieldElement},
macros::{abigen, felt},
providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider},
signers::{LocalWallet, SigningKey},
};

use url::Url;
use std::sync::Arc;
use url::Url;

// All the events are always grouped in one enun called `Event`
// in the ABI.
Expand All @@ -34,15 +32,15 @@ async fn main() {

let contract_address = felt!("CONTRACT_ADDRESS_HEX");

let event_contract = Contract::new(contract_address, Arc::clone(&provider))
.with_account(Arc::new(account));
let event_contract =
Contract::new(contract_address, Arc::clone(&provider)).with_account(Arc::new(account));

// Let emits some events by calling two externals.
event_contract
.emit_a(&FieldElement::ONE, &vec![felt!("0xff"), felt!("0xf1")])
.await
.expect("Emit a invoke failed");

event_contract
.emit_b(&felt!("0x1234"))
.await
Expand All @@ -51,16 +49,19 @@ async fn main() {
// Fetch events with some filters with a chunck size of 100 without continuation
// token.
// This will not work on the gateway, you need to use JsonRPC node.
let event_page = provider.get_events(
EventFilter {
from_block: Some(BlockId::Number(0)),
to_block: Some(BlockId::Tag(BlockTag::Latest)),
address: None,
keys: None,
},
None,
100,
).await.expect("Fetch events failed");
let event_page = provider
.get_events(
EventFilter {
from_block: Some(BlockId::Number(0)),
to_block: Some(BlockId::Tag(BlockTag::Latest)),
address: None,
keys: None,
},
None,
100,
)
.await
.expect("Fetch events failed");

for e in event_page.events {
// abigen! macro generate for you the `TryFrom<EmittedEvent` for the
Expand Down
2 changes: 1 addition & 1 deletion starknet-contract/src/abi/cairo_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ mod error;
pub use error::{Error, Result};

pub mod types;
pub use types::*;
pub use types::starknet::*;
pub use types::*;

use starknet_core::types::FieldElement;

Expand Down
2 changes: 1 addition & 1 deletion starknet-contract/src/abi/parser/cairo_struct.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::collections::HashMap;
use starknet_core::types::contract::AbiNamedMember;
use std::collections::HashMap;

use super::abi_types::{AbiType, AbiTypeAny};

Expand Down
25 changes: 9 additions & 16 deletions starknet-macros/src/abigen/contract_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
//! [{ .... }]
//! "#);
//!
use starknet_core::types::contract::AbiEntry;
use std::fs::File;
use syn::{
parse::{Parse, ParseStream, Result},
Ident, LitStr, Token,
};
use starknet_core::types::contract::AbiEntry;

#[derive(Clone, Debug)]
pub(crate) struct ContractAbi {
Expand All @@ -37,20 +37,13 @@ impl Parse for ContractAbi {

// Path rooted to the Cargo.toml location.
let json_path = input.parse::<LitStr>()?;

let abi = serde_json::from_reader::<_, Vec<AbiEntry>>(
File::open(json_path.value())
.map_err(|e| {
syn::Error::new(json_path.span(), format!("JSON open file error: {}", e))
})?
)
.map_err(|e| {
syn::Error::new(json_path.span(), format!("JSON parse error: {}", e))
})?;

Ok(ContractAbi {
name,
abi,
})

let abi =
serde_json::from_reader::<_, Vec<AbiEntry>>(File::open(json_path.value()).map_err(
|e| syn::Error::new(json_path.span(), format!("JSON open file error: {}", e)),
)?)
.map_err(|e| syn::Error::new(json_path.span(), format!("JSON parse error: {}", e)))?;

Ok(ContractAbi { name, abi })
}
}
7 changes: 4 additions & 3 deletions starknet-macros/src/abigen/expand/enum.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Enums expansion, taking in account generic types if any.
use super::{
generic, Expandable,
utils::{str_to_ident, str_to_type}
generic,
utils::{str_to_ident, str_to_type},
Expandable,
};

use starknet_contract::abi::parser::{
CairoEnum,
abi_types::{AbiType, AbiTypeAny},
CairoEnum,
};

use proc_macro2::TokenStream as TokenStream2;
Expand Down
4 changes: 2 additions & 2 deletions starknet-macros/src/abigen/expand/event.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
//! Events expansion.
use super::{
utils::{str_to_ident, str_to_litstr, str_to_type},
Expandable, ExpandableEvent,
utils::{str_to_ident, str_to_litstr, str_to_type}
};

use starknet_contract::abi::parser::{
abi_types::{AbiType, AbiTypeAny},
CairoEvent, CairoEventInner,
abi_types::{AbiType, AbiTypeAny}
};
use starknet_core::types::contract::EventFieldKind;

Expand Down
8 changes: 4 additions & 4 deletions starknet-macros/src/abigen/expand/function.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use super::{
Expandable,
utils::{str_to_ident, str_to_type},
Expandable,
};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use starknet_contract::abi::parser::{
abi_types::{AbiType, AbiTypeAny},
CairoFunction,
abi_types::{AbiType, AbiTypeAny}
};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use starknet_core::types::contract::StateMutability;

impl Expandable for CairoFunction {
Expand Down
2 changes: 1 addition & 1 deletion starknet-macros/src/abigen/expand/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub(crate) mod generic;
pub(crate) mod r#struct;
pub(crate) mod utils;

use starknet_contract::abi::parser::CairoEvent;
use proc_macro2::TokenStream as TokenStream2;
use starknet_contract::abi::parser::CairoEvent;

pub trait Expandable {
fn expand_decl(&self) -> TokenStream2;
Expand Down
7 changes: 4 additions & 3 deletions starknet-macros/src/abigen/expand/struct.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Struct expansion, taking in account generic types if any.
use super::{
generic, Expandable,
utils::{str_to_ident, str_to_type}
generic,
utils::{str_to_ident, str_to_type},
Expandable,
};

use starknet_contract::abi::parser::{
CairoStruct,
abi_types::{AbiType, AbiTypeAny},
CairoStruct,
};

use proc_macro2::TokenStream as TokenStream2;
Expand Down
4 changes: 2 additions & 2 deletions starknet-macros/src/abigen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
//! checked for genericty to avoid duplicated types and detect correctly
//! the members/variants that are generic.
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
use syn::parse_macro_input;
use proc_macro2::TokenStream as TokenStream2;

use std::collections::HashMap;

use starknet_contract::abi::parser::{CairoEnum, CairoEvent, CairoFunction, CairoStruct};
use starknet_contract::abi::cairo_types::{CAIRO_BASIC_ENUMS, CAIRO_BASIC_STRUCTS};
use starknet_contract::abi::parser::{CairoEnum, CairoEvent, CairoFunction, CairoStruct};
use starknet_core::types::contract::AbiEntry;

mod expand;
Expand Down

0 comments on commit d1fefcd

Please sign in to comment.