-
Notifications
You must be signed in to change notification settings - Fork 193
/
icacallbacks_reinvest.go
82 lines (72 loc) · 2.95 KB
/
icacallbacks_reinvest.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
package keeper
import (
"fmt"
epochtypes "github.com/Stride-Labs/stride/x/epochs/types"
"github.com/Stride-Labs/stride/x/icacallbacks"
icacallbackstypes "github.com/Stride-Labs/stride/x/icacallbacks/types"
recordstypes "github.com/Stride-Labs/stride/x/records/types"
"github.com/Stride-Labs/stride/x/stakeibc/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
"github.com/golang/protobuf/proto"
)
func (k Keeper) MarshalReinvestCallbackArgs(ctx sdk.Context, reinvestCallback types.ReinvestCallback) ([]byte, error) {
out, err := proto.Marshal(&reinvestCallback)
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("MarshalReinvestCallbackArgs %v", err.Error()))
return nil, err
}
return out, nil
}
func (k Keeper) UnmarshalReinvestCallbackArgs(ctx sdk.Context, reinvestCallback []byte) (*types.ReinvestCallback, error) {
unmarshalledReinvestCallback := types.ReinvestCallback{}
if err := proto.Unmarshal(reinvestCallback, &unmarshalledReinvestCallback); err != nil {
k.Logger(ctx).Error(fmt.Sprintf("UnmarshalReinvestCallbackArgs %s", err.Error()))
return nil, err
}
return &unmarshalledReinvestCallback, nil
}
func ReinvestCallback(k Keeper, ctx sdk.Context, packet channeltypes.Packet, ack *channeltypes.Acknowledgement, args []byte) error {
k.Logger(ctx).Info("ReinvestCallback executing", "packet", packet)
if ack == nil {
// handle timeout
k.Logger(ctx).Error(fmt.Sprintf("ReinvestCallback timeout, ack is nil, packet %v", packet))
return nil
}
txMsgData, err := icacallbacks.GetTxMsgData(ctx, *ack, k.Logger(ctx))
if err != nil {
k.Logger(ctx).Error(fmt.Sprintf("failed to fetch txMsgData, packet %v", packet))
return sdkerrors.Wrap(icacallbackstypes.ErrTxMsgData, err.Error())
}
if len(txMsgData.Data) == 0 {
// handle tx failure
k.Logger(ctx).Error(fmt.Sprintf("ReinvestCallback tx failed, txMsgData is empty, ack error, packet %v", packet))
return nil
}
// deserialize the args
reinvestCallback, err := k.UnmarshalReinvestCallbackArgs(ctx, args)
if err != nil {
return err
}
amount := reinvestCallback.ReinvestAmount.Amount
denom := reinvestCallback.ReinvestAmount.Denom
// fetch epoch
strideEpochTracker, found := k.GetEpochTracker(ctx, epochtypes.STRIDE_EPOCH)
if !found {
k.Logger(ctx).Error("failed to find epoch")
return sdkerrors.Wrapf(types.ErrInvalidLengthEpochTracker, "no number for epoch (%s)", epochtypes.STRIDE_EPOCH)
}
epochNumber := strideEpochTracker.EpochNumber
// create a new record so that rewards are reinvested
record := recordstypes.DepositRecord{
Amount: amount.Int64(),
Denom: denom,
HostZoneId: reinvestCallback.HostZoneId,
Status: recordstypes.DepositRecord_STAKE,
Source: recordstypes.DepositRecord_WITHDRAWAL_ICA,
DepositEpochNumber: epochNumber,
}
k.RecordsKeeper.AppendDepositRecord(ctx, record)
return nil
}