Browse Source

[dev]一次获取多个token价格

Zhangzhenhua 1 week ago
parent
commit
b07ae32e32
3 changed files with 45 additions and 7 deletions
  1. 2 2
      api/chain/v1/chain.go
  2. 3 0
      internal/consts/consts.go
  3. 40 5
      internal/library/birdeye/defi/defi.go

+ 2 - 2
api/chain/v1/chain.go

@@ -8,8 +8,8 @@ type NetWorkReq struct {
 
 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"`
+	Address   []string `v:"required" json:"email"  dc:"email"`
+	ChainName string   `v:"required" json:"chainName"  dc:"chainName"`
 }
 
 type CommonRes struct {

+ 3 - 0
internal/consts/consts.go

@@ -6,6 +6,7 @@ const (
 
 	NET_WORKS        string = "networks"
 	PRICE            string = "price"
+	MULTIPLE_PRICE   string = "multi_price"
 	QUESTION_MARK    string = "?"
 	X_API_KEY        string = "X-API-KEY"
 	DEFI_URL         string = "DEFI_URL"
@@ -16,4 +17,6 @@ const (
 	ACCEPT           string = "accept"
 	X_CHAIN          string = "x_chain"
 	APPLICATION_JSON string = "application/json"
+	LIST_ADDRESS     string = "list_address"
+	COMMAS           string = ","
 )

+ 40 - 5
internal/library/birdeye/defi/defi.go

@@ -6,6 +6,7 @@ import (
 	"net/url"
 	"os"
 	"strconv"
+	"strings"
 
 	"birdcall/internal/consts"
 	"birdcall/internal/library/birdeye"
@@ -13,10 +14,10 @@ import (
 )
 
 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:"是否返回流动数据" `
+	Address          []string ` json:"address" dc:"代币地址"`
+	Chain            string   ` json:"chain" dc:"链名"`
+	CheckLiquidity   float64  ` json:"check_liquidity" dc:"流动性验证"`
+	IncludeLiquidity bool     ` json:"include_liquidity" dc:"是否返回流动数据" `
 }
 
 func (d *Defi) Support() (*birdeye.CommonRes, error) {
@@ -52,7 +53,7 @@ func (d *Defi) Price() (*birdeye.CommonRes, error) {
 	params := url.Values{}
 	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)
+	params.Add(consts.ADDRESS, d.Address[0])
 	fullURL := baseURL + consts.PRICE + consts.QUESTION_MARK + params.Encode()
 
 	apiReq, err := http.NewRequest(consts.GET, fullURL, nil)
@@ -78,3 +79,37 @@ func (d *Defi) Price() (*birdeye.CommonRes, error) {
 	}
 	return commonRes, nil
 }
+
+func (d *Defi) MultiplePrice() (*birdeye.CommonRes, error) {
+	//构建基本请求连接
+	baseURL := os.Getenv(consts.DEFI_URL)
+	params := url.Values{}
+	params.Add(consts.CheckLiquidity, strconv.FormatFloat(d.CheckLiquidity, 'f', 2, 64))
+	params.Add(consts.IncludeLiquidity, strconv.FormatBool(d.IncludeLiquidity))
+	addresses := strings.Join(d.Address, consts.COMMAS)
+	params.Add(consts.LIST_ADDRESS, addresses)
+	fullURL := baseURL + consts.MULTIPLE_PRICE + consts.QUESTION_MARK + params.Encode()
+
+	apiReq, err := http.NewRequest(consts.GET, fullURL, nil)
+	if err != nil {
+		return nil, err
+	}
+
+	//构建请求头
+	apiReq = birdeye.MakeHeader(apiReq, d.Chain)
+
+	//发送请求
+	apiRes, err := http.DefaultClient.Do(apiReq)
+	if err != nil {
+		return nil, err
+	}
+
+	//处理结果
+	body, _ := io.ReadAll(apiRes.Body)
+	var commonRes *birdeye.CommonRes
+	err = json.Unmarshal(body, &commonRes)
+	if err != nil {
+		return nil, err
+	}
+	return commonRes, nil
+}