-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriptions.go
95 lines (73 loc) · 2.83 KB
/
subscriptions.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
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/furya-official/fury-blockchain/x/payments/types"
)
// -------------------------------------------------------- Subscriptions Get/Set
func (k Keeper) GetSubscriptionIterator(ctx sdk.Context) sdk.Iterator {
store := ctx.KVStore(k.storeKey)
return sdk.KVStorePrefixIterator(store, types.SubscriptionKeyPrefix)
}
func (k Keeper) MustGetSubscriptionByKey(ctx sdk.Context, key []byte) types.Subscription {
store := ctx.KVStore(k.storeKey)
if !store.Has(key) {
panic("subscription not found")
}
bz := store.Get(key)
var subscription types.Subscription
k.cdc.MustUnmarshalLengthPrefixed(bz, &subscription)
return subscription
}
func (k Keeper) SubscriptionExists(ctx sdk.Context, subscriptionId string) bool {
store := ctx.KVStore(k.storeKey)
return store.Has(types.GetSubscriptionKey(subscriptionId))
}
func (k Keeper) GetSubscription(ctx sdk.Context, subscriptionId string) (types.Subscription, error) {
store := ctx.KVStore(k.storeKey)
key := types.GetSubscriptionKey(subscriptionId)
bz := store.Get(key)
if bz == nil {
return types.Subscription{}, fmt.Errorf("invalid subscription")
}
var subscription types.Subscription
k.cdc.MustUnmarshalLengthPrefixed(bz, &subscription)
return subscription, nil
}
func (k Keeper) SetSubscription(ctx sdk.Context, subscription types.Subscription) {
store := ctx.KVStore(k.storeKey)
key := types.GetSubscriptionKey(subscription.Id)
store.Set(key, k.cdc.MustMarshalLengthPrefixed(&subscription))
}
// -------------------------------------------------------- Subscriptions Payment
func (k Keeper) EffectSubscriptionPayment(ctx sdk.Context, subscriptionId string) error {
subscription, err := k.GetSubscription(ctx, subscriptionId)
if err != nil {
return err
}
// Check if should effect
if !subscription.ShouldEffect(ctx) {
return types.ErrTriedToEffectSubscriptionPaymentWhenShouldnt
}
// Effect payment
effected, err := k.EffectPayment(ctx, k.bankKeeper, subscription.PaymentContractId)
if err != nil {
return err
}
// If max number of periods has not been reached, then the payment (if any)
// was due to current period, so we can move to the next period. Otherwise,
// it means we're tackling accumulated periods, and if payment was actually
// effected, then we should deduct one from the accumulated periods.
if !subscription.MaxPeriodsReached() {
subscription.NextPeriod(effected)
} else if effected {
subscription.PeriodsAccumulated =
subscription.PeriodsAccumulated.Sub(sdk.OneUint())
}
// If the payment was not effected (assuming err == nil) this is because (i)
// the payer does not have enough coins, or (ii) because the payment *cannot*
// be effected (i.e. maximum payment reached, contract not authorised, etc.)
// Update subscription
k.SetSubscription(ctx, subscription)
return nil
}