rig_onchain_kit/
common.rs

1use anyhow::{anyhow, Result};
2use std::future::Future;
3use std::sync::Arc;
4use tokio::sync::mpsc;
5
6use crate::signer::{SignerContext, TransactionSigner};
7
8pub async fn wrap_unsafe<F, Fut, T>(f: F) -> Result<T>
9where
10    F: FnOnce() -> Fut + Send + 'static,
11    Fut: Future<Output = Result<T>> + Send + 'static,
12    T: Send + 'static,
13{
14    let (tx, mut rx) = mpsc::channel(1);
15
16    tokio::spawn(async move {
17        let result = f().await;
18        let _ = tx.send(result).await;
19    });
20
21    rx.recv().await.ok_or_else(|| anyhow!("Channel closed"))?
22}
23
24pub async fn spawn_with_signer<F, Fut, T>(
25    signer: Arc<dyn TransactionSigner>,
26    f: F,
27) -> tokio::task::JoinHandle<Result<T>>
28where
29    F: FnOnce() -> Fut + Send + 'static,
30    Fut: Future<Output = Result<T>> + Send + 'static,
31    T: Send + 'static,
32{
33    tokio::spawn(async move {
34        SignerContext::with_signer(signer, async { f().await }).await
35    })
36}
37
38use rig::agent::{Agent, AgentBuilder};
39use rig::providers::anthropic::completion::CompletionModel as AnthropicCompletionModel;
40
41pub fn claude_agent_builder() -> AgentBuilder<AnthropicCompletionModel> {
42    rig::providers::anthropic::Client::from_env()
43        .agent(rig::providers::anthropic::CLAUDE_3_5_SONNET)
44}
45
46pub async fn plain_agent() -> Result<Agent<AnthropicCompletionModel>> {
47    Ok(claude_agent_builder()
48        .preamble("be nice to the users")
49        .max_tokens(1024)
50        .build())
51}
52
53pub const PREAMBLE_COMMON: &str = "";