-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_tx_status.go
More file actions
83 lines (68 loc) · 2.24 KB
/
get_tx_status.go
File metadata and controls
83 lines (68 loc) · 2.24 KB
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
// Copyright (C) 2021 Creditor Corp. Group.
// See LICENSE for copying information.
package venly
import (
"context"
"encoding/json"
"net/http"
"github.com/zeebo/errs"
)
// GetTXStatusRequest fields that required for get tx status request.
type GetTXStatusRequest struct {
SecretType string
TXHash string
}
// GetTXStatusResponse fields that returns from get tx status.
type GetTXStatusResponse struct {
Success bool `json:"success"`
Result struct {
Hash string `json:"hash"`
Status string `json:"status"`
Confirmations int `json:"confirmations"`
BlockHash string `json:"blockHash"`
BlockNumber int `json:"blockNumber"`
Nonce int `json:"nonce"`
Gas int `json:"gas"`
GasUsed int `json:"gasUsed"`
GasPrice int64 `json:"gasPrice"`
Logs []struct {
LogIndex int `json:"logIndex"`
Data string `json:"data"`
Type interface{} `json:"type"`
Topics []string `json:"topics"`
} `json:"logs"`
From string `json:"from"`
To string `json:"to"`
} `json:"result"`
}
// GetTXStatus retrieves tx status.
func (client *Client) GetTXStatus(ctx context.Context, accessToken string, r GetTXStatusRequest) (response GetTXStatusResponse, err error) {
req, err := http.NewRequest(http.MethodGet, client.config.DefaultURL+"transactions/"+r.SecretType+"/"+r.TXHash+"/status", nil)
if err != nil {
return GetTXStatusResponse{}, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
resp, err := client.http.Do(req.WithContext(ctx))
if err != nil {
return GetTXStatusResponse{}, err
}
defer func() {
err = errs.Combine(err, resp.Body.Close())
}()
if resp.StatusCode != http.StatusOK {
errorResp := ErrorResponse{}
if err = json.NewDecoder(resp.Body).Decode(&errorResp); err != nil {
return GetTXStatusResponse{}, err
}
if !errorResp.Success {
return GetTXStatusResponse{}, errs.New(errorResp.Errors[0].Code)
}
return GetTXStatusResponse{}, errs.New(resp.Status)
}
var getTXStatusResponse GetTXStatusResponse
if err = json.NewDecoder(resp.Body).Decode(&getTXStatusResponse); err != nil {
return GetTXStatusResponse{}, err
}
return getTXStatusResponse, nil
}