Skip to content

Commit

Permalink
updated types and finalized waiting for execution of transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
alenmestrov committed Nov 22, 2024
1 parent 659f8d0 commit e6c2420
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 17 deletions.
20 changes: 19 additions & 1 deletion crates/context/config/src/client/env/proxy/types/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,28 @@ impl From<Vec<ProposalAction>> for StarknetProposalActionWithArgs {
deposit,
gas,
} => {
// Parse the args string (which is a JSON array of strings) into Felts
let args_vec: Vec<String> = serde_json::from_str(&args)
.unwrap_or_else(|_| Vec::new());

let felt_args: Vec<Felt> = args_vec
.iter()
.map(|arg| {
// If the arg starts with "0x", parse as hex
if arg.starts_with("0x") {
// Use from_hex_unchecked for hex strings
Felt::from_hex_unchecked(arg)
} else {
// Otherwise parse as bytes
Felt::from_bytes_be_slice(arg.as_bytes())
}
})
.collect();

StarknetProposalActionWithArgs::ExternalFunctionCall(
Felt::from_bytes_be_slice(receiver_id.as_bytes()),
Felt::from_bytes_be_slice(method_name.as_bytes()),
vec![], // TODO: parse args string into Felts
felt_args,
)
}
ProposalAction::Transfer {
Expand Down
65 changes: 49 additions & 16 deletions crates/context/config/src/client/protocol/starknet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use std::sync::Arc;
use serde::{Deserialize, Serialize};
use starknet::accounts::{Account, ConnectedAccount, ExecutionEncoding, SingleOwnerAccount};
use starknet::core::codec::Decode;
use starknet::core::types::{BlockId, BlockTag, Call, Felt, FunctionCall};
use starknet::core::types::{BlockId, BlockTag, Call, ExecutionResult, Felt, FunctionCall, TransactionFinalityStatus};
use starknet::core::utils::get_selector_from_name;
use starknet::providers::jsonrpc::HttpTransport;
use starknet::providers::{JsonRpcClient, Provider, Url};
use starknet::signers::{LocalWallet, SigningKey};
use thiserror::Error;
use std::time::{Instant, Duration};

use super::Protocol;
use crate::client::env::proxy::starknet::StarknetProposalWithApprovals;
Expand Down Expand Up @@ -310,42 +311,74 @@ impl Network {
}])
.send()
.await
.unwrap();

let receipt = account
.provider()
.get_transaction_receipt(response.transaction_hash)
.await
.unwrap();
.map_err(|e| StarknetError::Custom {
operation: ErrorOperation::Mutate,
reason: format!("Failed to send transaction: {}", e),
})?;

let sent_at = Instant::now();
let timeout = Duration::from_secs(60); // Same 60-second timeout as NEAR

let receipt = loop {
match account
.provider()
.get_transaction_receipt(response.transaction_hash)
.await
{
Ok(receipt) => {
if let starknet::core::types::TransactionReceipt::Invoke(invoke_receipt) = &receipt.receipt {
if matches!(invoke_receipt.finality_status,
TransactionFinalityStatus::AcceptedOnL2 |
TransactionFinalityStatus::AcceptedOnL1) {
break receipt;
}

if sent_at.elapsed() > timeout {
return Err(StarknetError::TransactionTimeout);
}
continue;
}
},
Err(err) => {
return Err(StarknetError::Custom {
operation: ErrorOperation::Mutate,
reason: err.to_string(),
});
}
}
};


// Process the receipt
match receipt.receipt {
starknet::core::types::TransactionReceipt::Invoke(invoke_receipt) => {
match invoke_receipt.execution_result {
starknet::core::types::ExecutionResult::Succeeded => {
ExecutionResult::Succeeded => {
// Process events and return result
for event in invoke_receipt.events.iter() {
if event.from_address == contract_id {
let result = StarknetProposalWithApprovals::decode(&event.data)
.map_err(|e| StarknetError::Custom {
operation: ErrorOperation::Query,
reason: format!("Failed to decode event: {:?}", e),
})?;
// Add length prefix (32 bytes)
let mut encoded = vec![0u8; 32];
// Add proposal_id high part (32 bytes)
encoded.extend_from_slice(&result.proposal_id.high.to_bytes_be());
// Add proposal_id low part (32 bytes)
encoded.extend_from_slice(&result.proposal_id.low.to_bytes_be());
// Add num_approvals (32 bytes)
encoded.extend_from_slice(&result.num_approvals.to_bytes_be());

return Ok(encoded);
}
}
Ok(vec![])
},
ExecutionResult::Reverted { reason } => {
Err(StarknetError::Custom {
operation: ErrorOperation::Mutate,
reason: format!("Transaction reverted: {}", reason),
})
}
_ => Ok(vec![0]),
}
}
},
_ => Ok(vec![0]),
}
}
Expand Down

0 comments on commit e6c2420

Please sign in to comment.