2023-03-31 10:34:09 +00:00
|
|
|
use core::time::Duration;
|
|
|
|
|
|
|
|
use tokio::time::sleep;
|
|
|
|
|
2023-12-07 07:30:09 +00:00
|
|
|
use serai_client::{Transaction, Serai};
|
2023-03-31 10:34:09 +00:00
|
|
|
|
|
|
|
#[allow(dead_code)]
|
2023-12-07 07:30:09 +00:00
|
|
|
pub async fn publish_tx(serai: &Serai, tx: &Transaction) -> [u8; 32] {
|
2023-11-28 07:29:50 +00:00
|
|
|
let mut latest = serai
|
|
|
|
.block(serai.latest_finalized_block_hash().await.unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap()
|
|
|
|
.unwrap()
|
|
|
|
.number();
|
2023-03-31 10:34:09 +00:00
|
|
|
|
|
|
|
serai.publish(tx).await.unwrap();
|
|
|
|
|
|
|
|
// Get the block it was included in
|
|
|
|
// TODO: Add an RPC method for this/check the guarantee on the subscription
|
|
|
|
let mut ticks = 0;
|
|
|
|
loop {
|
|
|
|
latest += 1;
|
|
|
|
|
|
|
|
let block = {
|
|
|
|
let mut block;
|
|
|
|
while {
|
2023-11-28 07:29:50 +00:00
|
|
|
block = serai.finalized_block_by_number(latest).await.unwrap();
|
2023-03-31 10:34:09 +00:00
|
|
|
block.is_none()
|
|
|
|
} {
|
|
|
|
sleep(Duration::from_secs(1)).await;
|
|
|
|
ticks += 1;
|
|
|
|
|
|
|
|
if ticks > 60 {
|
|
|
|
panic!("60 seconds without inclusion in a finalized block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
block.unwrap()
|
|
|
|
};
|
|
|
|
|
2023-12-07 07:30:09 +00:00
|
|
|
for transaction in &block.transactions {
|
|
|
|
if transaction == tx {
|
2023-04-15 21:38:47 +00:00
|
|
|
return block.hash();
|
2023-03-31 10:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|