Skip to content

Commit

Permalink
fix some use to don't clash with main imports
Browse files Browse the repository at this point in the history
  • Loading branch information
glihm committed Sep 24, 2023
1 parent d1fefcd commit f94c1c5
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
3 changes: 3 additions & 0 deletions examples/abigen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use starknet::{

use std::sync::Arc;

// TODO: search where this one is missing...!
use starknet::contract::abi::cairo_types;

// Generate the bindings for the contract and also includes
// all the structs and enums present in the ABI with the exact
// same name.
Expand Down
4 changes: 2 additions & 2 deletions examples/abigen_events.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use starknet::{
accounts::{ExecutionEncoding, SingleOwnerAccount},
core::types::{EventFilter, FieldElement},
accounts::{Account, ExecutionEncoding, SingleOwnerAccount},
core::types::{BlockId, BlockTag, EventFilter, FieldElement},
macros::{abigen, felt},
providers::{jsonrpc::HttpTransport, JsonRpcClient, Provider},
signers::{LocalWallet, SigningKey},
Expand Down
5 changes: 0 additions & 5 deletions starknet-macros/src/abigen/expand/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ impl CairoContract {
pub fn expand(contract_name: Ident) -> TokenStream2 {
quote! {

use starknet::contract::abi::cairo_types::{self, Error as CairoError};
use starknet::contract::abi::CairoType;
use starknet::core::types::{BlockId, BlockTag};
use starknet::accounts::Account;

#[derive(Debug)]
pub struct #contract_name<P>
where
Expand Down
5 changes: 3 additions & 2 deletions starknet-macros/src/abigen/expand/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Expandable for CairoEnum {
let impl_line = if self.is_generic() {
generic::impl_with_gentys_tokens(&enum_name, &gentys)
} else {
quote!(impl cairo_types::CairoType for #enum_name)
quote!(impl starknet::contract::abi::CairoType for #enum_name)
};

let rust_type = if self.is_generic() {
Expand Down Expand Up @@ -138,7 +138,8 @@ impl Expandable for CairoEnum {
}
}

fn deserialize(felts: &[starknet::core::types::FieldElement], offset: usize) -> cairo_types::Result<Self::RustType> {
fn deserialize(felts: &[starknet::core::types::FieldElement], offset: usize) -> starknet::contract::abi::cairo_types::Result<Self::RustType> {

let index:u128 = felts[offset].try_into().unwrap();
match index as usize {
#(#deserializations),*
Expand Down
2 changes: 2 additions & 0 deletions starknet-macros/src/abigen/expand/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ impl ExpandableEvent for CairoEvent {
type Error = String;

fn try_from(event: starknet::core::types::EmittedEvent) -> Result<Self, Self::Error> {
use starknet::contract::abi::CairoType;

if event.keys.is_empty() {
return Err("Missing event selector, no keys found".to_string());
}
Expand Down
24 changes: 16 additions & 8 deletions starknet-macros/src/abigen/expand/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Expandable for CairoFunction {
StateMutability::View => match &self.output {
Some(o) => {
let oty = str_to_type(&o.to_rust_type());
quote!(-> cairo_types::Result<#oty>)
quote!(-> starknet::contract::abi::cairo_types::Result<#oty>)
}
None => quote!(),
},
Expand Down Expand Up @@ -80,6 +80,10 @@ impl Expandable for CairoFunction {
match &self.state_mutability {
StateMutability::View => quote! {
#decl {
use starknet::contract::abi::cairo_types::{self, Error as CairoError};
use starknet::contract::abi::CairoType;
use starknet::core::types::{BlockId, BlockTag};

let mut calldata = vec![];
#(#serializations)*

Expand All @@ -94,7 +98,7 @@ impl Expandable for CairoFunction {
)
.await.map_err(
|err|
cairo_types::Error::Deserialize(
starknet::contract::abi::cairo_types::Error::Deserialize(
format!("Deserialization error {}", err)))?;

#out_res
Expand All @@ -110,6 +114,10 @@ impl Expandable for CairoFunction {
// The estimate only may be done at the function level, to avoid
// altering the contract instance itself and hence races.
#decl {
use starknet::contract::abi::cairo_types::{self, Error as CairoError};
use starknet::contract::abi::CairoType;
use starknet::accounts::Account;

// TODO: I don't know how to easily store the SingleOwnerAccount
// and it's generic types without complexifiying the whole typing.
// So it's constructed at every call. There is surely a better approach.
Expand Down Expand Up @@ -145,12 +153,12 @@ impl Expandable for CairoFunction {
#[cfg(test)]
mod tests {
use crate::Expandable;

Check failure on line 155 in starknet-macros/src/abigen/expand/function.rs

View workflow job for this annotation

GitHub Actions / WASM tests

unresolved import `crate::Expandable`
use cairo_type_parser::{
use proc_macro2::TokenStream2;

Check failure on line 156 in starknet-macros/src/abigen/expand/function.rs

View workflow job for this annotation

GitHub Actions / WASM tests

unresolved import `proc_macro2::TokenStream2`
use quote::quote;
use starknet::contract::abi::parser::{

Check failure on line 158 in starknet-macros/src/abigen/expand/function.rs

View workflow job for this annotation

GitHub Actions / WASM tests

failed to resolve: use of undeclared crate or module `starknet`
abi_types::{AbiType, AbiTypeAny},
CairoFunction,
};
use proc_macro2::TokenStream2;
use quote::quote;
use starknet::core::types::contract::StateMutability;

Check failure on line 162 in starknet-macros/src/abigen/expand/function.rs

View workflow job for this annotation

GitHub Actions / WASM tests

failed to resolve: use of undeclared crate or module `starknet`

#[test]
Expand All @@ -166,7 +174,7 @@ mod tests {
};
let te1 = cf.expand_decl();
let tef1: TokenStream2 = quote!(
pub async fn my_func(&self, v1: &starknet::core::types::FieldElement, v2: &starknet::core::types::FieldElement) -> cairo_types::Result<starknet::core::types::FieldElement>
pub async fn my_func(&self, v1: &starknet::core::types::FieldElement, v2: &starknet::core::types::FieldElement) -> starknet::contract::abi::cairo_types::Result<starknet::core::types::FieldElement>
);

assert_eq!(te1.to_string(), tef1.to_string());
Expand All @@ -191,7 +199,7 @@ mod tests {
&self,
v1: &starknet::core::types::FieldElement,
v2: &starknet::core::types::FieldElement
) -> cairo_types::Result<starknet::core::types::FieldElement> {
) -> starknet::contract::abi::cairo_types::Result<starknet::core::types::FieldElement> {
let mut calldata = vec![];
calldata.extend(starknet::core::types::FieldElement::serialize(v1));
calldata.extend(starknet::core::types::FieldElement::serialize(v2));
Expand All @@ -205,7 +213,7 @@ mod tests {
},
BlockId::Tag(BlockTag::Pending),
)
.await.map_err(|err| cairo_types::Error::Deserialize(format!("Deserialization error {:}" , err)))?;
.await.map_err(|err| starknet::contract::abi::cairo_types::Error::Deserialize(format!("Deserialization error {:}" , err)))?;

starknet::core::types::FieldElement::deserialize(&r, 0)
}
Expand Down
4 changes: 2 additions & 2 deletions starknet-macros/src/abigen/expand/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ pub fn impl_with_gentys_tokens(entity_name: &Ident, gentys: &Vec<Ident>) -> Toke
let mut tokens = vec![];

tokens.push(quote! {
impl<#(#gentys),* , #(#gentys_rust),*> cairo_types::CairoType for #entity_name<#(#gentys),*>
impl<#(#gentys),* , #(#gentys_rust),*> starknet::contract::abi::CairoType for #entity_name<#(#gentys),*>
where
});

for (i, g) in gentys.iter().enumerate() {
let gr = &gentys_rust[i];
tokens.push(quote!(#g: CairoType<RustType = #gr>,));
tokens.push(quote!(#g: starknet::contract::abi::CairoType<RustType = #gr>,));
}

quote!(#(#tokens)*)
Expand Down
4 changes: 2 additions & 2 deletions starknet-macros/src/abigen/expand/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl Expandable for CairoStruct {
let impl_line = if self.is_generic() {
generic::impl_with_gentys_tokens(&struct_name, &gentys)
} else {
quote!(impl cairo_types::CairoType for #struct_name)
quote!(impl starknet::contract::abi::CairoType for #struct_name)
};

let rust_type = if self.is_generic() {
Expand Down Expand Up @@ -116,7 +116,7 @@ impl Expandable for CairoStruct {
out
}

fn deserialize(felts: &[starknet::core::types::FieldElement], offset: usize) -> cairo_types::Result<Self::RustType> {
fn deserialize(felts: &[starknet::core::types::FieldElement], offset: usize) -> starknet::contract::abi::cairo_types::Result<Self::RustType> {
let mut offset = offset;
#(#desers)*
Ok(#struct_name {
Expand Down

0 comments on commit f94c1c5

Please sign in to comment.