Skip to content

Commit

Permalink
Fixing remaining issues
Browse files Browse the repository at this point in the history
  • Loading branch information
leboiko committed Jan 27, 2025
1 parent c1b8fe7 commit ed244d9
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ services:
container_name: rpc-proxy
image: ghcr.io/0xintuition/rpc-proxy:latest
environment:
DATABASE_URL: $DATABASE_URL
PROXY_DATABASE_URL: $DATABASE_URL
PROXY_API_PORT: $PROXY_API_PORT
PROXY_SCHEMA: $PROXY_SCHEMA
BASE_MAINNET_RPC_URL: $BASE_MAINNET_RPC_URL
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
ALTER TYPE base_proxy.method ADD VALUE 'eth_getBlockByNumber';

ALTER TABLE base_proxy.json_rpc_cache ALTER COLUMN to_address DROP NOT NULL;
4 changes: 2 additions & 2 deletions rpc-proxy/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use utoipa_swagger_ui::SwaggerUi;
#[derive(Clone, Deserialize, Default)]
pub struct Env {
pub proxy_api_port: u16,
pub database_url: String,
pub proxy_database_url: String,
pub proxy_schema: String,
pub base_mainnet_rpc_url: String,
pub base_sepolia_rpc_url: String,
Expand Down Expand Up @@ -92,7 +92,7 @@ impl App {
dotenvy::dotenv().ok();
// Load the environment variables into our struct
let env = envy::from_env::<Env>().map_err(ApiError::from)?;
let pg_pool = connect_to_db(&env.database_url).await?;
let pg_pool = connect_to_db(&env.proxy_database_url).await?;
let reqwest_client = Client::new();
Ok(Self {
env,
Expand Down
47 changes: 20 additions & 27 deletions rpc-proxy/src/endpoints/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,28 @@ impl JsonRpcRequest {
}

/// Get the contract address from the request.
pub fn get_contract_address(&self, chain_id: u64) -> Result<String, ApiError> {
match chain_id {
84532 => Ok("0x1A6950807E33d5bC9975067e6D6b5Ea4cD661665"
.to_string()
.to_lowercase()),
8453 => Ok("0x430BbF52503Bd4801E51182f4cB9f8F534225DE5"
.to_string()
.to_lowercase()),
1 => Ok("0x4200000000000000000000000000000000000006"
.to_string()
.to_lowercase()),
_ => Err(ApiError::InvalidInput("Chain ID not supported".into())),
pub fn get_contract_address(&self) -> Result<Option<String>, ApiError> {
match Method::from_str(&self.method) {
Ok(Method::EthCall) => Ok(Some(
self.params[0]["to"]
.to_string()
.trim_matches('"')
.to_string(),
)),
Ok(Method::EthBlockByNumber) => Ok(None),
_ => Err(ApiError::InvalidInput("Method not supported".into())),
}
}

/// Get the input from the request.
pub fn get_input(&self, method: Method) -> Result<String, ApiError> {
match method {
Method::EthCall => {
let input = self.params[0]["input"].to_string();
// Trim any leading or trailing quotes
let trimmed_input = input.trim_matches('"').to_string();
Ok(trimmed_input)
let input = self.params[0]["input"]
.to_string()
.trim_matches('"')
.to_string();
Ok(input)
}
Method::EthBlockByNumber => {
let input = self.params[0].as_str().unwrap();
Expand Down Expand Up @@ -133,30 +132,24 @@ impl JsonRpcRequest {
result: Value,
method: Method,
) -> Result<JsonRpcCache, ApiError> {
let share_price = JsonRpcCache {
let cached_request = JsonRpcCache {
chain_id: chain_id as i64,
block_number: self.block_number()?.ok_or(ApiError::BlockNumberNotFound)?,
method: method.clone(),
to_address: self
.get_contract_address(chain_id)?
.trim_matches('"')
.to_string(),
input: self
.get_input(method.clone())?
.trim_matches('"')
.to_string(),
to_address: self.get_contract_address()?,
input: self.get_input(method.clone())?,
result: match method {
Method::EthBlockByNumber => {
serde_json::to_string(&result["result"]).unwrap_or_default()
}
_ => result["result"].as_str().unwrap_or("").to_string(),
},
};
share_price
cached_request
.insert(&state.pg_pool, &state.env.proxy_schema)
.await
.map_err(|e| ApiError::Model(models::error::ModelError::SqlError(e)))?;
Ok(share_price)
Ok(cached_request)
}
}

Expand Down
17 changes: 7 additions & 10 deletions rpc-proxy/src/models/json_rpc_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub struct JsonRpcCache {
pub chain_id: i64,
pub block_number: i64,
pub method: Method,
pub to_address: String,
pub to_address: Option<String>,
pub input: String,
pub result: String,
}
Expand Down Expand Up @@ -82,7 +82,10 @@ impl JsonRpcCache {
let query = format!(
r#"
SELECT * FROM {}.json_rpc_cache
WHERE chain_id = $1 AND block_number = $2 AND to_address = $3 AND input = $4
WHERE chain_id = $1
AND block_number = $2
AND (($3::text IS NULL AND to_address IS NULL) OR to_address = $3)
AND input = $4
"#,
app_state.env.proxy_schema,
);
Expand All @@ -94,13 +97,7 @@ impl JsonRpcCache {
.block_number()?
.ok_or(ApiError::JsonRpc("Block number is required".to_string()))?,
)
.bind(
payload
.get_contract_address(chain_id as u64)?
.trim_matches('"')
.to_string()
.to_lowercase(),
)
.bind(payload.get_contract_address()?)
.bind(payload.get_input(method)?)
.fetch_optional(&app_state.pg_pool)
.await?)
Expand Down Expand Up @@ -130,7 +127,7 @@ mod tests {
chain_id,
block_number: block_number_parsed,
method: Method::EthCall,
to_address: "0x1a6950807e33d5bc9975067e6d6b5ea4cd661665".to_string(),
to_address: Some("0x1a6950807e33d5bc9975067e6d6b5ea4cd661665".to_string()),
input: "0xee9dd98f00000000000000000000000000000000000000000000000000000000000003ec"
.to_string(),
result: "test_result".to_string(),
Expand Down

0 comments on commit ed244d9

Please sign in to comment.