Giter Site home page Giter Site logo

uniswapx-sdk-rs's Introduction

A crate for consuming, and coordinating filler services for UniswapX orders.

see cargo doc --open to see the full docs

The crate will have 3 main parts, api, server and core.

  • api:
    • subscribers and clients are defined here. There is a default uniswap client implementation in api/src/uniswap/
    • you can wrap any api by implementing the async trait Client<SignedOrder> over it
  • core:
    • Alloy-rs structs live here
    • contains validations in the style of the UniswapX-sdk
    • an order cache that can be shared between subscribers
    • order builders (coming soon)
  • server (coming soon)
    • A tokio friendly UniswapX order api framework

An example setup

use ethers::providers::{Http, Middleware};
use futures::StreamExt;
use std::sync::Arc;
use uniswapx_sdk_api::{subscriber::OrderSubscriber, uniswap::UniswapClient};
use uniswapx_sdk_core::{
    order::SignedOrder,
    utils::{spawn_with_shutdown, OrderCache},
};

const PROVIDER_URL: &str = "";

#[tokio::main]
async fn main() {
    tracing_subscriber::fmt::init();

    let provider = Arc::new(
        ethers::providers::Provider::<Http>::try_from(PROVIDER_URL).expect("provider url to parse"),
    );

    // a thread safe cache that will flush itself every 10 seconds
    let cache = OrderCache::new(provider.clone(), 10);

    // a client for chain id = 1
    let client = UniswapClient::new(1);

    // could be made into a util function
    // spawn_order_handler or something
    spawn_with_shutdown(async move {
        // a stream of unvalidated orders
        let mut sub = OrderSubscriber::subscribe(cache.clone(), client, 5);

        while let Some(order) = sub.next().await {
            tokio::spawn(handle_order(order, provider.clone()));
        }
    })
    .await
    .unwrap();
}

async fn handle_order<M: Middleware + 'static>(order: SignedOrder, provider: Arc<M>) {
    println!("reactor: {:?}", order.reactor_address());

    match order.validate_ethers(provider.clone()).await {
        Ok(ans) => {
            println!("Isvalid? {:?}", ans);
            println!("deadline: {:?}", order.deadline());

            // do stuff with order
        }
        Err(e) => {
            println!("error: {:?}", e);
        }
    }
}

uniswapx-sdk-rs's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.