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

[Feature]: decode a slice of FieldElement into a list of Token #318

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
11 changes: 7 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ starknet-contract = { version = "0.1.0", path = "./starknet-contract" }
starknet-signers = { version = "0.1.0", path = "./starknet-signers" }
starknet-accounts = { version = "0.1.0", path = "./starknet-accounts" }
starknet-macros = { version = "0.1.0", path = "./starknet-macros" }
crypto-bigint = { version = "0.4.9", default-features = false }

[dev-dependencies]
serde_json = "1.0.74"
Expand Down
74 changes: 74 additions & 0 deletions examples/decoder_example.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use starknet::{
core::types::FieldElement,
providers::{Provider, SequencerGatewayProvider},
};
use starknet_core::{
decoder::{decode, Address, Decode, ParamType, Token},
types::{TransactionType, ValueOutOfRangeError},
};

use crypto_bigint::U256;
use starknet_macros::Decode;

#[derive(Debug)]
struct Uint {
low: U256,
high: U256,
}

#[derive(Debug, Decode)]
struct Transfer {
from: Address,
to: Address,
amount: Uint,
}

impl TryFrom<&Token> for Uint {
type Error = ValueOutOfRangeError;
fn try_from(value: &Token) -> Result<Self, Self::Error> {
if let Token::Tuple(v) = value {
Ok(Uint {
low: U256::from(&v[0]),
high: U256::from(&v[1]),
})
} else {
Err(ValueOutOfRangeError)
}
}
}

impl TryFrom<Vec<Token>> for Transfer {
type Error = ValueOutOfRangeError;

fn try_from(value: Vec<Token>) -> Result<Self, Self::Error> {
let from = Address::try_from(&value[0])?;
let to = Address::try_from(&value[1])?;
let amount = Uint::try_from(&value[2])?;
Ok(Self { from, to, amount })
}
}

#[tokio::main]
async fn main() {
let provider = SequencerGatewayProvider::starknet_alpha_goerli();

let tx_hash = "0x03a4ce1bb249ed3f8b190012dc7ca0ab2caff155d1b81727aebf2bb7ee12b04b";
let tx_hash = FieldElement::from_hex_be(tx_hash).unwrap();
let tx = provider.get_transaction(tx_hash).await.unwrap();
println!("tx: {tx:?}");

let tx_type = tx.r#type.unwrap();

let types = [
ParamType::FieldElement,
ParamType::FieldElement,
ParamType::Tuple(2),
];
if let TransactionType::L1Handler(tx) = tx_type {
let decoded = decode(&types, &tx.calldata).unwrap();
println!("decoded: {decoded:?}");

let transfer = Transfer::decode(&decoded);
println!("transfer: {transfer:?}");
}
}
1 change: 1 addition & 0 deletions starknet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ serde_json = { version = "1.0.74", features = ["arbitrary_precision"] }
serde_with = "2.2.0"
sha3 = "0.10.0"
thiserror = "1.0.30"
crypto-bigint = { version = "0.4.9", default-features = false }

[dev-dependencies]
criterion = { version = "0.4.0", default-features = false }
Expand Down
Loading