Skip to content

Commit

Permalink
Merge pull request #9 from zTgx/main
Browse files Browse the repository at this point in the history
feat: add story protocol
  • Loading branch information
piotrostr authored Feb 21, 2025
2 parents 969feab + 28b284f commit 49c63f9
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod cross_chain;
pub mod dexscreener;
pub mod reasoning_loop;
pub mod signer;
pub mod story;

#[ctor::ctor]
fn init() {
Expand Down
52 changes: 52 additions & 0 deletions src/story/license.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use super::{transaction::StoryConfig, STORY_API_URL};
use anyhow::Result;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct GetLicenseTokenResponse {
pub data: TokenData,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TokenData {
pub id: String,
pub licensor_ip_id: String,
pub license_template: String,
pub license_terms_id: String,
pub transferable: String,
pub owner: String,
pub burnt_at: String,
pub block_number: String,
pub block_time: String,
}

/// Retrieve a LicenseToken
///
/// # Arguments
///
/// * `config` - API Config
/// * `license_token_id` - License Token ID
///
/// # Returns
///
/// GetLicenseTokenResponse
pub async fn get_license_token(
config: &StoryConfig,
license_token_id: &str,
) -> Result<GetLicenseTokenResponse> {
let url = format!("{}/licenses/tokens/{}", STORY_API_URL, license_token_id);

let client = reqwest::Client::new();
let response = client
.get(url)
.header("X-Api-Key", &config.api_key)
.header("X-Chain", config.chain.clone())
.header("accept", "application/json")
.send()
.await?
.json::<GetLicenseTokenResponse>()
.await?;

Ok(response)
}
5 changes: 5 additions & 0 deletions src/story/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub mod license;
pub mod tools;
pub mod transaction;

const STORY_API_URL: &str = "https://staging-api.storyprotocol.net/api/v3";
19 changes: 19 additions & 0 deletions src/story/tools.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::Result;
use rig_tool_macro::tool;
use super::{license::{get_license_token, GetLicenseTokenResponse}, transaction::{get_a_transaction, GetTransactionResponse, StoryConfig}};

#[tool(description = "Retrieve a Transaction on Story protocol")]
pub async fn get_a_transaction_on_story(
config: StoryConfig,
trx_id: String,
) -> Result<GetTransactionResponse> {
get_a_transaction(&config, &trx_id).await
}

#[tool(description = "Retrieve a LicenseToken on Story protocol")]
pub async fn get_license_token_on_story(
config: StoryConfig,
license_token_id: String,
) -> Result<GetLicenseTokenResponse> {
get_license_token(&config, &license_token_id).await
}
87 changes: 87 additions & 0 deletions src/story/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use anyhow::Result;
use serde::{Deserialize, Serialize};

use super::STORY_API_URL;

#[derive(Debug, Serialize, Deserialize)]
pub struct GetTransactionResponse {
pub data: TransactionData,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct TransactionData {
pub id: String,

#[serde(rename = "blockNumber")]
pub block_number: String,

#[serde(rename = "blockTimestamp")]
pub block_timestamp: String,

#[serde(rename = "createdAt")]
pub created_at: String,

#[serde(rename = "actionType")]
pub action_type: Option<String>,

pub initiator: Option<String>,

#[serde(rename = "ipId")]
pub ip_id: Option<String>,

#[serde(rename = "resourceId")]
pub resource_id: Option<String>,

#[serde(rename = "resourceType")]
pub resource_type: Option<String>,

pub tx_hash: Option<String>,

#[serde(rename = "logIndex")]
pub log_index: Option<String>,

#[serde(rename = "transactionIndex")]
pub transaction_index: Option<String>,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct StoryConfig {
pub api_key: String,
pub chain: String,
}

impl StoryConfig {
pub fn new(api_key: String, chain: String) -> Self {
StoryConfig { api_key, chain }
}
}

/// Retrieve a Transaction
///
/// # Arguments
///
/// * `config` - API Config
/// * `trx_id` - Transaction ID
///
/// # Returns
///
/// GetTransactionResponse
pub async fn get_a_transaction(
config: &StoryConfig,
trx_id: &str,
) -> Result<GetTransactionResponse> {
let url = format!("{}/transactions/{}", STORY_API_URL, trx_id);

let client = reqwest::Client::new();
let response = client
.get(url)
.header("X-Api-Key", &config.api_key)
.header("X-Chain", config.chain.clone())
.header("accept", "application/json")
.send()
.await?
.json::<GetTransactionResponse>()
.await?;

Ok(response)
}

0 comments on commit 49c63f9

Please sign in to comment.