#[cfg(test)] mod tests { use std::{env, sync::Arc}; pub use kuru_sdk_rs::token::Token; use kuru_sdk_rs::{ inventory::Inventory, orderbook::{self, LimitOrder, Order, OrderSide}, process_orders, utils, MarginAccount, }; use alloy::{ network::{Ethereum, EthereumWallet}, primitives::{U256, U32}, providers::{ fillers::{ BlobGasFiller, ChainIdFiller, FillProvider, GasFiller, JoinFill, NonceFiller, WalletFiller, }, Identity, Provider, ProviderBuilder, RootProvider, }, signers::local::PrivateKeySigner, transports::{http::Http, Transport}, }; use alloy_json_rpc::RequestPacket; use orderbook::Orderbook; use rand::Rng; use reqwest::{Client, Url}; use dotenv::dotenv; use tower_service::Service; use utils::fetch_kuru_order_id; use super::*; fn setup() -> ( Arc< FillProvider< JoinFill< JoinFill< Identity, JoinFill< GasFiller, JoinFill>, >, >, WalletFiller, >, RootProvider>, Http, Ethereum, >, >, PrivateKeySigner, ) { dotenv().ok(); let pk = env::var("PK").expect("Private key not found in env"); let signer: PrivateKeySigner = pk.parse().expect("Error parsing private key"); let user = signer.address(); println!("Signer Address: {}", user); let wallet = EthereumWallet::from(signer.clone()); let rpc_url: Url = env::var("RPC_URL") .expect("RPC URL not found in env") .parse() .expect("Parsing URL unsuccessful"); let provider = Arc::new( ProviderBuilder::new() .with_recommended_fillers() .wallet(wallet) .on_http(rpc_url), ); (provider, signer) } struct ContractAddress { orderbook_address: &'static str, margin_account_address: &'static str, forwarder_address: &'static str, usdc_address: &'static str, wbtc_address: &'static str, } pub static TESTING_ADDRESSES: ContractAddress = ContractAddress { orderbook_address: "0x29BdC6fc3Bb87fb461Bd41DBc50f9097123f6aef", margin_account_address: "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318", forwarder_address: "0x0165878A594ca255338adfa4d48449f69242Eb8F", usdc_address: "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512", wbtc_address: "0x5FbDB2315678afecb367f032d93F642f64180aa3" }; #[tokio::test] async fn test_market_buy() { let (provider, signer) = setup(); let user = signer.address(); let margin_account = MarginAccount::new( "margin acc", &TESTING_ADDRESSES.margin_account_address, provider.clone(), ) .unwrap(); let wbtc = Token::new( "wbtc", &TESTING_ADDRESSES.wbtc_address, provider.clone(), ) .unwrap(); let usdc = Token::new( "usdc", &TESTING_ADDRESSES.usdc_address, provider.clone(), ) .unwrap(); let orderbook = Arc::new(Orderbook::new( &TESTING_ADDRESSES.orderbook_address, Arc::clone(&provider), ) .await .unwrap()); let amount: alloy::primitives::Uint<256, 4> = U256::from(100000000) * U256::from(10).pow(U256::from(18)); let _ = margin_account.deposit(&user, &usdc, &amount).await; let _ = margin_account.deposit(&user, &wbtc, &amount).await; let address = signer.address(); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance before: {}", usdc_balance.unwrap()); let _ = wbtc.increase_allowance(&orderbook.address(), &amount).await; let size = "2"; let min_amount_out = "0"; let fill_or_kill = false; let is_margin = true; let tx = orderbook .market_buy(size, &min_amount_out, is_margin, fill_or_kill, None, None) .await; dbg!(tx.unwrap()); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance After: {}", usdc_balance.unwrap()); } #[tokio::test] async fn test_market_sell() { let (provider, signer) = setup(); let user = signer.address(); let margin_account = MarginAccount::new( "margin acc", &TESTING_ADDRESSES.margin_account_address, provider.clone(), ) .unwrap(); let wbtc = Token::new( "wbtc", &TESTING_ADDRESSES.wbtc_address, provider.clone(), ) .unwrap(); let usdc = Token::new( "usdc", &TESTING_ADDRESSES.usdc_address, provider.clone(), ) .unwrap(); let orderbook = Arc::new(Orderbook::new( &TESTING_ADDRESSES.orderbook_address, Arc::clone(&provider), ) .await .unwrap()); let amount: alloy::primitives::Uint<256, 4> = U256::from(100000000) * U256::from(10).pow(U256::from(18)); let _ = margin_account.deposit(&user, &usdc, &amount).await; let _ = margin_account.deposit(&user, &wbtc, &amount).await; let address = signer.address(); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance before: {}", usdc_balance.unwrap()); let _ = wbtc.increase_allowance(&orderbook.address(), &amount).await; let size = "100"; let min_amount_out = "0"; let fill_or_kill = false; let is_margin = true; let tx = orderbook .market_sell(size, &min_amount_out, is_margin, fill_or_kill, None, None) .await; dbg!(tx.unwrap()); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance After: {}", usdc_balance.unwrap()); } // Limit orders #[tokio::test] async fn test_limit_buy() { let (provider, signer) = setup(); let user = signer.address(); let margin_account = MarginAccount::new( "margin acc", &TESTING_ADDRESSES.margin_account_address, provider.clone(), ) .unwrap(); let wbtc = Token::new( "wbtc", &TESTING_ADDRESSES.wbtc_address, provider.clone(), ) .unwrap(); let usdc = Token::new( "usdc", &TESTING_ADDRESSES.usdc_address, provider.clone(), ) .unwrap(); let orderbook = Arc::new(Orderbook::new( &TESTING_ADDRESSES.orderbook_address, Arc::clone(&provider), ) .await .unwrap()); let amount: alloy::primitives::Uint<256, 4> = U256::from(100000000) * U256::from(10).pow(U256::from(18)); let _ = margin_account.deposit(&user, &usdc, &amount).await; let _ = margin_account.deposit(&user, &wbtc, &amount).await; let address = signer.address(); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance before: {}", usdc_balance.unwrap()); let _ = wbtc.increase_allowance(&orderbook.address(), &amount).await; let price = "1"; let size = "1"; let post_only = false; let tx = orderbook .add_buy_order(price, size, post_only, None, None) .await; dbg!(tx.unwrap()); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance After: {}", usdc_balance.unwrap()); } #[tokio::test] async fn test_limit_sell() { let (provider, signer) = setup(); let user = signer.address(); let margin_account = MarginAccount::new( "margin acc", &TESTING_ADDRESSES.margin_account_address, provider.clone(), ) .unwrap(); let wbtc = Token::new( "wbtc", &TESTING_ADDRESSES.wbtc_address, provider.clone(), ) .unwrap(); let usdc = Token::new( "usdc", &TESTING_ADDRESSES.usdc_address, provider.clone(), ) .unwrap(); let orderbook = Arc::new(Orderbook::new( &TESTING_ADDRESSES.orderbook_address, Arc::clone(&provider), ) .await .unwrap()); let amount: alloy::primitives::Uint<256, 4> = U256::from(100000000) * U256::from(10).pow(U256::from(18)); let _ = margin_account.deposit(&user, &usdc, &amount).await; let _ = margin_account.deposit(&user, &wbtc, &amount).await; let address = signer.address(); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance before: {}", usdc_balance.unwrap()); let _ = wbtc.increase_allowance(&orderbook.address(), &amount).await; let size = "100"; let min_amount_out = "0"; let fill_or_kill = false; let is_margin = true; let tx = orderbook .market_sell(size, &min_amount_out, is_margin, fill_or_kill, None, None) .await; dbg!(tx.unwrap()); let usdc_balance = usdc.balance_of(&address).await; println!("Usdc balance After: {}", usdc_balance.unwrap()); } // #[test] // #[should_panic(expected = "assertion failed")] // fn test_failed_assertion() { // } // #[test] // #[ignore] // fn expensive_test() { // } }