/
main.go
111 lines (89 loc) · 3.59 KB
/
main.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
// Package orbit provides client access to a orbit server, allowing an
// application to post transactions and lookup ledger information.
//
// Create an instance of `Client` to customize the server used, or alternatively
// use `DefaultTestNetClient` or `DefaultPublicNetClient` to access the SDF run
// orbit servers.
package orbit
import (
"context"
"net/http"
"net/url"
"sync"
"github.com/laxmicoinofficial/go/build"
"github.com/laxmicoinofficial/go/support/errors"
)
// DefaultTestNetClient is a default client to connect to test network
var DefaultTestNetClient = &Client{
URL: "https://orbit-testnet.rover.network",
HTTP: http.DefaultClient,
}
// DefaultPublicNetClient is a default client to connect to public network
var DefaultPublicNetClient = &Client{
URL: "https://orbit.rover.network",
HTTP: http.DefaultClient,
}
// At is a paging parameter that can be used to override the URL loaded in a
// remote method call to orbit.
type At string
// Cursor represents `cursor` param in queries
type Cursor string
// Limit represents `limit` param in queries
type Limit uint
// Order represents `order` param in queries
type Order string
const (
OrderAsc Order = "asc"
OrderDesc Order = "desc"
)
var (
// ErrResultCodesNotPopulated is the error returned from a call to
// ResultCodes() against a `Problem` value that doesn't have the
// "result_codes" extra field populated when it is expected to be.
ErrResultCodesNotPopulated = errors.New("result_codes not populated")
// ErrEnvelopeNotPopulated is the error returned from a call to
// Envelope() against a `Problem` value that doesn't have the
// "envelope_xdr" extra field populated when it is expected to be.
ErrEnvelopeNotPopulated = errors.New("envelope_xdr not populated")
)
// Client struct contains data required to connect to Orbit instance
type Client struct {
// URL of Orbit server to connect
URL string
// HTTP client to make requests with
HTTP HTTP
fixURLOnce sync.Once
}
type ClientInterface interface {
Root() (Root, error)
HomeDomainForAccount(aid string) (string, error)
LoadAccount(accountID string) (Account, error)
LoadAccountOffers(accountID string, params ...interface{}) (offers OffersPage, err error)
LoadMemo(p *Payment) error
LoadOrderBook(selling Asset, buying Asset, params ...interface{}) (orderBook OrderBookSummary, err error)
StreamLedgers(ctx context.Context, cursor *Cursor, handler LedgerHandler) error
StreamPayments(ctx context.Context, accountID string, cursor *Cursor, handler PaymentHandler) error
StreamTransactions(ctx context.Context, accountID string, cursor *Cursor, handler TransactionHandler) error
SubmitTransaction(txeBase64 string) (TransactionSuccess, error)
}
// Error struct contains the problem returned by Orbit
type Error struct {
Response *http.Response
Problem Problem
}
// HTTP represents the HTTP client that a orbit client uses to communicate
type HTTP interface {
Do(req *http.Request) (resp *http.Response, err error)
Get(url string) (resp *http.Response, err error)
PostForm(url string, data url.Values) (resp *http.Response, err error)
}
// LedgerHandler is a function that is called when a new ledger is received
type LedgerHandler func(Ledger)
// PaymentHandler is a function that is called when a new payment is received
type PaymentHandler func(Payment)
// TransactionHandler is a function that is called when a new transaction is received
type TransactionHandler func(Transaction)
// ensure that the orbit client can be used as a SequenceProvider
var _ build.SequenceProvider = &Client{}
// ensure that the orbit client implements ClientInterface
var _ ClientInterface = &Client{}