Browse Source

[fix]birdeye工具包封装

Zhangzhenhua 2 weeks ago
parent
commit
9fb4f55a3a

+ 1 - 1
.env

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

+ 16 - 0
api/chain/chain.go

@@ -0,0 +1,16 @@
+// =================================================================================
+// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT.
+// =================================================================================
+
+package chain
+
+import (
+	"context"
+
+	"birdcall/api/chain/v1"
+)
+
+type IChainV1 interface {
+	NetWork(ctx context.Context, req *v1.NetWorkReq) (res *v1.CommonRes, err error)
+	TokenPrice(ctx context.Context, req *v1.TokenPriceReq) (res *v1.CommonRes, err error)
+}

+ 18 - 0
api/chain/v1/chain.go

@@ -0,0 +1,18 @@
+package v1
+
+import "github.com/gogf/gf/v2/frame/g"
+
+type NetWorkReq struct {
+	g.Meta `path:"/netWork" tags:"netWork" method:"get" summary:"netWork"`
+}
+
+type TokenPriceReq struct {
+	g.Meta    `path:"/price" tags:"price" method:"post" summary:"price"`
+	Address   string `v:"required" json:"email"  dc:"email"`
+	ChainName string `v:"required" json:"chainName"  dc:"chainName"`
+}
+
+type CommonRes struct {
+	Success bool        `json:"success" dc:"是否成功"`
+	Data    interface{} `json:"data"    dc:"数据"`
+}

+ 0 - 12
api/defi/defi.go

@@ -1,12 +0,0 @@
-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)
-}

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

