-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_entry.go
128 lines (111 loc) · 3.83 KB
/
account_entry.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package resourceadapter
import (
"context"
"fmt"
protocol "github.com/shantanu-hashcash/go/protocols/aurora"
auroraContext "github.com/shantanu-hashcash/go/services/aurora/internal/context"
"github.com/shantanu-hashcash/go/services/aurora/internal/db2/history"
"github.com/shantanu-hashcash/go/support/errors"
"github.com/shantanu-hashcash/go/support/render/hal"
"github.com/shantanu-hashcash/go/xdr"
)
// PopulateAccountEntry fills out the resource's fields
func PopulateAccountEntry(
ctx context.Context,
dest *protocol.Account,
account history.AccountEntry,
accountData []history.Data,
accountSigners []history.AccountSigner,
trustLines []history.TrustLine,
ledger *history.Ledger,
) error {
dest.ID = account.AccountID
dest.PT = account.AccountID
dest.AccountID = account.AccountID
dest.Sequence = account.SequenceNumber
if account.SequenceLedger.Valid && account.SequenceTime.Valid {
dest.SequenceLedger = uint32(account.SequenceLedger.Int64)
dest.SequenceTime = fmt.Sprintf("%d", account.SequenceTime.Int64)
}
dest.SubentryCount = int32(account.NumSubEntries)
dest.InflationDestination = account.InflationDestination
dest.HomeDomain = account.HomeDomain
dest.LastModifiedLedger = account.LastModifiedLedger
if ledger != nil {
dest.LastModifiedTime = &ledger.ClosedAt
}
dest.Flags.AuthRequired = account.IsAuthRequired()
dest.Flags.AuthRevocable = account.IsAuthRevocable()
dest.Flags.AuthImmutable = account.IsAuthImmutable()
dest.Flags.AuthClawbackEnabled = account.IsAuthClawbackEnabled()
dest.Thresholds.LowThreshold = account.ThresholdLow
dest.Thresholds.MedThreshold = account.ThresholdMedium
dest.Thresholds.HighThreshold = account.ThresholdHigh
// populate balances
dest.Balances = make([]protocol.Balance, len(trustLines)+1)
for i, tl := range trustLines {
var err error
switch tl.AssetType {
case xdr.AssetTypeAssetTypePoolShare:
err = PopulatePoolShareBalance(&dest.Balances[i], tl)
default:
err = PopulateAssetBalance(&dest.Balances[i], tl)
}
if err != nil {
return errors.Wrap(err, "populating balance")
}
}
// add native balance
err := PopulateNativeBalance(
&dest.Balances[len(dest.Balances)-1],
xdr.Int64(account.Balance),
xdr.Int64(account.BuyingLiabilities),
xdr.Int64(account.SellingLiabilities),
)
if err != nil {
return errors.Wrap(err, "populating native balance")
}
// populate data
dest.Data = make(map[string]string)
for _, d := range accountData {
dest.Data[d.Name] = d.Value.Base64()
}
masterKeyIncluded := false
// populate signers
dest.Signers = make([]protocol.Signer, len(accountSigners))
for i, signer := range accountSigners {
dest.Signers[i].Weight = signer.Weight
dest.Signers[i].Key = signer.Signer
dest.Signers[i].Type = protocol.MustKeyTypeFromAddress(signer.Signer)
if signer.Sponsor.Valid {
dest.Signers[i].Sponsor = signer.Sponsor.String
}
if account.AccountID == signer.Signer {
masterKeyIncluded = true
}
}
if !masterKeyIncluded {
dest.Signers = append(dest.Signers, protocol.Signer{
Weight: int32(account.MasterWeight),
Key: account.AccountID,
Type: protocol.MustKeyTypeFromAddress(account.AccountID),
})
}
dest.NumSponsoring = account.NumSponsoring
dest.NumSponsored = account.NumSponsored
if account.Sponsor.Valid {
dest.Sponsor = account.Sponsor.String
}
lb := hal.LinkBuilder{auroraContext.BaseURL(ctx)}
self := fmt.Sprintf("/accounts/%s", account.AccountID)
dest.Links.Self = lb.Link(self)
dest.Links.Transactions = lb.PagedLink(self, "transactions")
dest.Links.Operations = lb.PagedLink(self, "operations")
dest.Links.Payments = lb.PagedLink(self, "payments")
dest.Links.Effects = lb.PagedLink(self, "effects")
dest.Links.Offers = lb.PagedLink(self, "offers")
dest.Links.Trades = lb.PagedLink(self, "trades")
dest.Links.Data = lb.Link(self, "data/{key}")
dest.Links.Data.PopulateTemplated()
return nil
}