Skip to content

Commit

Permalink
Added GET error checks, changed ArrayTable() for errors
Browse files Browse the repository at this point in the history
Added new module with a GetDataFromURL function, does GET commands now. Added tests for the ModFunctions module. Change ArrayTable function for empty results, see issue #2
  • Loading branch information
krijnsent committed Nov 29, 2017
1 parent 00e373d commit 11c0282
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 93 deletions.
17 changes: 6 additions & 11 deletions ModExchBittrex.bas
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ End Sub
Function PublicBittrex(Method As String, Optional MethodOptions As String) As String

'https://bittrex.com/home/api
Dim Url As String
PublicApiSite = "https://bittrex.com"
urlPath = "/api/v1.1/public/" & Method & MethodOptions
Url = PublicApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", Url
objHTTP.Send
objHTTP.WaitForResponse
PublicBittrex = objHTTP.ResponseText
Set objHTTP = Nothing
PublicBittrex = GetDataFromURL(Url, "GET")

End Function
Function PrivateBittrex(Method As String, apikey As String, secretkey As String, Optional MethodOptions As String) As String
Expand All @@ -60,11 +55,11 @@ APIsign = ComputeHash_C("SHA512", TradeApiSite & postdata, secretkey, "STRHEX")

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
Debug.Print "POST: " & TradeApiSite & postdata
'Debug.Print "POST: " & TradeApiSite & postdata
objHTTP.Open "POST", TradeApiSite & postdata, False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.SetRequestHeader "apisign", APIsign
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "apisign", APIsign
objHTTP.Send (postdata)

objHTTP.WaitForResponse
Expand Down
15 changes: 5 additions & 10 deletions ModExchCoinone.bas
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,12 @@ End Sub
Function PublicCoinone(Method As String, Optional MethodOptions As String) As String

'https://Coinone.com/home/api
Dim Url As String
PublicApiSite = "https://api.coinone.co.kr/"
urlPath = Method & "/" & MethodOptions
Url = PublicApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", Url
objHTTP.Send
objHTTP.WaitForResponse
PublicCoinone = objHTTP.ResponseText
Set objHTTP = Nothing
PublicCoinone = GetDataFromURL(Url, "GET")

