Skip to content

Commit b71fa66

Browse files
authored
Merge pull request #36 from LATOKEN/feature-swap-status-by-txhash
Added handler for getting status by txHash
2 parents 6e76286 + e1a5608 commit b71fa66

4 files changed

Lines changed: 45 additions & 0 deletions

File tree

src/app/app.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (a *App) setRouters() {
5555
a.Get("/", a.Endpoints)
5656
a.Get("/status", a.StatusHandler)
5757
a.Get("/status/{destination_chain}/{sender}/{receipt}/{amount}/{tx_hash}", a.SwapStatusHandler)
58+
a.Get("/status/{tx_hash}", a.SwapStatusByTxHashHandler)
5859
// a.Get("/resend_tx/{id}", a.ResendTxHandler)
5960
// a.Get("/set_mode/{mode}", a.SetModeHandler)
6061
}

src/app/handles.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (a *App) Endpoints(w http.ResponseWriter, r *http.Request) {
1919
Endpoints: []string{
2020
"/status",
2121
"/status/{destination_chain}/{sender}/{receipt}/{amount}/{tx_hash}",
22+
"/status/{tx_hash}",
2223
},
2324
}
2425

@@ -49,6 +50,24 @@ func (a *App) SwapStatusHandler(w http.ResponseWriter, r *http.Request) {
4950
common.ResponJSON(w, http.StatusOK, status)
5051
}
5152

53+
// StatusHandler ...
54+
func (a *App) SwapStatusByTxHashHandler(w http.ResponseWriter, r *http.Request) {
55+
txHash := mux.Vars(r)["tx_hash"]
56+
if txHash == "" {
57+
a.logger.Errorf("Empty request(/status/{tx_hash})")
58+
common.ResponJSON(w, http.StatusInternalServerError, createNewError("empty request", ""))
59+
return
60+
}
61+
62+
status, err := a.relayer.GetSwapByTxHashStatus(txHash)
63+
if err != nil {
64+
common.ResponJSON(w, http.StatusNotFound, createNewError("get swap from database", err.Error()))
65+
return
66+
}
67+
68+
common.ResponJSON(w, http.StatusOK, status)
69+
}
70+
5271
// StatusHandler ...
5372
func (a *App) StatusHandler(w http.ResponseWriter, r *http.Request) {
5473
status, err := a.relayer.StatusOfWorkers()

src/service/relayer.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,19 @@ func (r *RelayerSRV) GetSwapStatus(req *models.SwapStatus) (storage.SwapStatus,
8888
return "", nil
8989
}
9090

91+
func (r *RelayerSRV) GetSwapByTxHashStatus(txHash string) (storage.SwapStatus, error) {
92+
swap, err := r.storage.GetSwapByTxHash(txHash)
93+
if err != nil {
94+
r.logger.Errorf("GetSwapByTxHashStatus, txHash: %v, failed with error: %v", txHash, err)
95+
return "", err
96+
}
97+
if swap != nil {
98+
return swap.Status, nil
99+
}
100+
101+
return "", nil
102+
}
103+
91104
// Status ...
92105
func (r *RelayerSRV) StatusOfWorkers() (map[string]*models.WorkerStatus, error) {
93106
// get blockchain heights from workers and from database

src/service/storage/swap.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,18 @@ func (d *DataBase) GetSwapByStatus(swapType SwapType, sender, receipt string, am
4545
return swap, nil
4646
}
4747

48+
// GetSwapByTxHash ...
49+
func (d *DataBase) GetSwapByTxHash(txHash string) (*Swap, error) {
50+
swap := &Swap{}
51+
if err := d.db.Where("tx_hash = ? and status in (?)", txHash,
52+
[]SwapStatus{SwapStatusDepositConfirmed, SwapStatusClaimSent, SwapStatusClaimConfirmed, SwapStatusClaimSentFailed, SwapStatusDepositFailed, SwapStatusPassedConfirmed, SwapStatusPassedSent, SwapStatusSpendSent, SwapStatusSpendConfirmed, SwapStatusRejected}).
53+
Find(&swap).Error; err != nil {
54+
return nil, err
55+
}
56+
57+
return swap, nil
58+
}
59+
4860
// UpdateSwapStatus ...
4961
func (d *DataBase) UpdateSwapStatus(swap *Swap, status SwapStatus, rOutAmount string) {
5062
swap.Status = status

0 commit comments

Comments
 (0)