Skip to content

Commit 65a050b

Browse files
author
corey
committed
add Stablecoin
1 parent c5ebf10 commit 65a050b

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

token-price-oracle/client/bitget_sdk.go

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"math/big"
99
"net/http"
1010
"strconv"
11+
"strings"
1112
"sync"
1213
"time"
1314

@@ -16,6 +17,10 @@ import (
1617

1718
const (
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
6675
func (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,

token-price-oracle/env.example

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ TOKEN_PRICE_ORACLE_PRICE_THRESHOLD=100 # basis points (bps), e.g. 100 means 1%
1818
TOKEN_PRICE_ORACLE_PRICE_FEED_PRIORITY=bitget
1919

2020
# Token mapping for Bitget (tokenID:tradingPair,tokenID:tradingPair)
21-
TOKEN_PRICE_ORACLE_TOKEN_MAPPING_BITGET=1:BGBUSDT,2:BTCUSDT
22-
23-
# Token mapping for Binance (optional)
24-
# TOKEN_PRICE_ORACLE_TOKEN_MAPPING_BINANCE=1:BGBUSDT,2:BTCUSDT
21+
# Format:
22+
# - Regular tokens: tokenID:SYMBOL (e.g., 1:BGBUSDT, 2:BTCUSDT)
23+
# - Stablecoins: tokenID:$PRICE (e.g., 3:$1.0 for USDT pegged to $1 USD)
24+
# Example: 1:BGBUSDT,2:BTCUSDT,3:$1.0,4:$0.9999
25+
TOKEN_PRICE_ORACLE_TOKEN_MAPPING_BITGET=1:BGBUSDT,2:BTCUSDT,3:$1.0
26+
27+
# Token mapping for Binance (optional, same format as Bitget)
28+
# TOKEN_PRICE_ORACLE_TOKEN_MAPPING_BINANCE=1:BGBUSDT,2:BTCUSDT,3:$1.0
2529

2630
# API base URLs (optional, defaults provided)
2731
TOKEN_PRICE_ORACLE_BITGET_API_BASE_URL=https://api.bitget.com

token-price-oracle/local.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
--price-update-interval 30s \
99
--price-threshold 100 \
1010
--price-feed-priority bitget \
11-
--token-mapping-bitget "1:BGBUSDT,2:BTCUSDT" \
11+
--token-mapping-bitget "1:BGBUSDT,2:BTCUSDT,3:\$1.0" \
1212
--bitget-api-base-url https://api.bitget.com \
1313
--log-level info \
1414
--metrics-server-enable
1515

1616
# Price threshold examples (in basis points):
1717
# 1 bps = 0.01%, 10 bps = 0.1%, 100 bps = 1%, 500 bps = 5%, 1000 bps = 10%
1818

19+
# Token mapping format:
20+
# - Regular tokens: tokenID:SYMBOL (e.g., 1:BGBUSDT, 2:BTCUSDT)
21+
# - Stablecoins: tokenID:$PRICE (e.g., 3:$1.0 for USDT pegged to $1 USD)
22+
# Note: Use \$ in bash to escape the dollar sign
23+

0 commit comments

Comments
 (0)