solagent_plugin_gibwork/
create_gibwork_task.rsuse base64::{engine::general_purpose, Engine};
use serde::{Deserialize, Serialize};
use solagent_core::{
solana_sdk::{commitment_config::CommitmentConfig, pubkey::Pubkey, transaction::VersionedTransaction},
SolanaAgentKit,
};
#[derive(Serialize)]
struct TaskRequest {
title: String,
content: String,
requirements: String,
tags: Vec<String>,
payer: String,
token: TokenInfo,
}
#[derive(Serialize)]
struct TokenInfo {
#[serde(rename = "mintAddress")]
mint_address: String,
amount: u64,
}
#[derive(Deserialize)]
struct TaskResponse {
#[serde(rename = "taskId")]
task_id: String,
#[serde(rename = "serializedTransaction")]
serialized_transaction: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct GibworkCreateTaskResponse {
pub status: String,
pub task_id: String,
pub signature: String,
}
#[allow(clippy::too_many_arguments)]
pub async fn create_gibwork_task(
agent: &SolanaAgentKit,
title: &str,
content: &str,
requirements: &str,
tags: Vec<String>,
token_mint_address: &str,
token_amount: u64,
payer: Option<Pubkey>,
) -> Result<GibworkCreateTaskResponse, Box<dyn std::error::Error>> {
let request = TaskRequest {
title: title.to_string(),
content: content.to_string(),
requirements: requirements.to_string(),
tags,
payer: payer.unwrap_or(agent.wallet.address).to_string(),
token: TokenInfo { mint_address: token_mint_address.to_string(), amount: token_amount },
};
let client = reqwest::Client::new();
let response = client.post("https://api2.gib.work/tasks/public/transaction").json(&request).send().await?;
if !response.status().is_success() {
return Err(format!("API request failed: {}", response.status()).into());
}
let task_response: TaskResponse = response.json().await?;
let transaction_data = general_purpose::STANDARD.decode(task_response.serialized_transaction.as_str())?;
let mut versioned_transaction: VersionedTransaction = bincode::deserialize(&transaction_data)?;
let blockhash = agent.connection.get_latest_blockhash()?;
versioned_transaction.message.set_recent_blockhash(blockhash);
let signed_transaction = VersionedTransaction::try_new(versioned_transaction.message, &[&agent.wallet.wallet])?;
let signature = agent.connection.send_transaction(&signed_transaction)?;
agent.connection.confirm_transaction_with_spinner(&signature, &blockhash, CommitmentConfig::confirmed())?;
Ok(GibworkCreateTaskResponse {
status: "success".to_string(),
task_id: task_response.task_id,
signature: signature.to_string(),
})
}