/
transient_store.go
79 lines (69 loc) · 4.2 KB
/
transient_store.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
package keeper
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gridironOne/gstake-native/v2/x/lscosmos/types"
)
// SetIBCTransientStore sets tokens that are in ibc transition
func (k Keeper) SetIBCTransientStore(ctx sdk.Context, ibcAmountTransientStore types.IBCAmountTransientStore) {
store := ctx.KVStore(k.storeKey)
store.Set(types.IBCTransientStoreKey, k.cdc.MustMarshal(&ibcAmountTransientStore))
}
// GetIBCTransientStore gets tokens that are in ibc transition
func (k Keeper) GetIBCTransientStore(ctx sdk.Context) types.IBCAmountTransientStore {
store := ctx.KVStore(k.storeKey)
var ibcAmountTransientStore types.IBCAmountTransientStore
k.cdc.MustUnmarshal(store.Get(types.IBCTransientStoreKey), &ibcAmountTransientStore)
return ibcAmountTransientStore
}
// AddIBCTransferToTransientStore adds ibctransfer tokens that are in ibc transition
// CONTRACT: to be used atomically with IBCTransfer of tokens from delegation account to it's host counterpart
func (k Keeper) AddIBCTransferToTransientStore(ctx sdk.Context, amount sdk.Coin) {
transientStore := k.GetIBCTransientStore(ctx)
transientStore.IBCTransfer = transientStore.IBCTransfer.Add(amount)
k.SetIBCTransientStore(ctx, transientStore)
}
// RemoveIBCTransferFromTransientStore removes ibctransfer tokens that are in ibc transition
// CONTRACT: to be used atomically with AddBalanceToDelegationState
func (k Keeper) RemoveIBCTransferFromTransientStore(ctx sdk.Context, amount sdk.Coin) {
transientStore := k.GetIBCTransientStore(ctx)
transientStore.IBCTransfer = transientStore.IBCTransfer.Sub(sdk.NewCoins(amount)...)
k.SetIBCTransientStore(ctx, transientStore)
}
// AddICADelegateToTransientStore adds ibctransfer tokens that are in ibc transition
// CONTRACT: to be used atomically with RemoveBalanceFromDelegationState
func (k Keeper) AddICADelegateToTransientStore(ctx sdk.Context, amount sdk.Coin) {
transientStore := k.GetIBCTransientStore(ctx)
if !transientStore.ICADelegate.IsValid() || transientStore.ICADelegate.IsZero() { // only because initialised coin is invalid
transientStore.ICADelegate = amount
} else {
transientStore.ICADelegate = transientStore.ICADelegate.Add(amount)
}
k.SetIBCTransientStore(ctx, transientStore)
}
// RemoveICADelegateFromTransientStore removes ibctransfer tokens that are in ibc transition
// Contract: to be used atomically with AddHostAccountDelegation and AddBalanceToDelegationState(incase of failed txns)
func (k Keeper) RemoveICADelegateFromTransientStore(ctx sdk.Context, amount sdk.Coin) {
transientStore := k.GetIBCTransientStore(ctx)
transientStore.ICADelegate = transientStore.ICADelegate.Sub(amount)
k.SetIBCTransientStore(ctx, transientStore)
}
// AddUndelegationTransferToTransientStore adds ibctransfer tokens that are in ibc transition from host chain to controller chain
// CONTRACT: to be used atomically with RemoveHostAccountUndelegation (after successful undelegations)
func (k Keeper) AddUndelegationTransferToTransientStore(ctx sdk.Context, undelegationTransfer types.TransientUndelegationTransfer) {
transientStore := k.GetIBCTransientStore(ctx)
transientStore.UndelegatonCompleteIBCTransfer = append(transientStore.UndelegatonCompleteIBCTransfer, undelegationTransfer)
k.SetIBCTransientStore(ctx, transientStore)
}
// RemoveUndelegationTransferFromTransientStore removes ibctransfer tokens that are in ibc transition from host chain to controller chain
// Contract: to be used atomically with MatureUnbondingEpochCValue (after successful undelegations) and AddHostAccountUndelegation ( after failed ICA+IBC txn - matured undelegations)
func (k Keeper) RemoveUndelegationTransferFromTransientStore(ctx sdk.Context, amount sdk.Coin) (types.TransientUndelegationTransfer, error) {
transientStore := k.GetIBCTransientStore(ctx)
for i, undelegationTransfer := range transientStore.UndelegatonCompleteIBCTransfer {
if undelegationTransfer.AmountUnbonded.IsEqual(amount) {
transientStore.UndelegatonCompleteIBCTransfer = append(transientStore.UndelegatonCompleteIBCTransfer[:i], transientStore.UndelegatonCompleteIBCTransfer[i+1:]...)
k.SetIBCTransientStore(ctx, transientStore)
return undelegationTransfer, nil
}
}
return types.TransientUndelegationTransfer{}, types.ErrTransientUndelegationTransferNotFound
}