-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverinfo.go
More file actions
84 lines (75 loc) · 2.22 KB
/
serverinfo.go
File metadata and controls
84 lines (75 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package net
import (
"log"
"github.com/buger/jsonparser"
)
// ServerInfo struct
type ServerInfo struct {
FeeBase int
FeeRef int
DropsPerByte int
LoadBase int
LoadFactor int
LedgerIndex int
TxnSuccess int
TxnFailure int
TxnCount int
// Ledgerhash string
// ServerStatus string
Updated bool
}
//NewServerInfo is constructor
func NewServerInfo() *ServerInfo {
return &ServerInfo{
Updated: false,
DropsPerByte: 976,
FeeRef: 10,
FeeBase: 10,
LoadBase: 256,
LoadFactor: 256,
}
}
//Update update ServerInfo from json result
func (s *ServerInfo) Update(result string) {
s.GetFieldInt(result, &s.FeeBase, "fee_base")
s.GetFieldInt(result, &s.FeeRef, "fee_ref")
s.GetFieldInt(result, &s.DropsPerByte, "drops_per_byte")
s.GetFieldInt(result, &s.LoadBase, "load_base")
s.GetFieldInt(result, &s.LoadFactor, "load_factor")
s.GetFieldInt(result, &s.LedgerIndex, "ledger_index")
s.GetFieldInt(result, &s.TxnSuccess, "txn_success")
s.GetFieldInt(result, &s.TxnFailure, "txn_failure")
s.GetFieldInt(result, &s.TxnCount, "txn_count")
// s.GetFieldString(result, &s.Ledgerhash, "ledger_hash")
// s.GetFieldString(result, &s.ServerStatus, "server_status")
log.Printf("!!!!!!!!!!ServerUpdate ledger_index=%d\n", s.LedgerIndex)
s.Updated = true
}
//GetFieldInt get value from json
func (s *ServerInfo) GetFieldInt(result string, field *int, fieldInJSON string) {
nValue, err := jsonparser.GetInt([]byte(result), fieldInJSON)
if err == nil {
*field = int(nValue)
} else if fieldInJSON == "ledger_index" {
log.Printf("GetFieldInt error for field %s:%s\n", fieldInJSON, result)
}
}
//GetFieldString get value from json
func (s *ServerInfo) GetFieldString(result string, field *string, fieldInJSON string) {
sValue, err := jsonparser.GetString([]byte(result), fieldInJSON)
if err == nil {
*field = sValue
} else {
log.Printf("GetFieldString error for field %s:%s\n", fieldInJSON, result)
}
}
// ComputeFee compute the basic transaction fee
func (s *ServerInfo) ComputeFee() int {
if !s.Updated {
return 0
}
feeUnit := float32(s.FeeBase) / float32(s.FeeRef)
feeUnit *= float32(s.LoadFactor) / float32(s.LoadBase)
fee := int(float32(s.FeeBase) * feeUnit * 1.1)
return fee
}