Skip to content

Commit

Permalink
feat: add testing-indexer
Browse files Browse the repository at this point in the history
feat: nearblocks_api_key
  • Loading branch information
Tguntenaar committed Nov 15, 2024
1 parent 8da3f7c commit 4a3cef0
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ jobs:
SQLX_OFFLINE: true
CONTRACT: devhub.near
DATABASE_URL: postgres://devhub_cache_api_rs:password@127.0.0.1:5432/devhub_cache_api_rs
NEARBLOCKS_API_KEY: ${{ secrets.NEARBLOCKS_API_KEY }}

- name: Run clippy and audit
run:
Expand Down
26 changes: 26 additions & 0 deletions fly.testing-indexer.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# fly.toml app configuration file generated for testing-indexer on 2024-11-15T08:25:29-06:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = 'testing-indexer'
primary_region = 'ams'

[build]

[env]
CONTRACT = 'testing-indexer.near'
ROCKET_PROFILE = 'release'

[http_service]
internal_port = 8080
force_https = true
auto_stop_machines = 'stop'
auto_start_machines = true
min_machines_running = 1
processes = ['app']

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1
3 changes: 2 additions & 1 deletion src/entrypoints/proposal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async fn get_proposals(
filters: Option<GetProposalFilters>,
db: &State<DB>,
contract: &State<AccountId>,
nearblocks_api_key: &State<String>,
) -> Option<Json<PaginatedResponse<ProposalWithLatestSnapshotView>>> {
let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_timestamp = db.get_last_updated_timestamp().await.unwrap();
Expand Down Expand Up @@ -95,7 +96,7 @@ async fn get_proposals(

println!("Fetching not yet indexed method calls from nearblocks");

let nearblocks_client = nearblocks_client::ApiClient::default();
let nearblocks_client = nearblocks_client::ApiClient::new(nearblocks_api_key.inner().clone());

let nearblocks_unwrapped = match nearblocks_client
.get_account_txns_by_pagination(
Expand Down
5 changes: 3 additions & 2 deletions src/entrypoints/rfp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ async fn get_rfps(
filters: Option<GetRfpFilters>,
db: &State<DB>,
contract: &State<AccountId>,
nearblocks_api_key: &State<String>,
) -> Option<Json<PaginatedResponse<RfpWithLatestSnapshotView>>> {
let current_timestamp_nano = chrono::Utc::now().timestamp_nanos_opt().unwrap();
let last_updated_timestamp = db.get_last_updated_timestamp().await.unwrap();
Expand Down Expand Up @@ -93,7 +94,7 @@ async fn get_rfps(
}
println!("Fetching not yet indexed method calls from nearblocks");

let nearblocks_client = nearblocks_client::ApiClient::default();
let nearblocks_client = nearblocks_client::ApiClient::new(nearblocks_api_key.inner().clone());

let nearblocks_unwrapped = match nearblocks_client
.get_account_txns_by_pagination(
Expand Down Expand Up @@ -157,7 +158,7 @@ async fn get_rfp(rfp_id: i32, contract: &State<AccountId>) -> Result<Json<Versio
{
Ok(rfp) => Ok(Json(rfp.data)),
Err(e) => {
eprintln!("Failed to get rfp from RPC: {:?}", e);
eprintln!("In /rfp/rfp_id; Failed to get rfp from RPC: {:?}", e);
Err(Status::InternalServerError)
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ fn bad_request() -> &'static str {
pub struct Env {
contract: Contract,
database_url: String,
nearblocks_api_key: String,
}

#[launch]
Expand Down Expand Up @@ -114,12 +115,14 @@ fn rocket() -> _ {
.merge(("databases.my_db.url", env.database_url));

let contract: AccountId = env.contract.parse::<AccountId>().unwrap();
let nearblocks_api_key = env.nearblocks_api_key;

rocket::custom(figment)
.attach(cors)
.attach(db::stage())
.mount("/", routes![robots, index])
.manage(contract)
.manage(nearblocks_api_key)
.mount("/test", rocket::routes![test])
.attach(entrypoints::stage())
.mount(
Expand Down
11 changes: 9 additions & 2 deletions src/nearblocks_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,26 @@ pub struct ApiResponse {
pub struct ApiClient {
base_url: String,
client: Client,
api_key: String,
}

impl Default for ApiClient {
fn default() -> Self {
Self {
base_url: "https://api.nearblocks.io/".to_string(),
client: Client::new(),
api_key: "".to_string(),
}
}
}

impl ApiClient {
pub fn new() -> Self {
Self::default()
pub fn new(api_key: String) -> Self {
Self {
base_url: "https://api.nearblocks.io/".to_string(),
client: Client::new(),
api_key,
}
}

pub async fn get_account_txns_by_pagination(
Expand All @@ -51,6 +57,7 @@ impl ApiClient {
println!("Fetching from {}", url);
self.client
.get(&url)
.header("Authorization", format!("Bearer {}", self.api_key))
.send()
.await?
.json::<ApiResponse>()
Expand Down

0 comments on commit 4a3cef0

Please sign in to comment.