github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/endpoints/exchange.go (about)

     1  /*
     2   * Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
     3   *
     4   * This program is free software: you can redistribute it and/or modify
     5   * it under the terms of the GNU General Public License as published by
     6   * the Free Software Foundation, either version 3 of the License, or
     7   * (at your option) any later version.
     8   *
     9   * This program is distributed in the hope that it will be useful,
    10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12   * GNU General Public License for more details.
    13   *
    14   * You should have received a copy of the GNU General Public License
    15   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16   */
    17  
    18  package endpoints
    19  
    20  import (
    21  	"strings"
    22  
    23  	"github.com/gin-gonic/gin"
    24  	"github.com/mysteriumnetwork/go-rest/apierror"
    25  
    26  	"github.com/mysteriumnetwork/node/tequilapi/contract"
    27  	"github.com/mysteriumnetwork/node/tequilapi/utils"
    28  )
    29  
    30  type exchangeEndpoint struct {
    31  	me mystexchange
    32  }
    33  
    34  type mystexchange interface {
    35  	GetMystExchangeRate() (map[string]float64, error)
    36  }
    37  
    38  // NewExchangeEndpoint returns a new exchange endpoint
    39  func NewExchangeEndpoint(mystex mystexchange) *exchangeEndpoint {
    40  	return &exchangeEndpoint{
    41  		me: mystex,
    42  	}
    43  }
    44  
    45  // swagger:operation GET /exchange/myst/{currency} Exchange ExchangeMyst
    46  //
    47  //	---
    48  //	summary: Returns the myst price in the given currency
    49  //	description: Returns the myst price in the given currency (dai is deprecated)
    50  //	parameters:
    51  //	  - name: currency
    52  //	    in: path
    53  //	    description: Currency to which MYST is converted
    54  //	    type: string
    55  //	    required: true
    56  //	responses:
    57  //	  200:
    58  //	    description: MYST price in given currency
    59  //	    schema:
    60  //	      "$ref": "#/definitions/CurrencyExchangeDTO"
    61  //	  404:
    62  //	    description: Currency is not supported
    63  //	    schema:
    64  //	      "$ref": "#/definitions/APIError"
    65  //	  500:
    66  //	    description: Internal server error
    67  //	    schema:
    68  //	      "$ref": "#/definitions/APIError"
    69  func (e *exchangeEndpoint) ExchangeMyst(c *gin.Context) {
    70  	currency := strings.ToUpper(c.Param("currency"))
    71  
    72  	rates, err := e.me.GetMystExchangeRate()
    73  	if err != nil {
    74  		c.Error(err)
    75  		return
    76  	}
    77  
    78  	var ok bool
    79  	amount, ok := rates[currency]
    80  	if !ok {
    81  		c.Error(apierror.NotFound("Currency is not supported"))
    82  		return
    83  	}
    84  
    85  	rate := contract.CurrencyExchangeDTO{
    86  		Amount:   amount,
    87  		Currency: currency,
    88  	}
    89  
    90  	utils.WriteAsJSON(rate, c.Writer)
    91  }
    92  
    93  // AddRoutesForCurrencyExchange attaches exchange endpoints to router.
    94  func AddRoutesForCurrencyExchange(mystex mystexchange) func(*gin.Engine) error {
    95  	e := NewExchangeEndpoint(mystex)
    96  	return func(g *gin.Engine) error {
    97  		ex := g.Group("/exchange")
    98  		{
    99  			ex.GET("/myst/:currency", e.ExchangeMyst)
   100  		}
   101  		return nil
   102  	}
   103  }