Package: dexblue-api
dex.blue python api wrapperThis is the official Python API wrapper for communicating with the dex.blue API.For further information head to theAPI DocumentationInstallationInstall viapippipinstalldexblue-apiIntroductiondex.blue is a trustless, non-custodial exchange. This means, that every input which moves funds needs to by signed with your private key.You either have to sign orders directly from your wallet address or use aDelegated Signing Key.For the most straightforward integration, which does not require you to directly interact with the blockchain, you can just use ourwebinterfacefor deposits & withdrawals and register aDelegated Signing Keyin the settings ⚙ section.If you want to handle deposits and withdrawals from your bot, please check outthis pageof our documentation.Basic exampleimportdexbluedb=dexblue.WsAPI()db.methods.getListed()db.on('listed',print)Initializing a connectionTha connection can be initialized with different parameters.importdexbluedb=dexblue.WsAPI(delegate=<DEXBLUEDELEGATEKEY>,account=<ETHEREUMPRIVATEKEY>,endpoint=<DEXBLUEWEBSOCKETENDPOINT>,# default: wss://api.dex.blue/wsweb3Provider=<ETHEREUMWEB3RPCENDPOINT>,# default: https://mainnet.infura.io/network=<ETHEREUMNETWORK>,# default: mainnetautoAuth=<AUTHENTICATEWHENCONNECTIONOPENS># default: True)defcallback(packet):# your code heredb.on('wsOpen',callback)It is possible to use a encrypted key tho authenticate your connectionkey=dexblue.utils.readPrivateKeyFromFile(keyfile,password)db=dexblue.WsAPI(delegate=key)Calling a methodThis Library provides a wrapper function forevery method offered by the dex.blue API, which can be invoked with eg:db.methods.getOrderBookSnapshot(parameters, callback, <callback arguments>...).For a full list of the available methods and parameters, please refer to thewebsocket API documentation.Additionally the library offers some helper functions to deal with all of the hard and annoying stuff like hashing and signing:db.authenticate(privKey)- called automatically, when you pass an account to the constructordb.authenticateDelegate(privKey)- Called automatically, when you pass an delegate to the constructordb.placeOrder(order, callback)- This function abstracts all the stress of contructing and signing orders away from you. Very recommended to use this!db.hashOrder(order) returns hash- This function helps you hashing the order inputs correctly. You then need to sign the order by yourself.EventsYou can subscribe to any server and websocket events using the following functions:Events:Market Events:book20d5...book20d1Orderbook with a depth of 20 with 5 ... 1 decimal precision (for the rate)book100d5...book100d1Orderbook with a depth of 10 with 5 ... 1 decimal precision (for the rate)bookd5...bookd1Full orderbook with 5 ... 1 decimal precision (for the rate)tradesTrades Feed of the markettickerThe ticker of the marketOther Events:ratesubscribe to a ETH to fiat conversion rate e.g. ETHUSD, available are ETH traded against the config.conversion_currencies. (sub with:{markets:["ETHUSD"],events:["rate"]})chainStatssubscribe to the servers block height and gas price (sub with:{markets:["ethereum"],events:["chainStats"]})Websocket Events (no need to subscribe, just listen)wsOpenwebsocket connection is openedmessageevery received messagewsErrorwebsocket erroredwsClosewebsocket conn is closedSubscribing to eventsdb.methods.subscribe({"markets":["ETHDAI","MKRETH"],"events":["trades","book20d5"]})db.on('events',print)CallbackA callback must have at least one paramater which is the received data. The following arguments are passed through from the callback definition.defcallback(packet,parameter1,parameter2,...):print(packet,parameter1,parameter2)db.on('listed',callback,"parameter1","parameter2")The packet parameter is a dict, which has the following structure{"chan":<CHANNEL>,# The channel id is documented in the dex.blue api docs<EVENTNAME>:<PARSEDPACKET>,"packet":<UNPARSEDPACKET># the same packet which the server sent}Placing an orderdefcallback(packet):# If you passed an account of delegate to the constructor, you will authenticated automatically# All private commands should be sent after we are successfully authenticated# If no expiry is passed, a default expiry of one month will be applied# This function supports either very abstracted inputdb.placeOrder({"market":"ETHDAI","amount":-1,# positive amount implies buy order, negative sell"rate":300},print)# This function supports either very abstracted inputorderIdentifier=int(time.time())# client-set order identifierdb.placeOrder({"cid":orderIdentifier,"sellToken":"0x0000000000000000000000000000000000000000",# ETH"sellAmount":"1000000000000000000",# 1 ETH"buyToken":"0x89d24a6b4ccb1b6faa2625fe562bdd9a23260359",# DAI"buyAmount":"300000000000000000000",# 300 DAI"expiry":int(time.time()+86400*2),# order is valid 2 days (different from the timeInForce parameter)"hidden":False,"postOnly":True,# order is either maker or canceled"rebateToken":"buy",# we want to receive our rebate in DAI (the token we buy)# ... more possibilities are listed here: https://docs.dex.blue/websocket/#placeorder},print)db.on('auth',callback)Error handelingdefreconnect_cb(packet):print('Reconnect in: '+str(packet["reconnect"]["timeout"])+'s. Message: '+packet["reconnect"]["message"])db.on('reconnect',reconnect_cb)# server sent a reconnect instructiondb.on('error',print)# handle error (probably resulting in a disconnect)db.on('wsClose',print)# handle disconnect