-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdex_traits.rs
More file actions
220 lines (209 loc) · 8.7 KB
/
Copy pathdex_traits.rs
File metadata and controls
220 lines (209 loc) · 8.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use super::{
amm_calc::{amm_buy_get_token_out, amm_sell_get_sol_out, calculate_with_slippage_buy, calculate_with_slippage_sell},
types::{BatchBuyParam, BatchSellParam, Create, CreateATA, PoolInfo, SwapInfo, TokenAmountType},
};
use crate::{
common::trading_endpoint::{BatchTxItem, TradingEndpoint},
instruction::builder::{build_sol_buy_instructions, build_sol_sell_instructions, build_wsol_buy_instructions, build_wsol_sell_instructions, PriorityFee},
};
use solana_sdk::{
hash::Hash,
instruction::Instruction,
pubkey::Pubkey,
signature::{Keypair, Signature},
signer::Signer,
};
use std::{any::Any, sync::Arc};
#[async_trait::async_trait]
pub trait DexTrait: Send + Sync + Any {
async fn initialize(&self) -> anyhow::Result<()>;
fn initialized(&self) -> anyhow::Result<()>;
fn use_wsol(&self) -> bool;
fn get_trading_endpoint(&self) -> Arc<TradingEndpoint>;
async fn get_pool(&self, mint: &Pubkey) -> anyhow::Result<PoolInfo>;
async fn create(&self, payer: Keypair, create: Create, fee: Option<PriorityFee>, tip: Option<u64>) -> anyhow::Result<Vec<Signature>>;
fn build_buy_instruction(&self, payer: &Keypair, mint: &Pubkey, creator_vault: Option<&Pubkey>, buy: SwapInfo) -> anyhow::Result<Instruction>;
fn build_sell_instruction(&self, payer: &Keypair, mint: &Pubkey, creator_vault: Option<&Pubkey>, sell: SwapInfo) -> anyhow::Result<Instruction>;
async fn buy(
&self,
payer: &Keypair,
mint: &Pubkey,
sol_amount: u64,
slippage_basis_points: u64,
fee: Option<PriorityFee>,
tip: Option<u64>,
) -> anyhow::Result<Vec<Signature>> {
let trading_endpoint = self.get_trading_endpoint();
let (pool_info, blockhash) = tokio::try_join!(self.get_pool(mint), trading_endpoint.get_latest_blockhash(),)?;
let buy_token_amount = amm_buy_get_token_out(pool_info.sol_reserves, pool_info.token_reserves, sol_amount);
let sol_lamports_with_slippage = calculate_with_slippage_buy(sol_amount, slippage_basis_points);
self.buy_immediately(
payer,
mint,
pool_info.extra_address.as_ref(),
sol_lamports_with_slippage,
buy_token_amount,
blockhash,
CreateATA::Idempotent,
fee,
tip,
)
}
fn buy_immediately(
&self,
payer: &Keypair,
mint: &Pubkey,
extra_address: Option<&Pubkey>,
sol_amount: u64,
token_amount: u64,
blockhash: Hash,
create_ata: CreateATA,
fee: Option<PriorityFee>,
tip: Option<u64>,
) -> anyhow::Result<Vec<Signature>> {
let instruction = self.build_buy_instruction(payer, mint, extra_address, SwapInfo { token_amount, sol_amount })?;
let instructions = if self.use_wsol() {
build_wsol_buy_instructions(payer, mint, sol_amount, instruction, create_ata)?
} else {
build_sol_buy_instructions(payer, mint, instruction, create_ata)?
};
let signatures = self
.get_trading_endpoint()
.build_and_broadcast_tx(payer, instructions, blockhash, fee, tip, None)?;
Ok(signatures)
}
async fn sell(
&self,
payer: &Keypair,
mint: &Pubkey,
token_amount: TokenAmountType,
slippage_basis_points: u64,
close_mint_ata: bool,
fee: Option<PriorityFee>,
tip: Option<u64>,
) -> anyhow::Result<Vec<Signature>> {
let trading_endpoint = self.get_trading_endpoint();
let payer_pubkey = payer.pubkey();
let (pool_info, blockhash, token_amount) = tokio::try_join!(
self.get_pool(&mint),
trading_endpoint.get_latest_blockhash(),
token_amount.to_amount(trading_endpoint.rpc.clone(), &payer_pubkey, mint)
)?;
let sol_lamports = amm_sell_get_sol_out(pool_info.sol_reserves, pool_info.token_reserves, token_amount);
let sol_lamports_with_slippage = calculate_with_slippage_sell(sol_lamports, slippage_basis_points);
self.sell_immediately(
payer,
mint,
pool_info.extra_address.as_ref(),
token_amount,
sol_lamports_with_slippage,
close_mint_ata,
blockhash,
fee,
tip,
)
}
fn sell_immediately(
&self,
payer: &Keypair,
mint: &Pubkey,
extra_address: Option<&Pubkey>,
token_amount: u64,
sol_amount: u64,
close_mint_ata: bool,
blockhash: Hash,
fee: Option<PriorityFee>,
tip: Option<u64>,
) -> anyhow::Result<Vec<Signature>> {
let instruction = self.build_sell_instruction(payer, mint, extra_address, SwapInfo { token_amount, sol_amount })?;
let instructions = if self.use_wsol() {
build_wsol_sell_instructions(payer, mint, instruction, close_mint_ata)?
} else {
build_sol_sell_instructions(payer, mint, instruction, close_mint_ata)?
};
let signatures = self
.get_trading_endpoint()
.build_and_broadcast_tx(payer, instructions, blockhash, fee, tip, None)?;
Ok(signatures)
}
async fn batch_buy(
&self,
mint: &Pubkey,
slippage_basis_points: u64,
fee: PriorityFee,
tip: u64,
items: Vec<BatchBuyParam>,
) -> anyhow::Result<Vec<Signature>> {
let trading_endpoint = self.get_trading_endpoint();
let (pool_info, blockhash) = tokio::try_join!(self.get_pool(&mint), trading_endpoint.get_latest_blockhash(),)?;
let mut pool_token_amount = pool_info.token_reserves;
let mut pool_sol_amount = pool_info.sol_reserves;
let mut batch_items = vec![];
for item in items {
let sol_lamports_with_slippage = calculate_with_slippage_buy(item.sol_amount, slippage_basis_points);
let buy_token_amount = amm_buy_get_token_out(pool_sol_amount, pool_token_amount, item.sol_amount);
let instruction = self.build_buy_instruction(
&item.payer,
&mint,
pool_info.creator_vault.as_ref(),
SwapInfo {
token_amount: buy_token_amount,
sol_amount: sol_lamports_with_slippage,
},
)?;
let instructions = if self.use_wsol() {
build_wsol_buy_instructions(&item.payer, mint, sol_lamports_with_slippage, instruction, CreateATA::Idempotent)?
} else {
build_sol_buy_instructions(&item.payer, mint, instruction, CreateATA::Idempotent)?
};
batch_items.push(BatchTxItem {
payer: item.payer,
instructions,
});
pool_sol_amount += item.sol_amount;
pool_token_amount -= buy_token_amount;
}
let signatures = trading_endpoint.build_and_broadcast_batch_txs(batch_items, blockhash, fee, tip).await?;
Ok(signatures)
}
async fn batch_sell(
&self,
mint: &Pubkey,
slippage_basis_points: u64,
fee: PriorityFee,
tip: u64,
items: Vec<BatchSellParam>,
) -> anyhow::Result<Vec<Signature>> {
let trading_endpoint = self.get_trading_endpoint();
let (pool_info, blockhash) = tokio::try_join!(self.get_pool(&mint), trading_endpoint.get_latest_blockhash(),)?;
let mut pool_token_amount = pool_info.token_reserves;
let mut pool_sol_amount = pool_info.sol_reserves;
let mut batch_items = vec![];
for item in items {
let sol_amount = amm_sell_get_sol_out(pool_sol_amount, pool_token_amount, item.token_amount);
let sol_lamports_with_slippage = calculate_with_slippage_sell(sol_amount, slippage_basis_points);
let instruction = self.build_sell_instruction(
&item.payer,
&mint,
pool_info.creator_vault.as_ref(),
SwapInfo {
token_amount: sol_amount,
sol_amount: sol_lamports_with_slippage,
},
)?;
let instructions = if self.use_wsol() {
build_wsol_sell_instructions(&item.payer, mint, instruction, item.close_mint_ata)?
} else {
build_sol_sell_instructions(&item.payer, mint, instruction, item.close_mint_ata)?
};
batch_items.push(BatchTxItem {
payer: item.payer,
instructions,
});
pool_sol_amount -= sol_amount;
pool_token_amount += item.token_amount;
}
let signatures = trading_endpoint.build_and_broadcast_batch_txs(batch_items, blockhash, fee, tip).await?;
Ok(signatures)
}
}