Skip to content

Commit 94227b1

Browse files
committed
patch to prevent price exploitation
1 parent 1fbb3fd commit 94227b1

File tree

4 files changed

+73
-29
lines changed

4 files changed

+73
-29
lines changed

x/oracle/keeper/abci.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ func (k Keeper) EndBlock(ctx sdk.Context) {
1010
assetInfos := k.GetAllAssetInfo(ctx)
1111

1212
for _, info := range assetInfos {
13-
allAssetPrice := k.GetAllAssetPrice(ctx, info.Display)
13+
allAssetPrice := k.GetAllAssetPrice(ctx, info.Display, true)
1414
total := len(allAssetPrice)
1515

1616
// Need to sort it because order fetched from GetAllAssetPrice will not be in ascending order - depending on source

x/oracle/keeper/abci_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ func (suite *KeeperTestSuite) TestEndBlock() {
103103
return ethPriceData[i].Timestamp < ethPriceData[j].Timestamp
104104
})
105105

106-
allPrices := suite.app.OracleKeeper.GetAllAssetPrice(suite.ctx, "BTC")
106+
allPrices := suite.app.OracleKeeper.GetAllAssetPrice(suite.ctx, "BTC", true)
107107
suite.Require().Equal(1, len(allPrices))
108108
suite.Require().Equal(btcPriceData[len(btcPriceData)-1], allPrices[0])
109109

110-
allPrices = suite.app.OracleKeeper.GetAllAssetPrice(suite.ctx, "ETH")
110+
allPrices = suite.app.OracleKeeper.GetAllAssetPrice(suite.ctx, "ETH", true)
111111
suite.Require().Equal(1, len(allPrices))
112112
suite.Require().Equal(ethPriceData[len(ethPriceData)-1], allPrices[0])
113113
}

x/oracle/keeper/price.go

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,33 @@ import (
1212
"sort"
1313
)
1414

15+
var pricesMap = map[string]math.LegacyDec{
16+
"SCRT": math.LegacyMustNewDecFromStr("0.1603"),
17+
"KAVA": math.LegacyMustNewDecFromStr("0.1687"),
18+
"WETH": math.LegacyMustNewDecFromStr("3797.49"),
19+
"SAGA": math.LegacyMustNewDecFromStr("0.1268"),
20+
"ATOM": math.LegacyMustNewDecFromStr("3.05"),
21+
"NTRN": math.LegacyMustNewDecFromStr("0.05013"),
22+
"XRP": math.LegacyMustNewDecFromStr("2.38"),
23+
"FET": math.LegacyMustNewDecFromStr("0.3877"),
24+
"INJ": math.LegacyMustNewDecFromStr("8.95"),
25+
"ONDO": math.LegacyMustNewDecFromStr("0.7362"),
26+
"TIA": math.LegacyMustNewDecFromStr("0.9503"),
27+
"USDT": math.LegacyMustNewDecFromStr("1.0"),
28+
"OSMO": math.LegacyMustNewDecFromStr("0.1106"),
29+
"AKT": math.LegacyMustNewDecFromStr("0.7323"),
30+
"USDC": math.LegacyMustNewDecFromStr("0.9998"),
31+
"OM": math.LegacyMustNewDecFromStr("0.1084"),
32+
"PAXG": math.LegacyMustNewDecFromStr("3981.96"),
33+
"WBTC": math.LegacyMustNewDecFromStr("112534.90"),
34+
"LINK": math.LegacyMustNewDecFromStr("17.51"),
35+
"BABY": math.LegacyMustNewDecFromStr("0.03276"),
36+
"XION": math.LegacyMustNewDecFromStr("0.5202"),
37+
}
38+
39+
var _startHeight int64 = 6947415
40+
var _endHeight = _startHeight + 5
41+
1542
// SetPrice set a specific price in the store from its index
1643
func (k Keeper) SetPrice(ctx sdk.Context, price types.Price) {
1744
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
@@ -82,40 +109,57 @@ func (k Keeper) GetAllPrice(ctx sdk.Context) (list []types.Price) {
82109
return
83110
}
84111

85-
func (k Keeper) GetAllAssetPrice(ctx sdk.Context, asset string) (list []types.Price) {
86-
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.PriceKeyPrefixAsset(asset))
87-
iterator := storetypes.KVStorePrefixIterator(store, []byte{})
112+
func (k Keeper) GetAllAssetPrice(ctx sdk.Context, asset string, endBlocker bool) (list []types.Price) {
113+
if !endBlocker && ctx.BlockHeight() > _startHeight && ctx.BlockHeight() <= _endHeight {
114+
for key, value := range pricesMap {
115+
p := types.Price{
116+
Asset: key,
117+
Price: value,
118+
Source: "elys",
119+
Provider: "elys16qgewtplahkqwqa0aqv4pxnxa58ulu48k6crhj",
120+
Timestamp: uint64(ctx.BlockTime().Unix()),
121+
BlockHeight: uint64(ctx.BlockHeight()),
122+
}
123+
list = append(list, p)
124+
}
125+
} else {
126+
store := prefix.NewStore(runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx)), types.PriceKeyPrefixAsset(asset))
127+
iterator := storetypes.KVStorePrefixIterator(store, []byte{})
88128

89-
defer iterator.Close()
129+
defer iterator.Close()
90130

91-
for ; iterator.Valid(); iterator.Next() {
92-
var val types.Price
93-
k.cdc.MustUnmarshal(iterator.Value(), &val)
94-
list = append(list, val)
131+
for ; iterator.Valid(); iterator.Next() {
132+
var val types.Price
133+
k.cdc.MustUnmarshal(iterator.Value(), &val)
134+
list = append(list, val)
135+
}
95136
}
96-
97137
return
98138
}
99139

