DiviProject / Divi-Desktop-Public

Public, open source development repository for the Divi Desktop Smart Wallet
GNU General Public License v2.0
7 stars 6 forks source link

Additional exchange/aggregator price data #48

Open Divicoin opened 5 years ago

Divicoin commented 5 years ago

Allow the user to choose multiple sources from which the price is pulled, or choose an average of the available options.

We have a couple of aggregators, but need to add our exchanges. If someone wants to send in USD, the amount of DIVI should reflect the current price, not the one 5 minutes ago.

Divicoin commented 5 years ago

➤ Dmitry Kostenevich commented:

Hi Nick Saponaro. I can't find api for get USD price from Bitrue and Crex24. May be you have any info about these exchanges?

hirscr commented 5 years ago

Here are the docs for Bittrue https://github.com/Bitrue-exchange/bitrue-official-api-docs

Here they are for Crex24 https://crex24.com/trade-api

Crex has better examples and was easier to iplement

For bitrue, their API tends to be more wonky. you need to start with getting THEIR timestmap

I start with a base URL for all the commands: baseUrl = "https://www.bitrue.com"

I also have a structure that holds the amount of different coins available and locked in an account

    funds={  'btc' : {'avail' : 0, 'locked' : 0},
             'divi': {'avail' : 0, 'locked' : 0},
             'xrp' : {'avail' : 0, 'locked' : 0},
             'btr' : {'avail' : 0, 'locked' : 0}}
def getBitrueTime() :
    #get timestamp at bittrue
    ts=requests.get(baseUrl+'/api/v1/time')
    timestamp=ts.json()['serverTime']
    sleep(0.1)
    return(timestamp)    

here is an example of getting the price of a pair:

def getBitruePrice (coin,base):
#    timestamp=getBitrueTime().   actually dont need
#    GET /api/v1/ticker/price this endpoint is not working atm

    coins=list(funds.keys())
    if coin in coins :
        try:
            trades=requests.get(baseUrl+'/api/v1/trades?symbol='+coin+base)
            sleep(0.1)
            price=trades.json()[0]['price']
        except:
            price=0 #send back 0 if the call failed
    return (float(price))

I am checking the price of all the coin in the 'funds' dictionary with this method.

Here is a way to check the balances of the coins in 'funds' in an account

def getBitrueBalance (apikey,secret):
    coins=list(funds.keys())
    headers={'X-MBX-APIKEY': apikey}
    funds=clearFunds()
    timestamp=getBitrueTime()
    #create the sig and query 
    query = "timestamp="+str(timestamp)+"&recvWindow=10000"
    sig = hmac.new(bytes(secret,'utf-8'), bytes(query, 'utf-8'), hashlib.sha256).hexdigest()
    r=0
    r = requests.get(baseUrl+'/api/v1/account' + '?' + query + '&signature=' + sig, headers=headers)

    try:
        balances=r.json()['balances'] #the balance of all the coins bitrue has is here
    except:
        balances=[]
        print('OOPS! No Access to this account')
    for balance in balances:        #pull out only the coins we care about
        if balance['asset'] in coins:  
            funds[balance['asset']]['avail']=float(balance['free'])
            funds[balance['asset']]['locked']=float(balance['locked'])
        else:
            break

    sleep(0.1)
    return (funds) 

Hopefully you can work from there for bitrue

oriz456 commented 5 years ago

@dkostenevich @ilagin please see above

Divicoin commented 4 years ago

➤ Ori Zeiger commented:

We should be using what we use for mobile app; Ideally we should give people the option in settings to choose(for example, if I only want the amounts I send or see in $ in my wallet to go based on bitrue, I can check that box. if someone else wants an average, a different exchange or an aggregator, they can check that)