@@ -1,22 +0,0 @@
-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:"数据"`
-}

+ 3 - 4
internal/cmd/cmd.go

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

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

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

+ 3 - 3
internal/controller/defi/defi_new.go → internal/controller/chain/chain_new.go

@@ -2,14 +2,14 @@
 // This is auto-generated by GoFrame CLI tool only once. Fill this file as you wish.
 // =================================================================================
 
-package defi
+package chain
 
 import (
-	"birdcall/api/defi"
+	"birdcall/api/chain"
 )
 
 type ControllerV1 struct{}
 
-func NewV1() defi.IDefiV1 {
+func NewV1() chain.IChainV1 {
 	return &ControllerV1{}
 }

+ 16 - 0
internal/controller/chain/chain_v1_net_work.go

@@ -0,0 +1,16 @@
+package chain
+
+import (
+	"context"
+
+	v1 "birdcall/api/chain/v1"
+	"birdcall/internal/service"
+)
+
+func (c *ControllerV1) NetWork(ctx context.Context, req *v1.NetWorkReq) (res *v1.CommonRes, err error) {
+	res, err = service.Chain().NetWork(ctx, req)
+	if err != nil {
+		return nil, err
+	}
+	return res, nil
+}

+ 16 - 0
internal/controller/chain/chain_v1_token_price.go

@@ -0,0 +1,16 @@
+package chain
+
+import (
+	"context"
+
+	v1 "birdcall/api/chain/v1"
+	"birdcall/internal/service"
+)
+
+func (c *ControllerV1) TokenPrice(ctx context.Context, req *v1.TokenPriceReq) (res *v1.CommonRes, err error) {
+	res, err = service.Chain().TokenPrice(ctx, req)
+	if err != nil {
+		return nil, err
+	}
+	return res, nil
+}

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

@@ -1,19 +0,0 @@
-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
-}

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

@@ -1,19 +0,0 @@
-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
-}

+ 21 - 0
internal/library/birdeye/common.go

@@ -0,0 +1,21 @@
+package birdeye
+
+import (
+	"birdcall/internal/consts"
+	"net/http"
+	"os"
+)
+
+type CommonRes struct {
+	Success bool        `json:"success" dc:"是否成功"`
+	Data    interface{} `json:"data"    dc:"数据"`
+}
+
+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
+}

+ 18 - 30
internal/logic/defi/defi.go → internal/library/birdeye/defi/defi.go

@@ -1,26 +1,25 @@
 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{})
+	"birdcall/internal/consts"
+	"birdcall/internal/library/birdeye"
+	"encoding/json"
+)
 
+type Defi struct {
+	Address          string  ` json:"address" dc:"代币地址"`
+	Chain            string  ` json:"chain" dc:"链名"`
+	CheckLiquidity   float64 ` json:"check_liquidity" dc:"流动性验证"`
+	IncludeLiquidity bool    ` json:"include_liquidity" dc:"是否返回流动数据" `
 }
 
-func (s *sDefi) Support(ctx context.Context, req *v1.SupportReq) (*v1.CommonRes, error) {
+func (d *Defi) Support() (*birdeye.CommonRes, error) {
 	//构建基本请求连接
 	baseURL := os.Getenv(consts.DEFI_URL)
 	fullURL := baseURL + consts.NET_WORKS
@@ -29,10 +28,8 @@ func (s *sDefi) Support(ctx context.Context, req *v1.SupportReq) (*v1.CommonRes,
 	if err != nil {
 		return nil, err
 	}
-
-	apiKey := os.Getenv(consts.BIRD_EYE_API_KEY)
 	//构建请求头
-	apiReq.Header.Add(consts.X_API_KEY, apiKey)
+	apiReq = birdeye.MakeHeader(apiReq, d.Chain)
 
 	apiRes, err := http.DefaultClient.Do(apiReq)
 	if err != nil {
@@ -41,7 +38,7 @@ func (s *sDefi) Support(ctx context.Context, req *v1.SupportReq) (*v1.CommonRes,
 
 	//处理结果
 	body, _ := io.ReadAll(apiRes.Body)
-	var commonRes *v1.CommonRes
+	var commonRes *birdeye.CommonRes
 	err = json.Unmarshal(body, &commonRes)
 	if err != nil {
 		return nil, err
@@ -49,13 +46,13 @@ func (s *sDefi) Support(ctx context.Context, req *v1.SupportReq) (*v1.CommonRes,
 	return commonRes, nil
 }
 
-func (s *sDefi) Price(ctx context.Context, req *v1.PriceReq) (*v1.CommonRes, error) {
+func (d *Defi) Price() (*birdeye.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)
+	params.Add(consts.CheckLiquidity, strconv.FormatFloat(d.CheckLiquidity, 'f', 2, 64))
+	params.Add(consts.IncludeLiquidity, strconv.FormatBool(d.IncludeLiquidity))
+	params.Add(consts.ADDRESS, d.Address)
 	fullURL := baseURL + consts.PRICE + consts.QUESTION_MARK + params.Encode()
 
 	apiReq, err := http.NewRequest(consts.GET, fullURL, nil)
@@ -64,7 +61,7 @@ func (s *sDefi) Price(ctx context.Context, req *v1.PriceReq) (*v1.CommonRes, err
 	}
 
 	//构建请求头
-	apiReq = makeHeader(apiReq, req.Chain)
+	apiReq = birdeye.MakeHeader(apiReq, d.Chain)
 
 	//发送请求
 	apiRes, err := http.DefaultClient.Do(apiReq)
@@ -74,19 +71,10 @@ func (s *sDefi) Price(ctx context.Context, req *v1.PriceReq) (*v1.CommonRes, err
 
 	//处理结果
 	body, _ := io.ReadAll(apiRes.Body)
-	var commonRes *v1.CommonRes
+	var commonRes *birdeye.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
-}

+ 1 - 0
internal/library/birdeye/pair/pair.go

@@ -0,0 +1 @@
+package pair

+ 1 - 0
internal/library/birdeye/search/search.go

@@ -0,0 +1 @@
+package search

+ 1 - 0
internal/library/birdeye/token/token.go

@@ -0,0 +1 @@
+package token

+ 1 - 0
internal/library/birdeye/trader/trader.go

@@ -0,0 +1 @@
+package trader

+ 1 - 0
internal/library/birdeye/wallet/wallet.go

@@ -0,0 +1 @@
+package wallet

+ 51 - 0
internal/logic/chain/chain.go

@@ -0,0 +1,51 @@
+package chain
+
+import (
+	v1 "birdcall/api/chain/v1"
+	"birdcall/internal/library/birdeye/defi"
+	"birdcall/internal/service"
+	"context"
+
+	"github.com/gogf/gf/v2/util/gconv"
+)
+
+type sChain struct{}
+
+func init() {
+	service.RegisterChain(&sChain{})
+}
+
+func (s *sChain) NetWork(ctx context.Context, req *v1.NetWorkReq) (*v1.CommonRes, error) {
+	dm := new(defi.Defi)
+	birdRes, err := dm.Support()
+	if err != nil {
+		return nil, err
+	}
+	var res *v1.CommonRes
+	err = gconv.Struct(birdRes, &res)
+	if err != nil {
+		return nil, err
+	}
+
+	return res, nil
+}
+
+func (s *sChain) TokenPrice(ctx context.Context, req *v1.TokenPriceReq) (*v1.CommonRes, error) {
+	dm := new(defi.Defi)
+	dm.Address = req.Address
+	dm.Chain = req.ChainName
+	dm.CheckLiquidity = 100
+	dm.IncludeLiquidity = true
+
+	birdRes, err := dm.Price()
+	if err != nil {
+		return nil, err
+	}
+
+	var res *v1.CommonRes
+	err = gconv.Struct(birdRes, &res)
+	if err != nil {
+		return nil, err
+	}
+	return res, nil
+}

+ 1 - 1
internal/logic/logic.go

@@ -5,5 +5,5 @@
 package logic
 
 import (
-	_ "birdcall/internal/logic/defi"
+	_ "birdcall/internal/logic/chain"
 )

+ 11 - 11
internal/service/defi.go → internal/service/chain.go

@@ -6,28 +6,28 @@
 package service
 
 import (
-	v1 "birdcall/api/defi/v1"
+	v1 "birdcall/api/chain/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)
+	IChain interface {
+		NetWork(ctx context.Context, req *v1.NetWorkReq) (*v1.CommonRes, error)
+		TokenPrice(ctx context.Context, req *v1.TokenPriceReq) (*v1.CommonRes, error)
 	}
 )
 
 var (
-	localDefi IDefi
+	localChain IChain
 )
 
-func Defi() IDefi {
-	if localDefi == nil {
-		panic("implement not found for interface IDefi, forgot register?")
+func Chain() IChain {
+	if localChain == nil {
+		panic("implement not found for interface IChain, forgot register?")
 	}
-	return localDefi
+	return localChain
 }
 
-func RegisterDefi(i IDefi) {
-	localDefi = i
+func RegisterChain(i IChain) {
+	localChain = i
 }