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

Commit dd91b4f

Browse files
mmsc2juanbonofmolettaxqft
authored andcommitted
From/TryFrom starknet api types (#962)
* 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 <99273364+fmoletta@users.noreply.github.com> * change to try_from * Move functions to its respective files * Test * Delete test * Fix format * Fix test --------- Co-authored-by: Juan Bono <juanbono94@gmail.com> Co-authored-by: fmoletta <99273364+fmoletta@users.noreply.github.com> Co-authored-by: Estéfano Bargas <estefano.bargas@fing.edu.uy>
1 parent 9f79697 commit dd91b4f

File tree

6 files changed

+166
-55
lines changed

6 files changed

+166
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ lcov.info
5959
.rusty-hook.toml
6060
!/starknet_programs/raw_contract_classes/*.json
6161
cairo-*.tar
62+
starknet-pypy-env/

Cargo.lock

Lines changed: 5 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ members = ["cli", "fuzzer", "rpc_state_reader", "rpc_state_reader_sn_api"]
1515

1616
[workspace.dependencies]
1717
cairo-vm = { version = "0.8.5", features = ["cairo-1-hints"] }
18-
starknet_api = "0.3.0"
18+
starknet_api = "0.4.1"
1919
num-traits = "0.2.15"
2020
starknet = "0.5.0"
2121
thiserror = "1.0.32"

src/services/api/contract_classes/deprecated_contract_class.rs

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,7 @@ mod tests {
265265
felt::{felt_str, PRIME_STR},
266266
serde::deserialize_program::BuiltinName,
267267
};
268-
use starknet_api::deprecated_contract_class::{
269-
FunctionAbiEntry, FunctionAbiEntryType, FunctionAbiEntryWithType, TypedParameter,
270-
};
268+
use starknet_api::deprecated_contract_class::{FunctionAbiEntry, TypedParameter};
271269

272270
#[test]
273271
fn deserialize_contract_class() {
@@ -333,34 +331,28 @@ mod tests {
333331
// This specific contract compiles with --no_debug_info
334332
let res = ContractClass::from_path("starknet_programs/fibonacci.json");
335333
let contract_class = res.expect("should be able to read file");
336-
337-
let expected_abi = Some(vec![ContractClassAbiEntry::Function(
338-
FunctionAbiEntryWithType {
339-
r#type: FunctionAbiEntryType::Function,
340-
entry: FunctionAbiEntry {
341-
name: "fib".to_string(),
342-
inputs: vec![
343-
TypedParameter {
344-
name: "first_element".to_string(),
345-
r#type: "felt".to_string(),
346-
},
347-
TypedParameter {
348-
name: "second_element".to_string(),
349-
r#type: "felt".to_string(),
350-
},
351-
TypedParameter {
352-
name: "n".to_string(),
353-
r#type: "felt".to_string(),
354-
},
355-
],
356-
outputs: vec![TypedParameter {
357-
name: "res".to_string(),
358-
r#type: "felt".to_string(),
359-
}],
360-
state_mutability: None,
334+
let expected_abi = Some(vec![ContractClassAbiEntry::Function(FunctionAbiEntry {
335+
name: "fib".to_string(),
336+
inputs: vec![
337+
TypedParameter {
338+
name: "first_element".to_string(),
339+
r#type: "felt".to_string(),
340+
},
341+
TypedParameter {
342+
name: "second_element".to_string(),
343+
r#type: "felt".to_string(),
344+
},
345+
TypedParameter {
346+
name: "n".to_string(),
347+
r#type: "felt".to_string(),
361348
},
362-
},
363-
)]);
349+
],
350+
outputs: vec![TypedParameter {
351+
name: "res".to_string(),
352+
r#type: "felt".to_string(),
353+
}],
354+
state_mutability: None,
355+
})]);
364356
assert_eq!(contract_class.abi, expected_abi);
365357
}
366358

src/transaction/deploy_account.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,51 @@ impl DeployAccount {
395395
}
396396
}
397397

398+
// ----------------------------------
399+
// Try from starknet api
400+
// ----------------------------------
401+
402+
impl TryFrom<starknet_api::transaction::DeployAccountTransaction> for DeployAccount {
403+
type Error = SyscallHandlerError;
404+
405+
fn try_from(
406+
value: starknet_api::transaction::DeployAccountTransaction,
407+
) -> Result<Self, SyscallHandlerError> {
408+
let max_fee = value.max_fee.0;
409+
let version = Felt252::from_bytes_be(value.version.0.bytes());
410+
let nonce = Felt252::from_bytes_be(value.nonce.0.bytes());
411+
let class_hash: [u8; 32] = value.class_hash.0.bytes().try_into().unwrap();
412+
let contract_address_salt = Felt252::from_bytes_be(value.contract_address_salt.0.bytes());
413+
414+
let signature = value
415+
.signature
416+
.0
417+
.iter()
418+
.map(|f| Felt252::from_bytes_be(f.bytes()))
419+
.collect();
420+
let constructor_calldata = value
421+
.constructor_calldata
422+
.0
423+
.as_ref()
424+
.iter()
425+
.map(|f| Felt252::from_bytes_be(f.bytes()))
426+
.collect();
427+
428+
let chain_id = Felt252::zero();
429+
430+
DeployAccount::new(
431+
class_hash,
432+
max_fee,
433+
version,
434+
nonce,
435+
constructor_calldata,
436+
signature,
437+
contract_address_salt,
438+
chain_id,
439+
)
440+
}
441+
}
442+
398443
#[cfg(test)]
399444
mod tests {
400445
use std::{collections::HashMap, path::PathBuf, sync::Arc};

src/transaction/invoke_function.rs

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use crate::{
2020
use crate::services::api::contract_classes::deprecated_contract_class::EntryPointType;
2121
use cairo_vm::felt::Felt252;
2222
use getset::Getters;
23-
use num_traits::Zero;
23+
use num_traits::{One, Zero};
2424

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

@@ -397,6 +397,97 @@ pub(crate) fn preprocess_invoke_function_fields(
397397
}
398398
}
399399

400+
// ----------------------------------
401+
// Try from starknet api
402+
// ----------------------------------
403+
404+
fn convert_invoke_v0(
405+
value: starknet_api::transaction::InvokeTransactionV0,
406+
) -> Result<InvokeFunction, TransactionError> {
407+
let contract_address = Address(Felt252::from_bytes_be(
408+
value.contract_address.0.key().bytes(),
409+
));
410+
let max_fee = value.max_fee.0;
411+
let entry_point_selector = Felt252::from_bytes_be(value.entry_point_selector.0.bytes());
412+
let version = Felt252::zero();
413+
let nonce = None;
414+
let chain_id = Felt252::zero();
415+
416+
let signature = value
417+
.signature
418+
.0
419+
.iter()
420+
.map(|f| Felt252::from_bytes_be(f.bytes()))
421+
.collect();
422+
let calldata = value
423+
.calldata
424+
.0
425+
.as_ref()
426+
.iter()
427+
.map(|f| Felt252::from_bytes_be(f.bytes()))
428+
.collect();
429+
430+
InvokeFunction::new(
431+
contract_address,
432+
entry_point_selector,
433+
max_fee,
434+
version,
435+
calldata,
436+
signature,
437+
chain_id,
438+
nonce,
439+
)
440+
}
441+
442+
fn convert_invoke_v1(
443+
value: starknet_api::transaction::InvokeTransactionV1,
444+
) -> Result<InvokeFunction, TransactionError> {
445+
let contract_address = Address(Felt252::from_bytes_be(value.sender_address.0.key().bytes()));
446+
let max_fee = value.max_fee.0;
447+
let version = Felt252::one();
448+
let nonce = Felt252::from_bytes_be(value.nonce.0.bytes());
449+
let chain_id = Felt252::zero();
450+
let entry_point_selector = EXECUTE_ENTRY_POINT_SELECTOR.clone();
451+
452+
let signature = value
453+
.signature
454+
.0
455+
.iter()
456+
.map(|f| Felt252::from_bytes_be(f.bytes()))
457+
.collect();
458+
let calldata = value
459+
.calldata
460+
.0
461+
.as_ref()
462+
.iter()
463+
.map(|f| Felt252::from_bytes_be(f.bytes()))
464+
.collect();
465+
466+
InvokeFunction::new(
467+
contract_address,
468+
entry_point_selector,
469+
max_fee,
470+
version,
471+
calldata,
472+
signature,
473+
chain_id,
474+
Some(nonce),
475+
)
476+
}
477+
478+
impl TryFrom<starknet_api::transaction::InvokeTransaction> for InvokeFunction {
479+
type Error = TransactionError;
480+
481+
fn try_from(
482+
value: starknet_api::transaction::InvokeTransaction,
483+
) -> Result<Self, TransactionError> {
484+
match value {
485+
starknet_api::transaction::InvokeTransaction::V0(v0) => convert_invoke_v0(v0),
486+
starknet_api::transaction::InvokeTransaction::V1(v1) => convert_invoke_v1(v1),
487+
}
488+
}
489+
}
490+
400491
#[cfg(test)]
401492
mod tests {
402493
use super::*;

0 commit comments

Comments
 (0)