Don't forward payment until all outputs are unlocked

This commit is contained in:
silverpill 2022-09-11 09:37:29 +00:00
parent 0c7f23bc76
commit 5122fe2b78
2 changed files with 28 additions and 12 deletions

View file

@ -25,7 +25,12 @@ use crate::models::{
},
users::queries::get_user_by_id,
};
use super::wallet::{send_monero, DEFAULT_ACCOUNT, MoneroError};
use super::wallet::{
get_subaddress_balance,
send_monero,
DEFAULT_ACCOUNT,
MoneroError,
};
const INVOICE_TIMEOUT: i64 = 30 * 60; // 30 minutes
@ -108,17 +113,14 @@ pub async fn check_monero_subscriptions(
for invoice in paid_invoices {
let address = Address::from_str(&invoice.payment_address)?;
let address_index = wallet_client.get_address_index(address).await?;
let balance_data = wallet_client.get_balance(
address_index.major,
Some(vec![address_index.minor]),
let balance_data = get_subaddress_balance(
&wallet_client,
&address_index,
).await?;
let unlocked_balance = if let [subaddress_data] = &balance_data.per_subaddress[..] {
subaddress_data.unlocked_balance
} else {
return Err(MoneroError::OtherError("invalid response from wallet"));
};
if unlocked_balance == Amount::ZERO {
// Not ready for forwarding
if balance_data.balance != balance_data.unlocked_balance ||
balance_data.balance == Amount::ZERO
{
// Don't forward payment until all outputs are unlocked
continue;
};
let sender = get_profile_by_id(db_client, &invoice.sender_id).await?;

View file

@ -1,13 +1,15 @@
use monero_rpc::{
RpcClient,
SubaddressBalanceData,
SweepAllArgs,
TransferPriority,
WalletClient,
};
use monero_rpc::monero::{
cryptonote::subaddress::Index,
util::address::Error as AddressError,
Address,
Amount,
util::address::Error as AddressError,
};
use crate::config::MoneroConfig;
@ -64,6 +66,18 @@ fn get_single_item<T: Clone>(items: Vec<T>) -> Result<T, MoneroError> {
}
}
pub async fn get_subaddress_balance(
wallet_client: &WalletClient,
subaddress_index: &Index,
) -> Result<SubaddressBalanceData, MoneroError> {
let balance_data = wallet_client.get_balance(
subaddress_index.major,
Some(vec![subaddress_index.minor]),
).await?;
let subaddress_data = get_single_item(balance_data.per_subaddress)?;
Ok(subaddress_data)
}
/// http://monerotoruzizulg5ttgat2emf4d6fbmiea25detrmmy7erypseyteyd.onion/resources/developer-guides/wallet-rpc.html#sweep_all
pub async fn send_monero(
wallet_client: &WalletClient,