GithubHelp home page GithubHelp logo

sgaliamov / tokio-binance Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mglolenstine/tokio-binance

0.0 1.0 1.0 177 KB

Client library for Binance

License: Other

Dockerfile 1.81% Rust 98.19%

tokio-binance's Introduction

tokio-binance

Unofficial async client for Binance.

Crates.io Documentation MIT/Apache-2 licensed Build Status

Fork

This is the fork from the tokio-binance project, created in order to create missing api and to update dependencies.

Examples

Add this in your Cargo.toml:

[dependencies]
tokio-binance = "https://github.com/sgaliamov/tokio-binance"

Client

use tokio_binance::{AccountClient,  ID};
use serde_json::Value;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = AccountClient::connect("<api-key>", "<secret-key>", "<api-url>")?;
    let response = client
        .get_order("BNBUSDT", ID::ClientOId("<uuid>"))
        // optional: processing time for request; default is 5000, can't be above 60000.
        .with_recv_window(8000)
        .json::<Value>()
        .await?;
    Ok(())
}

Websocket

use tokio_binance::*;
use tokio::time::{delay_for, Duration};
use serde_json::Value;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = UserDataClient::connect("<api-key>", "<api-url>")?;
    let value = client.start_stream().json::<Value>().await?;

    let listen_key = value["listenKey"].as_str().unwrap();
    let listen_key_copy = listen_key.to_string();

    tokio::spawn(async move {
        loop {
            delay_for(Duration::from_secs(30*60)).await;
            if let Err(e) = client.keep_alive(&listen_key_copy).text().await {
                eprintln!("{}", e);
                return
            }
        }
    });

    let channel = Channel::UserData(listen_key);
    let mut stream = WebSocketStream::connect(channel, "<ws-url>").await?;

    while let Some(value) = stream.json::<Value>().await? {
        if channel == value["stream"] {
            println!("{}", serde_json::to_string_pretty(&value)?);
        }
    }
    Ok(())
}

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

tokio-binance's People

Contributors

kgeronim avatar mglolenstine avatar sgaliamov avatar

Watchers

 avatar

Forkers

picardcc

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.