88 "math/big"
99 "net/http"
1010 "strconv"
11+ "strings"
1112 "sync"
1213 "time"
1314
@@ -16,6 +17,10 @@ import (
1617
1718const (
1819 bitgetTickerPath = "/api/v2/spot/market/tickers"
20+
21+ // StablecoinPrefix is used to mark stablecoins with fixed USD price
22+ // Format: "$1.0" means the token is pegged to $1.0 USD
23+ StablecoinPrefix = "$"
1924)
2025
2126// BitgetSDKPriceFeed uses Bitget REST API to fetch prices
@@ -63,6 +68,10 @@ func NewBitgetSDKPriceFeed(tokenMap map[uint16]string, baseURL string) *BitgetSD
6368
6469// GetTokenPrice returns token price in USD
6570// Note: Caller should ensure ETH price is updated via GetBatchTokenPrices for batch operations
71+ //
72+ // Stablecoin handling:
73+ // - If the symbol starts with "$" (e.g., "$1.0"), it's treated as a stablecoin with fixed price
74+ // - Example: "3:$1.0" means token ID 3 is a stablecoin pegged to $1.0 USD
6675func (b * BitgetSDKPriceFeed ) GetTokenPrice (ctx context.Context , tokenID uint16 ) (* TokenPrice , error ) {
6776 b .mu .RLock ()
6877 symbol , exists := b .tokenMap [tokenID ]
@@ -73,23 +82,43 @@ func (b *BitgetSDKPriceFeed) GetTokenPrice(ctx context.Context, tokenID uint16)
7382 return nil , fmt .Errorf ("token ID %d not mapped to trading pair" , tokenID )
7483 }
7584
76- // Fetch token price
77- tokenPrice , err := b .fetchPrice (ctx , symbol )
78- if err != nil {
79- return nil , fmt .Errorf ("failed to fetch price for %s: %w" , symbol , err )
80- }
81-
8285 // Use cached ETH price (should be updated by GetBatchTokenPrices)
8386 if ethPrice .Cmp (big .NewFloat (0 )) == 0 {
8487 return nil , fmt .Errorf ("ETH price not initialized, please call GetBatchTokenPrices first" )
8588 }
8689
87- b .log .Info ("Fetched price from Bitget" ,
88- "source" , "bitget" ,
89- "token_id" , tokenID ,
90- "symbol" , symbol ,
91- "token_price_usd" , tokenPrice .String (),
92- "eth_price_usd" , ethPrice .String ())
90+ var tokenPrice * big.Float
91+
92+ // Check if this is a stablecoin with fixed price (e.g., "$1.0")
93+ if strings .HasPrefix (symbol , StablecoinPrefix ) {
94+ priceStr := strings .TrimPrefix (symbol , StablecoinPrefix )
95+ fixedPrice , err := strconv .ParseFloat (priceStr , 64 )
96+ if err != nil {
97+ return nil , fmt .Errorf ("invalid stablecoin price format '%s': %w" , symbol , err )
98+ }
99+ tokenPrice = big .NewFloat (fixedPrice )
100+
101+ b .log .Info ("Using fixed stablecoin price" ,
102+ "source" , "stablecoin" ,
103+ "token_id" , tokenID ,
104+ "symbol" , symbol ,
105+ "token_price_usd" , tokenPrice .String (),
106+ "eth_price_usd" , ethPrice .String ())
107+ } else {
108+ // Fetch token price from exchange
109+ var err error
110+ tokenPrice , err = b .fetchPrice (ctx , symbol )
111+ if err != nil {
112+ return nil , fmt .Errorf ("failed to fetch price for %s: %w" , symbol , err )
113+ }
114+
115+ b .log .Info ("Fetched price from Bitget" ,
116+ "source" , "bitget" ,
117+ "token_id" , tokenID ,
118+ "symbol" , symbol ,
119+ "token_price_usd" , tokenPrice .String (),
120+ "eth_price_usd" , ethPrice .String ())
121+ }
93122
94123 return & TokenPrice {
95124 TokenID : tokenID ,
0 commit comments