/
atomic_order.go
156 lines (139 loc) · 5.03 KB
/
atomic_order.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package keeper
import (
"encoding/binary"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/sideprotocol/ibcswap/v6/modules/apps/100-atomic-swap/types"
)
// GetAuctionCount get the total number of auction
func (k Keeper) GetAtomicOrderCount(ctx sdk.Context) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKeyCountKey)
byteKey := types.OTCOrderBookKeyIndexKey
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// GetAuctionCount get the total number of auction
func (k Keeper) GetAtomicOrderCountByOrderId(ctx sdk.Context, orderId string) uint64 {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKeyIndexKey)
byteKey := []byte(orderId)
bz := store.Get(byteKey)
// Count doesn't exist: no element
if bz == nil {
return 0
}
// Parse bytes
return binary.BigEndian.Uint64(bz)
}
// SetAuctionCount set the total number of auction
func (k Keeper) SetAtomicOrderCount(ctx sdk.Context, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKeyCountKey)
byteKey := types.OTCOrderBookKeyIndexKey
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// SetAuctionCount set the total number of auction
func (k Keeper) SetAtomicOrderCountToOrderID(ctx sdk.Context, orderId string, count uint64) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKeyIndexKey)
byteKey := []byte(orderId)
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, count)
store.Set(byteKey, bz)
}
// AppendAuction appends a auction in the store with a new id and update the count
func (k Keeper) AppendAtomicOrder(
ctx sdk.Context,
order types.Order,
) uint64 {
// Create the auction
count := k.GetAtomicOrderCount(ctx)
// Set the ID of the appended value
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
appendedValue := k.cdc.MustMarshal(&order)
store.Set(GetOrderIDBytes(count), appendedValue)
// Update auction count
k.SetAtomicOrderCountToOrderID(ctx, order.Id, count)
k.SetAtomicOrderCount(ctx, count+1)
return count
}
// SetAuction set a specific auction in the store
func (k Keeper) SetAtomicOrder(ctx sdk.Context, order types.Order) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
b := k.cdc.MustMarshal(&order)
id := k.GetAtomicOrderCountByOrderId(ctx, order.Id)
store.Set(GetOrderIDBytes(id), b)
}
// GetAuction returns a auction from its id
func (k Keeper) GetAtomicOrder(ctx sdk.Context, orderId string) (val types.Order, found bool) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
id := k.GetAtomicOrderCountByOrderId(ctx, orderId)
b := store.Get(GetOrderIDBytes(id))
if b == nil {
return val, false
}
k.cdc.MustUnmarshal(b, &val)
return val, true
}
// RemoveAuction removes a auction from the store
func (k Keeper) RemoveOrder(ctx sdk.Context, orderId string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
id := k.GetAtomicOrderCountByOrderId(ctx, orderId)
store.Delete(GetOrderIDBytes(id))
}
// GetAllAuction returns all auction
func (k Keeper) GetAllOrder(ctx sdk.Context) (list []types.Order) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
iterator := sdk.KVStorePrefixIterator(store, []byte{})
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var val types.Order
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}
return
}
// GetAuctionIDBytes returns the byte representation of the ID
func GetOrderIDBytes(id uint64) []byte {
bz := make([]byte, 8)
binary.BigEndian.PutUint64(bz, id)
return bz
}
// GetAuctionIDFromBytes returns ID in uint64 format from a byte array
func GetBidIDFromBytes(bz []byte) uint64 {
return binary.BigEndian.Uint64(bz)
}
func (k Keeper) MoveOrderToBottom(ctx sdk.Context, orderId string) error {
// Step 1: Retrieve the order based on the given ID.
order, found := k.GetAtomicOrder(ctx, orderId)
if !found {
return types.ErrNotFoundOrder
}
// Step 2: Remove the original order from its position.
k.RemoveOrder(ctx, orderId)
// Step 3: Append the order to the end of the list.
// (Since AppendAtomicOrder manages the order count and index mapping,
// we can reuse this function to simplify our logic.)
k.AppendAtomicOrder(ctx, order)
return nil
}
func (k Keeper) TrimExcessOrders(ctx sdk.Context) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.OTCOrderBookKey)
totalCount := k.GetAtomicOrderCount(ctx)
if totalCount <= types.MaxOrderCount {
return
}
// Calculate number of items to be removed
excess := totalCount - types.MaxOrderCount
for i := uint64(0); i < excess; i++ {
// As items are appended, to remove from the bottom, we need to remove the items
// starting from totalCount - i (i.e., the last item in the list, then the second last, etc.)
idToRemove := totalCount - i - 1
store.Delete(GetOrderIDBytes(idToRemove))
k.SetAtomicOrderCount(ctx, totalCount-i-1)
}
}