fix: Get correct runtime everywhere

Made an utils function for it to make sure it is used correctly

--- don't change below this line ---
ENG-4260 Took 9 minutes
This commit is contained in:
Diana Carvalho
2025-02-27 11:16:21 +00:00
parent f95c74fbc6
commit 6a6f2d3221
3 changed files with 38 additions and 21 deletions

View File

@@ -1,7 +1,8 @@
use std::cmp::max;
use std::{cmp::max, sync::Arc};
use alloy_primitives::{aliases::U24, keccak256, Address, FixedBytes, Keccak256, U256, U8};
use num_bigint::BigUint;
use tokio::runtime::{Handle, Runtime};
use tycho_core::Bytes;
use crate::encoding::{
@@ -120,3 +121,15 @@ pub fn get_static_attribute(swap: &Swap, attribute_name: &str) -> Result<Vec<u8>
})?
.to_vec())
}
pub fn get_runtime() -> Result<(Handle, Option<Arc<Runtime>>), EncodingError> {
match Handle::try_current() {
Ok(h) => Ok((h, None)),
Err(_) => {
let rt = Arc::new(Runtime::new().map_err(|_| {
EncodingError::FatalError("Failed to create a new tokio runtime".to_string())
})?);
Ok((rt.handle().clone(), Some(rt)))
}
}
}