-
Notifications
You must be signed in to change notification settings - Fork 39
/
query.go
66 lines (59 loc) · 1.98 KB
/
query.go
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
package stake
import (
"database/sql"
"github.com/ethereum/go-ethereum/common"
)
func QueryCandidates() (candidates Candidates) {
db := getDb()
cond := make(map[string]interface{})
cond["active"] = "Y"
return queryCandidates(db, cond)
}
func QueryCandidateByAddress(address common.Address) *Candidate {
db := getDb()
cond := make(map[string]interface{})
cond["address"] = address.String()
candidates := queryCandidates(db, cond)
if len(candidates) == 0 {
return nil
} else {
return candidates[0]
}
}
func QueryCandidateById(id int64) *Candidate {
db := getDb()
cond := make(map[string]interface{})
cond["id"] = id
candidates := queryCandidates(db, cond)
if len(candidates) == 0 {
return nil
} else {
return candidates[0]
}
}
func QueryDelegationsByAddress(delegatorAddress common.Address) (delegations []*Delegation) {
db := getDb()
cond := make(map[string]interface{})
cond["delegator_address"] = delegatorAddress.String()
return queryDelegations(db, cond)
}
func queryCandidates(db *sql.DB, cond map[string]interface{}) (candidates Candidates) {
clause, params := buildQueryClause(cond)
rows, err := db.Query("select id, pub_key, address, shares, voting_power, pending_voting_power, max_shares, comp_rate, name, website, location, profile, email, verified, active, block_height, rank, state, num_of_delegators, created_at from candidates"+clause, params...)
if err != nil {
panic(err)
}
defer rows.Close()
candidates = composeCandidateResults(rows)
return
}
func queryDelegations(db *sql.DB, cond map[string]interface{}) (delegations []*Delegation) {
clause, params := buildQueryClause(cond)
rows, err := db.Query("select id, delegator_address, candidate_id, delegate_amount, award_amount, withdraw_amount, pending_withdraw_amount, slash_amount, comp_rate, voting_power, state, block_height, average_staking_date, created_at from delegations"+clause, params...)
if err != nil {
panic(err)
}
defer rows.Close()
delegations = composeDelegationResults(rows)
return
}