-
Notifications
You must be signed in to change notification settings - Fork 499
/
transaction.go
158 lines (136 loc) · 4.78 KB
/
transaction.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"
"encoding/base64"
"fmt"
"time"
"github.com/guregu/null"
horizonContext "github.com/stellar/go/services/horizon/internal/context"
"github.com/stellar/go/xdr"
protocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/services/horizon/internal/db2/history"
"github.com/stellar/go/support/render/hal"
)
// Populate fills out the details
func PopulateTransaction(
ctx context.Context,
transactionHash string,
dest *protocol.Transaction,
row history.Transaction,
) error {
dest.ID = transactionHash
dest.PT = row.PagingToken()
dest.Successful = row.Successful
dest.Hash = transactionHash
dest.Ledger = row.LedgerSequence
dest.LedgerCloseTime = row.LedgerCloseTime
dest.Account = row.Account
if row.AccountMuxed.Valid {
dest.AccountMuxed = row.AccountMuxed.String
muxedAccount := xdr.MustMuxedAddress(dest.AccountMuxed)
dest.AccountMuxedID = uint64(muxedAccount.Med25519.Id)
}
dest.AccountSequence = row.AccountSequence
dest.FeeCharged = row.FeeCharged
dest.OperationCount = row.OperationCount
dest.EnvelopeXdr = row.TxEnvelope
dest.ResultXdr = row.TxResult
dest.ResultMetaXdr = row.TxMeta
dest.FeeMetaXdr = row.TxFeeMeta
dest.MemoType = row.MemoType
dest.Memo = row.Memo.String
if row.MemoType == "text" {
if memoBytes, err := memoBytes(row.TxEnvelope); err != nil {
return err
} else {
dest.MemoBytes = memoBytes
}
}
dest.Signatures = row.Signatures
if row.HasPreconditions() {
dest.Preconditions = &protocol.TransactionPreconditions{}
}
if !row.TimeBounds.Null {
// Action needed in release: horizon-v3.0.0: remove ValidBefore and ValidAfter
dest.ValidBefore = timeString(row.TimeBounds.Upper)
dest.ValidAfter = timeString(row.TimeBounds.Lower)
if dest.Preconditions.TimeBounds == nil {
dest.Preconditions.TimeBounds = &protocol.TransactionPreconditionsTimebounds{}
}
dest.Preconditions.TimeBounds.MaxTime = timeString(row.TimeBounds.Upper)
dest.Preconditions.TimeBounds.MinTime = timeString(row.TimeBounds.Lower)
}
if !row.LedgerBounds.Null {
if dest.Preconditions.LedgerBounds == nil {
dest.Preconditions.LedgerBounds = &protocol.TransactionPreconditionsLedgerbounds{}
}
if row.LedgerBounds.MinLedger.Valid {
dest.Preconditions.LedgerBounds.MinLedger = uint32(row.LedgerBounds.MinLedger.Int64)
}
if row.LedgerBounds.MaxLedger.Valid {
dest.Preconditions.LedgerBounds.MaxLedger = uint32(row.LedgerBounds.MaxLedger.Int64)
}
}
if row.MinAccountSequence.Valid {
dest.Preconditions.MinAccountSequence = fmt.Sprint(row.MinAccountSequence.Int64)
}
if row.MinAccountSequenceAge.Valid {
dest.Preconditions.MinAccountSequenceAge = row.MinAccountSequenceAge.String
}
if row.MinAccountSequenceLedgerGap.Valid {
dest.Preconditions.MinAccountSequenceLedgerGap = uint32(row.MinAccountSequenceLedgerGap.Int64)
}
if row.ExtraSigners != nil {
dest.Preconditions.ExtraSigners = row.ExtraSigners
}
if row.InnerTransactionHash.Valid {
dest.FeeAccount = row.FeeAccount.String
if row.FeeAccountMuxed.Valid {
dest.FeeAccountMuxed = row.FeeAccountMuxed.String
muxedAccount := xdr.MustMuxedAddress(dest.FeeAccountMuxed)
dest.FeeAccountMuxedID = uint64(muxedAccount.Med25519.Id)
}
dest.MaxFee = row.NewMaxFee.Int64
dest.FeeBumpTransaction = &protocol.FeeBumpTransaction{
Hash: row.TransactionHash,
Signatures: dest.Signatures,
}
dest.InnerTransaction = &protocol.InnerTransaction{
Hash: row.InnerTransactionHash.String,
MaxFee: row.MaxFee,
Signatures: row.InnerSignatures,
}
if transactionHash != row.TransactionHash {
dest.Signatures = dest.InnerTransaction.Signatures
}
} else {
dest.FeeAccount = dest.Account
dest.FeeAccountMuxed = dest.AccountMuxed
dest.FeeAccountMuxedID = dest.AccountMuxedID
dest.MaxFee = row.MaxFee
}
lb := hal.LinkBuilder{Base: horizonContext.BaseURL(ctx)}
dest.Links.Account = lb.Link("/accounts", dest.Account)
dest.Links.Ledger = lb.Link("/ledgers", fmt.Sprintf("%d", dest.Ledger))
dest.Links.Operations = lb.PagedLink("/transactions", dest.ID, "operations")
dest.Links.Effects = lb.PagedLink("/transactions", dest.ID, "effects")
dest.Links.Self = lb.Link("/transactions", dest.ID)
dest.Links.Transaction = dest.Links.Self
dest.Links.Succeeds = lb.Linkf("/transactions?order=desc&cursor=%s", dest.PT)
dest.Links.Precedes = lb.Linkf("/transactions?order=asc&cursor=%s", dest.PT)
return nil
}
func memoBytes(envelopeXDR string) (string, error) {
var parsedEnvelope xdr.TransactionEnvelope
if err := xdr.SafeUnmarshalBase64(envelopeXDR, &parsedEnvelope); err != nil {
return "", err
}
memo := *parsedEnvelope.Memo().Text
return base64.StdEncoding.EncodeToString([]byte(memo)), nil
}
func timeString(in null.Int) string {
if !in.Valid {
return ""
}
return time.Unix(in.Int64, 0).UTC().Format(time.RFC3339)
}