quicknode-sdk 0.6.0

Core library for quicknode sdk
Documentation
use std::{thread, time::Duration};

use quicknode_sdk::{
    webhooks::{
        ActivateWebhookParams, CreateWebhookFromTemplateParams, EvmContractEventsTemplate,
        EvmWalletFilterTemplate, GetWebhooksParams, TemplateArgs, UpdateWebhookParams,
        WebhookDestinationAttributes, WebhookStartFrom,
    },
    QuicknodeSdk, SdkFullConfig,
};

#[tokio::main]
#[allow(clippy::unwrap_used, clippy::expect_used)]
async fn main() {
    let config = SdkFullConfig::from_env().expect("Config from env failed");
    let qn = QuicknodeSdk::new(&config).expect("sdk failed to initialize");

    let before = qn
        .webhooks
        .list_webhooks(&GetWebhooksParams::default())
        .await
        .expect("list_webhooks failed");
    println!(
        "webhooks before: {} (total={})",
        before.data.len(),
        before.page_info.total
    );

    let count = qn
        .webhooks
        .get_enabled_count()
        .await
        .expect("get_enabled_count failed");
    println!("enabled count: {}", count.total);

    let template_args = TemplateArgs::EvmWalletFilter(EvmWalletFilterTemplate {
        wallets: vec!["0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string()],
    });

    let create_params = CreateWebhookFromTemplateParams {
        name: "E2E Test Webhook".to_string(),
        network: "ethereum-mainnet".to_string(),
        notification_email: None,
        destination_attributes: WebhookDestinationAttributes {
            url: "https://webhook.site/ae19071a-2dcc-4035-9cdf-406dcb4719ef".to_string(),
            security_token: None,
            compression: "none".to_string(),
        },
        template_args,
    };
    let webhook = qn
        .webhooks
        .create_webhook_from_template(&create_params)
        .await
        .expect("create_webhook_from_template failed");
    let id = webhook.id.clone();
    println!("created: {} | {}", id, webhook.status);
    thread::sleep(Duration::from_secs(1));

    let fetched = qn
        .webhooks
        .get_webhook(&id)
        .await
        .expect("get_webhook failed");
    println!("fetched: {} | {}", fetched.id, fetched.name);

    let update_params = UpdateWebhookParams {
        name: Some("E2E Test Webhook Updated".to_string()),
        ..Default::default()
    };
    let updated = qn
        .webhooks
        .update_webhook(&id, &update_params)
        .await
        .expect("update_webhook failed");
    println!("updated name: {}", updated.name);
    thread::sleep(Duration::from_secs(1));

    qn.webhooks
        .pause_webhook(&id)
        .await
        .expect("pause_webhook failed");
    println!("paused");

    let activate_params = ActivateWebhookParams {
        start_from: WebhookStartFrom::Latest,
    };
    qn.webhooks
        .activate_webhook(&id, &activate_params)
        .await
        .expect("activate_webhook failed");
    println!("activated");

    qn.webhooks
        .delete_webhook(&id)
        .await
        .expect("delete_webhook failed");
    println!("deleted: {id}");
    thread::sleep(Duration::from_secs(1));

    // Exercise the evm-contract-events template, which carries the multi-word
    // `event_hashes` field. The API expects `eventHashes` on the wire.
    let contract_events_args = TemplateArgs::EvmContractEvents(EvmContractEventsTemplate {
        contracts: vec!["0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48".to_string()],
        event_hashes: vec![
            "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef".to_string(),
        ],
    });
    let contract_events_params = CreateWebhookFromTemplateParams {
        name: "E2E Test Webhook (evmContractEvents)".to_string(),
        network: "ethereum-mainnet".to_string(),
        notification_email: None,
        destination_attributes: WebhookDestinationAttributes {
            url: "https://webhook.site/ae19071a-2dcc-4035-9cdf-406dcb4719ef".to_string(),
            security_token: None,
            compression: "none".to_string(),
        },
        template_args: contract_events_args,
    };
    let ce_webhook = qn
        .webhooks
        .create_webhook_from_template(&contract_events_params)
        .await
        .expect("create_webhook_from_template (evmContractEvents) failed");
    println!(
        "created (evmContractEvents): {} | {}",
        ce_webhook.id, ce_webhook.status
    );
    thread::sleep(Duration::from_secs(1));
    qn.webhooks
        .delete_webhook(&ce_webhook.id)
        .await
        .expect("delete_webhook (evmContractEvents) failed");
    println!("deleted (evmContractEvents): {}", ce_webhook.id);
    thread::sleep(Duration::from_secs(1));

    let after = qn
        .webhooks
        .list_webhooks(&GetWebhooksParams::default())
        .await
        .expect("list_webhooks failed");
    println!(
        "webhooks after: {} (total={})",
        after.data.len(),
        after.page_info.total
    );
}