thesonyman101
thesonyman101

Reputation: 821

Cryptopia API getting bad request for Sell

def sell():
    #Market: The market symbol of the trade e.g. 'DOT/BTC' (not required if 'TradePairId' supplied)
    #TradePairId: The Cryptopia tradepair identifier of trade e.g. '100' (not required if 'Market' supplied)
    #Type: the type of trade e.g. 'Buy' or 'Sell'
    #Rate: the rate or price to pay for the coins e.g. 0.00000034
   #Amount: the amount of coins to buy e.g. 123.00000000
   #SubmitTrade', {'Market': Market, 'Type': Type, 'Rate': Rate, 'Amount': Amount
    c = cryptopia(key,secret)
    c.SubmitTrade('CHC/BTC','Sell','1','.1')

So, in this i'm trying to sell .1 chc for 1 btc I'm using this wrapper for the api https://pastebin.com/nL089zwF. When I run the current method I get ( Response ): {"Success":false,"Error":"Bad Request","Data":null} Thank you.

Upvotes: 0

Views: 1487

Answers (1)

t.m.adam
t.m.adam

Reputation: 15376

I modified the api_query method and managed to get a valid response from the api. However the code is far from optimal and you should consider using some other library.

Original code:

def api_query(self, method, values, req = None ):
    if not req:
        req = {}
    #print "def api_query( method = " + method + ", req = " + str( req ) + " ):"
    time.sleep(1)
    if method in self.public:
        url = 'https://www.cryptopia.co.nz/api/'
    elif method in self.private:
        url = 'https://www.cryptopia.co.nz/api/'
    else:
        return 'Call Not Identified - Something Went Wrong.'

    url += method + '?' + urllib.urlencode(values)

    if method not in self.public:
        url = "https://www.cryptopia.co.nz/Api/" + method
        nonce = str( int( time.time() ) )
        post_data = json.dumps( req );
        m = hashlib.md5()
        m.update(post_data)
        requestContentBase64String = base64.b64encode(m.digest())
        signature = self.key + "POST" + urllib.quote_plus( url ).lower() + nonce + requestContentBase64String
        hmacsignature = base64.b64encode(hmac.new(base64.b64decode( self.secret ), signature, hashlib.sha256).digest())
        header_value = "amx " + self.key + ":" + hmacsignature + ":" + nonce
        headers = { 'Authorization': header_value, 'Content-Type':'application/json; charset=utf-8' }
        r = requests.post( url, data = post_data, headers = headers )
    response = r.text

    print "( Response ): " + response
    return response.replace("false","False").replace("true","True").replace('":null','":None' )

Modified code:

def api_query(self, method, values={}, req={}):
    time.sleep(1)
    if method in self.private:
        url = "https://www.cryptopia.co.nz/Api/" + method
        nonce = str(int(time.time()))
        requestContentBase64String = base64.b64encode(hashlib.md5(json.dumps(values)).digest())
        signature = self.key + "POST" + urllib.quote_plus(url).lower() + nonce + requestContentBase64String
        hmacsignature = base64.b64encode(hmac.new(base64.b64decode(self.secret), signature, hashlib.sha256).digest())
        header_value = "amx " + self.key + ":" + hmacsignature + ":" + nonce
        headers = { 'Authorization': header_value }
        r = requests.post(url, json = values, headers=headers)
    elif method in self.public:
        url = 'https://www.cryptopia.co.nz/api/' + method 
        r = requests.get(url, params=values) 
    else: 
        return 'Call Not Identified - Something Went Wrong.'
    response = r.content
    print "( Response ): " + response
    return response.replace("false","False").replace("true","True").replace('":null','":None' )

Notice:
I haven't tested the code thoroughly and i can't guarantee for its security or functionality; i'm just providing a working example.


Upvotes: 1

Related Questions