From 9e350afed542310fa1ed171329223447a070f11f Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 22 Feb 2022 14:28:54 +0800 Subject: [PATCH] ftxapi: add get coins api --- pkg/exchange/ftx/ftxapi/client.go | 31 +++++ .../ftxapi/get_coins_request_requestgen.go | 115 ++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 pkg/exchange/ftx/ftxapi/get_coins_request_requestgen.go diff --git a/pkg/exchange/ftx/ftxapi/client.go b/pkg/exchange/ftx/ftxapi/client.go index 500c8fec6..b94c8f481 100644 --- a/pkg/exchange/ftx/ftxapi/client.go +++ b/pkg/exchange/ftx/ftxapi/client.go @@ -304,3 +304,34 @@ func (c *RestClient) NewGetMarketRequest(market string) *GetMarketRequest { market: market, } } + +type Coin struct { + Bep2Asset *string `json:"bep2Asset"` + CanConvert bool `json:"canConvert"` + CanDeposit bool `json:"canDeposit"` + CanWithdraw bool `json:"canWithdraw"` + Collateral bool `json:"collateral"` + CollateralWeight fixedpoint.Value `json:"collateralWeight"` + CreditTo *string `json:"creditTo"` + Erc20Contract string `json:"erc20Contract"` + Fiat bool `json:"fiat"` + HasTag bool `json:"hasTag"` + Id string `json:"id"` + IsToken bool `json:"isToken"` + Methods []string `json:"methods"` + Name string `json:"name"` + SplMint string `json:"splMint"` + Trc20Contract string `json:"trc20Contract"` + UsdFungible bool `json:"usdFungible"` +} + +//go:generate GetRequest -url "/api/coins" -type GetCoinsRequest -responseDataType []Coin +type GetCoinsRequest struct { + client requestgen.AuthenticatedAPIClient +} + +func (c *RestClient) NewGetCoinsRequest() *GetCoinsRequest { + return &GetCoinsRequest{ + client: c, + } +} diff --git a/pkg/exchange/ftx/ftxapi/get_coins_request_requestgen.go b/pkg/exchange/ftx/ftxapi/get_coins_request_requestgen.go new file mode 100644 index 000000000..df0687096 --- /dev/null +++ b/pkg/exchange/ftx/ftxapi/get_coins_request_requestgen.go @@ -0,0 +1,115 @@ +// Code generated by "requestgen -method GET -responseType .APIResponse -responseDataField Result -url /api/coins -type GetCoinsRequest -responseDataType []Coin"; DO NOT EDIT. + +package ftxapi + +import ( + "context" + "encoding/json" + "fmt" + "net/url" + "regexp" +) + +// GetQueryParameters builds and checks the query parameters and returns url.Values +func (g *GetCoinsRequest) GetQueryParameters() (url.Values, error) { + var params = map[string]interface{}{} + + query := url.Values{} + for k, v := range params { + query.Add(k, fmt.Sprintf("%v", v)) + } + + return query, nil +} + +// GetParameters builds and checks the parameters and return the result in a map object +func (g *GetCoinsRequest) GetParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + + return params, nil +} + +// GetParametersQuery converts the parameters from GetParameters into the url.Values format +func (g *GetCoinsRequest) GetParametersQuery() (url.Values, error) { + query := url.Values{} + + params, err := g.GetParameters() + if err != nil { + return query, err + } + + for k, v := range params { + query.Add(k, fmt.Sprintf("%v", v)) + } + + return query, nil +} + +// GetParametersJSON converts the parameters from GetParameters into the JSON format +func (g *GetCoinsRequest) GetParametersJSON() ([]byte, error) { + params, err := g.GetParameters() + if err != nil { + return nil, err + } + + return json.Marshal(params) +} + +// GetSlugParameters builds and checks the slug parameters and return the result in a map object +func (g *GetCoinsRequest) GetSlugParameters() (map[string]interface{}, error) { + var params = map[string]interface{}{} + + return params, nil +} + +func (g *GetCoinsRequest) applySlugsToUrl(url string, slugs map[string]string) string { + for k, v := range slugs { + needleRE := regexp.MustCompile(":" + k + "\\b") + url = needleRE.ReplaceAllString(url, v) + } + + return url +} + +func (g *GetCoinsRequest) GetSlugsMap() (map[string]string, error) { + slugs := map[string]string{} + params, err := g.GetSlugParameters() + if err != nil { + return slugs, nil + } + + for k, v := range params { + slugs[k] = fmt.Sprintf("%v", v) + } + + return slugs, nil +} + +func (g *GetCoinsRequest) Do(ctx context.Context) ([]Coin, error) { + + // no body params + var params interface{} + query := url.Values{} + + apiURL := "/api/coins" + + req, err := g.client.NewAuthenticatedRequest(ctx, "GET", apiURL, query, params) + if err != nil { + return nil, err + } + + response, err := g.client.SendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse APIResponse + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + var data []Coin + if err := json.Unmarshal(apiResponse.Result, &data); err != nil { + return nil, err + } + return data, nil +}