prepare for more complex variable pricing

This commit is contained in:
Bryan Stitt 2023-09-18 12:26:52 -07:00
parent c5c670169f
commit 7e60a21956
2 changed files with 15 additions and 5 deletions

@ -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":"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 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
``` ```
``` ```

@ -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)] #[derive(Debug)]
pub struct ComputeUnit(Decimal); pub struct ComputeUnit(Decimal);
@ -39,7 +43,7 @@ impl ComputeUnit {
pub fn new(method: &str, chain_id: u64, response_bytes: u64) -> Self { 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 // TODO: this works, but this is fragile. think of a better way to check the method is a subscription
if method.ends_with(')') { 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_") { let cu = if method.starts_with("admin_") || method.starts_with("alchemy_") {
@ -139,7 +143,7 @@ impl ComputeUnit {
(_, "trace_replayTransaction") => 2983, (_, "trace_replayTransaction") => 2983,
(_, "trace_transaction") => 26, (_, "trace_transaction") => 26,
(_, "txpool_content") => { (_, "txpool_content") => {
return Self::subscription_response(response_bytes) + 1000; return Self::variable_price(chain_id, method, response_bytes) + 1000;
} }
(_, "invalid_method") => 100, (_, "invalid_method") => 100,
(_, "web3_clientVersion") => 15, (_, "web3_clientVersion") => 15,
@ -174,9 +178,12 @@ impl ComputeUnit {
/// notifications and subscription responses cost per-byte /// notifications and subscription responses cost per-byte
#[instrument(level = "trace")] #[instrument(level = "trace")]
pub fn subscription_response<D: Into<Decimal> + std::fmt::Debug>(num_bytes: D) -> Self { pub fn variable_price<D: Into<Decimal> + std::fmt::Debug>(
// TODO: get multiplier from config chain_id: u64,
let cu = num_bytes.into() * Decimal::new(4, 2); method: &str,
num_bytes: D,
) -> Self {
let cu = num_bytes.into() * default_cu_per_byte(chain_id);
Self(cu) Self(cu)
} }