forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction_request.go
73 lines (61 loc) · 2.34 KB
/
transaction_request.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
package horizonclient
import (
"context"
"encoding/json"
"fmt"
"net/url"
hProtocol "github.com/stellar/go/protocols/horizon"
"github.com/stellar/go/support/errors"
)
// BuildURL creates the endpoint to be queried based on the data in the TransactionRequest struct.
// If no data is set, it defaults to the build the URL for all transactions
func (tr TransactionRequest) BuildURL() (endpoint string, err error) {
nParams := countParams(tr.ForAccount, tr.ForLedger, tr.forTransactionHash)
if nParams > 1 {
return endpoint, errors.New("invalid request: too many parameters")
}
endpoint = "transactions"
if tr.ForAccount != "" {
endpoint = fmt.Sprintf("accounts/%s/transactions", tr.ForAccount)
}
if tr.ForClaimableBalance != "" {
endpoint = fmt.Sprintf("claimable_balances/%s/transactions", tr.ForClaimableBalance)
}
if tr.ForLedger > 0 {
endpoint = fmt.Sprintf("ledgers/%d/transactions", tr.ForLedger)
}
if tr.forTransactionHash != "" {
endpoint = fmt.Sprintf("transactions/%s", tr.forTransactionHash)
}
queryParams := addQueryParams(cursor(tr.Cursor), limit(tr.Limit), tr.Order,
includeFailed(tr.IncludeFailed))
if queryParams != "" {
endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams)
}
_, err = url.Parse(endpoint)
if err != nil {
err = errors.Wrap(err, "failed to parse endpoint")
}
return endpoint, err
}
// TransactionHandler is a function that is called when a new transaction is received
type TransactionHandler func(hProtocol.Transaction)
// StreamTransactions streams executed transactions. It can be used to stream all transactions and transactions for an account. Use context.WithCancel to stop streaming or context.Background() if you want
// to stream indefinitely. TransactionHandler is a user-supplied function that is executed for each streamed transaction received.
func (tr TransactionRequest) StreamTransactions(ctx context.Context, client *Client,
handler TransactionHandler) (err error) {
endpoint, err := tr.BuildURL()
if err != nil {
return errors.Wrap(err, "unable to build endpoint")
}
url := fmt.Sprintf("%s%s", client.fixHorizonURL(), endpoint)
return client.stream(ctx, url, func(data []byte) error {
var transaction hProtocol.Transaction
err = json.Unmarshal(data, &transaction)
if err != nil {
return errors.Wrap(err, "error unmarshaling data")
}
handler(transaction)
return nil
})
}