Prechádzať zdrojové kódy

[dev] token获取列表工具实现

Zhangzhenhua 2 týždňov pred
rodič
commit
605843b3e3

+ 7 - 0
internal/consts/consts.go

@@ -10,6 +10,7 @@ const (
 	QUESTION_MARK    string = "?"
 	X_API_KEY        string = "X-API-KEY"
 	DEFI_URL         string = "DEFI_URL"
+	TOKEN_URL        string = "DEFI_URL"
 	BIRD_EYE_API_KEY string = "BIRD_EYE_API_KEY"
 	CheckLiquidity   string = "check_liquidity"
 	IncludeLiquidity string = "include_liquidity"
@@ -19,4 +20,10 @@ const (
 	APPLICATION_JSON string = "application/json"
 	LIST_ADDRESS     string = "list_address"
 	COMMAS           string = ","
+	SORT_BY          string = "sort_by"
+	SORT_TYPE        string = "sort_type"
+	OFFSET           string = "offset"
+	LIMIT            string = "limit"
+	MIN_LIQUIDITY    string = "min_liquidity"
+	TOKEN_LIST       string = "tokenlist"
 )

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

@@ -1 +1,56 @@
 package token
+
+import (
+	"birdcall/internal/consts"
+	"birdcall/internal/library/birdeye"
+	"birdcall/internal/model/birdeye/token"
+	"encoding/json"
+	"net/http"
+	"net/url"
+	"os"
+	"strconv"
+)
+
+type Token struct{}
+
+// GetListV1
+//
+//	@param chain_name
+//	@param sort_by
+//	@param sort_type
+//	@param offset
+//	@param limit
+//	@param min_liquidity
+//	@return *token.ListRes
+//	@return error
+func (t *Token) GetListV1(chain_name string, sort_by string, sort_type string, offset uint64, limit uint64, min_liquidity uint64) (*token.ListRes, error) {
+	//构建基本请求连接
+	baseURL := os.Getenv(consts.TOKEN_URL)
+	params := url.Values{}
+	params.Add(consts.SORT_BY, sort_by)
+	params.Add(consts.SORT_TYPE, sort_type)
+	params.Add(consts.OFFSET, strconv.FormatUint(offset, 10))
+	params.Add(consts.LIMIT, strconv.FormatUint(limit, 10))
+	params.Add(consts.MIN_LIQUIDITY, strconv.FormatUint(min_liquidity, 10))
+
+	fullURL := baseURL + consts.TOKEN_LIST + consts.QUESTION_MARK + params.Encode()
+	apiReq, err := http.NewRequest(consts.GET, fullURL, nil)
+	if err != nil {
+		return nil, err
+	}
+	//构建请求头
+	apiReq = birdeye.MakeHeader(apiReq, chain_name)
+
+	body, err := birdeye.SendRequest(apiReq)
+	if err != nil {
+		return nil, err
+	}
+
+	//处理返回数据
+	var res *token.ListRes
+	err = json.Unmarshal([]byte(body), &res)
+	if err != nil {
+		return nil, err
+	}
+	return res, nil
+}

+ 3 - 2
internal/logic/chain/chain.go

@@ -2,8 +2,8 @@ package chain
 
 import (
 	v1 "birdcall/api/chain/v1"
-	"birdcall/internal/consts"
 	"birdcall/internal/library/birdeye/defi"
+	"birdcall/internal/library/birdeye/token"
 	"birdcall/internal/service"
 	"context"
 
@@ -17,6 +17,7 @@ func init() {
 }
 
 var dm defi.Defi
+var tk token.Token
 
 func (s *sChain) NetWork(ctx context.Context, req *v1.NetWorkReq) (*v1.CommonRes, error) {
 
@@ -34,7 +35,7 @@ func (s *sChain) NetWork(ctx context.Context, req *v1.NetWorkReq) (*v1.CommonRes
 }
 
 func (s *sChain) TokenPrice(ctx context.Context, req *v1.TokenPriceReq) (*v1.CommonRes, error) {
-	birdRes, err := dm.MultiplePrice(req.ChainName, 100, true, req.Address, consts.GET)
+	birdRes, err := tk.GetListV1(req.ChainName, "v24hUSD", "desc", 0, 50, 100)
 	if err != nil {
 		return nil, err
 	}

+ 26 - 0
internal/model/birdeye/token/token.go

@@ -0,0 +1,26 @@
+package token
+
+type ListRes struct {
+	Success bool `json:"success" dc:"是否成功"`
+	Data    List `json:"data"`
+}
+
+type List struct {
+	UpdateUnixTime uint32  `json:"updateUnixTime"`
+	UpdateTime     uint32  `json:"updateime"`
+	Tokens         []Token `json:"tokens"`
+	Total          uint64  `json:"total"`
+}
+
+type Token struct {
+	Address           string  `json:"address" dc:"代币地址"`
+	Decimals          int     `json:"decimals" dc:"小数位数"`
+	LastTradeUnixTime int64   `json:"lastTradeUnixTime" dc:"最后交易时间的 Unix 时间戳"`
+	Liquidity         float64 `json:"liquidity" dc:"流动性"`
+	LogoURI           string  `json:"logoURI" dc:"Logo URI"`
+	MC                float64 `json:"mc" dc:"市值"`
+	Name              string  `json:"name" dc:"代币名称"`
+	Symbol            string  `json:"symbol" dc:"代币符号"`
+	V24hChangePercent float64 `json:"v24hChangePercent" dc:"24小时价格变动百分比"`
+	V24hUSD           float64 `json:"v24hUSD" dc:"24小时交易额USD"`
+}