Implement payment forwarding

This commit is contained in:
silverpill 2022-08-27 18:05:08 +00:00
parent 6b60e9a0da
commit 4e73bff32e
2 changed files with 63 additions and 7 deletions

View file

@ -15,7 +15,7 @@ use crate::models::{
profiles::types::PaymentOption,
users::queries::get_user_by_id,
};
use super::wallet::{DEFAULT_ACCOUNT, MoneroError};
use super::wallet::{send_monero, DEFAULT_ACCOUNT, MoneroError};
pub async fn check_monero_subscriptions(
config: &MoneroConfig,
@ -119,11 +119,17 @@ pub async fn check_monero_subscriptions(
continue;
};
let payout_address = Address::from_str(&payment_info.payout_address)?;
log::info!(
"invoice {}, payout address {}",
invoice.id,
let _payout_amount = send_monero(
&wallet_client,
address_index.minor,
payout_address,
);
).await?;
set_invoice_status(
db_client,
&invoice.id,
InvoiceStatus::Forwarded,
).await?;
log::info!("processed payment for invoice {}", invoice.id);
};
Ok(())
}

View file

@ -1,5 +1,14 @@
use monero_rpc::RpcClient;
use monero_rpc::monero::{Address, util::address::Error as AddressError};
use monero_rpc::{
RpcClient,
SweepAllArgs,
TransferPriority,
WalletClient,
};
use monero_rpc::monero::{
Address,
Amount,
util::address::Error as AddressError,
};
use crate::config::MoneroConfig;
use crate::errors::DatabaseError;
@ -46,3 +55,44 @@ pub async fn create_monero_address(
log::info!("created monero address {}/{}", DEFAULT_ACCOUNT, address_index);
Ok(address)
}
fn get_single_item<T: Clone>(items: Vec<T>) -> Result<T, MoneroError> {
if let [item] = &items[..] {
Ok(item.clone())
} else {
Err(MoneroError::OtherError("invalid response from wallet"))
}
}
/// http://monerotoruzizulg5ttgat2emf4d6fbmiea25detrmmy7erypseyteyd.onion/resources/developer-guides/wallet-rpc.html#sweep_all
pub async fn send_monero(
wallet_client: &WalletClient,
from_address: u32,
to_address: Address,
) -> Result<Amount, MoneroError> {
let sweep_args = SweepAllArgs {
address: to_address,
account_index: DEFAULT_ACCOUNT,
subaddr_indices: Some(vec![from_address]),
priority: TransferPriority::Default,
mixin: 15,
ring_size: 16,
unlock_time: 1,
get_tx_keys: None,
below_amount: None,
do_not_relay: None,
get_tx_hex: None,
get_tx_metadata: None,
};
let sweep_data = wallet_client.sweep_all(sweep_args).await?;
let tx_hash = get_single_item(sweep_data.tx_hash_list)?;
let amount = get_single_item(sweep_data.amount_list)?;
let fee = get_single_item(sweep_data.fee_list)?;
log::info!(
"sent transaction {}, amount {}, fee {}",
tx_hash,
amount,
fee,
);
Ok(amount)
}