-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathexpiration.go
59 lines (51 loc) · 1.25 KB
/
expiration.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
package server
import (
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/regen-network/regen-ledger/orm"
"github.com/regen-network/regen-ledger/x/ecocredit"
)
// PruneOrders checks if there are any expired sell or buy orders and removes them from state.
func (s serverImpl) PruneOrders(ctx sdk.Context) error {
blockTime := uint64(ctx.BlockTime().Add(time.Nanosecond).UnixNano())
minTime := uint64(0)
sellOrdersIter, err := s.sellOrderByExpirationIndex.PrefixScan(ctx, minTime, blockTime)
if err != nil {
return err
}
defer sellOrdersIter.Close()
var sellOrder ecocredit.SellOrder
for {
_, err := sellOrdersIter.LoadNext(&sellOrder)
if err != nil {
if orm.ErrIteratorDone.Is(err) {
break
}
return err
}
err = s.sellOrderTable.Delete(ctx, sellOrder.OrderId)
if err != nil {
return err
}
}
buyOrdersIter, err := s.buyOrderByExpirationIndex.PrefixScan(ctx, minTime, blockTime)
if err != nil {
return err
}
defer buyOrdersIter.Close()
var buyOrder ecocredit.BuyOrder
for {
_, err := buyOrdersIter.LoadNext(&buyOrder)
if err != nil {
if orm.ErrIteratorDone.Is(err) {
break
}
return err
}
err = s.buyOrderTable.Delete(ctx, buyOrder.BuyOrderId)
if err != nil {
return err
}
}
return nil
}