Skip to content

Commit

Permalink
feat: support generic payments
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach committed Aug 27, 2024
1 parent 4643330 commit ad12048
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 12 deletions.
3 changes: 3 additions & 0 deletions sn_node/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@ pub enum Error {
/// Error occurred in an async thread
#[error("Error occured in async thread: {0}")]
JoinErrorInAsyncThread(String),

#[error("Unsupported payment method: {0}")]
UnsupportedPaymentMethod(String),
}
27 changes: 21 additions & 6 deletions sn_node/src/put_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use sn_protocol::{
};
use sn_registers::SignedRegister;
use sn_transfers::{
calculate_royalties_fee, CashNote, CashNoteRedemption, HotWallet, NanoTokens, Payment,
SignedSpend, Transfer, TransferError, UniquePubkey, WalletError, NETWORK_ROYALTIES_PK,
calculate_royalties_fee, CashNote, CashNoteRedemption, HotWallet, NanoTokens, NativePayment,
Payment, SignedSpend, Transfer, TransferError, UniquePubkey, WalletError, NETWORK_ROYALTIES_PK,
};
use std::collections::BTreeSet;
use tokio::task::JoinSet;
Expand Down Expand Up @@ -522,15 +522,13 @@ impl Node {
}
}

/// Perform validations on the provided `Record`.
async fn payment_for_us_exists_and_is_still_valid(
async fn check_native_payment(
&self,
address: &NetworkAddress,
payment: Payment,
payment: NativePayment,
) -> Result<()> {
let key = address.to_record_key();
let pretty_key = PrettyPrintRecordKey::from(&key).into_owned();
debug!("Validating record payment for {pretty_key}");

// load wallet
let mut wallet = HotWallet::load_from(self.network().root_dir_path())?;
Expand Down Expand Up @@ -613,6 +611,23 @@ impl Node {
Ok(())
}

/// Perform validations on the provided `Record`.
async fn payment_for_us_exists_and_is_still_valid(
&self,
address: &NetworkAddress,
payment: Payment,
) -> Result<()> {
let key = address.to_record_key();
let pretty_key = PrettyPrintRecordKey::from(&key).into_owned();
debug!("Validating record payment for {pretty_key}");

match payment {
Payment::Native(payment) => self.check_native_payment(address, payment).await,
Payment::Erc20 => Err(Error::UnsupportedPaymentMethod("Erc20".to_string())),
Payment::Bitcoin => Err(Error::UnsupportedPaymentMethod("Bitcoin".to_string())),
}
}

async fn register_validation(
&self,
register: &SignedRegister,
Expand Down
4 changes: 2 additions & 2 deletions sn_transfers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ pub use genesis::{
};
pub use transfers::{CashNoteRedemption, SignedTransaction, Transfer, UnsignedTransaction};
pub use wallet::{
bls_secret_from_hex, wallet_lockfile_name, Error as WalletError, HotWallet, Payment,
PaymentQuote, QuotingMetrics, Result as WalletResult, WalletApi, WatchOnlyWallet,
bls_secret_from_hex, wallet_lockfile_name, Error as WalletError, HotWallet, NativePayment,
Payment, PaymentQuote, QuotingMetrics, Result as WalletResult, WalletApi, WatchOnlyWallet,
QUOTE_EXPIRATION_SECS, WALLET_DIR_NAME,
};

Expand Down
2 changes: 1 addition & 1 deletion sn_transfers/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ mod watch_only;

pub use self::{
api::{WalletApi, WALLET_DIR_NAME},
data_payments::{Payment, PaymentQuote, QuotingMetrics, QUOTE_EXPIRATION_SECS},
data_payments::{NativePayment, Payment, PaymentQuote, QuotingMetrics, QUOTE_EXPIRATION_SECS},
error::{Error, Result},
hot_wallet::HotWallet,
keys::bls_secret_from_hex,
Expand Down
19 changes: 16 additions & 3 deletions sn_transfers/src/wallet/data_payments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@ pub const QUOTE_EXPIRATION_SECS: u64 = 3600;
/// The margin allowed for live_time
const LIVE_TIME_MARGIN: u64 = 10;


/// A generic data payment type supporting multiple payment methods
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, Debug)]
pub enum Payment {
/// The autonomy network native payment method using ANT (Autonomy Network Token)
Native(NativePayment),
/// The ERC20 payment method
Erc20,
/// The Bitcoin payment method
Bitcoin,
}

/// The autonomy network native payment method using ANT (Autonomy Network Token)
#[derive(Clone, Serialize, Deserialize, Eq, PartialEq, custom_debug::Debug)]
pub struct Payment {
pub struct NativePayment {
/// The transfers we make
#[debug(skip)]
pub transfers: Vec<Transfer>,
Expand Down Expand Up @@ -47,10 +60,10 @@ pub struct PaymentDetails {
impl PaymentDetails {
/// create a Payment for a PaymentDetails
pub fn to_payment(&self) -> Payment {
Payment {
Payment::Native(NativePayment {
transfers: vec![self.transfer.0.clone(), self.royalties.0.clone()],
quote: self.quote.clone(),
}
})
}
}

Expand Down

0 comments on commit ad12048

Please sign in to comment.