-
Notifications
You must be signed in to change notification settings - Fork 193
/
convert.go
138 lines (117 loc) · 5.43 KB
/
convert.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package v2
import (
sdkmath "cosmossdk.io/math"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/golang/protobuf/proto" //nolint:staticcheck
icacallbacktypes "github.com/Stride-Labs/stride/v6/x/icacallbacks/types"
oldstakeibctypes "github.com/Stride-Labs/stride/v6/x/stakeibc/migrations/v2/types"
stakeibctypes "github.com/Stride-Labs/stride/v6/x/stakeibc/types"
)
const (
ICACallbackID_Delegate = "delegate"
ICACallbackID_Rebalance = "rebalance"
ICACallbackID_Undelegate = "undelegate"
)
func convertDelegateCallback(oldDelegateCallback oldstakeibctypes.DelegateCallback) stakeibctypes.DelegateCallback {
newSplitDelegations := []*stakeibctypes.SplitDelegation{}
for _, oldSplitDelegation := range oldDelegateCallback.SplitDelegations {
newSplitDelegation := stakeibctypes.SplitDelegation{
Validator: oldSplitDelegation.Validator,
Amount: sdkmath.NewIntFromUint64(oldSplitDelegation.Amount),
}
newSplitDelegations = append(newSplitDelegations, &newSplitDelegation)
}
return stakeibctypes.DelegateCallback{
HostZoneId: oldDelegateCallback.HostZoneId,
DepositRecordId: oldDelegateCallback.DepositRecordId,
SplitDelegations: newSplitDelegations,
}
}
func convertUndelegateCallback(oldUndelegateCallback oldstakeibctypes.UndelegateCallback) stakeibctypes.UndelegateCallback {
newSplitDelegations := []*stakeibctypes.SplitDelegation{}
for _, oldSplitDelegation := range oldUndelegateCallback.SplitDelegations {
newSplitDelegation := stakeibctypes.SplitDelegation{
Validator: oldSplitDelegation.Validator,
Amount: sdkmath.NewIntFromUint64(oldSplitDelegation.Amount),
}
newSplitDelegations = append(newSplitDelegations, &newSplitDelegation)
}
return stakeibctypes.UndelegateCallback{
HostZoneId: oldUndelegateCallback.HostZoneId,
SplitDelegations: newSplitDelegations,
EpochUnbondingRecordIds: oldUndelegateCallback.EpochUnbondingRecordIds,
}
}
func convertRebalanceCallback(oldRebalanceCallback oldstakeibctypes.RebalanceCallback) stakeibctypes.RebalanceCallback {
newRebalancings := []*stakeibctypes.Rebalancing{}
for _, oldRebalancing := range oldRebalanceCallback.Rebalancings {
newRebalancing := stakeibctypes.Rebalancing{
SrcValidator: oldRebalancing.SrcValidator,
DstValidator: oldRebalancing.DstValidator,
Amt: sdkmath.NewIntFromUint64(oldRebalancing.Amt),
}
newRebalancings = append(newRebalancings, &newRebalancing)
}
return stakeibctypes.RebalanceCallback{
HostZoneId: oldRebalanceCallback.HostZoneId,
Rebalancings: newRebalancings,
}
}
func convertCallbackData(oldCallbackData icacallbacktypes.CallbackData) (icacallbacktypes.CallbackData, error) {
var newCallbackArgs []byte
switch oldCallbackData.CallbackId {
case ICACallbackID_Delegate:
// Deserialize the callback args with the old DelegateCallback type
oldDelegateCallback := oldstakeibctypes.DelegateCallback{}
if err := proto.Unmarshal(oldCallbackData.CallbackArgs, &oldDelegateCallback); err != nil {
return icacallbacktypes.CallbackData{}, sdkerrors.Wrapf(stakeibctypes.ErrUnmarshalFailure, err.Error())
}
// Convert and serialize with the new DelegateCallback type
newDelegateCallback := convertDelegateCallback(oldDelegateCallback)
newDelegateCallbackBz, err := proto.Marshal(&newDelegateCallback)
if err != nil {
return icacallbacktypes.CallbackData{}, sdkerrors.Wrapf(stakeibctypes.ErrMarshalFailure, err.Error())
}
// Update the CallbackData with the new args
newCallbackArgs = newDelegateCallbackBz
case ICACallbackID_Undelegate:
// Deserialize the callback args with the old UndelegateCallback type
oldUndelegateCallback := oldstakeibctypes.UndelegateCallback{}
if err := proto.Unmarshal(oldCallbackData.CallbackArgs, &oldUndelegateCallback); err != nil {
return icacallbacktypes.CallbackData{}, sdkerrors.Wrapf(stakeibctypes.ErrUnmarshalFailure, err.Error())
}
// Convert and serialize with the new UndelegateCallback type
newUndelegateCallback := convertUndelegateCallback(oldUndelegateCallback)
newUndelegateCallbackBz, err := proto.Marshal(&newUndelegateCallback)
if err != nil {
return icacallbacktypes.CallbackData{}, sdkerrors.Wrapf(stakeibctypes.ErrMarshalFailure, err.Error())
}
// Update the CallbackData with the new args
newCallbackArgs = newUndelegateCallbackBz
case ICACallbackID_Rebalance:
// Deserialize the callback args with the old RebalanceCallback type
oldRebalanceCallback := oldstakeibctypes.RebalanceCallback{}
if err := proto.Unmarshal(oldCallbackData.CallbackArgs, &oldRebalanceCallback); err != nil {
return icacallbacktypes.CallbackData{}, sdkerrors.Wrapf(stakeibctypes.ErrUnmarshalFailure, err.Error())
}
// Convert and serialize with the new RebalanceCallback type
newRebalanceCallback := convertRebalanceCallback(oldRebalanceCallback)
newRebalanceCallbackBz, err := proto.Marshal(&newRebalanceCallback)
if err != nil {
return icacallbacktypes.CallbackData{}, sdkerrors.Wrapf(stakeibctypes.ErrMarshalFailure, err.Error())
}
// Update the CallbackData with the new args
newCallbackArgs = newRebalanceCallbackBz
default:
newCallbackArgs = oldCallbackData.CallbackArgs
}
newCallbackData := icacallbacktypes.CallbackData{
CallbackKey: oldCallbackData.CallbackKey,
PortId: oldCallbackData.PortId,
ChannelId: oldCallbackData.ChannelId,
Sequence: oldCallbackData.Sequence,
CallbackId: oldCallbackData.CallbackId,
CallbackArgs: newCallbackArgs,
}
return newCallbackData, nil
}