diff --git a/crypto/.gitignore b/crypto/.gitignore new file mode 100644 index 0000000..ae0ef0b --- /dev/null +++ b/crypto/.gitignore @@ -0,0 +1,2 @@ + +crypto diff --git a/crypto/Dockerfile b/crypto/Dockerfile new file mode 100644 index 0000000..5a35c84 --- /dev/null +++ b/crypto/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +ADD crypto /crypto +ENTRYPOINT [ "/crypto" ] diff --git a/crypto/Makefile b/crypto/Makefile new file mode 100644 index 0000000..2e731aa --- /dev/null +++ b/crypto/Makefile @@ -0,0 +1,28 @@ + +GOPATH:=$(shell go env GOPATH) +.PHONY: init +init: + go get -u github.com/golang/protobuf/proto + go get -u github.com/golang/protobuf/protoc-gen-go + go get github.com/micro/micro/v3/cmd/protoc-gen-micro + go get github.com/micro/micro/v3/cmd/protoc-gen-openapi + +.PHONY: api +api: + protoc --openapi_out=. --proto_path=. proto/crypto.proto + +.PHONY: proto +proto: + protoc --proto_path=. --micro_out=. --go_out=:. proto/crypto.proto + +.PHONY: build +build: + go build -o crypto *.go + +.PHONY: test +test: + go test -v ./... -cover + +.PHONY: docker +docker: + docker build . -t crypto:latest diff --git a/crypto/README.md b/crypto/README.md new file mode 100644 index 0000000..567574c --- /dev/null +++ b/crypto/README.md @@ -0,0 +1,7 @@ +Today's cryptocurrency prices and quotes + +# Crypto Service + +Get up to the second accurate cryptocurrency prices, quotes and previous close information. + +Powered by [Finage](https://finage.co.uk) diff --git a/crypto/examples.json b/crypto/examples.json new file mode 100644 index 0000000..04f43d6 --- /dev/null +++ b/crypto/examples.json @@ -0,0 +1,45 @@ + +{ + "price": [{ + "title": "Get cryptocurrency price", + "description": "Returns the last traded price of a currency", + "request": { + "symbol": "BTCUSD" + }, + "response": { + "symbol": "BTCUSD", + "price": 131.265 + } + }], + "quote": [{ + "title": "Get a cryptocurrency quote", + "description": "Returns the last quote for a currency including bid and ask prices", + "request": { + "symbol": "BTCUSD" + }, + "response": { + "symbol": "BTCUSD", + "ask": 131.12, + "bid": 131.11, + "ask_size": 7, + "bid_size": 4, + "timestamp": "2021-06-18T13:49:23.678Z" + } + }], + "history": [{ + "title": "Get historic data", + "description": "Returns historic cryptocurrency data for a given date", + "request": { + "coin": "BTCUSD", + "date": "2020-10-01" + }, + "response": { + "symbol": "BTCUSD", + "open": 117.64, + "close": 116.79, + "high": 117.72, + "low": 115.83, + "date": "2020-10-01" + } + }] +} diff --git a/crypto/generate.go b/crypto/generate.go new file mode 100644 index 0000000..7d9db91 --- /dev/null +++ b/crypto/generate.go @@ -0,0 +1,3 @@ +package main + +//go:generate make proto diff --git a/crypto/handler/crypto.go b/crypto/handler/crypto.go new file mode 100644 index 0000000..91126e4 --- /dev/null +++ b/crypto/handler/crypto.go @@ -0,0 +1,192 @@ +package handler + +import ( + "context" + "encoding/json" + "fmt" + "io/ioutil" + "net/http" + "regexp" + "time" + + "github.com/micro/micro/v3/service/config" + "github.com/micro/micro/v3/service/errors" + "github.com/micro/micro/v3/service/logger" + pb "github.com/micro/services/crypto/proto" + "github.com/patrickmn/go-cache" +) + +var ( + re = regexp.MustCompile(`\d{4}-\d{2}-\d{2}`) +) + +type Crypto struct { + Api string + Key string + Cache *cache.Cache +} + +type Quote struct { + Symbol string + Ask float64 + Bid float64 + Asize float64 + Bsize float64 + Timestamp float64 +} + +type History struct { + Open float64 `json:"o"` + High float64 `json:"h"` + Low float64 `json:"l"` + Close float64 `json:"c"` + Volume float64 `json:"v"` + Timestamp float64 `json:"t"` +} + +type Previous struct { + Symbol string + TotalResults int32 + Results []*History +} + +func New() *Crypto { + // TODO: look for "crypto.provider" to determine the handler + v, err := config.Get("finage.api") + if err != nil { + logger.Fatalf("finage.api config not found: %v", err) + } + api := v.String("") + if len(api) == 0 { + logger.Fatal("finage.api config not found") + } + v, err = config.Get("finage.key") + if err != nil { + logger.Fatalf("finage.key config not found: %v", err) + } + key := v.String("") + if len(key) == 0 { + logger.Fatal("finage.key config not found") + } + + return &Crypto{ + Api: api, + Key: key, + Cache: cache.New(5*time.Minute, 10*time.Minute), + } +} + +func (s *Crypto) History(ctx context.Context, req *pb.HistoryRequest, rsp *pb.HistoryResponse) error { + if len(req.Symbol) <= 0 { + return errors.BadRequest("crypto.history", "invalid symbol") + } + + uri := fmt.Sprintf("%sagg/crypto/prev-close/%s?apikey=%s", s.Api, req.Symbol, s.Key) + + resp, err := http.Get(uri) + if err != nil { + logger.Errorf("Failed to get history: %v\n", err) + return errors.InternalServerError("crypto.history", "failed to get history") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get history (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("crypto.history", "failed to get history") + } + + var respBody Previous + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal history: %v\n", err) + return errors.InternalServerError("crypto.history", "failed to get history") + } + + if len(respBody.Results) != 1 { + return nil + } + + res := respBody.Results[0] + rsp.Symbol = req.Symbol + rsp.Open = res.Open + rsp.Close = res.Close + rsp.High = res.High + rsp.Low = res.Low + rsp.Date = time.Unix(0, int64(res.Timestamp)*int64(time.Millisecond)).UTC().Format("2006-01-02") + rsp.Volume = res.Volume + + return nil +} +func (s *Crypto) Quote(ctx context.Context, req *pb.QuoteRequest, rsp *pb.QuoteResponse) error { + if len(req.Symbol) <= 0 { + return errors.BadRequest("crypto.quote", "invalid symbol") + } + + uri := fmt.Sprintf("%slast/quote/crypto/%s?apikey=%s", s.Api, req.Symbol, s.Key) + + resp, err := http.Get(uri) + if err != nil { + logger.Errorf("Failed to get quote: %v\n", err) + return errors.InternalServerError("crypto.quote", "failed to get quote") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get quote (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("crypto.quote", "failed to get quote") + } + + var respBody Quote + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal quote: %v\n", err) + return errors.InternalServerError("crypto.quote", "failed to get quote") + } + + rsp.Symbol = respBody.Symbol + rsp.AskPrice = respBody.Ask + rsp.BidPrice = respBody.Bid + rsp.AskSize = respBody.Asize + rsp.BidSize = respBody.Bsize + rsp.Timestamp = time.Unix(0, int64(respBody.Timestamp)*int64(time.Millisecond)).UTC().Format(time.RFC3339Nano) + + return nil +} + +func (s *Crypto) Price(ctx context.Context, req *pb.PriceRequest, rsp *pb.PriceResponse) error { + if len(req.Symbol) <= 0 { + return errors.BadRequest("crypto.price", "invalid symbol") + } + + uri := fmt.Sprintf("%slast/crypto/%s?apikey=%s", s.Api, req.Symbol, s.Key) + + resp, err := http.Get(uri) + if err != nil { + logger.Errorf("Failed to get price: %v\n", err) + return errors.InternalServerError("crypto.trade", "failed to get price") + } + defer resp.Body.Close() + + b, _ := ioutil.ReadAll(resp.Body) + + if resp.StatusCode != 200 { + logger.Errorf("Failed to get price (non 200): %d %v\n", resp.StatusCode, string(b)) + return errors.InternalServerError("crypto.quote", "failed to get price") + } + + var respBody map[string]interface{} + + if err := json.Unmarshal(b, &respBody); err != nil { + logger.Errorf("Failed to unmarshal price: %v\n", err) + return errors.InternalServerError("crypto.price", "failed to get price") + } + + rsp.Symbol = req.Symbol + rsp.Price = respBody["price"].(float64) + + return nil +} diff --git a/crypto/main.go b/crypto/main.go new file mode 100644 index 0000000..87241ef --- /dev/null +++ b/crypto/main.go @@ -0,0 +1,24 @@ +package main + +import ( + "github.com/micro/micro/v3/service" + "github.com/micro/micro/v3/service/logger" + "github.com/micro/services/crypto/handler" + pb "github.com/micro/services/crypto/proto" +) + +func main() { + // Create service + srv := service.New( + service.Name("crypto"), + service.Version("latest"), + ) + + // Register handler + pb.RegisterCryptoHandler(srv.Server(), handler.New()) + + // Run service + if err := srv.Run(); err != nil { + logger.Fatal(err) + } +} diff --git a/crypto/micro.mu b/crypto/micro.mu new file mode 100644 index 0000000..3f056d9 --- /dev/null +++ b/crypto/micro.mu @@ -0,0 +1 @@ +service crypto diff --git a/crypto/proto/crypto.pb.go b/crypto/proto/crypto.pb.go new file mode 100644 index 0000000..28fb38c --- /dev/null +++ b/crypto/proto/crypto.pb.go @@ -0,0 +1,608 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.26.0 +// protoc v3.15.6 +// source: proto/crypto.proto + +package crypto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +// Get the last price for a given crypto ticker +type PriceRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // crypto symbol e.g BTCUSD + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` +} + +func (x *PriceRequest) Reset() { + *x = PriceRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_crypto_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PriceRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PriceRequest) ProtoMessage() {} + +func (x *PriceRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_crypto_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PriceRequest.ProtoReflect.Descriptor instead. +func (*PriceRequest) Descriptor() ([]byte, []int) { + return file_proto_crypto_proto_rawDescGZIP(), []int{0} +} + +func (x *PriceRequest) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +type PriceResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the crypto symbol e.g BTCUSD + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` + // the last price + Price float64 `protobuf:"fixed64,2,opt,name=price,proto3" json:"price,omitempty"` +} + +func (x *PriceResponse) Reset() { + *x = PriceResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_crypto_proto_msgTypes[1] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *PriceResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*PriceResponse) ProtoMessage() {} + +func (x *PriceResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_crypto_proto_msgTypes[1] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use PriceResponse.ProtoReflect.Descriptor instead. +func (*PriceResponse) Descriptor() ([]byte, []int) { + return file_proto_crypto_proto_rawDescGZIP(), []int{1} +} + +func (x *PriceResponse) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +func (x *PriceResponse) GetPrice() float64 { + if x != nil { + return x.Price + } + return 0 +} + +// Get the last quote for the crypto +type QuoteRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the crypto symbol e.g BTCUSD + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` +} + +func (x *QuoteRequest) Reset() { + *x = QuoteRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_crypto_proto_msgTypes[2] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *QuoteRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*QuoteRequest) ProtoMessage() {} + +func (x *QuoteRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_crypto_proto_msgTypes[2] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use QuoteRequest.ProtoReflect.Descriptor instead. +func (*QuoteRequest) Descriptor() ([]byte, []int) { + return file_proto_crypto_proto_rawDescGZIP(), []int{2} +} + +func (x *QuoteRequest) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +type QuoteResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the crypto symbol + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` + // the asking price + AskPrice float64 `protobuf:"fixed64,2,opt,name=ask_price,json=askPrice,proto3" json:"ask_price,omitempty"` + // the bidding price + BidPrice float64 `protobuf:"fixed64,3,opt,name=bid_price,json=bidPrice,proto3" json:"bid_price,omitempty"` + // the ask size + AskSize float64 `protobuf:"fixed64,4,opt,name=ask_size,json=askSize,proto3" json:"ask_size,omitempty"` + // the bid size + BidSize float64 `protobuf:"fixed64,5,opt,name=bid_size,json=bidSize,proto3" json:"bid_size,omitempty"` + // the UTC timestamp of the quote + Timestamp string `protobuf:"bytes,6,opt,name=timestamp,proto3" json:"timestamp,omitempty"` +} + +func (x *QuoteResponse) Reset() { + *x = QuoteResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_crypto_proto_msgTypes[3] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *QuoteResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*QuoteResponse) ProtoMessage() {} + +func (x *QuoteResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_crypto_proto_msgTypes[3] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use QuoteResponse.ProtoReflect.Descriptor instead. +func (*QuoteResponse) Descriptor() ([]byte, []int) { + return file_proto_crypto_proto_rawDescGZIP(), []int{3} +} + +func (x *QuoteResponse) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +func (x *QuoteResponse) GetAskPrice() float64 { + if x != nil { + return x.AskPrice + } + return 0 +} + +func (x *QuoteResponse) GetBidPrice() float64 { + if x != nil { + return x.BidPrice + } + return 0 +} + +func (x *QuoteResponse) GetAskSize() float64 { + if x != nil { + return x.AskSize + } + return 0 +} + +func (x *QuoteResponse) GetBidSize() float64 { + if x != nil { + return x.BidSize + } + return 0 +} + +func (x *QuoteResponse) GetTimestamp() string { + if x != nil { + return x.Timestamp + } + return "" +} + +// Returns the history for the previous close +type HistoryRequest struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the crypto symbol e.g BTCUSD + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` +} + +func (x *HistoryRequest) Reset() { + *x = HistoryRequest{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_crypto_proto_msgTypes[4] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HistoryRequest) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HistoryRequest) ProtoMessage() {} + +func (x *HistoryRequest) ProtoReflect() protoreflect.Message { + mi := &file_proto_crypto_proto_msgTypes[4] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HistoryRequest.ProtoReflect.Descriptor instead. +func (*HistoryRequest) Descriptor() ([]byte, []int) { + return file_proto_crypto_proto_rawDescGZIP(), []int{4} +} + +func (x *HistoryRequest) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +type HistoryResponse struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + // the crypto symbol + Symbol string `protobuf:"bytes,1,opt,name=symbol,proto3" json:"symbol,omitempty"` + // the open price + Open float64 `protobuf:"fixed64,2,opt,name=open,proto3" json:"open,omitempty"` + // the close price + Close float64 `protobuf:"fixed64,3,opt,name=close,proto3" json:"close,omitempty"` + // the peak price + High float64 `protobuf:"fixed64,4,opt,name=high,proto3" json:"high,omitempty"` + // the low price + Low float64 `protobuf:"fixed64,5,opt,name=low,proto3" json:"low,omitempty"` + // the volume + Volume float64 `protobuf:"fixed64,6,opt,name=volume,proto3" json:"volume,omitempty"` + // the date + Date string `protobuf:"bytes,7,opt,name=date,proto3" json:"date,omitempty"` +} + +func (x *HistoryResponse) Reset() { + *x = HistoryResponse{} + if protoimpl.UnsafeEnabled { + mi := &file_proto_crypto_proto_msgTypes[5] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *HistoryResponse) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*HistoryResponse) ProtoMessage() {} + +func (x *HistoryResponse) ProtoReflect() protoreflect.Message { + mi := &file_proto_crypto_proto_msgTypes[5] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use HistoryResponse.ProtoReflect.Descriptor instead. +func (*HistoryResponse) Descriptor() ([]byte, []int) { + return file_proto_crypto_proto_rawDescGZIP(), []int{5} +} + +func (x *HistoryResponse) GetSymbol() string { + if x != nil { + return x.Symbol + } + return "" +} + +func (x *HistoryResponse) GetOpen() float64 { + if x != nil { + return x.Open + } + return 0 +} + +func (x *HistoryResponse) GetClose() float64 { + if x != nil { + return x.Close + } + return 0 +} + +func (x *HistoryResponse) GetHigh() float64 { + if x != nil { + return x.High + } + return 0 +} + +func (x *HistoryResponse) GetLow() float64 { + if x != nil { + return x.Low + } + return 0 +} + +func (x *HistoryResponse) GetVolume() float64 { + if x != nil { + return x.Volume + } + return 0 +} + +func (x *HistoryResponse) GetDate() string { + if x != nil { + return x.Date + } + return "" +} + +var File_proto_crypto_proto protoreflect.FileDescriptor + +var file_proto_crypto_proto_rawDesc = []byte{ + 0x0a, 0x12, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x06, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x22, 0x26, 0x0a, 0x0c, + 0x50, 0x72, 0x69, 0x63, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, + 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x79, + 0x6d, 0x62, 0x6f, 0x6c, 0x22, 0x3d, 0x0a, 0x0d, 0x50, 0x72, 0x69, 0x63, 0x65, 0x52, 0x65, 0x73, + 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x12, 0x14, 0x0a, + 0x05, 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x70, 0x72, + 0x69, 0x63, 0x65, 0x22, 0x26, 0x0a, 0x0c, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x22, 0xb5, 0x01, 0x0a, 0x0d, + 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, + 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, + 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x73, 0x6b, 0x5f, 0x70, 0x72, 0x69, + 0x63, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x61, 0x73, 0x6b, 0x50, 0x72, 0x69, + 0x63, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x62, 0x69, 0x64, 0x5f, 0x70, 0x72, 0x69, 0x63, 0x65, 0x18, + 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x08, 0x62, 0x69, 0x64, 0x50, 0x72, 0x69, 0x63, 0x65, 0x12, + 0x19, 0x0a, 0x08, 0x61, 0x73, 0x6b, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x01, 0x52, 0x07, 0x61, 0x73, 0x6b, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x19, 0x0a, 0x08, 0x62, 0x69, + 0x64, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x18, 0x05, 0x20, 0x01, 0x28, 0x01, 0x52, 0x07, 0x62, 0x69, + 0x64, 0x53, 0x69, 0x7a, 0x65, 0x12, 0x1c, 0x0a, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, 0x61, + 0x6d, 0x70, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x74, 0x69, 0x6d, 0x65, 0x73, 0x74, + 0x61, 0x6d, 0x70, 0x22, 0x28, 0x0a, 0x0e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, + 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x22, 0xa5, 0x01, + 0x0a, 0x0f, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, + 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x12, 0x12, 0x0a, 0x04, 0x6f, 0x70, 0x65, + 0x6e, 0x18, 0x02, 0x20, 0x01, 0x28, 0x01, 0x52, 0x04, 0x6f, 0x70, 0x65, 0x6e, 0x12, 0x14, 0x0a, + 0x05, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x01, 0x52, 0x05, 0x63, 0x6c, + 0x6f, 0x73, 0x65, 0x12, 0x12, 0x0a, 0x04, 0x68, 0x69, 0x67, 0x68, 0x18, 0x04, 0x20, 0x01, 0x28, + 0x01, 0x52, 0x04, 0x68, 0x69, 0x67, 0x68, 0x12, 0x10, 0x0a, 0x03, 0x6c, 0x6f, 0x77, 0x18, 0x05, + 0x20, 0x01, 0x28, 0x01, 0x52, 0x03, 0x6c, 0x6f, 0x77, 0x12, 0x16, 0x0a, 0x06, 0x76, 0x6f, 0x6c, + 0x75, 0x6d, 0x65, 0x18, 0x06, 0x20, 0x01, 0x28, 0x01, 0x52, 0x06, 0x76, 0x6f, 0x6c, 0x75, 0x6d, + 0x65, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, 0x18, 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x04, 0x64, 0x61, 0x74, 0x65, 0x32, 0xb6, 0x01, 0x0a, 0x06, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, + 0x12, 0x36, 0x0a, 0x05, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x12, 0x14, 0x2e, 0x63, 0x72, 0x79, 0x70, + 0x74, 0x6f, 0x2e, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, + 0x15, 0x2e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x2e, 0x51, 0x75, 0x6f, 0x74, 0x65, 0x52, 0x65, + 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x12, 0x36, 0x0a, 0x05, 0x50, 0x72, 0x69, 0x63, + 0x65, 0x12, 0x14, 0x2e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x2e, 0x50, 0x72, 0x69, 0x63, 0x65, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x15, 0x2e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, + 0x2e, 0x50, 0x72, 0x69, 0x63, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, + 0x12, 0x3c, 0x0a, 0x07, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x12, 0x16, 0x2e, 0x63, 0x72, + 0x79, 0x70, 0x74, 0x6f, 0x2e, 0x48, 0x69, 0x73, 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x71, 0x75, + 0x65, 0x73, 0x74, 0x1a, 0x17, 0x2e, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x2e, 0x48, 0x69, 0x73, + 0x74, 0x6f, 0x72, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x10, + 0x5a, 0x0e, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x3b, 0x63, 0x72, 0x79, 0x70, 0x74, 0x6f, + 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_proto_crypto_proto_rawDescOnce sync.Once + file_proto_crypto_proto_rawDescData = file_proto_crypto_proto_rawDesc +) + +func file_proto_crypto_proto_rawDescGZIP() []byte { + file_proto_crypto_proto_rawDescOnce.Do(func() { + file_proto_crypto_proto_rawDescData = protoimpl.X.CompressGZIP(file_proto_crypto_proto_rawDescData) + }) + return file_proto_crypto_proto_rawDescData +} + +var file_proto_crypto_proto_msgTypes = make([]protoimpl.MessageInfo, 6) +var file_proto_crypto_proto_goTypes = []interface{}{ + (*PriceRequest)(nil), // 0: crypto.PriceRequest + (*PriceResponse)(nil), // 1: crypto.PriceResponse + (*QuoteRequest)(nil), // 2: crypto.QuoteRequest + (*QuoteResponse)(nil), // 3: crypto.QuoteResponse + (*HistoryRequest)(nil), // 4: crypto.HistoryRequest + (*HistoryResponse)(nil), // 5: crypto.HistoryResponse +} +var file_proto_crypto_proto_depIdxs = []int32{ + 2, // 0: crypto.Crypto.Quote:input_type -> crypto.QuoteRequest + 0, // 1: crypto.Crypto.Price:input_type -> crypto.PriceRequest + 4, // 2: crypto.Crypto.History:input_type -> crypto.HistoryRequest + 3, // 3: crypto.Crypto.Quote:output_type -> crypto.QuoteResponse + 1, // 4: crypto.Crypto.Price:output_type -> crypto.PriceResponse + 5, // 5: crypto.Crypto.History:output_type -> crypto.HistoryResponse + 3, // [3:6] is the sub-list for method output_type + 0, // [0:3] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_proto_crypto_proto_init() } +func file_proto_crypto_proto_init() { + if File_proto_crypto_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_proto_crypto_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PriceRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_crypto_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*PriceResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_crypto_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*QuoteRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_crypto_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*QuoteResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_crypto_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HistoryRequest); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + file_proto_crypto_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*HistoryResponse); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_proto_crypto_proto_rawDesc, + NumEnums: 0, + NumMessages: 6, + NumExtensions: 0, + NumServices: 1, + }, + GoTypes: file_proto_crypto_proto_goTypes, + DependencyIndexes: file_proto_crypto_proto_depIdxs, + MessageInfos: file_proto_crypto_proto_msgTypes, + }.Build() + File_proto_crypto_proto = out.File + file_proto_crypto_proto_rawDesc = nil + file_proto_crypto_proto_goTypes = nil + file_proto_crypto_proto_depIdxs = nil +} diff --git a/crypto/proto/crypto.pb.micro.go b/crypto/proto/crypto.pb.micro.go new file mode 100644 index 0000000..b5a70e1 --- /dev/null +++ b/crypto/proto/crypto.pb.micro.go @@ -0,0 +1,127 @@ +// Code generated by protoc-gen-micro. DO NOT EDIT. +// source: proto/crypto.proto + +package crypto + +import ( + fmt "fmt" + proto "github.com/golang/protobuf/proto" + math "math" +) + +import ( + context "context" + api "github.com/micro/micro/v3/service/api" + client "github.com/micro/micro/v3/service/client" + server "github.com/micro/micro/v3/service/server" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package + +// Reference imports to suppress errors if they are not otherwise used. +var _ api.Endpoint +var _ context.Context +var _ client.Option +var _ server.Option + +// Api Endpoints for Crypto service + +func NewCryptoEndpoints() []*api.Endpoint { + return []*api.Endpoint{} +} + +// Client API for Crypto service + +type CryptoService interface { + Quote(ctx context.Context, in *QuoteRequest, opts ...client.CallOption) (*QuoteResponse, error) + Price(ctx context.Context, in *PriceRequest, opts ...client.CallOption) (*PriceResponse, error) + History(ctx context.Context, in *HistoryRequest, opts ...client.CallOption) (*HistoryResponse, error) +} + +type cryptoService struct { + c client.Client + name string +} + +func NewCryptoService(name string, c client.Client) CryptoService { + return &cryptoService{ + c: c, + name: name, + } +} + +func (c *cryptoService) Quote(ctx context.Context, in *QuoteRequest, opts ...client.CallOption) (*QuoteResponse, error) { + req := c.c.NewRequest(c.name, "Crypto.Quote", in) + out := new(QuoteResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *cryptoService) Price(ctx context.Context, in *PriceRequest, opts ...client.CallOption) (*PriceResponse, error) { + req := c.c.NewRequest(c.name, "Crypto.Price", in) + out := new(PriceResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +func (c *cryptoService) History(ctx context.Context, in *HistoryRequest, opts ...client.CallOption) (*HistoryResponse, error) { + req := c.c.NewRequest(c.name, "Crypto.History", in) + out := new(HistoryResponse) + err := c.c.Call(ctx, req, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// Server API for Crypto service + +type CryptoHandler interface { + Quote(context.Context, *QuoteRequest, *QuoteResponse) error + Price(context.Context, *PriceRequest, *PriceResponse) error + History(context.Context, *HistoryRequest, *HistoryResponse) error +} + +func RegisterCryptoHandler(s server.Server, hdlr CryptoHandler, opts ...server.HandlerOption) error { + type crypto interface { + Quote(ctx context.Context, in *QuoteRequest, out *QuoteResponse) error + Price(ctx context.Context, in *PriceRequest, out *PriceResponse) error + History(ctx context.Context, in *HistoryRequest, out *HistoryResponse) error + } + type Crypto struct { + crypto + } + h := &cryptoHandler{hdlr} + return s.Handle(s.NewHandler(&Crypto{h}, opts...)) +} + +type cryptoHandler struct { + CryptoHandler +} + +func (h *cryptoHandler) Quote(ctx context.Context, in *QuoteRequest, out *QuoteResponse) error { + return h.CryptoHandler.Quote(ctx, in, out) +} + +func (h *cryptoHandler) Price(ctx context.Context, in *PriceRequest, out *PriceResponse) error { + return h.CryptoHandler.Price(ctx, in, out) +} + +func (h *cryptoHandler) History(ctx context.Context, in *HistoryRequest, out *HistoryResponse) error { + return h.CryptoHandler.History(ctx, in, out) +} diff --git a/crypto/proto/crypto.proto b/crypto/proto/crypto.proto new file mode 100644 index 0000000..b948051 --- /dev/null +++ b/crypto/proto/crypto.proto @@ -0,0 +1,69 @@ +syntax = "proto3"; + +package crypto; + +option go_package = "./proto;crypto"; + +service Crypto { + rpc Quote(QuoteRequest) returns (QuoteResponse) {} + rpc Price(PriceRequest) returns (PriceResponse) {} + rpc History(HistoryRequest) returns (HistoryResponse) {} +} + +// Get the last price for a given crypto ticker +message PriceRequest { + // crypto symbol e.g BTCUSD + string symbol = 1; +} + +message PriceResponse { + // the crypto symbol e.g BTCUSD + string symbol = 1; + // the last price + double price = 2; +} + +// Get the last quote for the crypto +message QuoteRequest { + // the crypto symbol e.g BTCUSD + string symbol = 1; +} + +message QuoteResponse { + // the crypto symbol + string symbol = 1; + // the asking price + double ask_price = 2; + // the bidding price + double bid_price = 3; + // the ask size + double ask_size = 4; + // the bid size + double bid_size = 5; + // the UTC timestamp of the quote + string timestamp = 6; +} + + +// Returns the history for the previous close +message HistoryRequest { + // the crypto symbol e.g BTCUSD + string symbol = 1; +} + +message HistoryResponse { + // the crypto symbol + string symbol = 1; + // the open price + double open = 2; + // the close price + double close = 3; + // the peak price + double high = 4; + // the low price + double low = 5; + // the volume + double volume = 6; + // the date + string date = 7; +} diff --git a/crypto/publicapi.json b/crypto/publicapi.json new file mode 100644 index 0000000..85e9164 --- /dev/null +++ b/crypto/publicapi.json @@ -0,0 +1,10 @@ +{ + "name": "crypto", + "icon": "₿", + "category": "money", + "pricing": { + "Crypto.Price": 20, + "Crypto.Quote": 20, + "Crypto.History": 35 + } +}