mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 03:16:45 +01:00
Merge pull request #200 from hermeznetwork/feature/api-gettoken
Add get token endpoint
This commit is contained in:
@@ -871,6 +871,31 @@ func assertExitAPIs(t *testing.T, expected, actual []exitAPI) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetToken(t *testing.T) {
|
||||
// Get all txs by their ID
|
||||
endpoint := apiURL + "tokens/"
|
||||
fetchedTokens := []historydb.TokenRead{}
|
||||
for _, token := range tc.tokens {
|
||||
fetchedToken := historydb.TokenRead{}
|
||||
assert.NoError(t, doGoodReq("GET", endpoint+strconv.Itoa(int(token.TokenID)), nil, &fetchedToken))
|
||||
fetchedTokens = append(fetchedTokens, fetchedToken)
|
||||
}
|
||||
assertTokensAPIs(t, tc.tokens, fetchedTokens)
|
||||
}
|
||||
|
||||
func assertTokensAPIs(t *testing.T, expected, actual []historydb.TokenRead) {
|
||||
require.Equal(t, len(expected), len(actual))
|
||||
for i := 0; i < len(actual); i++ { //nolint len(actual) won't change within the loop
|
||||
if expected[i].USDUpdate == nil {
|
||||
assert.Equal(t, expected[i].USDUpdate, actual[i].USDUpdate)
|
||||
} else {
|
||||
assert.Equal(t, expected[i].USDUpdate.Unix(), actual[i].USDUpdate.Unix())
|
||||
expected[i].USDUpdate = actual[i].USDUpdate
|
||||
}
|
||||
assert.Equal(t, expected[i], actual[i])
|
||||
}
|
||||
}
|
||||
|
||||
func doGoodReqPaginated(
|
||||
path, order string,
|
||||
iterStruct db.Paginationer,
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hermeznetwork/hermez-node/common"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
)
|
||||
|
||||
@@ -205,7 +206,20 @@ func getTokens(c *gin.Context) {
|
||||
}
|
||||
|
||||
func getToken(c *gin.Context) {
|
||||
|
||||
// Get TokenID
|
||||
tokenIDUint, err := parseParamUint("id", nil, 0, maxUint32, c)
|
||||
if err != nil {
|
||||
retBadReq(err, c)
|
||||
return
|
||||
}
|
||||
tokenID := common.TokenID(*tokenIDUint)
|
||||
// Fetch token from historyDB
|
||||
token, err := h.GetToken(tokenID)
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, token)
|
||||
}
|
||||
|
||||
func getRecommendedFee(c *gin.Context) {
|
||||
|
||||
@@ -288,6 +288,15 @@ func (hdb *HistoryDB) UpdateTokenValue(tokenSymbol string, value float64) error
|
||||
return err
|
||||
}
|
||||
|
||||
// GetToken returns a token from the DB given a TokenID
|
||||
func (hdb *HistoryDB) GetToken(tokenID common.TokenID) (*TokenRead, error) {
|
||||
token := &TokenRead{}
|
||||
err := meddler.QueryRow(
|
||||
hdb.db, token, `SELECT * FROM token WHERE token_id = $1;`, tokenID,
|
||||
)
|
||||
return token, err
|
||||
}
|
||||
|
||||
// GetTokens returns a list of tokens from the DB
|
||||
func (hdb *HistoryDB) GetTokens() ([]TokenRead, error) {
|
||||
var tokens []*TokenRead
|
||||
|
||||
Reference in New Issue
Block a user