100140
func (k Keeper) GetAssetPrice(ctx sdk.Context, asset string) (math.LegacyDec, bool) {
101-
// try out elys source
102-
val, found := k.GetLatestPriceFromAssetAndSource(ctx, asset, types.ELYS)
103-
if found {
104-
return val.Price, true
105-
}
141+
if ctx.BlockHeight() > _startHeight && ctx.BlockHeight() <= _endHeight {
142+
return pricesMap[asset], true
143+
} else {
144+
// try out elys source
145+
val, found := k.GetLatestPriceFromAssetAndSource(ctx, asset, types.ELYS)
146+
if found {
147+
return val.Price, true
148+
}
106149

107-
// try out band source
108-
val, found = k.GetLatestPriceFromAssetAndSource(ctx, asset, types.BAND)
109-
if found {
110-
return val.Price, true
111-
}
150+
// try out band source
151+
val, found = k.GetLatestPriceFromAssetAndSource(ctx, asset, types.BAND)
152+
if found {
153+
return val.Price, true
154+
}
112155

113-
// find from any source if band source does not exist
114-
price, found := k.GetLatestPriceFromAnySource(ctx, asset)
115-
if found {
116-
return price.Price, true
156+
// find from any source if band source does not exist
157+
price, found := k.GetLatestPriceFromAnySource(ctx, asset)
158+
if found {
159+
return price.Price, true
160+
}
161+
return math.LegacyDec{}, false
117162
}
118-
return math.LegacyDec{}, false
119163
}
120164

121165
func (k Keeper) GetDenomPrice(ctx sdk.Context, denom string) osmomath.BigDec {
@@ -134,7 +178,7 @@ func (k Keeper) GetDenomPrice(ctx sdk.Context, denom string) osmomath.BigDec {
134178
}
135179

136180
func (k Keeper) DeleteAXLPrices(ctx sdk.Context) {
137-
allAssetPrice := k.GetAllAssetPrice(ctx, "AXL")
181+
allAssetPrice := k.GetAllAssetPrice(ctx, "AXL", true)
138182
total := len(allAssetPrice)
139183

140184
// Need to sort it because order fetched from GetAllAssetPrice will not be in ascending order - depending on source

x/oracle/keeper/price_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func (suite *KeeperTestSuite) TestGetAllAssetPrice() {
156156
for _, price := range priceData {
157157
suite.app.OracleKeeper.SetPrice(suite.ctx, price)
158158
}
159-
allPrices := suite.app.OracleKeeper.GetAllAssetPrice(suite.ctx, "BTC")
159+
allPrices := suite.app.OracleKeeper.GetAllAssetPrice(suite.ctx, "BTC", true)
160160
suite.Require().Equal(len(priceData), len(allPrices))
161161
}
162162

0 commit comments

Comments
 (0)