Browse Source

[dev]wallet部分接口实现

Zhangzhenhua 1 week ago
parent
commit
f113e97362
4 changed files with 153 additions and 24 deletions
  1. 2 1
      .env
  2. 28 23
      internal/consts/consts.go
  3. 76 0
      internal/library/birdeye/wallet/wallet.go
  4. 47 0
      internal/model/birdeye/wallet/wallet.go

+ 2 - 1
.env

@@ -1,3 +1,4 @@
 BIRD_EYE_API_KEY = "b738f9a666c843a4a658211b523cb690"
 DEFI_URL = "https://public-api.birdeye.so/defi/"
-TOKEN_LIST_V2_URL = "https://public-api.birdeye.so/defi/v2/tokens/all"
+TOKEN_LIST_V2_URL = "https://public-api.birdeye.so/defi/v2/tokens/all"
+WALLET_URL = "https://public-api.birdeye.so/v1/wallet/"

+ 28 - 23
internal/consts/consts.go

@@ -4,27 +4,32 @@ const (
 	GET  string = "GET"
 	POST string = "POST"
 
-	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"
-	TOKEN_URL         string = "DEFI_URL"
-	TOKEN_LIST_V2_URL string = "TOKEN_LIST_V2_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"
-	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"
+	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"
+	TOKEN_URL            string = "DEFI_URL"
+	WALLET_URL           string = "WALLET_URL"
+	TOKEN_LIST_V2_URL    string = "TOKEN_LIST_V2_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"
+	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"
+	LIST_SUPPORTED_CHAIN string = "list_supported_chain"
+	TX_LIST              string = "tx_list"
+	WALLET               string = "wallet"
+	BEFORE               string = "before"
 )

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

@@ -1 +1,77 @@
 package wallet
+
+import (
+	"birdcall/internal/consts"
+	"birdcall/internal/library/birdeye"
+	w1 "birdcall/internal/model/birdeye/wallet"
+	"encoding/json"
+	"net/http"
+	"net/url"
+	"os"
+	"strconv"
+)
+
+type Wallet struct{}
+
+func (w *Wallet) Support() (*w1.SupportRes, error) {
+	//构建基本请求连接
+	baseURL := os.Getenv(consts.WALLET_URL)
+	fullURL := baseURL + consts.LIST_SUPPORTED_CHAIN
+
+	apiReq, err := http.NewRequest(consts.GET, fullURL, nil)
+	if err != nil {
+		return nil, err
+	}
+	//构建请求头
+	apiReq = birdeye.MakeHeader(apiReq)
+
+	body, err := birdeye.SendRequest(apiReq)
+	if err != nil {
+		return nil, err
+	}
+
+	//处理返回数据
+	var res *w1.SupportRes
+	err = json.Unmarshal([]byte(body), &res)
+	if err != nil {
+		return nil, err
+	}
+	return res, nil
+}
+
+//	 TransactionHistory
+//		@param wallet
+//		@param limit
+//		@param before
+//		@return *w1.TransactionHistoryRes
+//		@return error
+func (w *Wallet) TransactionHistory(wallet string, limit int64, before string) (*w1.TransactionHistoryRes, error) {
+	//构建基本请求连接
+	baseURL := os.Getenv(consts.WALLET_URL)
+	params := url.Values{}
+	params.Add(consts.WALLET, wallet)
+	params.Add(consts.LIMIT, strconv.FormatInt(limit, 10))
+	params.Add(consts.BEFORE, before)
+
+	fullURL := baseURL + consts.TX_LIST + consts.QUESTION_MARK + params.Encode()
+
+	apiReq, err := http.NewRequest(consts.GET, fullURL, nil)
+	if err != nil {
+		return nil, err
+	}
+	//构建请求头
+	apiReq = birdeye.MakeHeader(apiReq)
+
+	body, err := birdeye.SendRequest(apiReq)
+	if err != nil {
+		return nil, err
+	}
+
+	//处理返回数据
+	var res *w1.TransactionHistoryRes
+	err = json.Unmarshal([]byte(body), &res)
+	if err != nil {
+		return nil, err
+	}
+	return res, nil
+}

+ 47 - 0
internal/model/birdeye/wallet/wallet.go

@@ -0,0 +1,47 @@
+package wallet
+
+type SupportRes struct {
+	Success bool     `json:"success" dc:"是否成功"`
+	Data    []string `json:"data"`
+}
+
+type TransactionHistoryRes struct {
+	Success bool `json:"success" dc:"请求是否成功"`
+	Data    Data `json:"data" dc:"交易数据"`
+}
+
+type ContractLabelMetadata struct {
+	Icon string `json:"icon" dc:"合约标签元数据图标"` // 图标 URL
+}
+
+type ContractLabel struct {
+	Address  string                `json:"address" dc:"合约地址"`
+	Name     string                `json:"name" dc:"合约名称"`
+	Metadata ContractLabelMetadata `json:"metadata" dc:"合约标签元数据"`
+}
+
+type BalanceChange struct {
+	Amount   float64 `json:"amount" dc:"余额变动金额"`
+	Symbol   string  `json:"symbol" dc:"代币符号"`
+	Name     string  `json:"name" dc:"代币名称"`
+	Decimals int     `json:"decimals" dc:"代币精度"`
+	Address  string  `json:"address" dc:"代币地址"`
+	LogoURI  string  `json:"logoURI" dc:"代币Logo链接"`
+}
+
+type Transaction struct {
+	TxHash        string          `json:"txHash" dc:"交易哈希"`
+	BlockNumber   uint64          `json:"blockNumber" dc:"区块编号"`
+	BlockTime     string          `json:"blockTime" dc:"区块时间"`
+	Status        bool            `json:"status" dc:"交易状态"`
+	From          string          `json:"from" dc:"发送方"`
+	To            string          `json:"to" dc:"接收方"`
+	Fee           int             `json:"fee" dc:"交易费用"`
+	MainAction    string          `json:"mainAction" dc:"主要操作"`
+	BalanceChange []BalanceChange `json:"balanceChange" dc:"余额变动"`
+	ContractLabel ContractLabel   `json:"contractLabel" dc:"合约标签"`
+}
+
+type Data struct {
+	Solana []Transaction `json:"solana" dc:"Solana链的交易数据"`
+}