Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #53

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ type sendMsg struct {
// NewClient make ws client
func NewClient(address string, wsAddress string) (Client, error) {
c := new(client)
c.version = "v1.2.6"
c.version = "v1.2.7"
c.address = address
c.wsAddress = wsAddress

Expand Down Expand Up @@ -672,12 +672,21 @@ func (c *client) TxSearch(search *Search) (txs []*Transaction, totalCount uint64
_ = resp.Body.Close()
}()

var b []byte

if resp.StatusCode != 200 {
err = errors.New("transaction can not be broadcast")
return nil, 0, err
}
b, err = io.ReadAll(resp.Body)
if err != nil {
return nil, 0, err
}

var b []byte
var errorResponse Response
if err = json.Unmarshal(b, &errorResponse); err != nil {
return nil, 0, err
}

return nil, 0, errors.New(errorResponse.Detail)
}

b, err = io.ReadAll(resp.Body)
if err != nil {
Expand Down
23 changes: 20 additions & 3 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const (
type Search struct {
HeightOperator HeightOperator `json:"-"`
Height uint64 `json:"-"`
height string `json:"height,omitempty"`
PHeight string `json:"height,omitempty"`
RecipientAddresses []string `json:"recipient_addrs,omitempty"`
SenderAddresses []string `json:"sender_addrs,omitempty"`
Hashes []string `json:"hashes,omitempty"`
Expand Down Expand Up @@ -107,9 +107,18 @@ func (s *Search) IsValid() bool {
}

func (s *Search) ToJSON() ([]byte, error) {
s.height = fmt.Sprintf("%s %d", s.HeightOperator, s.Height)
s.PHeight = fmt.Sprintf("%s %d", s.HeightOperator, s.Height)

return json.Marshal(s)
buf := bytes.NewBuffer([]byte{})
enc := json.NewEncoder(buf)

enc.SetEscapeHTML(false)

if err := enc.Encode(s); err != nil {
return nil, err
}

return buf.Bytes(), nil
}

func (s *Search) ToRequest() (*http.Request, error) {
Expand All @@ -125,3 +134,11 @@ type SearchResponse struct {
TXS []*Transaction `json:"data"`
TotalCount uint64 `json:"total_count"`
}

type Response struct {
Data interface{} `json:"data"`
TotalCount uint64 `json:"total_count"`
Error bool `json:"error"`
Errors map[string]string `json:"errors"`
Detail string `json:"detail"`
}