-
Notifications
You must be signed in to change notification settings - Fork 210
/
hooks.go
91 lines (73 loc) · 1.96 KB
/
hooks.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
package hooks
import (
sdk "github.com/cosmos/cosmos-sdk/types"
dtypes "github.com/akash-network/akash-api/go/node/deployment/v1beta3"
etypes "github.com/akash-network/akash-api/go/node/escrow/v1beta3"
mtypes "github.com/akash-network/akash-api/go/node/market/v1beta4"
)
type Hooks interface {
OnEscrowAccountClosed(ctx sdk.Context, obj etypes.Account)
OnEscrowPaymentClosed(ctx sdk.Context, obj etypes.FractionalPayment)
}
type hooks struct {
dkeeper DeploymentKeeper
mkeeper MarketKeeper
}
func New(dkeeper DeploymentKeeper, mkeeper MarketKeeper) Hooks {
return &hooks{
dkeeper: dkeeper,
mkeeper: mkeeper,
}
}
func (h *hooks) OnEscrowAccountClosed(ctx sdk.Context, obj etypes.Account) {
id, found := dtypes.DeploymentIDFromEscrowAccount(obj.ID)
if !found {
return
}
deployment, found := h.dkeeper.GetDeployment(ctx, id)
if !found {
return
}
if deployment.State != dtypes.DeploymentActive {
return
}
h.dkeeper.CloseDeployment(ctx, deployment)
gstate := dtypes.GroupClosed
if obj.State == etypes.AccountOverdrawn {
gstate = dtypes.GroupInsufficientFunds
}
for _, group := range h.dkeeper.GetGroups(ctx, deployment.ID()) {
if group.ValidateClosable() == nil {
_ = h.dkeeper.OnCloseGroup(ctx, group, gstate)
h.mkeeper.OnGroupClosed(ctx, group.ID())
}
}
}
func (h *hooks) OnEscrowPaymentClosed(ctx sdk.Context, obj etypes.FractionalPayment) {
id, ok := mtypes.LeaseIDFromEscrowAccount(obj.AccountID, obj.PaymentID)
if !ok {
return
}
bid, ok := h.mkeeper.GetBid(ctx, id.BidID())
if !ok {
return
}
if bid.State != mtypes.BidActive {
return
}
order, ok := h.mkeeper.GetOrder(ctx, id.OrderID())
if !ok {
return
}
lease, ok := h.mkeeper.GetLease(ctx, id)
if !ok {
return
}
h.mkeeper.OnOrderClosed(ctx, order)
h.mkeeper.OnBidClosed(ctx, bid)
if obj.State == etypes.PaymentOverdrawn {
h.mkeeper.OnLeaseClosed(ctx, lease, mtypes.LeaseInsufficientFunds)
} else {
h.mkeeper.OnLeaseClosed(ctx, lease, mtypes.LeaseClosed)
}
}