Skip to content

Commit 9d33d62

Browse files
committed
Added UrlEncode
Added UrlEncode function as that's not available in Excel 2010-.
1 parent a14ee94 commit 9d33d62

File tree

6 files changed

+50
-18
lines changed

6 files changed

+50
-18
lines changed

ModExchCryptopia.bas

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ Function PrivateCryptopia(Method As String, apikey As String, secretkey As Strin
5353

5454
Dim NonceUnique As String
5555
Dim postdata As String
56+
Dim Url As String
5657

5758
'Cryptopia nonce
5859
NonceUnique = DateDiff("s", "1/1/1970", Now)
5960

6061
TradeApiSite = "https://www.cryptopia.co.nz"
6162
urlPath = "/api/"
6263
Url = TradeApiSite & urlPath & Method
63-
UrlEnc = LCase(WorksheetFunction.EncodeURL(Url))
64+
UrlEnc = LCase(URLEncode(Url))
6465

6566
postdata = MethodOptions '{"Currency":""}
6667
postdataJsonTxt = Replace(postdata, "=", Chr(34) & ":" & Chr(34))

ModFunctions.bas

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,42 @@ Function TransposeArr(ArrIn As Variant)
2222
Dim TempArr As Variant
2323

2424
ReDim TempArr(1 To UBound(ArrIn, 2), 1 To UBound(ArrIn, 1))
25-
For I = 1 To UBound(ArrIn, 2)
25+
For i = 1 To UBound(ArrIn, 2)
2626
For j = 1 To UBound(ArrIn, 1)
27-
TempArr(I, j) = ArrIn(j, I)
27+
TempArr(i, j) = ArrIn(j, i)
2828
Next
2929
Next
3030

3131
TransposeArr = TempArr
3232

3333
End Function
34+
35+
Public Function URLEncode(StringVal As String, Optional SpaceAsPlus As Boolean = False) As String
36+
'https://stackoverflow.com/questions/218181/how-can-i-url-encode-a-string-in-excel-vba
37+
Dim StringLen As Long: StringLen = Len(StringVal)
38+
39+
If StringLen > 0 Then
40+
ReDim result(StringLen) As String
41+
Dim i As Long, CharCode As Integer
42+
Dim Char As String, Space As String
43+
44+
If SpaceAsPlus Then Space = "+" Else Space = "%20"
45+
46+
For i = 1 To StringLen
47+
Char = Mid$(StringVal, i, 1)
48+
CharCode = Asc(Char)
49+
Select Case CharCode
50+
Case 97 To 122, 65 To 90, 48 To 57, 45, 46, 95, 126
51+
result(i) = Char
52+
Case 32
53+
result(i) = Space
54+
Case 0 To 15
55+
result(i) = "%0" & Hex(CharCode)
56+
Case Else
57+
result(i) = "%" & Hex(CharCode)
58+
End Select
59+
Next i
60+
URLEncode = Join(result, "")
61+
End If
62+
End Function
63+

ModHash.bas

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,15 @@ Function Base64Encode(inData)
183183
'rfc1521
184184
'2001 Antonin Foller, Motobit Software, http://Motobit.cz
185185
Const Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
186-
Dim cOut, sOut, I
186+
Dim cOut, sOut, i
187187

188188
'For each group of 3 bytes
189-
For I = 1 To Len(inData) Step 3
189+
For i = 1 To Len(inData) Step 3
190190
Dim nGroup, pOut, sGroup
191191

192192
'Create one long from this 3 bytes.
193-
nGroup = &H10000 * Asc(Mid(inData, I, 1)) + _
194-
&H100 * MyASC(Mid(inData, I + 1, 1)) + MyASC(Mid(inData, I + 2, 1))
193+
nGroup = &H10000 * Asc(Mid(inData, i, 1)) + _
194+
&H100 * MyASC(Mid(inData, i + 1, 1)) + MyASC(Mid(inData, i + 2, 1))
195195

196196
'Oct splits the long To 8 groups with 3 bits
197197
nGroup = Oct(nGroup)

ModJSON.bas

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -213,19 +213,19 @@ Function MaxDepth(ObjIn As Object, Optional MaxLvl As Integer = 1, Optional Node
213213
If TypeName(ObjIn) = "Collection" Then
214214
'arrays ([]) to collections, arrays only have values
215215
Set CollIn = ObjIn
216-
For I = 1 To CollIn.Count
216+
For i = 1 To CollIn.Count
217217
'item could be value, object or array, determine:
218218
Set iO = Nothing
219219
On Error Resume Next
220-
Set iO = CollIn(I)
220+
Set iO = CollIn(i)
221221
On Error GoTo 0
222222

223223
'item/value
224224
If Not (iO Is Nothing) Then
225225
If NodeLvl + 1 > MaxLvl Then MaxLvl = NodeLvl + 1
226226
NextLvl = MaxDepth(iO, MaxLvl, NodeLvl + 1)
227227
End If
228-
Next I
228+
Next i
229229
ElseIf TypeName(ObjIn) = "Dictionary" Then
230230
'objects ({}) to dictionaries, Objects have key:values
231231
Set DictIn = ObjIn
@@ -273,13 +273,13 @@ Function JsonToArray(ObjIn As Object, Optional ParentKey As String = "MAIN", Opt
273273
If TypeName(ObjIn) = "Collection" Then
274274
'arrays ([]) to collections, arrays only have values
275275
Set CollIn = ObjIn
276-
For I = 1 To CollIn.Count
276+
For i = 1 To CollIn.Count
277277
'item could be value, object or array, determine:
278278
iV = ""
279279
Set iO = Nothing
280280
On Error Resume Next
281-
iV = CollIn(I)
282-
Set iO = CollIn(I)
281+
iV = CollIn(i)
282+
Set iO = CollIn(i)
283283
On Error GoTo 0
284284

285285
'item/value
@@ -288,23 +288,23 @@ Function JsonToArray(ObjIn As Object, Optional ParentKey As String = "MAIN", Opt
288288
ReDim Preserve ResArr(1 To 5, 1 To UBound(ResArr, 2) + 1)
289289
ResArr(1, UBound(ResArr, 2)) = NodeLvl
290290
ResArr(2, UBound(ResArr, 2)) = ParentKey
291-
ResArr(3, UBound(ResArr, 2)) = I
291+
ResArr(3, UBound(ResArr, 2)) = i
292292
ResArr(4, UBound(ResArr, 2)) = iO.Count
293293
ResArr(5, UBound(ResArr, 2)) = "OBJ"
294294
'Debug.Print "LVL: " & NodeLvl & ", PARENT: " & ParentKey & " , KEY: " & I & " VALUE: count: " & iO.Count & " , TYPE:OBJ"
295-
ParentKey = I
296-
NextLvl = JsonToArray(iO, str(I), NodeLvl + 1, ResArr)
295+
ParentKey = i
296+
NextLvl = JsonToArray(iO, str(i), NodeLvl + 1, ResArr)
297297
Else
298298
'item, write simple value
299299
'Debug.Print "LVL: " & NodeLvl & ", PARENT: " & ParentKey & " , KEY: " & I & " VALUE:" & iV & " , TYPE:VAL"
300300
ReDim Preserve ResArr(1 To 5, 1 To UBound(ResArr, 2) + 1)
301301
ResArr(1, UBound(ResArr, 2)) = NodeLvl
302302
ResArr(2, UBound(ResArr, 2)) = ParentKey
303-
ResArr(3, UBound(ResArr, 2)) = I
303+
ResArr(3, UBound(ResArr, 2)) = i
304304
ResArr(4, UBound(ResArr, 2)) = iV
305305
ResArr(5, UBound(ResArr, 2)) = "VAL"
306306
End If
307-
Next I
307+
Next i
308308
ElseIf TypeName(ObjIn) = "Dictionary" Then
309309
'objects ({}) to dictionaries, Objects have key:values
310310
Set DictIn = ObjIn

crypto_vba_example.xlsm

1.74 KB
Binary file not shown.

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Import the .bas files you need or simply take the sample Excel file. In the modu
4343
- Build the Coinone API connector
4444
- Build the Cryptopia API connector
4545
- Build a working and tested VBA hash function
46+
- Added the UrlEncode function for Cryptopia
4647

4748
# Donate
4849
If this project/the Excel saves you a lot of programming time, consider sending me a coffee or a beer:<br/>

0 commit comments

Comments
 (0)