Refactor
This commit is contained in:
+11
-10
@@ -1,15 +1,14 @@
|
|||||||
use std::{collections::HashMap, sync::Arc};
|
use std::sync::Arc;
|
||||||
|
|
||||||
use anyhow::Context;
|
use anyhow::{Context, anyhow};
|
||||||
use helius::Helius;
|
use helius::Helius;
|
||||||
|
|
||||||
use tokio::sync::{Mutex, watch};
|
use tokio::sync::{Mutex, watch};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
account::{Account, AccountManager},
|
data::account::{Account, AccountManager},
|
||||||
executor::{ExecutorWrapper, pump_fun::PumpDev},
|
data::tradelog::TradeLog,
|
||||||
strategy::{Strategy, veloc::MomentumVelocityStrategy},
|
strategy::{Strategy, veloc::MomentumVelocityStrategy},
|
||||||
tradelog::TradeLog,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Bot {
|
pub struct Bot {
|
||||||
@@ -18,7 +17,7 @@ pub struct Bot {
|
|||||||
|
|
||||||
pub strategy: Mutex<Box<dyn Strategy>>,
|
pub strategy: Mutex<Box<dyn Strategy>>,
|
||||||
|
|
||||||
pub executor: Mutex<ExecutorWrapper>,
|
pub executor: Mutex<()>,
|
||||||
pub trade_log: Mutex<Vec<TradeLog>>,
|
pub trade_log: Mutex<Vec<TradeLog>>,
|
||||||
|
|
||||||
pub helius: Helius,
|
pub helius: Helius,
|
||||||
@@ -42,10 +41,7 @@ impl Bot {
|
|||||||
strategy: Mutex::new(Box::new(MomentumVelocityStrategy::new())),
|
strategy: Mutex::new(Box::new(MomentumVelocityStrategy::new())),
|
||||||
|
|
||||||
trade_log: Mutex::new(Vec::new()),
|
trade_log: Mutex::new(Vec::new()),
|
||||||
executor: Mutex::new(ExecutorWrapper {
|
executor: Mutex::new(()),
|
||||||
executor: Box::new(PumpDev {}),
|
|
||||||
positions: HashMap::new(),
|
|
||||||
}),
|
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,6 +80,11 @@ 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()
|
||||||
|
|||||||
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod account;
|
||||||
|
pub mod tradelog;
|
||||||
@@ -1,122 +0,0 @@
|
|||||||
pub mod pump_fun;
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
|
||||||
|
|
||||||
use rust_decimal::Decimal;
|
|
||||||
|
|
||||||
pub struct ExecutorWrapper {
|
|
||||||
pub executor: Box<dyn Executor>,
|
|
||||||
pub positions: HashMap<String, Decimal>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
pub trait Executor: Send + Sync {
|
|
||||||
async fn buy(
|
|
||||||
&self,
|
|
||||||
mint: &str,
|
|
||||||
amount: Decimal,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn sell(
|
|
||||||
&self,
|
|
||||||
mint: &str,
|
|
||||||
amount: Decimal,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn sell_percent(
|
|
||||||
&self,
|
|
||||||
mint: &str,
|
|
||||||
amount: u8,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ExecutorWrapper {
|
|
||||||
pub async fn buy(
|
|
||||||
&mut self,
|
|
||||||
mint: &str,
|
|
||||||
amount: Decimal,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
self.executor.buy(mint, amount, priority, slippage).await?;
|
|
||||||
|
|
||||||
let position = self
|
|
||||||
.positions
|
|
||||||
.entry(mint.to_string())
|
|
||||||
.or_insert(Decimal::ZERO);
|
|
||||||
|
|
||||||
*position += amount;
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn sell(
|
|
||||||
&mut self,
|
|
||||||
mint: &str,
|
|
||||||
amount: Decimal,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
self.executor.sell(mint, amount, priority, slippage).await?;
|
|
||||||
|
|
||||||
if let Some(position) = self.positions.get_mut(mint) {
|
|
||||||
*position -= amount;
|
|
||||||
|
|
||||||
if *position <= Decimal::ZERO {
|
|
||||||
self.positions.remove(mint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn sell_percent(
|
|
||||||
&mut self,
|
|
||||||
mint: &str,
|
|
||||||
amount: u8,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
let sell_amount = match self.positions.get(mint) {
|
|
||||||
Some(position) => *position * Decimal::from(amount) / Decimal::from(100),
|
|
||||||
None => return Ok(()),
|
|
||||||
};
|
|
||||||
|
|
||||||
self.executor
|
|
||||||
.sell_percent(mint, amount, priority, slippage)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
if let Some(position) = self.positions.get_mut(mint) {
|
|
||||||
*position -= sell_amount;
|
|
||||||
|
|
||||||
if *position <= Decimal::ZERO {
|
|
||||||
self.positions.remove(mint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn sell_all(&mut self, priority: Decimal, slippage: u16) -> anyhow::Result<()> {
|
|
||||||
for (mint, _) in self.positions.drain() {
|
|
||||||
self.executor
|
|
||||||
.sell_percent(&mint, 100, priority, slippage)
|
|
||||||
.await?;
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,68 +0,0 @@
|
|||||||
use rust_decimal::Decimal;
|
|
||||||
|
|
||||||
use crate::executor::Executor;
|
|
||||||
|
|
||||||
pub struct PumpDev {}
|
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl Executor for PumpDev {
|
|
||||||
async fn buy(
|
|
||||||
&self,
|
|
||||||
mint: &str,
|
|
||||||
amount: Decimal,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
log::info!("BUY {mint} {amount} SOL");
|
|
||||||
Ok(())
|
|
||||||
// self.trade(
|
|
||||||
// "buy",
|
|
||||||
// mint,
|
|
||||||
// amount.round_dp(3).to_string(),
|
|
||||||
// priority,
|
|
||||||
// slippage,
|
|
||||||
// true,
|
|
||||||
// )
|
|
||||||
// .await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn sell(
|
|
||||||
&self,
|
|
||||||
mint: &str,
|
|
||||||
amount: Decimal,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
log::info!("SELL {mint} {amount} SOL");
|
|
||||||
Ok(())
|
|
||||||
// self.trade(
|
|
||||||
// "sell",
|
|
||||||
// mint,
|
|
||||||
// amount.round_dp(3).to_string(),
|
|
||||||
// priority,
|
|
||||||
// slippage,
|
|
||||||
// false,
|
|
||||||
// )
|
|
||||||
// .await
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn sell_percent(
|
|
||||||
&self,
|
|
||||||
mint: &str,
|
|
||||||
amount: u8,
|
|
||||||
priority: Decimal,
|
|
||||||
slippage: u16,
|
|
||||||
) -> anyhow::Result<()> {
|
|
||||||
log::info!("SELL {mint} {amount}%");
|
|
||||||
Ok(())
|
|
||||||
// self.trade(
|
|
||||||
// "sell",
|
|
||||||
// mint,
|
|
||||||
// format!("{amount}%"),
|
|
||||||
// priority,
|
|
||||||
// slippage,
|
|
||||||
// false,
|
|
||||||
// )
|
|
||||||
// .await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-3
@@ -1,8 +1,6 @@
|
|||||||
pub mod account;
|
|
||||||
pub mod bot;
|
pub mod bot;
|
||||||
pub mod executor;
|
pub mod data;
|
||||||
pub mod strategy;
|
pub mod strategy;
|
||||||
pub mod tradelog;
|
|
||||||
pub mod types;
|
pub mod types;
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ use log::{debug, info, trace, warn};
|
|||||||
use rust_decimal::{Decimal, dec};
|
use rust_decimal::{Decimal, dec};
|
||||||
|
|
||||||
use crate::bot::Bot;
|
use crate::bot::Bot;
|
||||||
|
use crate::data::tradelog::{ExitReason, TradeLog};
|
||||||
use crate::strategy::Strategy;
|
use crate::strategy::Strategy;
|
||||||
use crate::tradelog::{ExitReason, TradeLog};
|
|
||||||
use crate::types::{NewToken, Trade, TradeType};
|
use crate::types::{NewToken, Trade, TradeType};
|
||||||
|
|
||||||
const BUY_AMOUNT_SOL: Decimal = dec!(0.2);
|
const BUY_AMOUNT_SOL: Decimal = dec!(0.2);
|
||||||
|
|||||||
Reference in New Issue
Block a user