From 82ee0e2a4ebf92c61a2e651bab68fea60bb8f7c2 Mon Sep 17 00:00:00 2001 From: Shady Khalifa Date: Mon, 27 Nov 2023 16:38:09 +0200 Subject: [PATCH] fix: wait till tx in block --- chainIdRpcs.json | 3 ++ src/faucet.rs | 81 +++++++++++++++++++++++++++++-------------- src/main.rs | 6 +++- src/txes/networks.rs | 5 +++ src/txes/processor.rs | 36 ++++++++++--------- 5 files changed, 87 insertions(+), 44 deletions(-) diff --git a/chainIdRpcs.json b/chainIdRpcs.json index fa20c37..a2070f4 100644 --- a/chainIdRpcs.json +++ b/chainIdRpcs.json @@ -61,5 +61,8 @@ ], "1081": [ "ws://3.144.199.9:9944" + ], + "1082": [ + "ws://127.0.0.1:9944" ] } diff --git a/src/faucet.rs b/src/faucet.rs index 592c01e..89b9689 100644 --- a/src/faucet.rs +++ b/src/faucet.rs @@ -142,34 +142,33 @@ pub async fn handle_token_transfer( Ok(result) } -#[post("/faucet", data = "")] -#[allow(clippy::too_many_arguments)] -pub async fn faucet( +pub async fn check_twitter( app_config: &State, twitter_bearer_token: auth::TwitterBearerToken<'_>, - payload: Json, - auth_db: &State, - evm_providers: &State>, - substrate_providers: &State< - SubstrateProviders>, - >, - evm_wallet: &State>, - signer_pair: &State, - tx_sender: &State>, -) -> Result, Error> { - let faucet_data = payload.clone().into_inner().faucet; +) -> Result { + // during debug builds, we return a dummy user + if cfg!(debug_assertions) { + let token = twitter_bearer_token.token(); + return Ok(twitter_v2::User { + id: NumericId::new(token.parse().unwrap()), + username: "dummy".to_string(), + name: "dummy".to_string(), + created_at: None, + description: None, + entities: None, + location: None, + pinned_tweet_id: None, + profile_image_url: None, + protected: None, + public_metrics: None, + url: None, + verified: None, + withheld: None, + }); + } let auth = BearerToken::new(twitter_bearer_token.token()); let twitter_api = TwitterApi::new(auth); - // Extract faucet request fields - let FaucetRequest { - wallet_address, - typed_chain_id, - .. - } = faucet_data.clone(); - println!( - "Requesting faucet for (address {}, chain: {:?}", - wallet_address, typed_chain_id - ); + let twitter_user: twitter_v2::User = twitter_api .get_users_me() .send() @@ -269,11 +268,41 @@ pub async fn faucet( ); if !is_following_webb { - return Err(Error::Custom( + Err(Error::Custom( "User is not following the webb twitter account".to_string(), - )); + )) + } else { + Ok(twitter_user) } +} +#[post("/faucet", data = "")] +#[allow(clippy::too_many_arguments)] +pub async fn faucet( + app_config: &State, + twitter_bearer_token: auth::TwitterBearerToken<'_>, + payload: Json, + auth_db: &State, + evm_providers: &State>, + substrate_providers: &State< + SubstrateProviders>, + >, + evm_wallet: &State>, + signer_pair: &State, + tx_sender: &State>, +) -> Result, Error> { + let twitter_user = check_twitter(app_config, twitter_bearer_token).await?; + let faucet_data = payload.clone().into_inner().faucet; + // Extract faucet request fields + let FaucetRequest { + wallet_address, + typed_chain_id, + .. + } = faucet_data.clone(); + println!( + "Requesting faucet for (address {}, chain: {:?}", + wallet_address, typed_chain_id + ); // Check if the user's last claim date is within the last 24 hours let claim_data = auth_db .get_last_claim_data(twitter_user.id.into(), typed_chain_id) diff --git a/src/main.rs b/src/main.rs index 79a1980..37e1ef5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -219,7 +219,11 @@ fn substrate_providers_firing() -> impl Fairing { let result: Result>, Error> = match rocket.state::() { Some(_config) => { - let networks = vec![Network::Tangle]; + let networks = vec![ + Network::Tangle, + #[cfg(debug_assertions)] + Network::TangleLocal, + ]; let mut futures_unordered = FuturesUnordered::new(); diff --git a/src/txes/networks.rs b/src/txes/networks.rs index cf56dbc..add97dd 100644 --- a/src/txes/networks.rs +++ b/src/txes/networks.rs @@ -16,6 +16,7 @@ pub enum Network { // Substrate Tangle, + TangleLocal, } impl Network { @@ -34,6 +35,7 @@ impl Network { "demeter" => Some(Self::Demeter), "tangle-evm-testnet" => Some(Self::TangleEVMTestnet), "tangle" => Some(Self::Tangle), + "tangle-local" => Some(Self::TangleLocal), _ => None, } } @@ -77,6 +79,7 @@ impl Network { pub fn from_substrate_chain_id(chain_id: u64) -> Option { match chain_id { 1081 => Some(Self::Tangle), + 1082 => Some(Self::TangleLocal), _ => None, } } @@ -84,6 +87,7 @@ impl Network { pub fn to_substrate_chain_id(&self) -> Option { match self { Self::Tangle => Some(1081), + Self::TangleLocal => Some(1082), _ => None, } } @@ -103,6 +107,7 @@ impl Network { pub fn to_typed_chain_id(&self) -> Option { match self { Self::Tangle => Some(TypedChainId::Substrate(1081)), + Self::TangleLocal => Some(TypedChainId::Substrate(1082)), Self::ArbitrumGoerli => Some(TypedChainId::Evm(421613)), Self::Athena => Some(TypedChainId::Evm(3884533461)), Self::Demeter => Some(TypedChainId::Evm(3884533463)), diff --git a/src/txes/processor.rs b/src/txes/processor.rs index 46add5e..61684bf 100644 --- a/src/txes/processor.rs +++ b/src/txes/processor.rs @@ -1,4 +1,3 @@ - use std::sync::Arc; use crate::subxt::utils::H256; @@ -248,28 +247,31 @@ async fn handle_substrate_native_tx( println!("Tranasction sent with TxHash: {:?}", tx_hash); - // let events = tx_result - // .wait_for_finalized_success() - // .await - // .map_err(|e| Error::Custom(e.to_string()))?; - // let block_hash = events.block_hash(); + let tx_block = tx_result + .wait_for_in_block() + .await + .map_err(|e| Error::Custom(e.to_string()))?; + let block_hash = tx_block.block_hash(); - // // Find a Transfer event and print it. - // let transfer_event = events - // .find_first::() - // .map_err(|e| Error::Custom(e.to_string()))?; - // if let Some(event) = transfer_event { - // let from = event.from; - // let to = event.to; - // let amount = event.amount.div(10u128.pow(18)); - // println!("Transfered {amount} tokens {from} -> {to}"); - // } + // Find a Transfer event and print it. + let transfer_event = tx_block + .fetch_events() + .await + .map_err(|e| Error::Custom(e.to_string()))? + .find_first::() + .map_err(|e| Error::Custom(e.to_string()))?; + if let Some(event) = transfer_event { + let from = event.from; + let to = event.to; + let amount = event.amount / (10u128.pow(18)); + println!("Transfered {amount} tokens {from} -> {to}"); + } // Return the transaction hash. result_sender .send(Ok(TxResult::Substrate { tx_hash, - block_hash: tx_hash, + block_hash, })) .map_err(|e| { Error::Custom(format!("Failed to send tx_hash: {:?}", e))