Przeglądaj źródła

[dev]defi-price功能实现,代码配置管理

Zhangzhenhua 2 tygodni temu
rodzic
commit
68eb928601

+ 2 - 0
.env

@@ -0,0 +1,2 @@
+BIRD_EYE_API_KEY = "ab8e664a67ea45969f6d549f0f78cabf"
+DEFI_URL = "https://public-api.birdeye.so/defi/"

+ 12 - 0
api/defi/defi.go

@@ -0,0 +1,12 @@
+package defi
+
+import (
+	"context"
+
+	v1 "birdcall/api/defi/v1"
+)
+
+type IDefiV1 interface {
+	Support(ctx context.Context, req *v1.SupportReq) (res *v1.CommonRes, err error)
+	Price(ctx context.Context, req *v1.PriceReq) (res *v1.CommonRes, err error)
+}

+ 22 - 0
api/defi/v1/defi.go

@@ -0,0 +1,22 @@
+package v1
+
+import (
+	"github.com/gogf/gf/v2/frame/g"
+)
+
+type SupportReq struct {
+	g.Meta `path:"/supprot" tags:"Support" method:"get" summary:"Get a list of all supported networks"`
+}
+
+type PriceReq struct {
+	g.Meta           `path:"/price" tags:"Price" method:"get" summary:"Get price update of a token."`
+	Address          string  ` v:"required" json:"address" dc:"代币地址"`
+	Chain            string  ` v:"required" json:"chain" dc:"链名"`
+	CheckLiquidity   float64 ` json:"check_liquidity" dc:"流动性验证"`
+	IncludeLiquidity bool    ` json:"include_liquidity" dc:"是否返回流动数据" `
+}
+
+type CommonRes struct {
+	Success bool        `json:"success" dc:"是否成功"`
+	Data    interface{} `json:"data"    dc:"数据"`
+}

+ 0 - 12
api/hello/v1/hello.go

@@ -1,12 +0,0 @@
-package v1
-
-import (
-	"github.com/gogf/gf/v2/frame/g"
-)
-
-type HelloReq struct {
-	g.Meta `path:"/hello" tags:"Hello" method:"get" summary:"You first hello api"`
-}
-type HelloRes struct {
-	g.Meta `mime:"text/html" example:"string"`
-}

+ 1 - 0
go.mod

