Skip to content

Commit

Permalink
Faucet hotfixes and improvements (#33)
Browse files Browse the repository at this point in the history
* Record claims only once tokens got claimed

* return errors to the client
  • Loading branch information
shekohex authored Dec 1, 2023
1 parent b64b76b commit 9495ec2
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 53 deletions.
62 changes: 33 additions & 29 deletions src/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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(
Expand Down
59 changes: 35 additions & 24 deletions src/txes/processor.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -207,8 +207,8 @@ async fn handle_substrate_tx(
asset_id: Option<u32>,
signer: subxt_signer::sr25519::Keypair,
result_sender: oneshot::Sender<Result<TxResult, Error>>,
) -> Result<H256, Error> {
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
Expand All @@ -219,29 +219,47 @@ 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(
api: OnlineClient<PolkadotConfig>,
to: AccountId32,
amount: u128,
signer: subxt_signer::sr25519::Keypair,
result_sender: oneshot::Sender<Result<TxResult, Error>>,
) -> Result<H256, Error> {
) -> Result<TxResult, Error> {
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();

Expand All @@ -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::<RuntimeApi::balances::events::Transfer>()
.map_err(|e| Error::Custom(e.to_string()))?;
if let Some(event) = transfer_event {
Expand All @@ -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,
})
}

0 comments on commit 9495ec2

Please sign in to comment.