#![allow(dead_code)] pub mod amm; pub mod constants; pub mod lending_protocol; pub mod payment; pub mod vault; pub mod xchain; pub use constants::CREDENTIAL_TYPE_KYC; use anyhow::Result; #[cfg(feature = "std")] use once_cell::sync::Lazy; #[cfg(feature = "std")] use tokio::sync::{Mutex, OnceCell}; #[cfg(all(feature = "websocket", not(feature = "std")))] use embedded_io_adapters::tokio_1::FromTokio; #[cfg(all(feature = "websocket", not(feature = "std")))] use rand::rngs::OsRng; #[cfg(all(feature = "websocket", not(feature = "std")))] use tokio::net::TcpStream; use url::Url; #[cfg(all(feature = "websocket", not(feature = "std")))] use xrpl::asynch::clients::{AsyncWebSocketClient, SingleExecutorMutex, WebSocketOpen}; use xrpl::{asynch::clients::AsyncJsonRpcClient, wallet::Wallet}; /// Genesis account seed (standalone rippled only). /// Address: rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh #[cfg(feature = "std")] const GENESIS_SEED: &str = "snoPBrXtMeMyMHUVTgbuqAfg1SUTb"; /// HTTP JSON-RPC endpoint for local Docker standalone mode. #[cfg(feature = "std")] const STANDALONE_URL: &str = "http://127.0.0.1:5005"; #[cfg(all(feature = "websocket", not(feature = "std")))] pub async fn open_websocket( uri: Url, ) -> Result< AsyncWebSocketClient<4096, FromTokio, OsRng, SingleExecutorMutex, WebSocketOpen>, > { use anyhow::anyhow; let port = uri.port().unwrap_or(80); let url = format!("{}:{}", uri.host_str().unwrap(), port); let tcp = TcpStream::connect(&url).await.unwrap(); let stream = FromTokio::new(tcp); let rng = OsRng; match AsyncWebSocketClient::open(stream, uri, rng, None, None).await { Ok(client) => Ok(client), Err(e) => Err(anyhow!(e)), } } #[cfg(all(not(feature = "std"), feature = "cli", test))] pub mod mock_cli; #[cfg(all(feature = "websocket", feature = "std"))] pub async fn open_websocket( uri: url::Url, ) -> Result< xrpl::asynch::clients::AsyncWebSocketClient< xrpl::asynch::clients::SingleExecutorMutex, xrpl::asynch::clients::WebSocketOpen, >, Box, > { xrpl::asynch::clients::AsyncWebSocketClient::open(uri) .await .map_err(Into::into) } #[cfg(feature = "std")] static CLIENT: OnceCell = OnceCell::const_new(); // Global mutex to ensure only one test accesses the blockchain at a time #[cfg(feature = "std")] static TEST_MUTEX: Lazy> = Lazy::new(|| Mutex::new(())); #[cfg(feature = "std")] pub async fn get_client() -> &'static AsyncJsonRpcClient { CLIENT .get_or_init(|| async { AsyncJsonRpcClient::connect(Url::parse(constants::STANDALONE_URL).unwrap()) }) .await } /// Generate a fresh funded wallet by sending 400 XRP from the genesis account. #[cfg(feature = "std")] pub async fn generate_funded_wallet() -> Wallet { use xrpl::asynch::transaction::sign_and_submit; use xrpl::models::transactions::payment::Payment; use xrpl::models::{Amount, XRPAmount}; let genesis = Wallet::new(GENESIS_SEED, 0).expect("genesis wallet"); let seed = xrpl::core::keypairs::generate_seed(None, None).expect("seed"); let new_wallet = Wallet::new(&seed, 0).expect("new wallet"); let mut payment = Payment::new( genesis.classic_address.clone().into(), None, // account_txn_id None, // fee None, // flags None, // last_ledger_sequence None, // memos None, // sequence None, // signers None, // source_tag None, // ticket_sequence Amount::XRPAmount(XRPAmount::from("400000000")), // 400 XRP new_wallet.classic_address.clone().into(), None, // deliver_min None, // destination_tag None, // invoice_id None, // paths None, // send_max ); // Create a fresh client scoped to the current Tokio runtime. // Using the static CLIENT here causes DispatchGone errors when sync // wrapper tests create and drop their own Runtime instances — the static // client's hyper dispatch task is tied to whichever runtime initialised it. let local_client = AsyncJsonRpcClient::connect(Url::parse(constants::STANDALONE_URL).unwrap()); let result = sign_and_submit(&mut payment, &local_client, &genesis, true, true) .await .expect("generate_funded_wallet: funding payment failed"); // Advance the ledger to confirm the funding payment ledger_accept().await; assert_eq!( result.engine_result, "tesSUCCESS", "generate_funded_wallet: funding payment engine_result was {}", result.engine_result ); new_wallet } /// Advance the ledger by one close. #[cfg(feature = "std")] pub async fn ledger_accept() { let _ = reqwest::Client::new() .post(constants::STANDALONE_URL) .json(&serde_json::json!({"method": "ledger_accept", "params": [{}]})) .send() .await; } /// Return the `close_time` of the most-recent validated ledger in Ripple epoch seconds. #[cfg(feature = "std")] pub async fn get_ledger_close_time() -> u64 { use xrpl::asynch::clients::XRPLAsyncClient; use xrpl::models::{requests::ledger::Ledger, results}; let client = get_client().await; let response = client .request( Ledger::new( None, None, None, None, None, None, Some("validated".into()), None, None, None, ) .into(), ) .await .expect("Failed to get validated ledger"); let ledger_result: results::ledger::Ledger<'_> = response.try_into().expect("Failed to parse ledger result"); ledger_result.ledger.close_time } /// Poll until `close_time >= target`, calling `ledger_accept` each iteration. /// Panics after 60 iterations to prevent silent hangs when the standalone server /// stops advancing (e.g. frozen or unresponsive `ledger_accept` requests). #[cfg(feature = "std")] pub async fn wait_for_ledger_close_time(target: u64) { wait_for_ledger_close_time_with_retries(target, 60).await } /// [`wait_for_ledger_close_time`] with an explicit retry budget. /// /// Each `ledger_accept` advances `close_time` by one close-time resolution /// interval, so jumping a long way forward — past a loan's payment due date, /// say — needs a larger budget than the default. #[cfg(feature = "std")] pub async fn wait_for_ledger_close_time_with_retries(target: u64, retries: u32) { if get_ledger_close_time().await >= target { return; } for _ in 0..retries { ledger_accept().await; if get_ledger_close_time().await >= target { return; } } panic!("ledger close_time did not advance to {target} after {retries} ledger_accept calls"); } /// Serialize all blockchain-mutating tests to prevent sequence number conflicts. #[cfg(feature = "std")] pub async fn with_blockchain_lock(f: F) -> T where F: FnOnce() -> Fut, Fut: std::future::Future, { let name = std::thread::current().name().unwrap_or("?").to_string(); eprintln!("[{name}] waiting for the blockchain lock"); let _guard = TEST_MUTEX.lock().await; eprintln!("[{name}] holds the blockchain lock"); tokio::time::timeout(std::time::Duration::from_secs(180), f()) .await .unwrap_or_else(|_| panic!("[{name}] exceeded 180s while holding the blockchain lock")) } /// Look up the OfferSequence for the first escrow owned by `account`. /// Queries account_objects to find the escrow, then looks up its creating /// transaction to extract the validated Sequence number. #[cfg(feature = "std")] pub async fn get_escrow_offer_sequence(account: &str) -> u32 { use xrpl::asynch::clients::XRPLAsyncClient; use xrpl::models::{ requests::account_objects::{AccountObjectType, AccountObjects}, requests::tx::Tx, results, }; let client = get_client().await; // Step 1: get account_objects and find the escrow entry let ao_response = client .request( AccountObjects::new( None, account.into(), None, None, Some(AccountObjectType::Escrow), None, None, None, ) .into(), ) .await .expect("get_escrow_offer_sequence: account_objects request failed"); let objects_result: results::account_objects::AccountObjects<'_> = ao_response .try_into() .expect("get_escrow_offer_sequence: failed to parse account_objects result"); assert!( !objects_result.account_objects.is_empty(), "get_escrow_offer_sequence: no escrow objects found for {}", account ); let prev_txn_id = objects_result.account_objects[0]["PreviousTxnID"] .as_str() .expect("PreviousTxnID missing from escrow object") .to_string(); // Step 2: look up the creating tx to get its validated Sequence let tx_response = client .request(Tx::new(None, None, None, None, Some(prev_txn_id.as_str().into())).into()) .await .expect("get_escrow_offer_sequence: tx request failed"); let tx_result: results::tx::TxVersionMap<'_> = tx_response .try_into() .expect("get_escrow_offer_sequence: failed to parse tx result"); match tx_result { results::tx::TxVersionMap::Default(tx) => tx.tx_json["Sequence"] .as_u64() .expect("Sequence missing in tx_json") as u32, results::tx::TxVersionMap::V1(tx) => tx.tx_json["Sequence"] .as_u64() .expect("Sequence missing in tx_json (V1)") as u32, } } /// Sign, submit, assert tesSUCCESS, and wait for the ledger to validate. /// /// Uses `sign_and_submit` to get the provisional `engine_result`, asserts /// it is `tesSUCCESS`, then calls `ledger_accept` to close the current /// ledger and `wait_for_ledger_close_time` to confirm the validated ledger /// has advanced past the submission point. This ensures the transaction /// is in a validated ledger before the caller proceeds. /// /// This replaces `submit_and_wait` in all integration tests. #[cfg(feature = "std")] pub async fn test_transaction<'a, T, F>(tx: &mut T, wallet: &Wallet) where T: xrpl::models::transactions::Transaction<'a, F> + xrpl::models::Model + serde::Serialize + serde::de::DeserializeOwned + Clone + core::fmt::Debug, F: strum::IntoEnumIterator + serde::Serialize + core::fmt::Debug + PartialEq + Clone + 'a, { test_transaction_with_result(tx, wallet, "tesSUCCESS").await; } /// Sign, submit, assert the engine result equals `expected_engine_result`, then /// advance the ledger. Pass `"tesSUCCESS"` for happy paths, or a specific /// `tec`/`tem`/`tef` code (e.g. `"tecNO_PERMISSION"`) to validate an expected /// failure case. pub async fn test_transaction_with_result<'a, T, F>( tx: &mut T, wallet: &Wallet, expected_engine_result: &str, ) where T: xrpl::models::transactions::Transaction<'a, F> + xrpl::models::Model + serde::Serialize + serde::de::DeserializeOwned + Clone + core::fmt::Debug, F: strum::IntoEnumIterator + serde::Serialize + core::fmt::Debug + PartialEq + Clone + 'a, { use xrpl::asynch::transaction::sign_and_submit; let client = get_client().await; // Record the validated ledger close_time before submission so we can // detect that a new ledger has been validated after the transaction lands. let pre_close = get_ledger_close_time().await; let result = sign_and_submit(tx, client, wallet, true, true) .await .expect("test_transaction: sign_and_submit failed"); assert_eq!( result.engine_result, expected_engine_result, "Expected {} but got: {} — {}", expected_engine_result, result.engine_result, result.engine_result_message ); // Advance the ledger and wait until a new validated ledger has closed, // ensuring the transaction is in validated state before returning. ledger_accept().await; wait_for_ledger_close_time(pre_close + 1).await; } /// Create an MPToken issuance and return the MPTokenIssuanceID. /// /// The ID is `{sequence as 4-byte BE hex}{account_id as 20-byte hex}`. #[cfg(feature = "std")] pub async fn create_mptoken_issuance(wallet: &Wallet) -> String { use xrpl::asynch::transaction::sign_and_submit; use xrpl::models::transactions::{ mptoken_issuance_create::{MPTokenIssuanceCreate, MPTokenIssuanceCreateFlag}, CommonFields, TransactionType, }; let mut tx = MPTokenIssuanceCreate { common_fields: CommonFields { account: wallet.classic_address.clone().into(), transaction_type: TransactionType::MPTokenIssuanceCreate, flags: vec![MPTokenIssuanceCreateFlag::TfMPTCanLock].into(), ..Default::default() }, ..Default::default() }; let client = get_client().await; let result = sign_and_submit(&mut tx, client, wallet, true, true) .await .expect("create_mptoken_issuance: sign_and_submit failed"); assert_eq!( result.engine_result, "tesSUCCESS", "create_mptoken_issuance: expected tesSUCCESS but got: {} — {}", result.engine_result, result.engine_result_message ); let pre_close = get_ledger_close_time().await; ledger_accept().await; wait_for_ledger_close_time(pre_close + 1).await; // Build MPTokenIssuanceID from the autofilled sequence + account ID let sequence = result.tx_json["Sequence"] .as_u64() .expect("Sequence missing from tx_json") as u32; let account_id = xrpl::core::addresscodec::decode_classic_address(&wallet.classic_address) .expect("failed to decode classic address"); let mut id_bytes = Vec::with_capacity(24); id_bytes.extend_from_slice(&sequence.to_be_bytes()); id_bytes.extend_from_slice(&account_id); hex::encode_upper(&id_bytes) } /// Create an MPToken issuance with TfMPTCanTransfer enabled and return its ID. /// Used by tests that need to send MPT via Payment transactions. #[cfg(feature = "std")] pub async fn create_transferable_mptoken_issuance(wallet: &Wallet) -> String { use xrpl::asynch::transaction::sign_and_submit; use xrpl::models::transactions::{ mptoken_issuance_create::{MPTokenIssuanceCreate, MPTokenIssuanceCreateFlag}, CommonFields, TransactionType, }; let mut tx = MPTokenIssuanceCreate { common_fields: CommonFields { account: wallet.classic_address.clone().into(), transaction_type: TransactionType::MPTokenIssuanceCreate, flags: vec![MPTokenIssuanceCreateFlag::TfMPTCanTransfer].into(), ..Default::default() }, ..Default::default() }; let client = get_client().await; let result = sign_and_submit(&mut tx, client, wallet, true, true) .await .expect("create_transferable_mptoken_issuance: sign_and_submit failed"); assert_eq!( result.engine_result, "tesSUCCESS", "create_transferable_mptoken_issuance: expected tesSUCCESS but got: {} — {}", result.engine_result, result.engine_result_message ); let pre_close = get_ledger_close_time().await; ledger_accept().await; wait_for_ledger_close_time(pre_close + 1).await; let sequence = result.tx_json["Sequence"] .as_u64() .expect("Sequence missing from tx_json") as u32; let account_id = xrpl::core::addresscodec::decode_classic_address(&wallet.classic_address) .expect("failed to decode classic address"); let mut id_bytes = Vec::with_capacity(24); id_bytes.extend_from_slice(&sequence.to_be_bytes()); id_bytes.extend_from_slice(&account_id); hex::encode_upper(&id_bytes) } /// Create an MPToken issuance with both `TfMPTCanTransfer` and `TfMPTCanClawback` /// enabled and return its ID. Used by the MPT vault lifecycle test, which both /// moves MPT into a vault and claws it back. #[cfg(feature = "std")] pub async fn create_transferable_clawbackable_mptoken_issuance(wallet: &Wallet) -> String { use xrpl::asynch::transaction::sign_and_submit; use xrpl::models::transactions::{ mptoken_issuance_create::{MPTokenIssuanceCreate, MPTokenIssuanceCreateFlag}, CommonFields, TransactionType, }; let mut tx = MPTokenIssuanceCreate { common_fields: CommonFields { account: wallet.classic_address.clone().into(), transaction_type: TransactionType::MPTokenIssuanceCreate, flags: vec![ MPTokenIssuanceCreateFlag::TfMPTCanTransfer, MPTokenIssuanceCreateFlag::TfMPTCanClawback, ] .into(), ..Default::default() }, ..Default::default() }; let client = get_client().await; let result = sign_and_submit(&mut tx, client, wallet, true, true) .await .expect("create_transferable_clawbackable_mptoken_issuance: sign_and_submit failed"); assert_eq!( result.engine_result, "tesSUCCESS", "create_transferable_clawbackable_mptoken_issuance: expected tesSUCCESS but got: {} — {}", result.engine_result, result.engine_result_message ); let pre_close = get_ledger_close_time().await; ledger_accept().await; wait_for_ledger_close_time(pre_close + 1).await; let sequence = result.tx_json["Sequence"] .as_u64() .expect("Sequence missing from tx_json") as u32; let account_id = xrpl::core::addresscodec::decode_classic_address(&wallet.classic_address) .expect("failed to decode classic address"); let mut id_bytes = Vec::with_capacity(24); id_bytes.extend_from_slice(&sequence.to_be_bytes()); id_bytes.extend_from_slice(&account_id); hex::encode_upper(&id_bytes) } /// Parameters for [`submit_tx`]. /// /// Use when asserting a specific non-success `engine_result` (tec/tem codes). /// For `tesSUCCESS` paths use [`test_transaction`] instead. /// /// Use struct literal syntax so each argument is self-documenting at call sites. #[cfg(feature = "std")] pub struct SubmitOptions<'w> { pub wallet: &'w Wallet, /// Auto-fill sequence, fee, and other transaction fields before signing. pub autofill: bool, /// Validate that the fee satisfies the network's minimum requirement. pub check_fee: bool, } /// Submit a transaction without asserting success. Returns the raw /// `engine_result` string so callers can assert specific `tec`/`tem` codes. /// /// Use [`test_transaction`] instead when you expect `tesSUCCESS`. #[cfg(feature = "std")] pub async fn submit_tx<'a, T, F>(tx: &mut T, opts: SubmitOptions<'_>) -> String where T: xrpl::models::transactions::Transaction<'a, F> + xrpl::models::Model + serde::Serialize + serde::de::DeserializeOwned + Clone + core::fmt::Debug, F: strum::IntoEnumIterator + serde::Serialize + core::fmt::Debug + PartialEq + Clone + 'a, { use xrpl::asynch::transaction::sign_and_submit; let client = get_client().await; sign_and_submit(tx, client, opts.wallet, opts.autofill, opts.check_fee) .await .expect("submit_tx: sign_and_submit failed") .engine_result .to_string() } /// Provision an accepted Credential and return its on-chain hash (ledger index). /// /// Submits CredentialCreate (issuer → subject) then CredentialAccept (subject), /// reads the resulting Credential ledger object and returns its `index` field. #[cfg(feature = "std")] pub async fn provision_credential( issuer: &xrpl::wallet::Wallet, subject: &xrpl::wallet::Wallet, credential_type: &str, ) -> String { use xrpl::asynch::clients::XRPLAsyncClient; use xrpl::models::{ requests::account_objects::{AccountObjectType, AccountObjects}, results, transactions::{ credential_accept::CredentialAccept, credential_create::CredentialCreate, CommonFields, TransactionType, }, }; let client = get_client().await; let mut create = CredentialCreate { common_fields: CommonFields { account: issuer.classic_address.clone().into(), transaction_type: TransactionType::CredentialCreate, ..Default::default() }, subject: subject.classic_address.clone().into(), credential_type: credential_type.to_owned().into(), ..Default::default() }; test_transaction(&mut create, issuer).await; let mut accept = CredentialAccept { common_fields: CommonFields { account: subject.classic_address.clone().into(), transaction_type: TransactionType::CredentialAccept, ..Default::default() }, issuer: issuer.classic_address.clone().into(), credential_type: credential_type.to_owned().into(), }; test_transaction(&mut accept, subject).await; let ao_resp = client .request( AccountObjects::new( None, subject.classic_address.clone().into(), None, None, Some(AccountObjectType::Credential), None, None, None, ) .into(), ) .await .expect("provision_credential: account_objects request failed"); let ao_result: results::account_objects::AccountObjects<'_> = ao_resp .try_into() .expect("provision_credential: parse account_objects"); assert!( !ao_result.account_objects.is_empty(), "provision_credential: no credential object found after CredentialAccept" ); ao_result.account_objects[0]["index"] .as_str() .expect("provision_credential: index field missing on credential object") .to_string() } /// Set up credential-based DepositPreauth: provision an accepted Credential /// (issuer → subject), then authorize it on `destination`. Returns the /// credential hash so callers can attach it to `credential_ids` on transactions. #[cfg(feature = "std")] pub async fn provision_credential_for_destination( issuer: &xrpl::wallet::Wallet, subject: &xrpl::wallet::Wallet, destination: &xrpl::wallet::Wallet, credential_type: &str, ) -> String { use xrpl::models::{ transactions::{deposit_preauth::DepositPreauth, CommonFields, TransactionType}, CredentialAuthorization, CredentialAuthorizationFields, }; let credential_hash = provision_credential(issuer, subject, credential_type).await; // Enable Deposit Authorization on destination so rippled enforces DepositPreauth rules. // Without lsfDepositAuth set, rippled ignores DepositPreauth entries entirely and any // payment flows through regardless of credential_ids. { use xrpl::models::transactions::account_set::{AccountSet, AccountSetFlag}; let mut acct_set = AccountSet { common_fields: CommonFields { account: destination.classic_address.clone().into(), transaction_type: TransactionType::AccountSet, ..Default::default() }, set_flag: Some(AccountSetFlag::AsfDepositAuth), ..Default::default() }; test_transaction(&mut acct_set, destination).await; } let creds = vec![CredentialAuthorization::new( CredentialAuthorizationFields::new( issuer.classic_address.clone().into(), credential_type.to_owned().into(), ), )]; let mut preauth = DepositPreauth { common_fields: CommonFields { account: destination.classic_address.clone().into(), transaction_type: TransactionType::DepositPreauth, ..Default::default() }, authorize_credentials: Some(creds), ..Default::default() }; test_transaction(&mut preauth, destination).await; credential_hash }