Skip to content

Commit

Permalink
voting power, url from comment permlink, create suggest password, esp…
Browse files Browse the repository at this point in the history
… etc
  • Loading branch information
SteakOverCooked authored and SteakOverCooked committed May 21, 2018
1 parent 1984fb4 commit 6586ee4
Show file tree
Hide file tree
Showing 7 changed files with 192 additions and 16 deletions.
4 changes: 2 additions & 2 deletions lib/formatter.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ Class Formatter
v = -v
End If
Reputation = v * 9 + 25
End Function
End Function

End Class
End Class
97 changes: 96 additions & 1 deletion lib/steem.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Class Steem
Private iNode
Private ErrorMessage
Private CachedAccountData
Private UseCache

' class constructor with parameters
Public Default Function Init(API_Node)
Expand Down Expand Up @@ -47,8 +48,19 @@ Class Steem
Node = DefaultSteemAPINode
ErrorMessage = ""
Set CachedAccountData = Nothing
Cache = True
End Sub

' should we use cache
Public Property Let Cache(ByVal v)
UseCache = v
End Property

' should we use cache
Public Property Get Cache
Cache = UseCache
End Property

' cached account
Public Property Get CachedAccount
CachedAccount = CachedAccountData
Expand Down Expand Up @@ -129,6 +141,11 @@ Class Steem

' check cache
Private Function CacheAvailable(id)
' disable cache manually
If Cache = False Then
CacheAvailable = False
Exit Function
End If
If CachedAccountData is Nothing Then
CacheAvailable = False
Exit Function
Expand Down Expand Up @@ -167,5 +184,83 @@ Class Steem
Set o = Nothing
Set json = Nothing
End Function


' get voting power
Public Function GetAccount_VotingPower(id)
Dim acc
If CacheAvailable(id) Then
Set acc = CachedAccountData
Else
Set acc = GetAccount(id)
End If
Dim vp, last_vote_time, sec
vp = acc("voting_power")
last_vote_time = Replace(acc("last_vote_time"), "T", " ")
sec = DateDiff("s", last_vote_time, Now)
Dim regen
regen = sec * 10000 / 86400 / 5
Dim total_vp
total_vp = (vp + regen) / 100
If total_vp >= 100 Then
total_vp = 100
End If
GetAccount_VotingPower = total_vp
End Function

' get vesting shares
Public Function GetAccount_VestingShares(id)
Dim acc
If CacheAvailable(id) Then
Set acc = CachedAccountData
Else
Set acc = GetAccount(id)
End If
GetAccount_VestingShares = Replace(acc("vesting_shares"), " VESTS", "")
End Function

' get delegated vesting shares
Public Function GetAccount_DelegatedVestingShares(id)
Dim acc
If CacheAvailable(id) Then
Set acc = CachedAccountData
Else
Set acc = GetAccount(id)
End If
GetAccount_DelegatedVestingShares = Replace(acc("delegated_vesting_shares"), " VESTS", "")
End Function

' get received_vesting_shares
Public Function GetAccount_ReceivedVestingShares(id)
Dim acc
If CacheAvailable(id) Then
Set acc = CachedAccountData
Else
Set acc = GetAccount(id)
End If
GetAccount_ReceivedVestingShares = Replace(acc("received_vesting_shares"), " VESTS", "")
End Function

' convert vests to sp
Public Function VestsToSp(vests)
'TODO, use real data
VestsToSp = vests / 2038
End Function

' get effective sp
Public Function GetAccount_EffectiveSteemPower(id)
Dim vests, vests_plus, vests_minus
Dim sp, sp_plus, sp_minus
' account owns
vests = GetAccount_VestingShares(id)
' received
vests_plus = GetAccount_ReceivedVestingShares(id)
' delegated
vests_minus = GetAccount_DelegatedVestingShares(id)
' convert to steem power
sp = VestsToSp(vests)
sp_plus = VestsToSp(vests_plus)
sp_minus = VestsToSp(vests_minus)
' simple math
GetAccount_EffectiveSteemPower = sp + sp_plus - sp_minus
End Function
End Class
62 changes: 49 additions & 13 deletions lib/utility.vbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
Option Explicit

Class Utility
Function ValidateAccountName(value)

Public Function ValidateAccountName(value)
Dim i, label, length, suffix
suffix = "Account name should "

Expand All @@ -25,13 +26,13 @@ Class Utility

Dim Re
Set Re = New RegExp
With re
With Re
.Pattern = "\."
.IgnoreCase = False
.Global = False
End With

