-
Notifications
You must be signed in to change notification settings - Fork 115
/
gettransactionbyid.go
64 lines (57 loc) · 2.34 KB
/
gettransactionbyid.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
package value
import (
"net/http"
"github.com/labstack/echo"
"github.com/iotaledger/goshimmer/packages/ledgerstate"
"github.com/iotaledger/goshimmer/plugins/messagelayer"
"github.com/iotaledger/goshimmer/plugins/webapi/jsonmodels/value"
)
// getTransactionByIDHandler gets the transaction by id.
func getTransactionByIDHandler(c echo.Context) error {
txID, err := ledgerstate.TransactionIDFromBase58(c.QueryParam("txnID"))
if err != nil {
return c.JSON(http.StatusBadRequest, value.GetTransactionByIDResponse{Error: err.Error()})
}
// get txn by txn id
cachedTxnMetaObj := messagelayer.Tangle().LedgerState.TransactionMetadata(txID)
defer cachedTxnMetaObj.Release()
if !cachedTxnMetaObj.Exists() {
return c.JSON(http.StatusNotFound, value.GetTransactionByIDResponse{Error: "Transaction not found"})
}
cachedTxnObj := messagelayer.Tangle().LedgerState.Transaction(txID)
defer cachedTxnObj.Release()
if !cachedTxnObj.Exists() {
return c.JSON(http.StatusNotFound, value.GetTransactionByIDResponse{Error: "Transaction not found"})
}
txn := value.ParseTransaction(cachedTxnObj.Unwrap())
txMetadata := cachedTxnMetaObj.Unwrap()
txInclusionState, err := messagelayer.Tangle().LedgerState.TransactionInclusionState(txID)
if err != nil {
return c.JSON(http.StatusOK, value.GetTransactionByIDResponse{Error: err.Error()})
}
cachedBranch := messagelayer.Tangle().LedgerState.BranchDAG.Branch(txMetadata.BranchID())
defer cachedBranch.Release()
if !cachedTxnObj.Exists() {
return c.JSON(http.StatusNotFound, value.GetTransactionByIDResponse{Error: "Branch not found"})
}
branch := cachedBranch.Unwrap()
return c.JSON(http.StatusOK, value.GetTransactionByIDResponse{
TransactionMetadata: value.TransactionMetadata{
BranchID: branch.ID().String(),
Solid: txMetadata.Solid(),
SolidificationTime: txMetadata.SolidificationTime().Unix(),
Finalized: txMetadata.Finalized(),
LazyBooked: txMetadata.LazyBooked(),
},
Transaction: txn,
InclusionState: value.InclusionState{
Confirmed: txInclusionState == ledgerstate.Confirmed,
Conflicting: messagelayer.Tangle().LedgerState.TransactionConflicting(txID),
Liked: branch.Liked(),
Solid: txMetadata.Solid(),
Rejected: txInclusionState == ledgerstate.Rejected,
Finalized: txMetadata.Finalized(),
Preferred: false,
},
})
}