Skip to content

Commit

Permalink
move wrap_unsafe to common.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrostr committed Jan 29, 2025
1 parent ba4bfb2 commit c0c8a24
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 42 deletions.
2 changes: 1 addition & 1 deletion listen-kit/examples/swarm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use {
anyhow::{anyhow, Result},
listen_kit::agent::{create_data_agent, create_trader_agent},
listen_kit::solana::util::wrap_unsafe,
listen_kit::common::wrap_unsafe,
rig::agent::Agent,
rig::cli_chatbot::cli_chatbot,
rig::completion::Prompt,
Expand Down
19 changes: 19 additions & 0 deletions listen-kit/src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::{anyhow, Result};
use std::future::Future;
use tokio::sync::mpsc;

pub async fn wrap_unsafe<F, Fut, T>(f: F) -> Result<T>
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<T>> + Send + 'static,
T: Send + 'static,
{
let (tx, mut rx) = mpsc::channel(1);

tokio::spawn(async move {
let result = f().await;
let _ = tx.send(result).await;
});

rx.recv().await.ok_or_else(|| anyhow!("Channel closed"))?
}
18 changes: 0 additions & 18 deletions listen-kit/src/evm/util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::future::Future;
use std::str::FromStr;
use tokio::sync::mpsc;

use alloy::providers::{ProviderBuilder, RootProvider};
use alloy::signers::local::PrivateKeySigner;
Expand All @@ -21,19 +19,3 @@ pub fn make_signer() -> Result<PrivateKeySigner> {
pub fn env(var: &str) -> String {
std::env::var(var).unwrap_or_else(|_| panic!("{} env var not set", var))
}

pub async fn wrap_unsafe<F, Fut, T>(f: F) -> Result<T>
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<T>> + Send + 'static,
T: Send + 'static,
{
let (tx, mut rx) = mpsc::channel(1);

tokio::spawn(async move {
let result = f().await;
let _ = tx.send(result).await;
});

rx.recv().await.ok_or_else(|| anyhow!("Channel closed"))?
}
4 changes: 2 additions & 2 deletions listen-kit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ pub mod solana;
#[cfg(feature = "evm")]
pub mod evm;

pub mod agent;

#[cfg(feature = "http")]
pub mod wallet_manager;

pub mod agent;
pub mod common;
pub mod signer;

#[ctor::ctor]
Expand Down
5 changes: 2 additions & 3 deletions listen-kit/src/solana/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ use solana_sdk::transaction::Transaction;
use std::future::Future;
use std::str::FromStr;

use crate::solana::{
data::PortfolioItem, dexscreener::PairInfo, util::wrap_unsafe,
};
use crate::common::wrap_unsafe;
use crate::solana::{data::PortfolioItem, dexscreener::PairInfo};

use super::data::holdings_to_portfolio;
use super::deploy_token::create_deploy_token_tx;
Expand Down
18 changes: 0 additions & 18 deletions listen-kit/src/solana/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ use solana_sdk::compute_budget::ComputeBudgetInstruction;
use solana_sdk::instruction::Instruction;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::signature::Keypair;
use std::future::Future;
use std::io::Write;
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::mpsc;

use crate::signer::solana::LocalSolanaSigner;
use crate::signer::TransactionSigner;
Expand Down Expand Up @@ -158,19 +156,3 @@ pub fn parse_pubkey(s: &str) -> Result<Pubkey> {
Err(e) => Err(anyhow!(e)),
}
}

pub async fn wrap_unsafe<F, Fut, T>(f: F) -> Result<T>
where
F: FnOnce() -> Fut + Send + 'static,
Fut: Future<Output = Result<T>> + Send + 'static,
T: Send + 'static,
{
let (tx, mut rx) = mpsc::channel(1);

tokio::spawn(async move {
let result = f().await;
let _ = tx.send(result).await;
});

rx.recv().await.ok_or_else(|| anyhow!("Channel closed"))?
}

0 comments on commit c0c8a24

Please sign in to comment.