diff --git a/Rocket.example.toml b/Rocket.example.toml index 5bd9615..84197a5 100644 --- a/Rocket.example.toml +++ b/Rocket.example.toml @@ -1,9 +1,11 @@ [default] db = "faucet" mnemonic="" -time_to_wait_between_claims = { secs = 0, nanos = 0 } -native_token_amount = 0.5 -token_amount = 10 +time_to_wait_between_claims = { secs = 30, nanos = 0 } +token_amount = 20 +native_token_amount = 3 +verify_following_webb = false +tx_timeout = 30000 [global.oauth.twitter] provider = "twitter" diff --git a/src/faucet.rs b/src/faucet.rs index d8076b1..34e8836 100644 --- a/src/faucet.rs +++ b/src/faucet.rs @@ -117,6 +117,9 @@ pub async fn handle_token_transfer( .as_u128(), asset_id: None, signer: signer_pair.inner().clone(), + timeout: std::time::Duration::from_millis( + app_config.tx_timeout, + ), result_sender, }) .expect("Failed to send transaction to processor"); diff --git a/src/main.rs b/src/main.rs index 37e1ef5..58f27dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,6 +74,10 @@ const fn default_verify_following_webb() -> bool { true } +const fn default_tx_timeout_ms() -> u64 { + 18_000 // 3 blocks +} + #[derive(Deserialize)] pub struct AppConfig { /// The database to use for the auth and claims @@ -93,6 +97,10 @@ pub struct AppConfig { /// Whether to verify that the user is following the webb twitter account #[serde(default = "default_verify_following_webb")] pub verify_following_webb: bool, + /// The timeout for transactions in milliseconds + /// This is to prevent the faucet from hanging + #[serde(default = "default_tx_timeout_ms")] + pub tx_timeout: u64, } fn auth_db_firing() -> impl Fairing { diff --git a/src/txes/processor.rs b/src/txes/processor.rs index 498462b..5cc942f 100644 --- a/src/txes/processor.rs +++ b/src/txes/processor.rs @@ -60,6 +60,7 @@ impl TransactionProcessingSystem { asset_id, signer, result_sender, + timeout, } => { let res = handle_substrate_tx( api, @@ -68,6 +69,7 @@ impl TransactionProcessingSystem { native_token_amount, asset_id, signer, + timeout, result_sender, ) .await; @@ -199,6 +201,7 @@ async fn handle_evm_token_tx( } } +#[allow(clippy::too_many_arguments)] async fn handle_substrate_tx( api: OnlineClient, to: AccountId32, @@ -206,6 +209,7 @@ async fn handle_substrate_tx( native_token_amount: u128, asset_id: Option, signer: subxt_signer::sr25519::Keypair, + timeout: std::time::Duration, result_sender: oneshot::Sender>, ) -> Result<(), Error> { let res = match asset_id { @@ -219,6 +223,7 @@ async fn handle_substrate_tx( to, native_token_amount, signer, + timeout, ) .await } @@ -236,8 +241,8 @@ async fn handle_substrate_native_tx( to: AccountId32, amount: u128, signer: subxt_signer::sr25519::Keypair, + timeout: std::time::Duration, ) -> Result { - const BLOCK_TIME: u64 = 6000; // 6 seconds let to_address = MultiAddress::Id(to.clone()); let balance_transfer_tx = RuntimeApi::tx().balances().transfer(to_address, amount); @@ -253,12 +258,12 @@ async fn handle_substrate_native_tx( let tx_result_fut = tx_api .sign_and_submit_then_watch_default(&balance_transfer_tx, &signer) .map_err(|e| Error::Custom(e.to_string())); - let timeout_fut = - tokio::time::sleep(std::time::Duration::from_millis(2 * BLOCK_TIME)); + + let timeout_fut = tokio::time::sleep(timeout); let tx_result = tokio::select! { res = tx_result_fut => res, - _ = timeout_fut => Err(Error::Custom("Timed out waiting for tx to be included in block".to_string())), + _ = timeout_fut => Err(Error::Custom("Timed out waiting for tx to be sent to the network, please try again".to_string())), }?; let tx_hash = tx_result.extrinsic_hash(); diff --git a/src/txes/types.rs b/src/txes/types.rs index b0e89f0..b1308a2 100644 --- a/src/txes/types.rs +++ b/src/txes/types.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; use std::sync::Arc; +use std::time::Duration; use ethers::types::U256; use ethers::{ @@ -62,6 +63,7 @@ pub enum Transaction { native_token_amount: u128, asset_id: Option, signer: subxt_signer::sr25519::Keypair, + timeout: Duration, result_sender: oneshot::Sender>, }, }