Skip to content

Commit

Permalink
Merge branch 'release/1.1.0-alpha.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nekofar committed Oct 19, 2023
2 parents 299be4e + ea12e44 commit 2806ebc
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 111 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

All notable changes to this project will be documented in this file.

## [1.1.0-alpha.0] - 2023-10-19

### Refactor

- Add `warpcast_url` to `FarcasterHandler` in all modules
- Extract proposal logic in meta gov farcaster

### Miscellaneous Tasks

- Update warpcast channel for meta gov to nouns

## [1.0.4-alpha.0] - 2023-10-18

### Refactor
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lilnouns-bots"
version = "1.0.4-alpha.0"
version = "1.1.0-alpha.0"
authors = ["Milad Nekofar <milad@nekofar.com>"]
edition = "2021"
description = "Our bots are designed to keep the Lil Nouns DAO community informed and engaged."
Expand Down
7 changes: 6 additions & 1 deletion src/lil_nouns/handler/farcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{

pub(crate) struct FarcasterHandler {
base_url: String,
warpcast_url: String,
bearer_token: String,
channel_key: String,
cache: Cache,
Expand All @@ -28,6 +29,7 @@ pub(crate) struct FarcasterHandler {
impl FarcasterHandler {
pub fn new(
base_url: String,
warpcast_url: String,
bearer_token: String,
channel_key: String,
cache: Cache,
Expand All @@ -36,6 +38,7 @@ impl FarcasterHandler {
) -> Self {
Self {
base_url,
warpcast_url,
bearer_token,
channel_key,
cache,
Expand All @@ -46,6 +49,7 @@ impl FarcasterHandler {

pub fn new_from_env(env: &Env) -> Result<FarcasterHandler> {
let base_url = env.var("LIL_NOUNS_BASE_URL")?.to_string();
let warpcast_url = env.var("WARP_CAST_API_BASE_URL")?.to_string();
let bearer_token = env.secret("LIL_NOUNS_WARP_CAST_TOKEN")?.to_string();
let channel_key = env.var("LIL_NOUNS_WARP_CAST_CHANNEL")?.to_string();

Expand All @@ -55,6 +59,7 @@ impl FarcasterHandler {

Ok(Self::new(
base_url,
warpcast_url,
bearer_token,
channel_key,
cache,
Expand All @@ -64,7 +69,7 @@ impl FarcasterHandler {
}

async fn make_http_request(&self, request_data: Value) -> Result<Response> {
let url = "https://api.warpcast.com/v2/casts";
let url = format!("{}/casts", self.warpcast_url);
let token = format!("Bearer {}", self.bearer_token);
let mut headers = HeaderMap::new();

Expand Down
197 changes: 96 additions & 101 deletions src/meta_gov/handler/farcaster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ use crate::{
utils::{ens::get_wallet_handle, link::Link},
};

pub struct FarcasterHandler {
pub(crate) struct FarcasterHandler {
base_url: String,
warpcast_url: String,
bearer_token: String,
channel_key: String,
cache: Cache,
Expand All @@ -29,6 +30,7 @@ pub struct FarcasterHandler {
impl FarcasterHandler {
pub fn new(
base_url: String,
warpcast_url: String,
bearer_token: String,
channel_key: String,
cache: Cache,
Expand All @@ -37,6 +39,7 @@ impl FarcasterHandler {
) -> Self {
Self {
base_url,
warpcast_url,
bearer_token,
channel_key,
cache,
Expand All @@ -47,15 +50,17 @@ impl FarcasterHandler {

pub fn new_from_env(env: &Env) -> Result<FarcasterHandler> {
let base_url = env.var("META_GOV_BASE_URL")?.to_string();
let warpcast_url = env.var("WARP_CAST_API_BASE_URL")?.to_string();
let bearer_token = env.secret("META_GOV_WARP_CAST_TOKEN")?.to_string();
let channel_key = env.secret("META_GOV_WARP_CAST_CHANNEL")?.to_string();
let channel_key = env.var("META_GOV_WARP_CAST_CHANNEL")?.to_string();

let cache = Cache::new_from_env(env);
let client = Client::new();
let link = Link::new_from_env(&env);

Ok(Self::new(
base_url,
warpcast_url,
bearer_token,
channel_key,
cache,
Expand All @@ -65,7 +70,7 @@ impl FarcasterHandler {
}

async fn make_http_request(&self, request_data: Value) -> Result<Response> {
let url = "https://api.warpcast.com/v2/casts";
let url = format!("{}/casts", self.warpcast_url);
let token = format!("Bearer {}", self.bearer_token);
let mut headers = HeaderMap::new();

Expand Down Expand Up @@ -115,68 +120,63 @@ impl FarcasterHandler {
#[async_trait(? Send)]
impl Handler for FarcasterHandler {
async fn handle_new_proposal(&self, proposal: &Proposal) -> Result<()> {
match self.extract_proposal_info(proposal.clone()).await {
Ok((proposal_id, proposal_title)) => {
info!("Handling new proposal: {}", proposal_title);

let url = &self
.link
.generate(format!("{}/{}", self.base_url, proposal_id))
.await
.unwrap_or_else(|_| format!("{}/{}", self.base_url, proposal_id));

let description = format!(
"A new Meta Gov proposal has been created: “{}”",
proposal_title
);

let request_data = json!({
"text": description,
"embeds": [url],
"channelKey": self.channel_key
});

let response = self.make_http_request(request_data).await.map_err(|e| {
error!("Failed to make HTTP request: {}", e);
return e;
})?;

let response_body = response.text().await.map_err(|e| {
error!("Failed to get text from response: {}", e);
Error::from(format!("Failed to get text from response: {}", e))
})?;

let parsed_body: serde_json::Result<Value> = serde_json::from_str(&response_body);

let response_body: Value = match parsed_body {
Ok(body) => body,
Err(e) => {
error!("Failed to parse JSON: {}", e);
return Err(e.into());
}
};

let cast_hash = response_body["result"]["cast"]["hash"]
.as_str()
.unwrap_or_default();

let mut proposals_casts = self
.cache
.get::<HashMap<String, String>>("meta_gov:proposals:casts")
.await?
.unwrap_or_default();

proposals_casts.insert(proposal_id, cast_hash.to_string());

self
.cache
.put("meta_gov:proposals:casts", &proposals_casts)
.await;
}
let (proposal_id, proposal_title) = self.extract_proposal_info(proposal.clone()).await?;

info!("Handling new proposal: {}", proposal_title);

let url = &self
.link
.generate(format!("{}/{}", self.base_url, proposal_id))
.await
.unwrap_or_else(|_| format!("{}/{}", self.base_url, proposal_id));

let description = format!(
"A new Meta Gov proposal has been created: “{}”",
proposal_title
);

let request_data = json!({
"text": description,
"embeds": [url],
"channelKey": self.channel_key
});

let response = self.make_http_request(request_data).await.map_err(|e| {
error!("Failed to make HTTP request: {}", e);
return e;
})?;

let response_body = response.text().await.map_err(|e| {
error!("Failed to get text from response: {}", e);
Error::from(format!("Failed to get text from response: {}", e))
})?;

let parsed_body: serde_json::Result<Value> = serde_json::from_str(&response_body);

let response_body: Value = match parsed_body {
Ok(body) => body,
Err(e) => {
error!("Failed to extract proposal info: {}", e);
error!("Failed to parse JSON: {}", e);
return Err(e.into());
}
}
};

let cast_hash = response_body["result"]["cast"]["hash"]
.as_str()
.unwrap_or_default();

let mut proposals_casts = self
.cache
.get::<HashMap<String, String>>("meta_gov:proposals:casts")
.await?
.unwrap_or_default();

proposals_casts.insert(proposal_id, cast_hash.to_string());

self
.cache
.put("meta_gov:proposals:casts", &proposals_casts)
.await;

Ok(())
}
Expand All @@ -196,44 +196,39 @@ impl Handler for FarcasterHandler {
.clone()
.ok_or("Proposal not found in the funding list.")?;

match self.extract_proposal_info(proposal.clone()).await {
Ok((proposal_id, proposal_title)) => {
let proposals_casts = self
.cache
.get::<HashMap<String, String>>("meta_gov:proposals:casts")
.await?
.unwrap_or_default();

let cast_hash = proposals_casts
.get(&proposal_id)
.ok_or("Cast hash not found")?;

let wallet = get_wallet_handle(&vote.voter, "xyz.farcaster").await;

let description = format!(
"{} has voted {} “{}” proposal.",
wallet,
match vote.choice {
1 => "for",
2 => "against",
3 => "abstain on",
_ => "unknown",
},
proposal_title
);

let request_data = json!({
"text": description,
"channelKey": self.channel_key,
"parent": {"hash": cast_hash},
});

self.make_http_request(request_data).await?;
}
Err(e) => {
error!("Failed to extract proposal info: {}", e);
}
}
let (proposal_id, proposal_title) = self.extract_proposal_info(proposal.clone()).await?;

let proposals_casts = self
.cache
.get::<HashMap<String, String>>("meta_gov:proposals:casts")
.await?
.unwrap_or_default();

let cast_hash = proposals_casts
.get(&proposal_id)
.ok_or("Cast hash not found")?;

let wallet = get_wallet_handle(&vote.voter, "xyz.farcaster").await;

let description = format!(
"{} has voted {} “{}” proposal.",
wallet,
match vote.choice {
1 => "for",
2 => "against",
3 => "abstain on",
_ => "unknown",
},
proposal_title
);

let request_data = json!({
"text": description,
"channelKey": self.channel_key,
"parent": {"hash": cast_hash},
});

self.make_http_request(request_data).await?;

Ok(())
}
Expand Down
Loading

0 comments on commit 2806ebc

Please sign in to comment.