github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/tequilapi/contract/proposal.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 contract
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/mysteriumnetwork/node/core/discovery/proposal"
    24  	"github.com/mysteriumnetwork/node/market"
    25  	"github.com/mysteriumnetwork/node/money"
    26  )
    27  
    28  // AutoNATType passed as nat_compatibility parameter to proposal discovery
    29  // indicates NAT type should be probed automatically immediately within given
    30  // request
    31  const AutoNATType = "auto"
    32  
    33  // NewProposalDTO maps to API service proposal.
    34  func NewProposalDTO(p proposal.PricedServiceProposal) ProposalDTO {
    35  	return ProposalDTO{
    36  		Format:         p.Format,
    37  		Compatibility:  p.Compatibility,
    38  		ProviderID:     p.ProviderID,
    39  		ServiceType:    p.ServiceType,
    40  		Location:       NewServiceLocationsDTO(p.Location),
    41  		AccessPolicies: p.AccessPolicies,
    42  		Quality: Quality{
    43  			Quality:   p.Quality.Quality,
    44  			Latency:   p.Quality.Latency,
    45  			Bandwidth: p.Quality.Bandwidth,
    46  			Uptime:    p.Quality.Uptime,
    47  		},
    48  		Price: Price{
    49  			Currency:      money.CurrencyMyst.String(),
    50  			PerHour:       p.Price.PricePerHour.Uint64(),
    51  			PerHourTokens: NewTokens(p.Price.PricePerHour),
    52  			PerGiB:        p.Price.PricePerGiB.Uint64(),
    53  			PerGiBTokens:  NewTokens(p.Price.PricePerGiB),
    54  		},
    55  	}
    56  }
    57  
    58  // NewServiceLocationsDTO maps to API service location.
    59  func NewServiceLocationsDTO(l market.Location) ServiceLocationDTO {
    60  	return ServiceLocationDTO{
    61  		Continent: l.Continent,
    62  		Country:   l.Country,
    63  		City:      l.City,
    64  		ASN:       l.ASN,
    65  		ISP:       l.ISP,
    66  		IPType:    l.IPType,
    67  	}
    68  }
    69  
    70  // ListProposalsResponse holds list of proposals.
    71  // swagger:model ListProposalsResponse
    72  type ListProposalsResponse struct {
    73  	Proposals []ProposalDTO `json:"proposals"`
    74  }
    75  
    76  // ListProposalsCountiesResponse holds number of proposals per country.
    77  // swagger:model ListProposalsCountiesResponse
    78  type ListProposalsCountiesResponse map[string]int
    79  
    80  // ProposalDTO holds service proposal details.
    81  // swagger:model ProposalDTO
    82  type ProposalDTO struct {
    83  	// Proposal format.
    84  	Format string `json:"format"`
    85  
    86  	// Compatibility level.
    87  	Compatibility int `json:"compatibility"`
    88  
    89  	// provider who offers service
    90  	// example: 0x0000000000000000000000000000000000000001
    91  	ProviderID string `json:"provider_id"`
    92  
    93  	// type of service provider offers
    94  	// example: openvpn
    95  	ServiceType string `json:"service_type"`
    96  
    97  	// Service location
    98  	Location ServiceLocationDTO `json:"location"`
    99  
   100  	// Service price
   101  	Price Price `json:"price"`
   102  
   103  	// AccessPolicies
   104  	AccessPolicies *[]market.AccessPolicy `json:"access_policies,omitempty"`
   105  
   106  	// Quality of the service.
   107  	Quality Quality `json:"quality"`
   108  }
   109  
   110  // Price represents the service price.
   111  // swagger:model Price
   112  type Price struct {
   113  	Currency      string `json:"currency"`
   114  	PerHour       uint64 `json:"per_hour"`
   115  	PerHourTokens Tokens `json:"per_hour_tokens"`
   116  	PerGiB        uint64 `json:"per_gib"`
   117  	PerGiBTokens  Tokens `json:"per_gib_tokens"`
   118  }
   119  
   120  func (p ProposalDTO) String() string {
   121  	return fmt.Sprintf("Provider: %s, ServiceType: %s, Country: %s", p.ProviderID, p.ServiceType, p.Location.Country)
   122  }
   123  
   124  // ListProposalFilterPresetsResponse holds a list of proposal filter presets.
   125  // swagger:model ListProposalFilterPresetsResponse
   126  type ListProposalFilterPresetsResponse struct {
   127  	Items []FilterPreset `json:"items"`
   128  }
   129  
   130  // FilterPreset is a pre-defined proposal filter.
   131  // swagger:model FilterPreset
   132  type FilterPreset struct {
   133  	ID   int    `json:"id"`
   134  	Name string `json:"name"`
   135  }
   136  
   137  // NewFilterPreset maps to the FilterPreset.
   138  func NewFilterPreset(preset proposal.FilterPreset) FilterPreset {
   139  	return FilterPreset{
   140  		ID:   preset.ID,
   141  		Name: preset.Name,
   142  	}
   143  }
   144  
   145  // ServiceLocationDTO holds service location metadata.
   146  // swagger:model ServiceLocationDTO
   147  type ServiceLocationDTO struct {
   148  	// example: EU
   149  	Continent string `json:"continent,omitempty"`
   150  	// example: NL
   151  	Country string `json:"country,omitempty"`
   152  	// example: Amsterdam
   153  	City string `json:"city,omitempty"`
   154  
   155  	// Autonomous System Number
   156  	// example: 00001
   157  	ASN int `json:"asn"`
   158  	// example: Telia Lietuva, AB
   159  	ISP string `json:"isp,omitempty"`
   160  	// example: residential
   161  	IPType string `json:"ip_type,omitempty"`
   162  }
   163  
   164  // Quality holds proposal quality metrics.
   165  // swagger:model Quality
   166  type Quality struct {
   167  	Quality   float64 `json:"quality"`
   168  	Latency   float64 `json:"latency"`
   169  	Bandwidth float64 `json:"bandwidth"`
   170  	Uptime    float64 `json:"uptime"`
   171  }