|
@@ -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
|
|
|
|
+}
|