forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
effects.go
158 lines (141 loc) · 5.25 KB
/
effects.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package resourceadapter
import (
"context"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/services/horizon/internal/httpx"
"github.com/stellar/go/support/render/hal"
"github.com/stellar/go/protocols/horizon/base"
"github.com/stellar/go/protocols/horizon/effects"
)
var EffectTypeNames = map[history.EffectType]string{
history.EffectAccountCreated: "account_created",
history.EffectAccountRemoved: "account_removed",
history.EffectAccountCredited: "account_credited",
history.EffectAccountDebited: "account_debited",
history.EffectAccountThresholdsUpdated: "account_thresholds_updated",
history.EffectAccountHomeDomainUpdated: "account_home_domain_updated",
history.EffectAccountFlagsUpdated: "account_flags_updated",
history.EffectAccountInflationDestinationUpdated: "account_inflation_destination_updated",
history.EffectSignerCreated: "signer_created",
history.EffectSignerRemoved: "signer_removed",
history.EffectSignerUpdated: "signer_updated",
history.EffectTrustlineCreated: "trustline_created",
history.EffectTrustlineRemoved: "trustline_removed",
history.EffectTrustlineUpdated: "trustline_updated",
history.EffectTrustlineAuthorized: "trustline_authorized",
history.EffectTrustlineDeauthorized: "trustline_deauthorized",
history.EffectOfferCreated: "offer_created",
history.EffectOfferRemoved: "offer_removed",
history.EffectOfferUpdated: "offer_updated",
history.EffectTrade: "trade",
history.EffectDataCreated: "data_created",
history.EffectDataRemoved: "data_removed",
history.EffectDataUpdated: "data_updated",
history.EffectSequenceBumped: "sequence_bumped",
}
// NewEffect creates a new effect resource from the provided database representation
// of the effect.
func NewEffect(
ctx context.Context,
row history.Effect,
ledger history.Ledger,
) (result hal.Pageable, err error) {
basev := effects.Base{}
PopulateBaseEffect(ctx, &basev, row, ledger)
switch row.Type {
case history.EffectAccountCreated:
e := effects.AccountCreated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectAccountCredited:
e := effects.AccountCredited{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectAccountDebited:
e := effects.AccountDebited{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectAccountThresholdsUpdated:
e := effects.AccountThresholdsUpdated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectAccountHomeDomainUpdated:
e := effects.AccountHomeDomainUpdated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectAccountFlagsUpdated:
e := effects.AccountFlagsUpdated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectSignerCreated:
e := effects.SignerCreated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectSignerUpdated:
e := effects.SignerUpdated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectSignerRemoved:
e := effects.SignerRemoved{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectTrustlineCreated:
e := effects.TrustlineCreated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectTrustlineUpdated:
e := effects.TrustlineUpdated{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectTrustlineRemoved:
e := effects.TrustlineRemoved{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectTrustlineAuthorized:
e := effects.TrustlineAuthorized{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectTrustlineDeauthorized:
e := effects.TrustlineDeauthorized{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectTrade:
e := effects.Trade{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
case history.EffectSequenceBumped:
e := effects.SequenceBumped{Base: basev}
err = row.UnmarshalDetails(&e)
result = e
default:
result = basev
}
if err != nil {
return
}
rh, ok := result.(base.Rehydratable)
if ok {
err = rh.Rehydrate()
}
return
}
// Populate loads this resource from `row`
func PopulateBaseEffect(ctx context.Context, this *effects.Base, row history.Effect, ledger history.Ledger) {
this.ID = row.ID()
this.PT = row.PagingToken()
this.Account = row.Account
populateEffectType(this, row)
this.LedgerCloseTime = ledger.ClosedAt
lb := hal.LinkBuilder{httpx.BaseURL(ctx)}
this.Links.Operation = lb.Linkf("/operations/%d", row.HistoryOperationID)
this.Links.Succeeds = lb.Linkf("/effects?order=desc&cursor=%s", this.PT)
this.Links.Precedes = lb.Linkf("/effects?order=asc&cursor=%s", this.PT)
}
func populateEffectType(this *effects.Base, row history.Effect) {
var ok bool
this.TypeI = int32(row.Type)
this.Type, ok = EffectTypeNames[row.Type]
if !ok {
this.Type = "unknown"
}
}