Skip to content

Commit 8df4978

Browse files
author
corey
committed
fix price-threshold
1 parent d1289bb commit 8df4978

File tree

5 files changed

+35
-20
lines changed

5 files changed

+35
-20
lines changed

token-price-oracle/config/config.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,10 @@ func LoadConfig(ctx *cli.Context) (*Config, error) {
9999

100100
cfg.PriceThreshold = ctx.Uint64(flags.PriceThresholdFlag.Name)
101101

102-
// Validate price threshold is reasonable (percentage should be 0-100)
103-
if cfg.PriceThreshold > 100 {
104-
return nil, fmt.Errorf("price threshold %d is too large (should be 0-100 for percentage)", cfg.PriceThreshold)
102+
// Validate price threshold is reasonable (basis points should be 0-10000)
103+
// 10000 bps = 100%
104+
if cfg.PriceThreshold > 10000 {
105+
return nil, fmt.Errorf("price threshold %d is too large (should be 0-10000 basis points, where 10000 bps = 100%%)", cfg.PriceThreshold)
105106
}
106107

107108
// Parse and validate price feed priority list

token-price-oracle/env.example

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ TOKEN_PRICE_ORACLE_PRIVATE_KEY=ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efca
1212

1313
# Price update configuration
1414
TOKEN_PRICE_ORACLE_PRICE_UPDATE_INTERVAL=30s
15-
TOKEN_PRICE_ORACLE_PRICE_THRESHOLD=5 # percentage (%), e.g. 5 means 5% price change triggers update
15+
TOKEN_PRICE_ORACLE_PRICE_THRESHOLD=100 # basis points (bps), e.g. 100 means 1% (100 bps), 10 means 0.1%, 1 means 0.01%
1616

1717
# Price feed priority (comma-separated: bitget,binance)
1818
TOKEN_PRICE_ORACLE_PRICE_FEED_PRIORITY=bitget

token-price-oracle/flags/flags.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646

4747
PriceThresholdFlag = cli.Uint64Flag{
4848
Name: "price-threshold",
49-
Usage: "Price change threshold percentage to trigger update (e.g. 5 for 5%)",
49+
Usage: "Price change threshold in basis points (bps) to trigger update (e.g. 100 for 1%, 10 for 0.1%, 1 for 0.01%)",
5050
Value: 0,
5151
EnvVar: prefixEnvVar("PRICE_THRESHOLD"),
5252
}

token-price-oracle/local.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
--l2-eth-rpc http://localhost:8545 \
77
--private-key 0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 \
88
--price-update-interval 30s \
9-
--price-threshold 0 \
9+
--price-threshold 100 \
1010
--price-feed-priority bitget \
1111
--token-mapping-bitget "1:BGBUSDT,2:BTCUSDT" \
1212
--bitget-api-base-url https://api.bitget.com \
1313
--log-level info \
1414
--metrics-server-enable
1515

16+
# Price threshold examples (in basis points):
17+
# 1 bps = 0.01%, 10 bps = 0.1%, 100 bps = 1%, 500 bps = 5%, 1000 bps = 10%
18+

token-price-oracle/updater/token_price.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -339,8 +339,9 @@ func (u *PriceUpdater) updateBalanceMetrics(ctx context.Context) error {
339339
}
340340

341341
// shouldUpdatePrice checks if the price change exceeds the threshold
342-
// Formula: |newPrice - lastPrice| / lastPrice * 100 >= threshold
343-
// Example: if threshold is 5, price must change by at least 5% to trigger update
342+
// Formula: |newPrice - lastPrice| / lastPrice * 10000 >= threshold
343+
// Threshold is in basis points (bps): 1 bps = 0.01%, 100 bps = 1%, 10000 bps = 100%
344+
// Example: if threshold is 100 (bps), price must change by at least 1% to trigger update
344345
func (u *PriceUpdater) shouldUpdatePrice(lastPrice, newPrice *big.Int) bool {
345346
// Validate inputs
346347
if lastPrice == nil || newPrice == nil {
@@ -352,27 +353,37 @@ func (u *PriceUpdater) shouldUpdatePrice(lastPrice, newPrice *big.Int) bool {
352353
return true // Always update if no previous price
353354
}
354355

355-
// Validate threshold is reasonable (should be < 100 for percentage)
356-
// If threshold is unreasonably large, log warning and use default
356+
// Validate threshold is reasonable (should be <= 10000 for basis points)
357+
// If threshold is unreasonably large, log warning and cap at 100% (10000 bps)
357358
threshold := u.priceThreshold
358-
if threshold > 100 {
359-
log.Warn("Price threshold is unusually large, capping at 100%",
360-
"configured_threshold", threshold)
361-
threshold = 100
359+
if threshold > 10000 {
360+
log.Warn("Price threshold is unusually large, capping at 100% (10000 bps)",
361+
"configured_threshold", threshold,
362+
"capped_threshold", 10000)
363+
threshold = 10000
362364
}
363365

364366
// Calculate absolute difference: |newPrice - lastPrice|
365367
diff := new(big.Int).Sub(newPrice, lastPrice)
366368
diff.Abs(diff)
367369

368-
// Calculate percentage change: diff * 100 / lastPrice
369-
// This gives us the percentage as an integer (e.g., 5 for 5%)
370-
percentage := new(big.Int).Mul(diff, big.NewInt(100))
371-
percentage.Div(percentage, lastPrice)
370+
// Calculate change in basis points: diff * 10000 / lastPrice
371+
// This gives us the change in bps (e.g., 100 for 1%, 10 for 0.1%, 1 for 0.01%)
372+
bps := new(big.Int).Mul(diff, big.NewInt(10000))
373+
bps.Div(bps, lastPrice)
372374

373-
// Compare with threshold (both are percentages)
375+
// Compare with threshold (both are in basis points)
374376
thresholdBig := big.NewInt(int64(threshold))
375-
return percentage.Cmp(thresholdBig) >= 0
377+
shouldUpdate := bps.Cmp(thresholdBig) >= 0
378+
379+
log.Debug("Price change check",
380+
"last_price", lastPrice.String(),
381+
"new_price", newPrice.String(),
382+
"change_bps", bps.String(),
383+
"threshold_bps", threshold,
384+
"should_update", shouldUpdate)
385+
386+
return shouldUpdate
376387
}
377388

378389
// fetchTokenIDsFromContract fetches supported token IDs from L2TokenRegistry contract

0 commit comments

Comments
 (0)