-
Notifications
You must be signed in to change notification settings - Fork 3
/
store.go
60 lines (52 loc) · 1.78 KB
/
store.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package v3
import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
v2liquidity "github.com/cosmosquad-labs/squad/v3/x/liquidity/legacy/v2"
"github.com/cosmosquad-labs/squad/v3/x/liquidity/types"
)
func MigrateOrders(store sdk.KVStore, cdc codec.BinaryCodec) error {
iter := sdk.KVStorePrefixIterator(store, types.OrderKeyPrefix)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var oldOrder v2liquidity.Order
if err := cdc.Unmarshal(iter.Value(), &oldOrder); err != nil {
return err
}
newOrder := types.Order{
// There's no way to determine whether the order was made through
// MsgLimitOrder or MsgMarketOrder, set the order type as OrderTypeLimit
// as a fallback.
Type: types.OrderTypeLimit,
Id: oldOrder.Id,
PairId: oldOrder.PairId,
MsgHeight: oldOrder.MsgHeight,
Orderer: oldOrder.Orderer,
// Only the type has changed, not the value, so simply type-cast here
Direction: types.OrderDirection(oldOrder.Direction),
OfferCoin: oldOrder.OfferCoin,
RemainingOfferCoin: oldOrder.RemainingOfferCoin,
ReceivedCoin: oldOrder.ReceivedCoin,
Price: oldOrder.Price,
Amount: oldOrder.Amount,
OpenAmount: oldOrder.OpenAmount,
BatchId: oldOrder.BatchId,
ExpireAt: oldOrder.ExpireAt,
// Only the type has changed, not the value, so simply type-cast here
Status: types.OrderStatus(oldOrder.Status),
}
bz, err := cdc.Marshal(&newOrder)
if err != nil {
return err
}
store.Set(iter.Key(), bz)
}
return nil
}
func MigrateStore(ctx sdk.Context, storeKey sdk.StoreKey, cdc codec.BinaryCodec) error {
store := ctx.KVStore(storeKey)
if err := MigrateOrders(store, cdc); err != nil {
return err
}
return nil
}