Add is_confirmed with a TODO: Remove

Closes https://github.com/serai-dex/serai/issues/129.
This commit is contained in:
Luke Parker
2022-10-15 19:51:59 -04:00
parent e9d5fb25c8
commit 4629e88a28
3 changed files with 36 additions and 13 deletions

View File

@@ -23,6 +23,20 @@ pub struct JsonRpcResponse<T> {
result: T,
}
#[derive(Deserialize, Debug)]
struct TransactionResponse {
tx_hash: String,
block_height: usize,
as_hex: String,
pruned_as_hex: String,
}
#[derive(Deserialize, Debug)]
struct TransactionsResponse {
#[serde(default)]
missed_tx: Vec<String>,
txs: Vec<TransactionResponse>,
}
#[derive(Clone, Error, Debug)]
pub enum RpcError {
#[error("internal error ({0})")]
@@ -149,19 +163,6 @@ impl Rpc {
return Ok(vec![]);
}
#[derive(Deserialize, Debug)]
struct TransactionResponse {
tx_hash: String,
as_hex: String,
pruned_as_hex: String,
}
#[derive(Deserialize, Debug)]
struct TransactionsResponse {
#[serde(default)]
missed_tx: Vec<String>,
txs: Vec<TransactionResponse>,
}
let txs: TransactionsResponse = self
.rpc_call(
"get_transactions",
@@ -205,6 +206,19 @@ impl Rpc {
self.get_transactions(&[tx]).await.map(|mut txs| txs.swap_remove(0))
}
pub async fn get_transaction_height(&self, tx: &[u8]) -> Result<usize, RpcError> {
let txs: TransactionsResponse =
self.rpc_call("get_transactions", Some(json!({ "txs_hashes": [hex::encode(tx)] }))).await?;
if !txs.missed_tx.is_empty() {
Err(RpcError::TransactionsNotFound(
txs.missed_tx.iter().map(|hash| hex::decode(hash).unwrap().try_into().unwrap()).collect(),
))?;
}
Ok(txs.txs[0].block_height)
}
pub async fn get_block(&self, height: usize) -> Result<Block, RpcError> {
#[derive(Deserialize, Debug)]
struct BlockResponse {