Skip to content

Commit

Permalink
feat: rav trigger config in GRT instead of wei
Browse files Browse the repository at this point in the history
Signed-off-by: Alexis Asseman <[email protected]>
  • Loading branch information
aasseman committed Nov 13, 2023
1 parent 8e6460f commit 05cac7c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
21 changes: 18 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tap-agent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ alloy-primitives = "0.4.2"
alloy-sol-types = "0.4.2"
anyhow = "1.0.72"
async-trait = "0.1.72"
bigdecimal = { version = "0.4.2", features = ["serde"] }
clap = { version = "4.4.3", features = ["derive", "env"] }
confy = "0.5.1"
dotenvy = "0.15.7"
Expand Down
59 changes: 56 additions & 3 deletions tap-agent/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright 2023-, GraphOps and Semiotic Labs.
// SPDX-License-Identifier: Apache-2.0

use std::path::PathBuf;
use std::{path::PathBuf, str::FromStr};

use alloy_primitives::Address;
use bigdecimal::{BigDecimal, ToPrimitive};
use clap::{command, Args, Parser, ValueEnum};
use dotenvy::dotenv;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -218,8 +219,9 @@ pub struct Tap {
long,
value_name = "rav-request-trigger-value",
env = "RAV_REQUEST_TRIGGER_VALUE",
help = "Value of unaggregated fees that triggers a RAV request (in GRT wei).",
default_value_t = 10_000_000_000_000_000_000 // 10 GRT
help = "Value of unaggregated fees that triggers a RAV request (in GRT).",
default_value = "10",
value_parser(parse_grt_value_to_nonzero_u64)
)]
pub rav_request_trigger_value: u64,
#[clap(
Expand Down Expand Up @@ -266,6 +268,26 @@ fn init_tracing(format: String) -> Result<(), SetGlobalDefaultError> {
}
}

fn parse_grt_value_to_nonzero_u64(s: &str) -> Result<u64, std::io::Error> {
let v = BigDecimal::from_str(s)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e.to_string()))?;
if v <= 0.into() {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"GRT value must be greater than 0".to_string(),
));
}
// Convert to wei
let v = v * BigDecimal::from(10u64.pow(18));
// Convert to u64
v.to_u64().ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::Other,
"GRT value cannot be represented as a u64 GRT wei value".to_string(),
)
})
}

impl Cli {
/// Parse config arguments If environmental variable for config is set to a valid
/// config file path, then parse from config Otherwise parse from command line
Expand Down Expand Up @@ -319,3 +341,34 @@ pub enum LogLevel {
Error,
Fatal,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_parse_grt_value_to_u64() {
assert_eq!(
parse_grt_value_to_nonzero_u64("1").unwrap(),
1_000_000_000_000_000_000
);
assert_eq!(
parse_grt_value_to_nonzero_u64("1.1").unwrap(),
1_100_000_000_000_000_000
);
assert_eq!(
parse_grt_value_to_nonzero_u64("1.000000000000000001").unwrap(),
1_000_000_000_000_000_001
);
assert_eq!(
parse_grt_value_to_nonzero_u64("0.000000000000000001").unwrap(),
1
);
assert!(parse_grt_value_to_nonzero_u64("0").is_err());
assert!(parse_grt_value_to_nonzero_u64("-1").is_err());
assert_eq!(
parse_grt_value_to_nonzero_u64("1.0000000000000000001").unwrap(),
1_000_000_000_000_000_000
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ impl Drop for SenderAllocationRelationshipsManager {

#[cfg(test)]
mod tests {

use indexer_common::{
prelude::{AllocationStatus, SubgraphDeployment},
subgraph_client::DeploymentDetails,
Expand Down

0 comments on commit 05cac7c

Please sign in to comment.