From 7e60a21956ba394f697f8afdb9557a70ec984e42 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 18 Sep 2023 12:26:52 -0700 Subject: [PATCH] prepare for more complex variable pricing --- README.md | 3 +++ web3_proxy/src/compute_units.rs | 17 ++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index d326414d..cfcfebf4 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,9 @@ Check that the proxy is working: curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"web3_clientVersion","id":1}' 127.0.0.1:8544 ``` ``` +curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_blockNumber","id":1}' 127.0.0.1:8544 +``` +``` curl -X POST -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getBlockByNumber", "params": ["latest", false],"id":1}' 127.0.0.1:8544 ``` ``` diff --git a/web3_proxy/src/compute_units.rs b/web3_proxy/src/compute_units.rs index 2f4c63c0..7a6fca64 100644 --- a/web3_proxy/src/compute_units.rs +++ b/web3_proxy/src/compute_units.rs @@ -19,6 +19,10 @@ pub fn default_usd_per_cu(chain_id: u64) -> Decimal { } } +pub fn default_cu_per_byte(_chain_id: u64) -> Decimal { + Decimal::new(4, 2).unwrap() +} + #[derive(Debug)] pub struct ComputeUnit(Decimal); @@ -39,7 +43,7 @@ impl ComputeUnit { pub fn new(method: &str, chain_id: u64, response_bytes: u64) -> Self { // TODO: this works, but this is fragile. think of a better way to check the method is a subscription if method.ends_with(')') { - return Self::subscription_response(response_bytes); + return Self::variable_price(chain_id, method, response_bytes); } let cu = if method.starts_with("admin_") || method.starts_with("alchemy_") { @@ -139,7 +143,7 @@ impl ComputeUnit { (_, "trace_replayTransaction") => 2983, (_, "trace_transaction") => 26, (_, "txpool_content") => { - return Self::subscription_response(response_bytes) + 1000; + return Self::variable_price(chain_id, method, response_bytes) + 1000; } (_, "invalid_method") => 100, (_, "web3_clientVersion") => 15, @@ -174,9 +178,12 @@ impl ComputeUnit { /// notifications and subscription responses cost per-byte #[instrument(level = "trace")] - pub fn subscription_response + std::fmt::Debug>(num_bytes: D) -> Self { - // TODO: get multiplier from config - let cu = num_bytes.into() * Decimal::new(4, 2); + pub fn variable_price + std::fmt::Debug>( + chain_id: u64, + method: &str, + num_bytes: D, + ) -> Self { + let cu = num_bytes.into() * default_cu_per_byte(chain_id); Self(cu) }