Helius on executor
This commit is contained in:
+5
-18
@@ -1,7 +1,6 @@
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::{Context, anyhow};
|
use anyhow::Context;
|
||||||
use helius::Helius;
|
|
||||||
|
|
||||||
use tokio::sync::{Mutex, watch};
|
use tokio::sync::{Mutex, watch};
|
||||||
|
|
||||||
@@ -15,15 +14,11 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
pub struct Bot {
|
pub struct Bot {
|
||||||
|
pub executor: Arc<Executor>,
|
||||||
|
pub strategy: Mutex<Box<dyn Strategy>>,
|
||||||
pub account_manager: Mutex<AccountManager>,
|
pub account_manager: Mutex<AccountManager>,
|
||||||
pub current_account: Mutex<Account>,
|
pub current_account: Mutex<Account>,
|
||||||
|
|
||||||
pub strategy: Mutex<Box<dyn Strategy>>,
|
|
||||||
|
|
||||||
pub executor: Mutex<Executor>,
|
|
||||||
pub trade_log: Mutex<Vec<TradeLog>>,
|
pub trade_log: Mutex<Vec<TradeLog>>,
|
||||||
|
|
||||||
pub helius: Helius,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bot {
|
impl Bot {
|
||||||
@@ -37,14 +32,11 @@ impl Bot {
|
|||||||
.clone();
|
.clone();
|
||||||
|
|
||||||
Ok(Arc::new(Self {
|
Ok(Arc::new(Self {
|
||||||
helius: Helius::new_async(&accounts.api_key, helius::types::Cluster::Devnet).await?,
|
executor: Executor::new(&accounts.api_key).await?,
|
||||||
|
strategy: Mutex::new(Box::new(MomentumVelocityStrategy::new())),
|
||||||
account_manager: Mutex::new(accounts),
|
account_manager: Mutex::new(accounts),
|
||||||
current_account: Mutex::new(account),
|
current_account: Mutex::new(account),
|
||||||
strategy: Mutex::new(Box::new(MomentumVelocityStrategy::new())),
|
|
||||||
|
|
||||||
trade_log: Mutex::new(Vec::new()),
|
trade_log: Mutex::new(Vec::new()),
|
||||||
executor: Mutex::new(Executor::new()),
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,11 +75,6 @@ impl Bot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn tick(self: &Arc<Self>) -> anyhow::Result<bool> {
|
pub async fn tick(self: &Arc<Self>) -> anyhow::Result<bool> {
|
||||||
let ws = self
|
|
||||||
.helius
|
|
||||||
.ws()
|
|
||||||
.ok_or_else(|| anyhow!("Unable to obtain ws"))?;
|
|
||||||
|
|
||||||
// drop(ws);
|
// drop(ws);
|
||||||
// self.strategy
|
// self.strategy
|
||||||
// .lock()
|
// .lock()
|
||||||
|
|||||||
+38
-13
@@ -1,11 +1,15 @@
|
|||||||
mod pump_fun;
|
mod pump_fun;
|
||||||
|
|
||||||
use crate::launchpad::pump_fun::PumpFun;
|
use crate::launchpad::pump_fun::PumpFun;
|
||||||
|
use helius::Helius;
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
use std::collections::HashMap;
|
use std::{collections::HashMap, sync::Arc};
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
pub struct Executor {
|
pub struct Executor {
|
||||||
pub pump_fun: PumpFun,
|
pub client: Helius,
|
||||||
|
|
||||||
|
pub pump_fun: Mutex<PumpFun>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@@ -13,6 +17,8 @@ pub struct Executor {
|
|||||||
pub trait Launchpad: Send + Sync {
|
pub trait Launchpad: Send + Sync {
|
||||||
async fn buy(
|
async fn buy(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
client: &Helius,
|
||||||
|
|
||||||
mint: &str,
|
mint: &str,
|
||||||
amount: Decimal,
|
amount: Decimal,
|
||||||
priority: Decimal,
|
priority: Decimal,
|
||||||
@@ -21,6 +27,8 @@ pub trait Launchpad: Send + Sync {
|
|||||||
|
|
||||||
async fn sell(
|
async fn sell(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
client: &Helius,
|
||||||
|
|
||||||
mint: &str,
|
mint: &str,
|
||||||
amount: u8,
|
amount: u8,
|
||||||
priority: Decimal,
|
priority: Decimal,
|
||||||
@@ -31,35 +39,52 @@ pub trait Launchpad: Send + Sync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Executor {
|
impl Executor {
|
||||||
pub fn new() -> Self {
|
pub async fn new(api_key: &str) -> anyhow::Result<Arc<Self>> {
|
||||||
Self {
|
Ok(Arc::new(Self {
|
||||||
pump_fun: PumpFun::new(),
|
client: Helius::new_async(api_key, helius::types::Cluster::Devnet).await?,
|
||||||
}
|
|
||||||
|
pump_fun: Mutex::new(PumpFun::new()),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn buy(
|
pub async fn buy(
|
||||||
&mut self,
|
self: &Arc<Self>,
|
||||||
mint: &str,
|
mint: &str,
|
||||||
amount: Decimal,
|
amount: Decimal,
|
||||||
priority: Decimal,
|
priority: Decimal,
|
||||||
slippage: u16,
|
slippage: u16,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.pump_fun.buy(mint, amount, priority, slippage).await
|
self.pump_fun
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.buy(&self.client, mint, amount, priority, slippage)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn sell(
|
pub async fn sell(
|
||||||
&mut self,
|
self: &Arc<Self>,
|
||||||
mint: &str,
|
mint: &str,
|
||||||
amount: u8,
|
amount: u8,
|
||||||
priority: Decimal,
|
priority: Decimal,
|
||||||
slippage: u16,
|
slippage: u16,
|
||||||
) -> anyhow::Result<()> {
|
) -> anyhow::Result<()> {
|
||||||
self.pump_fun.sell(mint, amount, priority, slippage).await
|
self.pump_fun
|
||||||
|
.lock()
|
||||||
|
.await
|
||||||
|
.sell(&self.client, mint, amount, priority, slippage)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn sell_all(&mut self, priority: Decimal, slippage: u16) -> anyhow::Result<()> {
|
pub async fn sell_all(
|
||||||
for (mint, _) in self.pump_fun.get_positions() {
|
self: &Arc<Self>,
|
||||||
self.pump_fun.sell(&mint, 100, priority, slippage).await?;
|
priority: Decimal,
|
||||||
|
slippage: u16,
|
||||||
|
) -> anyhow::Result<()> {
|
||||||
|
let mut pump = self.pump_fun.lock().await;
|
||||||
|
|
||||||
|
for (mint, _) in pump.get_positions() {
|
||||||
|
pump.sell(&self.client, &mint, 100, priority, slippage)
|
||||||
|
.await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
use helius::Helius;
|
||||||
use rust_decimal::Decimal;
|
use rust_decimal::Decimal;
|
||||||
|
|
||||||
use crate::launchpad::Launchpad;
|
use crate::launchpad::Launchpad;
|
||||||
@@ -21,6 +22,8 @@ impl PumpFun {
|
|||||||
impl Launchpad for PumpFun {
|
impl Launchpad for PumpFun {
|
||||||
async fn buy(
|
async fn buy(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
client: &Helius,
|
||||||
|
|
||||||
mint: &str,
|
mint: &str,
|
||||||
amount: Decimal,
|
amount: Decimal,
|
||||||
priority: Decimal,
|
priority: Decimal,
|
||||||
@@ -31,6 +34,8 @@ impl Launchpad for PumpFun {
|
|||||||
|
|
||||||
async fn sell(
|
async fn sell(
|
||||||
&mut self,
|
&mut self,
|
||||||
|
client: &Helius,
|
||||||
|
|
||||||
mint: &str,
|
mint: &str,
|
||||||
amount: u8,
|
amount: u8,
|
||||||
priority: Decimal,
|
priority: Decimal,
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ impl MomentumVelocityStrategy {
|
|||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl Strategy for MomentumVelocityStrategy {
|
impl Strategy for MomentumVelocityStrategy {
|
||||||
async fn execute_sell_all(&mut self, bot: Arc<Bot>) -> anyhow::Result<()> {
|
async fn execute_sell_all(&mut self, bot: Arc<Bot>) -> anyhow::Result<()> {
|
||||||
bot.executor.lock().await.sell_all(PRIORITY, SLIPPAGE).await
|
bot.executor.sell_all(PRIORITY, SLIPPAGE).await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn on_new_coin(&mut self, bot: Arc<Bot>, token: NewToken) -> anyhow::Result<()> {
|
async fn on_new_coin(&mut self, bot: Arc<Bot>, token: NewToken) -> anyhow::Result<()> {
|
||||||
@@ -170,11 +170,7 @@ impl Strategy for MomentumVelocityStrategy {
|
|||||||
price_change_pct * 100.0
|
price_change_pct * 100.0
|
||||||
);
|
);
|
||||||
|
|
||||||
bot.executor
|
bot.executor.sell(mint, 100, PRIORITY, SLIPPAGE).await?;
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.sell(mint, 100, PRIORITY, SLIPPAGE)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if let Some(mut pos) = self.positions.remove(mint) {
|
if let Some(mut pos) = self.positions.remove(mint) {
|
||||||
pos.trade.close(current_price, reason);
|
pos.trade.close(current_price, reason);
|
||||||
@@ -244,8 +240,6 @@ impl Strategy for MomentumVelocityStrategy {
|
|||||||
);
|
);
|
||||||
|
|
||||||
bot.executor
|
bot.executor
|
||||||
.lock()
|
|
||||||
.await
|
|
||||||
.buy(mint, BUY_AMOUNT_SOL, PRIORITY, SLIPPAGE)
|
.buy(mint, BUY_AMOUNT_SOL, PRIORITY, SLIPPAGE)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user