-
Notifications
You must be signed in to change notification settings - Fork 54
/
batch.go
78 lines (74 loc) · 2.41 KB
/
batch.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/crescent-network/crescent/v5/x/liquidity/types"
)
// ExecuteRequests executes all orders, deposit requests and withdraw requests.
// ExecuteRequests also handles order expiration.
func (k Keeper) ExecuteRequests(ctx sdk.Context) {
if err := k.IterateAllPairs(ctx, func(pair types.Pair) (stop bool, err error) {
if err := k.ExecuteMatching(ctx, pair); err != nil {
return false, err
}
return false, nil
}); err != nil {
panic(err)
}
if err := k.IterateAllOrders(ctx, func(order types.Order) (stop bool, err error) {
if order.Status.CanBeExpired() && order.ExpiredAt(ctx.BlockTime()) {
if err := k.FinishOrder(ctx, order, types.OrderStatusExpired); err != nil {
return false, err
}
} else if types.IsTooSmallOrderAmount(order.OpenAmount, order.Price) {
// TODO: should we introduce new order status for this type of expiration?
if err := k.FinishOrder(ctx, order, types.OrderStatusExpired); err != nil {
return false, err
}
}
return false, nil
}); err != nil {
panic(err)
}
if err := k.IterateAllDepositRequests(ctx, func(req types.DepositRequest) (stop bool, err error) {
if req.Status == types.RequestStatusNotExecuted {
if err := k.ExecuteDepositRequest(ctx, req); err != nil {
return false, err
}
}
return false, nil
}); err != nil {
panic(err)
}
if err := k.IterateAllWithdrawRequests(ctx, func(req types.WithdrawRequest) (stop bool, err error) {
if req.Status == types.RequestStatusNotExecuted {
if err := k.ExecuteWithdrawRequest(ctx, req); err != nil {
return false, err
}
}
return false, nil
}); err != nil {
panic(err)
}
}
// DeleteOutdatedRequests deletes outdated(should be deleted) requests.
// Determining if a request should be deleted is based on its status.
func (k Keeper) DeleteOutdatedRequests(ctx sdk.Context) {
_ = k.IterateAllDepositRequests(ctx, func(req types.DepositRequest) (stop bool, err error) {
if req.Status.ShouldBeDeleted() {
k.DeleteDepositRequest(ctx, req)
}
return false, nil
})
_ = k.IterateAllWithdrawRequests(ctx, func(req types.WithdrawRequest) (stop bool, err error) {
if req.Status.ShouldBeDeleted() {
k.DeleteWithdrawRequest(ctx, req)
}
return false, nil
})
_ = k.IterateAllOrders(ctx, func(order types.Order) (stop bool, err error) {
if order.Status.ShouldBeDeleted() {
k.DeleteOrder(ctx, order)
}
return false, nil
})
}