End Function
Function PrivateCoinone(Method As String, apikey As String, secretkey As String, Optional MethodOptions As String) As String
Expand Down Expand Up @@ -73,9 +68,9 @@ APIsign = ComputeHash_C("SHA512", Base64Encode(postdata_json_txt), secretkey, "S
'' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", Url, False
objHTTP.SetRequestHeader "Content-Type", "application/json"
objHTTP.SetRequestHeader "X-COINONE-PAYLOAD", postdata64
objHTTP.SetRequestHeader "X-COINONE-SIGNATURE", APIsign
objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.setRequestHeader "X-COINONE-PAYLOAD", postdata64
objHTTP.setRequestHeader "X-COINONE-SIGNATURE", APIsign
objHTTP.Send (postdata)

objHTTP.WaitForResponse
Expand Down
16 changes: 5 additions & 11 deletions ModExchCryptopia.bas
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,12 @@ End Sub
Function PublicCryptopia(Method As String, Optional MethodOptions As String) As String

'https://www.cryptopia.co.nz/forum/Thread/255

Dim Url As String
PublicApiSite = "https://www.cryptopia.co.nz"
urlPath = "/api/" & Method & MethodOptions
Url = PublicApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", Url
objHTTP.Send
objHTTP.WaitForResponse
PublicCryptopia = objHTTP.ResponseText
Set objHTTP = Nothing
PublicCryptopia = GetDataFromURL(Url, "GET")

End Function
Function PrivateCryptopia(Method As String, apikey As String, secretkey As String, Optional MethodOptions As String) As String
Expand Down Expand Up @@ -76,9 +70,9 @@ HeaderValue = "amx " & apikey & ":" & hmacSignature & ":" & NonceUnique
' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", Url, False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.SetRequestHeader "Content-Type", "application/json"
objHTTP.SetRequestHeader "Authorization", HeaderValue
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/json"
objHTTP.setRequestHeader "Authorization", HeaderValue
objHTTP.Send (postdataJsonTxt)

objHTTP.WaitForResponse
Expand Down
18 changes: 6 additions & 12 deletions ModExchKraken.bas
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,12 @@ End Sub
Function PublicKraken(Method As String, Optional MethodOptions As String) As String

'https://www.kraken.com/en-us/help/api#public-market-data

Dim Url As String
PublicApiSite = "https://api.kraken.com"
urlPath = "/0/public/" & Method & MethodOptions
Url = PublicApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", Url
objHTTP.Send
objHTTP.WaitForResponse
PublicKraken = objHTTP.ResponseText
Set objHTTP = Nothing
PublicKraken = GetDataFromURL(Url, "GET")

End Function
Function PrivateKraken(Method As String, apikey As String, secretkey As String, Optional MethodOptions As String) As String
Expand All @@ -70,10 +64,10 @@ APIsign = ComputeHash_C("SHA512", urlPath & ComputeHash_C("SHA256", NonceUnique
' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", Url, False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.SetRequestHeader "API-Key", apikey
objHTTP.SetRequestHeader "API-Sign", APIsign
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "API-Key", apikey
objHTTP.setRequestHeader "API-Sign", APIsign
objHTTP.Send (postdata)

objHTTP.WaitForResponse
Expand Down
26 changes: 10 additions & 16 deletions ModExchLiqui.bas
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ End Sub
Function PublicLiqui(Method As String, Optional MethodOptions As String) As String

'https://www.Liqui.com/en-us/help/api#public-market-data

Dim Url As String
PublicApiSite = "https://api.liqui.io"
urlPath = "/api/3/" & Method & MethodOptions
Url = PublicApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", Url
objHTTP.Send
objHTTP.WaitForResponse
PublicLiqui = objHTTP.ResponseText
Set objHTTP = Nothing
PublicLiqui = GetDataFromURL(Url, "GET")

End Function
Function PrivateLiqui(Method As String, apikey As String, secretkey As String, Optional MethodOptions As String) As String
Expand All @@ -64,18 +58,18 @@ urlPath = "/tapi/"
postdata = "method=" & Method & MethodOptions & "&nonce=" & NonceUnique
Url = TradeApiSite & urlPath
APIsign = ComputeHash_C("SHA512", postdata, secretkey, "STRHEX")
Debug.Print postdata
Debug.Print Url
Debug.Print apikey
Debug.Print APIsign
'Debug.Print postdata
'Debug.Print Url
'Debug.Print apikey
'Debug.Print APIsign

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", Url, False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.SetRequestHeader "Key", apikey
objHTTP.SetRequestHeader "Sign", APIsign
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "Key", apikey
objHTTP.setRequestHeader "Sign", APIsign
objHTTP.Send (postdata)

objHTTP.WaitForResponse
Expand Down
18 changes: 6 additions & 12 deletions ModExchPoloniex.bas
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,12 @@ End Sub
Function PublicPoloniex(Method As String, Optional MethodOptions As String) As String

'https://poloniex.com/support/api/

Dim Url As String
PublicApiSite = "https://poloniex.com"
urlPath = "/public?command=" & Method & MethodOptions
Url = PublicApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", Url
objHTTP.Send
objHTTP.WaitForResponse
PublicPoloniex = objHTTP.ResponseText
Set objHTTP = Nothing
PublicPoloniex = GetDataFromURL(Url, "GET")

End Function
Function PrivatePoloniex(Method As String, apikey As String, secretkey As String, Optional MethodOptions As String) As String
Expand All @@ -66,10 +60,10 @@ APIsign = ComputeHash_C("SHA512", postdata, secretkey, "STRHEX")
' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", Url, False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.SetRequestHeader "Key", apikey
objHTTP.SetRequestHeader "Sign", APIsign
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "Key", apikey
objHTTP.setRequestHeader "Sign", APIsign
objHTTP.Send (postdata)

objHTTP.WaitForResponse
Expand Down
24 changes: 9 additions & 15 deletions ModExchWEXnz.bas
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Debug.Print PublicWEXnz("ticker", "/ltc_btc-btc_eur")
t1 = DateToUnixTime("1/1/2014")
t2 = DateToUnixTime("1/1/2018")

Debug.Print t1, t2
'Debug.Print t1, t2
Debug.Print PrivateWEXnz("getInfo", apikey, secretkey)
'{"success":1,"return":{"funds":{"usd":0,"btc":0.14,"ltc":0,"nmc":0, etc...
Debug.Print PrivateWEXnz("TradeHistory", apikey, secretkey, "&since=" & t1 & "&end=" & t2)
Expand All @@ -34,24 +34,18 @@ End Sub

Function PublicWEXnz(Method As String, Optional MethodOptions As String) As String

'https://btc-e.com/api/3/docs

'https://wex.nz/api/3/docs
Dim Url As String
PublicApiSite = "https://wex.nz"
urlPath = "/api/3/" & Method & MethodOptions
Url = PublicApiSite & urlPath

' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "GET", Url
objHTTP.Send
objHTTP.WaitForResponse
PublicWEXnz = objHTTP.ResponseText
Set objHTTP = Nothing
PublicWEXnz = GetDataFromURL(Url, "GET")

End Function
Function PrivateWEXnz(Method As String, apikey As String, secretkey As String, Optional MethodOptions As String) As String

'https://btc-e.com/tapi/docs
'https://wex.nz/tapi/docs
Dim NonceUnique As String

'BTC-e wants a 10-digit Nonce
Expand All @@ -64,10 +58,10 @@ APIsign = ComputeHash_C("SHA512", postdata, secretkey, "STRHEX")
' Instantiate a WinHttpRequest object and open it
Set objHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
objHTTP.Open "POST", TradeApiSite, False
objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.SetRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.SetRequestHeader "Key", apikey
objHTTP.SetRequestHeader "Sign", APIsign
objHTTP.setRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
objHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objHTTP.setRequestHeader "Key", apikey
objHTTP.setRequestHeader "Sign", APIsign
objHTTP.Send (postdata)

objHTTP.WaitForResponse
Expand Down
38 changes: 38 additions & 0 deletions ModFunctions.bas
Original file line number Diff line number Diff line change
@@ -1,4 +1,42 @@
Attribute VB_Name = "ModFunctions"
'Functions in module:
'DateToUnixTime - retuns the UnixTime of a date/time
'UnixTimeToDate - returns the date/time of a UnixTime
'TransposeArr - Custom transpose function, worksheetfunction.transpose won't handle long strings
'URLEncode - especially for Excel 2013 and before, afterwards it's a standard function
'Source: https://github.com/krijnsent/crypto_vba
Sub TestFunctions()

Debug.Print DateToUnixTime(#4/26/2017#)
'1493164800
Debug.Print DateToUnixTime(Now)
'e.g. 1511958343
Debug.Print UnixTimeToDate(1493164800)
'26-4-2017
Debug.Print UnixTimeToDate(1511958343)
'29-11-2017 12:25:43

' Declare a two dimensional array
' Fill the array with text made up of i and j values
Dim TestArr(1 To 3, 1 To 2) As Variant
Dim i As Long, j As Long
For i = LBound(TestArr) To UBound(TestArr)
For j = LBound(TestArr, 2) To UBound(TestArr, 2)
TestArr(i, j) = CStr(i) & ":" & CStr(j)
Next j
Next i
FlipArr = TransposeArr(TestArr)
Debug.Print TestArr(1, 2)
Debug.Print FlipArr(2, 1)


Debug.Print URLEncode("http://www.github.com/")
'http%3A%2F%2Fwww.github.com%2F
Debug.Print URLEncode("https://github.com/search?q=crypto_vba&type=")
'https%3A%2F%2Fgithub.com%2Fsearch%3Fq%3Dcrypto_vba%26type%3D

End Sub

Function DateToUnixTime(dt) As Long
DateToUnixTime = DateDiff("s", "1/1/1970", dt)
End Function
Expand Down
2 changes: 1 addition & 1 deletion ModHash.bas
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Function ComputeHash_C(Meth As String, ByVal clearText As String, ByVal Key As S
ElseIf Meth = "SHA256" Then
Set SHAhasher = CreateObject("System.Security.Cryptography.SHA256Managed")
ElseIf Meth = "SHA384" Then
Set SHAhasher = CreateObject("System.Security.Cryptography.SHA256Managed")
Set SHAhasher = CreateObject("System.Security.Cryptography.SHA384Managed")
ElseIf Meth = "MD5" Then
Set SHAhasher = CreateObject("System.Security.Cryptography.MD5CryptoServiceProvider")
Else
Expand Down
32 changes: 27 additions & 5 deletions ModJSON.bas
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ Next
'4 -- 3 -- 1 -- 1924,41 -- VAL
'4 -- 4 -- 1 -- 1924,41522 -- VAL


End Sub

Sub TestArrayTable()
Expand Down Expand Up @@ -182,24 +181,46 @@ Debug.Print Tbl(4, 4)
'1924,41
'Sht.Range("B21").Resize(UBound(Tbl, 2), UBound(Tbl, 1)) = WorksheetFunction.Transpose(Tbl)


'Poloniex deposit/withdrawal
'Poloniex deposit/withdrawal, no header output
JsonResponse = "{""deposits"":[{""currency"":""BTC"",""address"":""DEP1"",""amount"":""0.01006132"",""confirmations"":10,""txid"":""17f819a91369a9ff6c4a34216d434597cfc1b4a3d0489b46bd6f924137a47701"",""timestamp"":1399305798,""status"":""COMPLETE""},{""currency"":""BTC"",""address"":""DEP2"",""amount"":""0.00404104"",""confirmations"":10,""txid"":""7acb90965b252e55a894b535ef0b0b65f45821f2899e4a379d3e43799604695c"",""timestamp"":1399245916,""status"":""COMPLETE""}],""withdrawals"":[{""withdrawalNumber"":134933,""currency"":""BTC"",""address"":""1N2i5n8DwTGzUq2Vmn9TUL8J1vdr1XBDFg"",""amount"":""5.00010000"", ""timestamp"":1399267904,""status"":""COMPLETE: 36e483efa6aff9fd53a235177579d98451c4eb237c210e66cd2b9a2d4a988f8e"",""ipAddress"":""IP192""}]}"
Set Json = JsonConverter.ParseJson(JsonResponse)
ResArr = JsonToArray(Json)
Tbl = ArrayTable(ResArr, False)
Debug.Print Tbl(1, 2)
Debug.Print Tbl(4, 2)
'deposits
'DEP2

'Remove last element
'Test no header reply
JsonResponse = "{""error"":[],""result"":{""XXBTZEUR"":[[1492606800,""1121.990"",""1124.912"",""1119.680"",""1124.912"",""1122.345"",""352.76808800"",602],[1492610400,""1124.499"",""1124.980"",""1119.680"",""1122.000"",""1122.194"",""218.62127780"",713],[1492614000,""1121.311"",""1122.900"",""1120.501"",""1122.899"",""1122.266"",""445.46426003"",851],[1492617600,""1122.894"",""1124.499"",""1120.710"",""1123.291"",""1123.068"",""253.55336370"",860],[1492621200,""1124.406"",""1126.000"",""1123.017"",""1125.990"",""1124.775"",""234.27612705"",918],[1492624800,""1125.610"",""1126.231"",""1123.010"",""1126.229"",""1125.453"",""243.42246123"",772]],""last"":1495191600}}"
Set Json = JsonConverter.ParseJson(JsonResponse)
Set JsonRes = Json("result")
ResArr = JsonToArray(Json)
Tbl = ArrayTable(ResArr, False)
Debug.Print Tbl(1, 2)
Debug.Print Tbl(4, 4)
'result
'1492617600

'Empty data set returned 1
JsonResponse = "{""success"":true,""message"":"""",""result"":[]}"
Set Json = JsonConverter.ParseJson(JsonResponse)
ResArr = JsonToArray(Json)
Tbl = ArrayTable(ResArr, True)
Debug.Print Tbl(1, 2)
Debug.Print Tbl(3, 2)
'Waar
'0

'Empty data set returned 2
JsonResponse = "{""success"":false,""message"":""APISIGN_NOT_PROVIDED"",""result"":null}"
Set Json = JsonConverter.ParseJson(JsonResponse)
ResArr = JsonToArray(Json)
Tbl = ArrayTable(ResArr, True)
Debug.Print Tbl(1, 2)
Debug.Print Tbl(2, 2)
'Onwaar
'APISIGN_NOT_PROVIDED

End Sub

Expand Down Expand Up @@ -367,7 +388,8 @@ For rw = LBound(ArrIn, 2) To UBound(ArrIn, 2)
Lvl = Val(ArrIn(1, rw))
If Lvl < MaxD And Lvl > 0 Then
TblHeaders.Add "GROUP_" & Lvl, "GROUP_" & Lvl
ElseIf Lvl = MaxD And ArrIn(5, rw) = "VAL" Then
'ElseIf Lvl = MaxD And ArrIn(5, rw) = "VAL" Then
ElseIf Lvl = MaxD Then
If Val(ArrIn(3, rw)) > 0 Then
TblHeaders.Add "VAL_" & ArrIn(3, rw), "VAL_" & ArrIn(3, rw)
Else
Expand Down
Loading

0 comments on commit 11c0282

Please sign in to comment.