11package keeper
22
33import (
4+ "fmt"
45 "sort"
56 "time"
67
@@ -24,69 +25,42 @@ func (k Keeper) GetCurrentPrice(ctx sdk.Context, assetCode dnTypes.AssetCode) ty
2425 return price
2526}
2627
27- // GetRawPrices fetches the set of all prices posted by oracles for an asset and specific blockHeight .
28- func (k Keeper ) GetRawPrices (ctx sdk.Context , assetCode dnTypes. AssetCode , blockHeight int64 ) []types. PostedPrice {
28+ // GetCurrentPricesList returns all current prices .
29+ func (k Keeper ) GetCurrentPricesList (ctx sdk.Context ) (types. CurrentPrices , error ) {
2930 k .modulePerms .AutoCheck (types .PermRead )
3031
3132 store := ctx .KVStore (k .storeKey )
32- bz := store .Get (types .GetRawPricesKey (assetCode , blockHeight ))
33+ iterator := sdk .KVStorePrefixIterator (store , types .GetCurrentPricePrefix ())
34+ defer iterator .Close ()
3335
34- var prices []types.PostedPrice
35- k .cdc .MustUnmarshalBinaryBare (bz , & prices )
36+ currentPrices := types.CurrentPrices {}
3637
37- return prices
38- }
38+ for ; iterator .Valid (); iterator .Next () {
39+ cPrice := types.CurrentPrice {}
40+ if err := k .cdc .UnmarshalBinaryBare (iterator .Value (), & cPrice ); err != nil {
41+ err = fmt .Errorf ("order unmarshal: %w" , err )
42+ return nil , err
43+ }
44+ currentPrices = append (currentPrices , cPrice )
45+ }
3946
40- // SetPrice updates the posted price for a specific oracle.
41- func (k Keeper ) SetPrice (
42- ctx sdk.Context ,
43- oracle sdk.AccAddress ,
44- assetCode dnTypes.AssetCode ,
45- price sdk.Int ,
46- receivedAt time.Time ) (types.PostedPrice , error ) {
47+ return currentPrices , nil
48+ }
4749
50+ // addCurrentPrice adds currentPrice item to the storage.
51+ func (k Keeper ) addCurrentPrice (ctx sdk.Context , currentPrice types.CurrentPrice ) {
4852 k .modulePerms .AutoCheck (types .PermWrite )
4953
50- // validate price receivedAt timestamp comparing to the current blockHeight timestamp
51- if err := k .checkPriceReceivedAtTimestamp (ctx , receivedAt ); err != nil {
52- return types.PostedPrice {}, err
53- }
54-
55- // find raw price for specified oracle
5654 store := ctx .KVStore (k .storeKey )
57- prices := k .GetRawPrices (ctx , assetCode , ctx .BlockHeight ())
58- var index int
59- found := false
60- for i := range prices {
61- if prices [i ].OracleAddress .Equals (oracle ) {
62- index = i
63- found = true
64- break
65- }
66- }
6755
68- // set the rawPrice for that particular oracle
69- if found {
70- prices [index ] = types.PostedPrice {
71- AssetCode : assetCode , OracleAddress : oracle ,
72- Price : price , ReceivedAt : receivedAt }
73- } else {
74- prices = append (prices , types.PostedPrice {
75- AssetCode : assetCode , OracleAddress : oracle ,
76- Price : price , ReceivedAt : receivedAt })
77- index = len (prices ) - 1
78- }
79-
80- store .Set (types .GetRawPricesKey (assetCode , ctx .BlockHeight ()), k .cdc .MustMarshalBinaryBare (prices ))
81-
82- return prices [index ], nil
56+ bz := k .cdc .MustMarshalBinaryBare (currentPrice )
57+ store .Set (types .GetCurrentPriceKey (currentPrice .AssetCode ), bz )
8358}
8459
8560// SetCurrentPrices updates the price of an asset to the median of all valid oracle inputs and cleans up previous inputs.
8661func (k Keeper ) SetCurrentPrices (ctx sdk.Context ) error {
8762 k .modulePerms .AutoCheck (types .PermWrite )
8863
89- store := ctx .KVStore (k .storeKey )
9064 assets := k .GetAssetParams (ctx )
9165
9266 updatesCnt := 0
@@ -144,7 +118,7 @@ func (k Keeper) SetCurrentPrices(ctx sdk.Context) error {
144118 ReceivedAt : medianReceivedAt ,
145119 }
146120
147- store . Set ( types . GetCurrentPriceKey ( assetCode ), k . cdc . MustMarshalBinaryBare ( newPrice ) )
121+ k . addCurrentPrice ( ctx , newPrice )
148122
149123 // save price to VM storage
150124 priceVmAccessPath , priceVmValue := types .NewResPriceStorageValuesPanic (newPrice .AssetCode , newPrice .Price )
@@ -162,6 +136,64 @@ func (k Keeper) SetCurrentPrices(ctx sdk.Context) error {
162136 return nil
163137}
164138
139+ // GetRawPrices fetches the set of all prices posted by oracles for an asset and specific blockHeight.
140+ func (k Keeper ) GetRawPrices (ctx sdk.Context , assetCode dnTypes.AssetCode , blockHeight int64 ) []types.PostedPrice {
141+ k .modulePerms .AutoCheck (types .PermRead )
142+
143+ store := ctx .KVStore (k .storeKey )
144+ bz := store .Get (types .GetRawPricesKey (assetCode , blockHeight ))
145+
146+ var prices []types.PostedPrice
147+ k .cdc .MustUnmarshalBinaryBare (bz , & prices )
148+
149+ return prices
150+ }
151+
152+ // SetPrice updates the posted price for a specific oracle.
153+ func (k Keeper ) SetPrice (
154+ ctx sdk.Context ,
155+ oracle sdk.AccAddress ,
156+ assetCode dnTypes.AssetCode ,
157+ price sdk.Int ,
158+ receivedAt time.Time ) (types.PostedPrice , error ) {
159+
160+ k .modulePerms .AutoCheck (types .PermWrite )
161+
162+ // validate price receivedAt timestamp comparing to the current blockHeight timestamp
163+ if err := k .checkPriceReceivedAtTimestamp (ctx , receivedAt ); err != nil {
164+ return types.PostedPrice {}, err
165+ }
166+
167+ // find raw price for specified oracle
168+ store := ctx .KVStore (k .storeKey )
169+ prices := k .GetRawPrices (ctx , assetCode , ctx .BlockHeight ())
170+ var index int
171+ found := false
172+ for i := range prices {
173+ if prices [i ].OracleAddress .Equals (oracle ) {
174+ index = i
175+ found = true
176+ break
177+ }
178+ }
179+
180+ // set the rawPrice for that particular oracle
181+ if found {
182+ prices [index ] = types.PostedPrice {
183+ AssetCode : assetCode , OracleAddress : oracle ,
184+ Price : price , ReceivedAt : receivedAt }
185+ } else {
186+ prices = append (prices , types.PostedPrice {
187+ AssetCode : assetCode , OracleAddress : oracle ,
188+ Price : price , ReceivedAt : receivedAt })
189+ index = len (prices ) - 1
190+ }
191+
192+ store .Set (types .GetRawPricesKey (assetCode , ctx .BlockHeight ()), k .cdc .MustMarshalBinaryBare (prices ))
193+
194+ return prices [index ], nil
195+ }
196+
165197// nolint:errcheck
166198// ValidatePostPrice makes sure the person posting the price is an oracle.
167199func (k Keeper ) ValidatePostPrice (ctx sdk.Context , msg types.MsgPostPrice ) error {
0 commit comments