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
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
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 {

View file

@ -55,6 +55,9 @@ pub trait Coin {
key: <Self::Curve as Curve>::G,
) -> Result<Vec<Self::Output>, CoinError>;
// TODO: Remove
async fn is_confirmed(&self, tx: &[u8], height: usize) -> Result<bool, CoinError>;
async fn prepare_send(
&self,
keys: FrostKeys<Self::Curve>,

View file

@ -147,6 +147,12 @@ impl Coin for Monero {
)
}
async fn is_confirmed(&self, tx: &[u8], height: usize) -> Result<bool, CoinError> {
let tx_height =
self.rpc.get_transaction_height(tx).await.map_err(|_| CoinError::ConnectionError)?;
Ok((height.saturating_sub(tx_height) + 1) >= 10)
}
async fn prepare_send(
&self,
keys: FrostKeys<Ed25519>,