Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
From/TryFrom starknet api types (#962)
Browse files Browse the repository at this point in the history
* From/TryFrom starknet api types

* Add deploy account

* Modify gitignore

* Deploy account and invoke function

* Change into_iter to iter

* Update .gitignore

Co-authored-by: fmoletta <[email protected]>

* change to try_from

* Move functions to its respective files

* Test

* Delete test

* Fix format

* Fix test

---------

Co-authored-by: Juan Bono <[email protected]>
Co-authored-by: fmoletta <[email protected]>
Co-authored-by: Estéfano Bargas <[email protected]>
  • Loading branch information
4 people authored Aug 30, 2023
1 parent d884e28 commit 60a38cd
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,4 @@ lcov.info
.rusty-hook.toml
!/starknet_programs/raw_contract_classes/*.json
cairo-*.tar
starknet-pypy-env/
28 changes: 5 additions & 23 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ members = ["cli", "fuzzer", "rpc_state_reader", "rpc_state_reader_sn_api"]

[workspace.dependencies]
cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] }
starknet_api = "0.3.0"
starknet_api = "0.4.1"
num-traits = "0.2.15"
starknet = "0.5.0"
thiserror = "1.0.32"
Expand Down
52 changes: 22 additions & 30 deletions src/services/api/contract_classes/deprecated_contract_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,7 @@ mod tests {
felt::{felt_str, PRIME_STR},
serde::deserialize_program::BuiltinName,
};
use starknet_api::deprecated_contract_class::{
FunctionAbiEntry, FunctionAbiEntryType, FunctionAbiEntryWithType, TypedParameter,
};
use starknet_api::deprecated_contract_class::{FunctionAbiEntry, TypedParameter};

#[test]
fn deserialize_contract_class() {
Expand Down Expand Up @@ -333,34 +331,28 @@ mod tests {
// This specific contract compiles with --no_debug_info
let res = ContractClass::from_path("starknet_programs/fibonacci.json");
let contract_class = res.expect("should be able to read file");

let expected_abi = Some(vec![ContractClassAbiEntry::Function(
FunctionAbiEntryWithType {
r#type: FunctionAbiEntryType::Function,
entry: FunctionAbiEntry {
name: "fib".to_string(),
inputs: vec![
TypedParameter {
name: "first_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "second_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "n".to_string(),
r#type: "felt".to_string(),
},
],
outputs: vec![TypedParameter {
name: "res".to_string(),
r#type: "felt".to_string(),
}],
state_mutability: None,
let expected_abi = Some(vec![ContractClassAbiEntry::Function(FunctionAbiEntry {
name: "fib".to_string(),
inputs: vec![
TypedParameter {
name: "first_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "second_element".to_string(),
r#type: "felt".to_string(),
},
TypedParameter {
name: "n".to_string(),
r#type: "felt".to_string(),
},
},
)]);
],
outputs: vec![TypedParameter {
name: "res".to_string(),
r#type: "felt".to_string(),
}],
state_mutability: None,
})]);
assert_eq!(contract_class.abi, expected_abi);
}

Expand Down
45 changes: 45 additions & 0 deletions src/transaction/deploy_account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,51 @@ impl DeployAccount {
}
}

// ----------------------------------
// Try from starknet api
// ----------------------------------

impl TryFrom<starknet_api::transaction::DeployAccountTransaction> for DeployAccount {
type Error = SyscallHandlerError;

fn try_from(
value: starknet_api::transaction::DeployAccountTransaction,
) -> Result<Self, SyscallHandlerError> {
let max_fee = value.max_fee.0;
let version = Felt252::from_bytes_be(value.version.0.bytes());
let nonce = Felt252::from_bytes_be(value.nonce.0.bytes());
let class_hash: [u8; 32] = value.class_hash.0.bytes().try_into().unwrap();
let contract_address_salt = Felt252::from_bytes_be(value.contract_address_salt.0.bytes());

let signature = value
.signature
.0
.iter()
.map(|f| Felt252::from_bytes_be(f.bytes()))
.collect();
let constructor_calldata = value
.constructor_calldata
.0
.as_ref()
.iter()
.map(|f| Felt252::from_bytes_be(f.bytes()))
.collect();

let chain_id = Felt252::zero();

DeployAccount::new(
class_hash,
max_fee,
version,
nonce,
constructor_calldata,
signature,
contract_address_salt,
chain_id,
)
}
}

#[cfg(test)]
mod tests {
use std::{collections::HashMap, path::PathBuf, sync::Arc};
Expand Down
93 changes: 92 additions & 1 deletion src/transaction/invoke_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
use cairo_vm::felt::Felt252;
use getset::Getters;
use num_traits::Zero;
use num_traits::{One, Zero};

use super::{fee::charge_fee, Transaction};

Expand Down Expand Up @@ -397,6 +397,97 @@ pub(crate) fn preprocess_invoke_function_fields(
}
}

// ----------------------------------
// Try from starknet api
// ----------------------------------

fn convert_invoke_v0(
value: starknet_api::transaction::InvokeTransactionV0,
) -> Result<InvokeFunction, TransactionError> {
let contract_address = Address(Felt252::from_bytes_be(
value.contract_address.0.key().bytes(),
));
let max_fee = value.max_fee.0;
let entry_point_selector = Felt252::from_bytes_be(value.entry_point_selector.0.bytes());
let version = Felt252::zero();
let nonce = None;
let chain_id = Felt252::zero();

let signature = value
.signature
.0
.iter()
.map(|f| Felt252::from_bytes_be(f.bytes()))
.collect();
let calldata = value
.calldata
.0
.as_ref()
.iter()
.map(|f| Felt252::from_bytes_be(f.bytes()))
.collect();

InvokeFunction::new(
contract_address,
entry_point_selector,
max_fee,
version,
calldata,
signature,
chain_id,
nonce,
)
}

fn convert_invoke_v1(
value: starknet_api::transaction::InvokeTransactionV1,
) -> Result<InvokeFunction, TransactionError> {
let contract_address = Address(Felt252::from_bytes_be(value.sender_address.0.key().bytes()));
let max_fee = value.max_fee.0;
let version = Felt252::one();
let nonce = Felt252::from_bytes_be(value.nonce.0.bytes());
let chain_id = Felt252::zero();
let entry_point_selector = EXECUTE_ENTRY_POINT_SELECTOR.clone();

let signature = value
.signature
.0
.iter()
.map(|f| Felt252::from_bytes_be(f.bytes()))
.collect();
let calldata = value
.calldata
.0
.as_ref()
.iter()
.map(|f| Felt252::from_bytes_be(f.bytes()))
.collect();

InvokeFunction::new(
contract_address,
entry_point_selector,
max_fee,
version,
calldata,
signature,
chain_id,
Some(nonce),
)
}

impl TryFrom<starknet_api::transaction::InvokeTransaction> for InvokeFunction {
type Error = TransactionError;

fn try_from(
value: starknet_api::transaction::InvokeTransaction,
) -> Result<Self, TransactionError> {
match value {
starknet_api::transaction::InvokeTransaction::V0(v0) => convert_invoke_v0(v0),
starknet_api::transaction::InvokeTransaction::V1(v1) => convert_invoke_v1(v1),
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit 60a38cd

Please sign in to comment.