Salus
Salus

Reputation: 11

poloniex API request BUY SELL CANCEL cause error = "Invalid API key/secret pair.";

Hello To every one I am writing in swift 3 trading app. I have problem only with 3 commands buy sell and cancel , that ones cause error = "Invalid API key/secret pair."; others like returnOpenOrders , returnTradehistory returnBalances works fine and returns proper values.

That is may request function :

func getRawJSON(paramss:[String : Any]){
    var paramss1:[String:Any] = [:]
    let APIURL = "https://poloniex.com/tradingApi"
    let timeNowInt = Int(NSDate().timeIntervalSince1970 ) * 10000000

    var zdanie2:String! = ""

for (x,y) in paramss{

                        paramss1[x]=y

                        }
    paramss1["nonce"]=timeNowInt


for (x,y) in paramss{
    if (zdanie2 == "")
    {zdanie2="\(x)=\(y)"
    }
    else
    {
        zdanie2=zdanie2+"&"+"\(x)=\(y)"
    }

}

zdanie2=zdanie2+"&nonce=\(timeNowInt)"



    let array: [UInt8] = Array(zdanie2.utf8)

    let hmac: Array<UInt8> = try! HMAC(key: secretKey!.utf8.map({$0}), variant: .sha512).authenticate(array)
    let hmacData = Data(bytes: hmac).toHexString()
       let headers  = ["Key": publicKey!,"Sign": hmacData] as   [String : String]


    request(APIURL,method: .post,parameters: paramss1,headers:headers).responseJSON {
        response in
        print(response)
       print(response.request)


    }

}

Here is my buy/sell function :

 func buy(currencyPair:String,rate:Double,amount:Double){
    return self.getRawJSON( paramss: ["command":"buy","currencyPair":currencyPair,"rate":rate ,"amount":amount])
}
func sell(currencyPair:String,rate:Double,amount:Double){
    return self.getRawJSON( paramss: ["command":"sell","currencyPair":currencyPair,"rate": rate ,"amount":amount])
}

The output from headers parameters(var paramss1 ) is :

["amount": 2.0, "command": "sell", "nonce": 15308121310000000, "currencyPair": "BTC_XRP", "rate": 7.6000000000000004e-05]

The array for sign is :

amount=2.0&command=sell&currencyPair=BTC_XRP&rate=7.6e-05&nonce=15308121310000000

I really dont know what is wrong Can you help with this problem ??

Upvotes: 1

Views: 224

Answers (1)

Talha &#199;elik
Talha &#199;elik

Reputation: 187

rate value is invalid.

rate and amount values must be like 1, 1.1, 1.00000001.

Invalid request: amount=2.0&command=sell&currencyPair=BTC_XRP&rate=7.6e-05&nonce=15308121310000000

Valid request: command=sell&amount=2.0&&currencyPair=BTC_XRP&rate=7.000006&nonce=15308121310000000

Upvotes: 0

Related Questions