-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
52 lines (44 loc) · 1.27 KB
/
account.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
package methods
import (
"context"
"github.com/TosinShada/stellar-core/clients/horizonclient"
"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/code"
"github.com/creachadair/jrpc2/handler"
)
type AccountInfo struct {
ID string `json:"id"`
Sequence int64 `json:"sequence,string"`
}
type AccountRequest struct {
Address string ` json:"address"`
}
type AccountStore struct {
Client *horizonclient.Client
}
func (a AccountStore) GetAccount(request AccountRequest) (AccountInfo, error) {
details, err := a.Client.AccountDetail(horizonclient.AccountRequest{AccountID: request.Address})
if err != nil {
return AccountInfo{}, err
}
return AccountInfo{
ID: details.AccountID,
Sequence: details.Sequence,
}, nil
}
// NewAccountHandler returns a json rpc handler to fetch account info
func NewAccountHandler(store AccountStore) jrpc2.Handler {
return handler.New(func(ctx context.Context, request AccountRequest) (AccountInfo, error) {
response, err := store.GetAccount(request)
if err != nil {
if herr, ok := err.(*horizonclient.Error); ok {
return response, (&jrpc2.Error{
Code: code.InvalidRequest,
Message: herr.Problem.Title,
}).WithData(herr.Problem.Extras)
}
return response, err
}
return response, nil
})
}