@@ -13,6 +13,7 @@ require (
 	github.com/go-logr/stdr v1.2.2 // indirect
 	github.com/gorilla/websocket v1.5.1 // indirect
 	github.com/grokify/html-strip-tags-go v0.1.0 // indirect
+	github.com/joho/godotenv v1.5.1 // indirect
 	github.com/magiconair/properties v1.8.7 // indirect
 	github.com/mattn/go-colorable v0.1.13 // indirect
 	github.com/mattn/go-isatty v0.0.20 // indirect

+ 2 - 0
go.sum

@@ -19,6 +19,8 @@ github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/
 github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
 github.com/grokify/html-strip-tags-go v0.1.0 h1:03UrQLjAny8xci+R+qjCce/MYnpNXCtgzltlQbOBae4=
 github.com/grokify/html-strip-tags-go v0.1.0/go.mod h1:ZdzgfHEzAfz9X6Xe5eBLVblWIxXfYSQ40S/VKrAOGpc=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
 github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
 github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=

+ 3 - 5
internal/cmd/cmd.go

@@ -7,7 +7,7 @@ import (
 	"github.com/gogf/gf/v2/net/ghttp"
 	"github.com/gogf/gf/v2/os/gcmd"
 
-	"birdcall/internal/controller/hello"
+	"birdcall/internal/controller/defi"
 )
 
 var (
@@ -17,11 +17,9 @@ var (
 		Brief: "start http server",
 		Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
 			s := g.Server()
-			s.Group("/", func(group *ghttp.RouterGroup) {
+			s.Group("/birdeye/defi", func(group *ghttp.RouterGroup) {
 				group.Middleware(ghttp.MiddlewareHandlerResponse)
-				group.Bind(
-					hello.NewV1(),
-				)
+				group.Bind(defi.NewV1())
 			})
 			s.Run()
 			return nil

+ 18 - 0
internal/consts/consts.go

@@ -1 +1,19 @@
 package consts
+
+const (
+	GET  string = "GET"
+	POST string = "POST"
+
+	NET_WORKS        string = "networks"
+	PRICE            string = "price"
+	QUESTION_MARK    string = "?"
+	X_API_KEY        string = "X-API-KEY"
+	DEFI_URL         string = "DEFI_URL"
+	BIRD_EYE_API_KEY string = "BIRD_EYE_API_KEY"
+	CheckLiquidity   string = "check_liquidity"
+	IncludeLiquidity string = "include_liquidity"
+	ADDRESS          string = "address"
+	ACCEPT           string = "accept"
+	X_CHAIN          string = "x_chain"
+	APPLICATION_JSON string = "application/json"
+)

+ 1 - 1
internal/controller/hello/hello.go → internal/controller/defi/defi.go

@@ -2,4 +2,4 @@
 // This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
 // =================================================================================
 
-package hello
+package defi

+ 4 - 5
internal/controller/hello/hello_new.go → internal/controller/defi/defi_new.go

@@ -1,16 +1,15 @@
 // =================================================================================
-// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. 
+// This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
 // =================================================================================
 
-package hello
+package defi
 
 import (
-	"birdcall/api/hello"
+	"birdcall/api/defi"
 )
 
 type ControllerV1 struct{}
 
-func NewV1() hello.IHelloV1 {
+func NewV1() defi.IDefiV1 {
 	return &ControllerV1{}
 }
-

+ 19 - 0
internal/controller/defi/defi_v1_price.go

@@ -0,0 +1,19 @@
+package defi
+
+import (
+	"context"
+
+	"github.com/gogf/gf/v2/frame/g"
+
+	v1 "birdcall/api/defi/v1"
+	"birdcall/internal/service"
+)
+
+func (c *ControllerV1) Price(ctx context.Context, req *v1.PriceReq) (res *v1.CommonRes, err error) {
+	result, err := service.Defi().Price(ctx, req)
+	if err != nil {
+		g.Log().Error(ctx, err.Error())
+		return nil, err
+	}
+	return result, nil
+}

+ 19 - 0
internal/controller/defi/defi_v1_support.go

@@ -0,0 +1,19 @@
+package defi
+
+import (
+	"context"
+
+	"github.com/gogf/gf/v2/frame/g"
+
+	v1 "birdcall/api/defi/v1"
+	"birdcall/internal/service"
+)
+
+func (c *ControllerV1) Support(ctx context.Context, req *v1.SupportReq) (res *v1.CommonRes, err error) {
+	result, err := service.Defi().Support(ctx, req)
+	if err != nil {
+		g.Log().Error(ctx, err.Error())
+		return nil, err
+	}
+	return result, nil
+}

+ 0 - 13
internal/controller/hello/hello_v1_hello.go

@@ -1,13 +0,0 @@
-package hello
-
-import (
-	"context"
-	"github.com/gogf/gf/v2/frame/g"
-
-	"birdcall/api/hello/v1"
-)
-
-func (c *ControllerV1) Hello(ctx context.Context, req *v1.HelloReq) (res *v1.HelloRes, err error) {
-	g.RequestFromCtx(ctx).Response.Writeln("Hello World!")
-	return
-}

+ 92 - 0
internal/logic/defi/defi.go

@@ -0,0 +1,92 @@
+package defi
+
+import (
+	v1 "birdcall/api/defi/v1"
+	"birdcall/internal/consts"
+	"birdcall/internal/service"
+	"context"
+	"encoding/json"
+	"io"
+	"net/http"
+	"net/url"
+	"os"
+	"strconv"
+)
+
+type sDefi struct{}
+
+func init() {
+	service.RegisterDefi(&sDefi{})
+
+}
+
+func (s *sDefi) Support(ctx context.Context, req *v1.SupportReq) (*v1.CommonRes, error) {
+	//构建基本请求连接
+	baseURL := os.Getenv(consts.DEFI_URL)
+	fullURL := baseURL + consts.NET_WORKS
+
+	apiReq, err := http.NewRequest("GET", fullURL, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	apiKey := os.Getenv(consts.BIRD_EYE_API_KEY)
+	//构建请求头
+	apiReq.Header.Add(consts.X_API_KEY, apiKey)
+
+	apiRes, err := http.DefaultClient.Do(apiReq)
+	if err != nil {
+		return nil, err
+	}
+
+	//处理结果
+	body, _ := io.ReadAll(apiRes.Body)
+	var commonRes *v1.CommonRes
+	err = json.Unmarshal(body, &commonRes)
+	if err != nil {
+		return nil, err
+	}
+	return commonRes, nil
+}
+
+func (s *sDefi) Price(ctx context.Context, req *v1.PriceReq) (*v1.CommonRes, error) {
+	//构建基本请求连接
+	baseURL := os.Getenv(consts.DEFI_URL)
+	params := url.Values{}
+	params.Add(consts.CheckLiquidity, strconv.FormatFloat(req.CheckLiquidity, 'f', 2, 64))
+	params.Add(consts.IncludeLiquidity, strconv.FormatBool(req.IncludeLiquidity))
+	params.Add(consts.ADDRESS, req.Address)
+	fullURL := baseURL + consts.PRICE + consts.QUESTION_MARK + params.Encode()
+
+	apiReq, err := http.NewRequest(consts.GET, fullURL, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	//构建请求头
+	apiReq = makeHeader(apiReq, req.Chain)
+
+	//发送请求
+	apiRes, err := http.DefaultClient.Do(apiReq)
+	if err != nil {
+		return nil, err
+	}
+
+	//处理结果
+	body, _ := io.ReadAll(apiRes.Body)
+	var commonRes *v1.CommonRes
+	err = json.Unmarshal(body, &commonRes)
+	if err != nil {
+		return nil, err
+	}
+	return commonRes, nil
+}
+
+func makeHeader(req *http.Request, chainName string) *http.Request {
+	apiKey := os.Getenv(consts.BIRD_EYE_API_KEY)
+
+	req.Header.Add(consts.ACCEPT, consts.APPLICATION_JSON)
+	req.Header.Add(consts.X_CHAIN, chainName)
+	req.Header.Add(consts.X_API_KEY, apiKey)
+	return req
+}

+ 4 - 10
api/hello/hello.go → internal/logic/logic.go

@@ -1,15 +1,9 @@
-// =================================================================================
+// ==========================================================================
 // Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
-// =================================================================================
+// ==========================================================================
 
-package hello
+package logic
 
 import (
-	"context"
-
-	"birdcall/api/hello/v1"
+	_ "birdcall/internal/logic/defi"
 )
-
-type IHelloV1 interface {
-	Hello(ctx context.Context, req *v1.HelloReq) (res *v1.HelloRes, err error)
-}

+ 33 - 0
internal/service/defi.go

@@ -0,0 +1,33 @@
+// ================================================================================
+// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
+// You can delete these comments if you wish manually maintain this interface file.
+// ================================================================================
+
+package service
+
+import (
+	v1 "birdcall/api/defi/v1"
+	"context"
+)
+
+type (
+	IDefi interface {
+		Support(ctx context.Context, req *v1.SupportReq) (*v1.CommonRes, error)
+		Price(ctx context.Context, req *v1.PriceReq) (*v1.CommonRes, error)
+	}
+)
+
+var (
+	localDefi IDefi
+)
+
+func Defi() IDefi {
+	if localDefi == nil {
+		panic("implement not found for interface IDefi, forgot register?")
+	}
+	return localDefi
+}
+
+func RegisterDefi(i IDefi) {
+	localDefi = i
+}

+ 11 - 0
main.go

@@ -3,11 +3,22 @@ package main
 import (
 	_ "birdcall/internal/packed"
 
+	_ "birdcall/internal/logic"
+
+	"github.com/joho/godotenv"
+
 	"github.com/gogf/gf/v2/os/gctx"
 
 	"birdcall/internal/cmd"
 )
 
 func main() {
+
+	//加载配置文件
+	err := godotenv.Load(".env")
+	if err != nil {
+		panic("Error loading .env file")
+	}
+
 	cmd.Main.Run(gctx.GetInitCtx())
 }