forked from stellar/go
/
account.go
71 lines (59 loc) · 1.71 KB
/
account.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
67
68
69
70
71
package core
import (
sq "github.com/Masterminds/squirrel"
"github.com/stellar/go/xdr"
)
// IsAuthRequired returns true if the account has the "AUTH_REQUIRED" option
// turned on.
func (ac Account) IsAuthRequired() bool {
return (ac.Flags & xdr.AccountFlagsAuthRequiredFlag) != 0
}
// IsAuthRevocable returns true if the account has the "AUTH_REVOCABLE" option
// turned on.
func (ac Account) IsAuthRevocable() bool {
return (ac.Flags & xdr.AccountFlagsAuthRevocableFlag) != 0
}
// AccountByAddress loads a row from `accounts`, by address
func (q *Q) AccountByAddress(dest interface{}, addy string) error {
sql := selectAccount.Limit(1).Where("accountid = ?", addy)
return q.Get(dest, sql)
}
// SequencesForAddresses loads the current sequence number for every accountid
// specified in `addys`
func (q *Q) SequencesForAddresses(dest interface{}, addys []string) error {
sql := sq.
Select("seqnum as sequence", "accountid as address").
From("accounts").
Where(sq.Eq{"accountid": addys})
return q.Select(dest, sql)
}
// SequenceProvider returns a new sequence provider.
func (q *Q) SequenceProvider() *SequenceProvider {
return &SequenceProvider{Q: q}
}
// Get implements `txsub.SequenceProvider`
func (sp *SequenceProvider) Get(addys []string) (map[string]uint64, error) {
rows := []struct {
Address string
Sequence uint64
}{}
err := sp.Q.SequencesForAddresses(&rows, addys)
if err != nil {
return nil, err
}
results := make(map[string]uint64)
for _, r := range rows {
results[r.Address] = r.Sequence
}
return results, nil
}
var selectAccount = sq.Select(
"a.accountid",
"a.balance",
"a.seqnum",
"a.numsubentries",
"a.inflationdest",
"a.homedomain",
"a.thresholds",
"a.flags",
).From("accounts a")