Skip to content

Commit

Permalink
added faucet withdraw limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Gianmarco Fraccaroli committed Dec 18, 2023
1 parent 2c72cd4 commit 8e789d9
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ impl ApplicationServer {
let chain_id = config.chain_id.clone();
let rpc = config.rpc.clone();
let chain_start = config.chain_start;
let withdraw_limit = config.withdraw_limit.unwrap_or(1000_u64);

let sk = config.private_key.clone();
let sk = sk_from_str(&sk);
Expand Down Expand Up @@ -114,6 +115,7 @@ impl ApplicationServer {
difficulty,
chain_id,
chain_start,
withdraw_limit
);

Router::new()
Expand Down
3 changes: 3 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct AppConfig {
#[clap(long, env)]
pub rpc: String,

#[clap(long, env)]
pub withdraw_limit: Option<u64>,

#[clap(long, env)]
pub auth_key: Option<String>,

Expand Down
1 change: 1 addition & 0 deletions src/dto/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@ pub struct FaucetSettingResponse {
pub difficulty: u64,
pub chain_id: String,
pub start_at: i64,
pub withdraw_limit: u64,
pub tokens_alias_to_address: HashMap<String, String>
}
3 changes: 3 additions & 0 deletions src/error/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ pub enum FaucetError {
ChainNotStarted,
#[error("Error while sending transfer: {0}")]
SdkError(String),
#[error("Withdraw limit must be less then {0}")]
InvalidWithdrawLimit(u64)
}

impl IntoResponse for FaucetError {
Expand All @@ -29,6 +31,7 @@ impl IntoResponse for FaucetError {
FaucetError::DuplicateChallenge => StatusCode::CONFLICT,
FaucetError::InvalidAddress => StatusCode::BAD_REQUEST,
FaucetError::ChainNotStarted => StatusCode::BAD_REQUEST,
FaucetError::InvalidWithdrawLimit(_) => StatusCode::BAD_REQUEST,
FaucetError::SdkError(_) => StatusCode::BAD_REQUEST,
};

Expand Down
5 changes: 5 additions & 0 deletions src/handler/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub async fn faucet_settings(
difficulty: state.difficulty,
chain_id: state.chain_id,
start_at: state.chain_start,
withdraw_limit: state.withdraw_limit,
tokens_alias_to_address: HashMap::from([("NAM".to_string(), nam_token_address.to_string())])
};

Expand All @@ -55,6 +56,10 @@ pub async fn request_transfer(
) -> Result<Json<FaucetResponseStatusDto>, ApiError> {
let auth_key: String = state.auth_key.clone();

if payload.transfer.amount > state.withdraw_limit {
return Err(FaucetError::InvalidWithdrawLimit(state.withdraw_limit).into())
}

let token_address = Address::decode(payload.transfer.token.clone());
let token_address = if let Ok(address) = token_address {
address
Expand Down
3 changes: 3 additions & 0 deletions src/state/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct FaucetState {
pub difficulty: u64,
pub chain_id: String,
pub chain_start: i64,
pub withdraw_limit: u64
}

impl FaucetState {
Expand All @@ -32,6 +33,7 @@ impl FaucetState {
difficulty: u64,
chain_id: String,
chain_start: i64,
withdraw_limit: u64
) -> Self {
Self {
faucet_service: FaucetService::new(data),
Expand All @@ -42,6 +44,7 @@ impl FaucetState {
difficulty,
chain_id,
chain_start,
withdraw_limit: withdraw_limit * 10_u64.pow(6)
}
}
}

0 comments on commit 8e789d9

Please sign in to comment.