If re.Test(value) Then
If Re.Test(value) Then
suffix = "Each account segment should "
End If

Expand All @@ -42,26 +43,26 @@ Class Utility
For i = 0 to length
label = ref(i)

re.Pattern = "^[a-z]"
If Not re.test(label) Then
Re.Pattern = "^[a-z]"
If Not Re.test(label) Then
ValidateAccountName = suffix + "start with a letter."
Exit Function
End If

re.Pattern = "^[a-z0-9-]*$"
If Not re.test(label) Then
Re.Pattern = "^[a-z0-9-]*$"
If Not Re.test(label) Then
ValidateAccountName = suffix + "have only letters, digits, or dashes."
Exit Function
End If

re.Pattern = "--"
If re.test(label) Then
Re.Pattern = "--"
If Re.test(label) Then
ValidateAccountName = suffix + "have only one dash in a row."
Exit Function
End If

re.Pattern = "[a-z0-9]$"
If Not re.test(label) Then
Re.Pattern = "[a-z0-9]$"
If Not Re.test(label) Then
ValidateAccountName = suffix + "end with a letter or digit."
Exit Function
End If
Expand All @@ -75,15 +76,50 @@ Class Utility
ValidateAccountName = Empty
End Function

' Check value in Array
Public Function InArray(needle, haystack)
InArray = False
needle = Trim(needle)
Dim hay
For Each hay in haystack
If trim(hay) = needle Then
If Trim(hay) = needle Then
InArray = True
Exit For
End If
Next
End Function
End Class

' Get original URL from comment permlink
Public Function GetUrlFromCommentPermLink(url)
Dim Author, Link, Re
Set Re = New RegExp
With Re
.Pattern = "(re-\w+-)*((\w+\-)*)"
.Global = False
End With
Dim my
Set my = Re.Execute(url)
If (my.Count >= 1) Then
Author = Split(my(0).submatches(0), "-")(1)
Link = Mid(my(0).submatches(1), 1, Len(my(0).submatches(1)) - 1)
GetUrlFromCommentPermLink = "https://steemit.com/@" + Author + "/" + Link
Else
GetUrlFromCommentPermLink = Empty
End If
Set Re = Nothing
End Function

' Create suggested password
Public Function CreateSuggestedPassword
Const PASSWORD_LENGTH = 32
Const AlphaBet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
Randomize
Dim i, s, t
s = ""
For i = 1 to PASSWORD_LENGTH
t = Int(Rnd * Len(AlphaBet)) + 1
s = s + Mid(AlphaBet, t, 1)
Next
CreateSuggestedPassword = s
End Function
End Class
12 changes: 12 additions & 0 deletions tests/test_createsuggestedpassword.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
' Test CreateSuggestedPassword

Dim x
Set x = New Utility

AssertEqual Len(x.CreateSuggestedPassword), 32, ""

AssertEqual Len(x.CreateSuggestedPassword), 32, ""

AssertNotEqual x.CreateSuggestedPassword, x.CreateSuggestedPassword, ""

Set x = Nothing
12 changes: 12 additions & 0 deletions tests/test_esp.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
' test GetAccount_EffectiveSteemPower

Dim SteemIt
Set SteemIt = New Steem

Dim esp
esp = SteemIt.GetAccount_EffectiveSteemPower("justyy")

WScript.Echo esp
AssertTrue esp >= 20000, "justyy esp should be larger than 20000"

Set SteemIt = Nothing
10 changes: 10 additions & 0 deletions tests/test_geturlfromcommentpermlink.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
' Test GetUrlFromCommentPermLink

Dim x
Set x = New Utility

AssertEqual x.GetUrlFromCommentPermLink("re-tvb-re-justyy-re-tvb-45qr3w-20171011t144205534z"), "https://steemit.com/@tvb/45qr3w", ""

AssertEqual x.GetUrlFromCommentPermLink("re-justyy-daily-quality-cn-posts-selected-and-rewarded-promo-cn-20180520t153728557z"), "https://steemit.com/@justyy/daily-quality-cn-posts-selected-and-rewarded-promo-cn", ""

Set x = Nothing
11 changes: 11 additions & 0 deletions tests/test_voting_power.vbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
' test GetAccount_VotingPower

Dim SteemIt
Set SteemIt = New Steem

Dim vp
vp = SteemIt.GetAccount_VotingPower("justyy")

AssertTrue vp >= 60 And vp <= 100, "justyy vp should be between 60 and 100"

Set SteemIt = Nothing

0 comments on commit 6586ee4

Please sign in to comment.