|
1 | 1 | // Copyright 2023-, GraphOps and Semiotic Labs.
|
2 | 2 | // SPDX-License-Identifier: Apache-2.0
|
3 | 3 |
|
4 |
| -use std::path::PathBuf; |
| 4 | +use std::{path::PathBuf, str::FromStr}; |
5 | 5 |
|
6 | 6 | use alloy_primitives::Address;
|
| 7 | +use bigdecimal::{BigDecimal, ToPrimitive}; |
7 | 8 | use clap::{command, Args, Parser, ValueEnum};
|
8 | 9 | use dotenvy::dotenv;
|
9 | 10 | use serde::{Deserialize, Serialize};
|
@@ -218,8 +219,9 @@ pub struct Tap {
|
218 | 219 | long,
|
219 | 220 | value_name = "rav-request-trigger-value",
|
220 | 221 | env = "RAV_REQUEST_TRIGGER_VALUE",
|
221 |
| - help = "Value of unaggregated fees that triggers a RAV request (in GRT wei).", |
222 |
| - default_value_t = 10_000_000_000_000_000_000 // 10 GRT |
| 222 | + help = "Value of unaggregated fees that triggers a RAV request (in GRT).", |
| 223 | + default_value = "10", |
| 224 | + value_parser(parse_grt_value_to_nonzero_u64) |
223 | 225 | )]
|
224 | 226 | pub rav_request_trigger_value: u64,
|
225 | 227 | #[clap(
|
@@ -266,6 +268,26 @@ fn init_tracing(format: String) -> Result<(), SetGlobalDefaultError> {
|
266 | 268 | }
|
267 | 269 | }
|
268 | 270 |
|
| 271 | +fn parse_grt_value_to_nonzero_u64(s: &str) -> Result<u64, std::io::Error> { |
| 272 | + let v = BigDecimal::from_str(s) |
| 273 | + .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?; |
| 274 | + if v <= 0.into() { |
| 275 | + return Err(std::io::Error::new( |
| 276 | + std::io::ErrorKind::Other, |
| 277 | + "GRT value must be greater than 0".to_string(), |
| 278 | + )); |
| 279 | + } |
| 280 | + // Convert to wei |
| 281 | + let v = v * BigDecimal::from(10u64.pow(18)); |
| 282 | + // Convert to u64 |
| 283 | + v.to_u64().ok_or_else(|| { |
| 284 | + std::io::Error::new( |
| 285 | + std::io::ErrorKind::Other, |
| 286 | + "GRT value cannot be represented as a u64 GRT wei value".to_string(), |
| 287 | + ) |
| 288 | + }) |
| 289 | +} |
| 290 | + |
269 | 291 | impl Cli {
|
270 | 292 | /// Parse config arguments If environmental variable for config is set to a valid
|
271 | 293 | /// config file path, then parse from config Otherwise parse from command line
|
@@ -319,3 +341,34 @@ pub enum LogLevel {
|
319 | 341 | Error,
|
320 | 342 | Fatal,
|
321 | 343 | }
|
| 344 | + |
| 345 | +#[cfg(test)] |
| 346 | +mod tests { |
| 347 | + use super::*; |
| 348 | + |
| 349 | + #[test] |
| 350 | + fn test_parse_grt_value_to_u64() { |
| 351 | + assert_eq!( |
| 352 | + parse_grt_value_to_nonzero_u64("1").unwrap(), |
| 353 | + 1_000_000_000_000_000_000 |
| 354 | + ); |
| 355 | + assert_eq!( |
| 356 | + parse_grt_value_to_nonzero_u64("1.1").unwrap(), |
| 357 | + 1_100_000_000_000_000_000 |
| 358 | + ); |
| 359 | + assert_eq!( |
| 360 | + parse_grt_value_to_nonzero_u64("1.000000000000000001").unwrap(), |
| 361 | + 1_000_000_000_000_000_001 |
| 362 | + ); |
| 363 | + assert_eq!( |
| 364 | + parse_grt_value_to_nonzero_u64("0.000000000000000001").unwrap(), |
| 365 | + 1 |
| 366 | + ); |
| 367 | + assert!(parse_grt_value_to_nonzero_u64("0").is_err()); |
| 368 | + assert!(parse_grt_value_to_nonzero_u64("-1").is_err()); |
| 369 | + assert_eq!( |
| 370 | + parse_grt_value_to_nonzero_u64("1.0000000000000000001").unwrap(), |
| 371 | + 1_000_000_000_000_000_000 |
| 372 | + ); |
| 373 | + } |
| 374 | +} |
0 commit comments