diff --git a/src/faucet.rs b/src/faucet.rs index 89b9689..d8076b1 100644 --- a/src/faucet.rs +++ b/src/faucet.rs @@ -335,27 +335,10 @@ pub async fn faucet( } } - let claim: ClaimsData = ClaimsData { - identity: twitter_user.id.into(), - address: wallet_address.clone().into(), - last_claimed_date: now, - }; - - auth_db - .put_last_claim_data(twitter_user.id.into(), typed_chain_id, claim) - .await?; - // Process the claim and build the response println!( - "{:?} Claiming for user: {:?}", - Utc::now().to_rfc3339(), + "Paying {} ({wallet_address}) on chain: {typed_chain_id:?}", twitter_user.username, ); - println!( - "{:?} Paying {} on chain: {:?}", - Utc::now().to_rfc3339(), - wallet_address, - typed_chain_id - ); match handle_token_transfer( faucet_data, @@ -368,17 +351,38 @@ pub async fn faucet( ) .await { - Ok(tx_result) => Ok(status::Custom( - Status::Ok, - json!({ - "wallet": wallet_address, - "typed_chain_id": typed_chain_id, - "last_claimed_date": now, - "user": twitter_user, - "tx_result": tx_result, - }) - .to_string(), - )), + Ok(tx_result) => { + let claim: ClaimsData = ClaimsData { + identity: twitter_user.id.into(), + address: wallet_address.clone().into(), + last_claimed_date: now, + }; + + auth_db + .put_last_claim_data( + twitter_user.id.into(), + typed_chain_id, + claim, + ) + .await?; + println!( + "{:?} Paid {} on chain: {:?}", + Utc::now().to_rfc3339(), + wallet_address, + typed_chain_id + ); + Ok(status::Custom( + Status::Ok, + json!({ + "wallet": wallet_address, + "typed_chain_id": typed_chain_id, + "last_claimed_date": now, + "user": twitter_user, + "tx_result": tx_result, + }) + .to_string(), + )) + } Err(e) => { rocket::log::private::error!("Error transferring tokens: {e:?}"); Ok(status::Custom( diff --git a/src/txes/processor.rs b/src/txes/processor.rs index 61684bf..498462b 100644 --- a/src/txes/processor.rs +++ b/src/txes/processor.rs @@ -1,9 +1,9 @@ use std::sync::Arc; -use crate::subxt::utils::H256; use ethers::prelude::ContractCall; use ethers::providers::Middleware; use ethers::types::{Address, TransactionReceipt, TransactionRequest}; +use rocket::futures::TryFutureExt; use rocket::tokio::{self, sync::oneshot}; use tokio::sync::mpsc::UnboundedReceiver; use webb::evm::contract::protocol_solidity::erc20_preset_minter_pauser::ERC20PresetMinterPauserContract; @@ -207,8 +207,8 @@ async fn handle_substrate_tx( asset_id: Option, signer: subxt_signer::sr25519::Keypair, result_sender: oneshot::Sender>, -) -> Result { - match asset_id { +) -> Result<(), Error> { + let res = match asset_id { Some(asset_id) => Err(Error::Custom(format!( "Substrate only supports sending native tokens. Asset ID {} not supported", asset_id @@ -219,11 +219,16 @@ async fn handle_substrate_tx( to, native_token_amount, signer, - result_sender, ) .await } - } + }; + + // Return the transaction hash. + result_sender.send(res).map_err(|e| { + Error::Custom(format!("Failed to send tx_hash: {:?}", e)) + })?; + Ok(()) } async fn handle_substrate_native_tx( @@ -231,17 +236,30 @@ async fn handle_substrate_native_tx( to: AccountId32, amount: u128, signer: subxt_signer::sr25519::Keypair, - result_sender: oneshot::Sender>, -) -> Result { +) -> 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); + println!( + "Sending tx: {}.{}({}, {})", + balance_transfer_tx.pallet_name(), + balance_transfer_tx.call_name(), + to, + balance_transfer_tx.call_data().value + ); // Sign and submit the extrinsic. - let tx_result = api - .tx() + let tx_api = api.tx(); + let tx_result_fut = tx_api .sign_and_submit_then_watch_default(&balance_transfer_tx, &signer) - .await - .map_err(|e| Error::Custom(e.to_string()))?; + .map_err(|e| Error::Custom(e.to_string())); + let timeout_fut = + tokio::time::sleep(std::time::Duration::from_millis(2 * BLOCK_TIME)); + + 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())), + }?; let tx_hash = tx_result.extrinsic_hash(); @@ -256,8 +274,8 @@ async fn handle_substrate_native_tx( // Find a Transfer event and print it. let transfer_event = tx_block .fetch_events() - .await - .map_err(|e| Error::Custom(e.to_string()))? + .map_err(|e| Error::Custom(e.to_string())) + .await? .find_first::() .map_err(|e| Error::Custom(e.to_string()))?; if let Some(event) = transfer_event { @@ -267,15 +285,8 @@ async fn handle_substrate_native_tx( println!("Transfered {amount} tokens {from} -> {to}"); } - // Return the transaction hash. - result_sender - .send(Ok(TxResult::Substrate { - tx_hash, - block_hash, - })) - .map_err(|e| { - Error::Custom(format!("Failed to send tx_hash: {:?}", e)) - })?; - - Ok(tx_hash) + Ok(TxResult::Substrate { + tx_hash, + block_hash, + }) }