Skip to content

Instantly share code, notes, and snippets.

View 0ex-d's full-sized avatar

Precious 0ex-d

View GitHub Profile
@0ex-d
0ex-d / solana-Multi-DEX-Arbitrage.rs
Created February 19, 2025 19:01
Solana Multi-DEX Arbitrage (Pseudocode only!) (Orca -> Whirlpool -> Orca)
pub fn execute_orca_whirlpool_arbitrage(
ctx: Context<OrcaWhirlpoolArbitrage>,
amount_in: u64,
minimum_amount_1: u64,
minimum_final_amount: u64,
) -> Result<()> {
// 1. Orca swap
_orca_swap(
&ctx.accounts.orca_accounts_1,
// abstract from std
{
enum Result <T, E> {
Ok(()),
Err(())
}
enum Option <T> {
Some(()),
None
@0ex-d
0ex-d / two-way-stack.rs
Created January 22, 2025 01:24
A two-way stack
#[derive(Debug)]
pub struct Stack<L1, L2> {
l1: L1,
l2: L2,
}
impl<L1, L2> Stack<L1, L2> {
const fn new(l1: L1, l2: L2) -> Self {
Self { l1, l2 }
}
@0ex-d
0ex-d / copy-default.rs
Created January 6, 2025 02:22
Copy & Clone : Various methods of keeping track of References (both single threaded(Rc) and multi-threaded (Arc,Mutex)
fn main(){
let p1 = 1_000;
let p2 = p1; // Copy
let v1 = Box::new(200); stack pointer but value on the heap
let v2 = v1; // Clone
}
pub fn builtin_exec(cmd: &str) -> Option<Exec> {
let f = match cmd {
"add" => add::exec,
"bench" => bench::exec,
"build" => build::exec,
"check" => check::exec,
"clean" => clean::exec,
"config" => config::exec,
"doc" => doc::exec,
"fetch" => fetch::exec,
@0ex-d
0ex-d / a-worker-with-tokio.rs
Created December 31, 2024 20:31
Cheapest tokio-sponsored blue collar worker you could hire
use tokio::time::{sleep, Duration, Instant};
use anyhow::Result;
use thiserror::Error;
#[derive(Error,Debug)]
enum WorkerError {
#[error("Worker {0} encountered an error")]
Failed(isize),
}
@0ex-d
0ex-d / a-worker-no-tokio.rs
Last active December 31, 2024 19:01
Cheapest blue collar worker you could hire 🎁 + without tokio runtime.
use std::thread;
use std::time::{Duration, Instant};
fn worker(id: isize) -> thread::JoinHandle<()> {
println!("starting worker {id}");
thread::spawn(move || {
thread::sleep(Duration::from_secs(10));
println!("worker {id} done");
})
}
fn simulate_mempool_flood() {
let mut mempool: Vec<Transaction> = Vec::new();
let mut rng = rand::thread_rng();
for i in 0..10_000 {
let tx = Transaction {
id: format!("tx_{}", i),
fee: rng.gen_range(1..=10),
vin: if rng.gen_bool(0.9) {
vec![format!("utxo_{}", rng.gen_range(1..=1000))]
} else {
@0ex-d
0ex-d / dex-to-dex.sol
Created December 9, 2024 22:43
Solidity Pseudocode for DEX-2-DEX automated, fast execution Arbitrage trader.
// DEX-2-DEX only
// A pseudocode of my automated solidity code to execute fast Arb trade.
// I would be using Uniswap and PancakeSwap DEXs
// 1 ETH = 3,234 USDT (Uniswap)
// 1 ETH = 3,334 USDT (PancakeSwap)
function executeArbitrage( address tokenA, address tokenB, uint amountIn, address dex1, address dex2) external {
uint256 feeDex1 = Dex1(dex1).getTradingFee(tokenA, tokenB);
uint256 feeDex2 = Dex2(dex2).getTradingFee(tokenB, tokenA);
// bid 1 ETH = 3,234 USDT (Uniswap)
@0ex-d
0ex-d / binary_tree.rs
Created November 21, 2024 20:13
Optimal Binary tree I use often in Rust
struct BinaryTree {
tree: Vec<u32>,
max_size: usize,
curr_size: usize,
}
impl BinaryTree {
fn new(max_size: usize) -> Self {
BinaryTree {
tree: vec![0; max_size + 1], // 1-based indexing