-
Notifications
You must be signed in to change notification settings - Fork 194
/
msg_server_register_host_zone.go
247 lines (221 loc) · 11 KB
/
msg_server_register_host_zone.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package keeper
import (
"context"
"fmt"
sdkmath "cosmossdk.io/math"
icatypes "github.com/cosmos/ibc-go/v7/modules/apps/27-interchain-accounts/types"
"github.com/Stride-Labs/stride/v17/utils"
epochtypes "github.com/Stride-Labs/stride/v17/x/epochs/types"
recordstypes "github.com/Stride-Labs/stride/v17/x/records/types"
"github.com/Stride-Labs/stride/v17/x/stakeibc/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
CommunityPoolStakeHoldingAddressKey = "community-pool-stake"
CommunityPoolRedeemHoldingAddressKey = "community-pool-redeem"
)
func (k msgServer) RegisterHostZone(goCtx context.Context, msg *types.MsgRegisterHostZone) (*types.MsgRegisterHostZoneResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Get ConnectionEnd (for counterparty connection)
connectionEnd, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, msg.ConnectionId)
if !found {
errMsg := fmt.Sprintf("invalid connection id, %s not found", msg.ConnectionId)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
counterpartyConnection := connectionEnd.Counterparty
// Get chain id from connection
chainId, err := k.GetChainIdFromConnectionId(ctx, msg.ConnectionId)
if err != nil {
errMsg := fmt.Sprintf("unable to obtain chain id from connection %s, err: %s", msg.ConnectionId, err.Error())
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
// get zone
_, found = k.GetHostZone(ctx, chainId)
if found {
errMsg := fmt.Sprintf("invalid chain id, zone for %s already registered", chainId)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
// check the denom is not already registered
hostZones := k.GetAllHostZone(ctx)
for _, hostZone := range hostZones {
if hostZone.HostDenom == msg.HostDenom {
errMsg := fmt.Sprintf("host denom %s already registered", msg.HostDenom)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
if hostZone.ConnectionId == msg.ConnectionId {
errMsg := fmt.Sprintf("connectionId %s already registered", msg.ConnectionId)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
if hostZone.TransferChannelId == msg.TransferChannelId {
errMsg := fmt.Sprintf("transfer channel %s already registered", msg.TransferChannelId)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
if hostZone.Bech32Prefix == msg.Bech32Prefix {
errMsg := fmt.Sprintf("bech32prefix %s already registered", msg.Bech32Prefix)
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
}
// create and save the zones's module account
depositAddress := types.NewHostZoneDepositAddress(chainId)
if err := utils.CreateModuleAccount(ctx, k.AccountKeeper, depositAddress); err != nil {
return nil, errorsmod.Wrapf(err, "unable to create deposit account for host zone %s", chainId)
}
// Create the host zone's community pool holding accounts
communityPoolStakeAddress := types.NewHostZoneModuleAddress(chainId, CommunityPoolStakeHoldingAddressKey)
communityPoolRedeemAddress := types.NewHostZoneModuleAddress(chainId, CommunityPoolRedeemHoldingAddressKey)
if err := utils.CreateModuleAccount(ctx, k.AccountKeeper, communityPoolStakeAddress); err != nil {
return nil, errorsmod.Wrapf(err, "unable to create community pool stake account for host zone %s", chainId)
}
if err := utils.CreateModuleAccount(ctx, k.AccountKeeper, communityPoolRedeemAddress); err != nil {
return nil, errorsmod.Wrapf(err, "unable to create community pool redeem account for host zone %s", chainId)
}
params := k.GetParams(ctx)
if msg.MinRedemptionRate.IsNil() || msg.MinRedemptionRate.IsZero() {
msg.MinRedemptionRate = sdk.NewDecWithPrec(int64(params.DefaultMinRedemptionRateThreshold), 2)
}
if msg.MaxRedemptionRate.IsNil() || msg.MaxRedemptionRate.IsZero() {
msg.MaxRedemptionRate = sdk.NewDecWithPrec(int64(params.DefaultMaxRedemptionRateThreshold), 2)
}
// set the zone
zone := types.HostZone{
ChainId: chainId,
ConnectionId: msg.ConnectionId,
Bech32Prefix: msg.Bech32Prefix,
IbcDenom: msg.IbcDenom,
HostDenom: msg.HostDenom,
TransferChannelId: msg.TransferChannelId,
// Start sharesToTokens rate at 1 upon registration
RedemptionRate: sdk.NewDec(1),
LastRedemptionRate: sdk.NewDec(1),
UnbondingPeriod: msg.UnbondingPeriod,
DepositAddress: depositAddress.String(),
CommunityPoolStakeHoldingAddress: communityPoolStakeAddress.String(),
CommunityPoolRedeemHoldingAddress: communityPoolRedeemAddress.String(),
MinRedemptionRate: msg.MinRedemptionRate,
MaxRedemptionRate: msg.MaxRedemptionRate,
// Default the inner bounds to the outer bounds
MinInnerRedemptionRate: msg.MinRedemptionRate,
MaxInnerRedemptionRate: msg.MaxRedemptionRate,
LsmLiquidStakeEnabled: msg.LsmLiquidStakeEnabled,
}
// write the zone back to the store
k.SetHostZone(ctx, zone)
appVersion := string(icatypes.ModuleCdc.MustMarshalJSON(&icatypes.Metadata{
Version: icatypes.Version,
ControllerConnectionId: zone.ConnectionId,
HostConnectionId: counterpartyConnection.ConnectionId,
Encoding: icatypes.EncodingProtobuf,
TxType: icatypes.TxTypeSDKMultiMsg,
}))
// generate delegate account
// NOTE: in the future, if we implement proxy governance, we'll need many more delegate accounts
delegateAccount := types.FormatHostZoneICAOwner(chainId, types.ICAAccountType_DELEGATION)
if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, zone.ConnectionId, delegateAccount, appVersion); err != nil {
errMsg := fmt.Sprintf("unable to register delegation account, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
// generate fee account
feeAccount := types.FormatHostZoneICAOwner(chainId, types.ICAAccountType_FEE)
if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, zone.ConnectionId, feeAccount, appVersion); err != nil {
errMsg := fmt.Sprintf("unable to register fee account, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
// generate withdrawal account
withdrawalAccount := types.FormatHostZoneICAOwner(chainId, types.ICAAccountType_WITHDRAWAL)
if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, zone.ConnectionId, withdrawalAccount, appVersion); err != nil {
errMsg := fmt.Sprintf("unable to register withdrawal account, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
// generate redemption account
redemptionAccount := types.FormatHostZoneICAOwner(chainId, types.ICAAccountType_REDEMPTION)
if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, zone.ConnectionId, redemptionAccount, appVersion); err != nil {
errMsg := fmt.Sprintf("unable to register redemption account, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
// create community pool deposit account
communityPoolDepositAccount := types.FormatHostZoneICAOwner(chainId, types.ICAAccountType_COMMUNITY_POOL_DEPOSIT)
if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, zone.ConnectionId, communityPoolDepositAccount, appVersion); err != nil {
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, "failed to register community pool deposit ICA")
}
// create community pool return account
communityPoolReturnAccount := types.FormatHostZoneICAOwner(chainId, types.ICAAccountType_COMMUNITY_POOL_RETURN)
if err := k.ICAControllerKeeper.RegisterInterchainAccount(ctx, zone.ConnectionId, communityPoolReturnAccount, appVersion); err != nil {
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, "failed to register community pool return ICA")
}
// add this host zone to unbonding hostZones, otherwise users won't be able to unbond
// for this host zone until the following day
dayEpochTracker, found := k.GetEpochTracker(ctx, epochtypes.DAY_EPOCH)
if !found {
return nil, errorsmod.Wrapf(types.ErrEpochNotFound, "epoch tracker (%s) not found", epochtypes.DAY_EPOCH)
}
epochUnbondingRecord, found := k.RecordsKeeper.GetEpochUnbondingRecord(ctx, dayEpochTracker.EpochNumber)
if !found {
errMsg := "unable to find latest epoch unbonding record"
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(recordstypes.ErrEpochUnbondingRecordNotFound, errMsg)
}
hostZoneUnbonding := &recordstypes.HostZoneUnbonding{
NativeTokenAmount: sdkmath.ZeroInt(),
StTokenAmount: sdkmath.ZeroInt(),
Denom: zone.HostDenom,
HostZoneId: zone.ChainId,
Status: recordstypes.HostZoneUnbonding_UNBONDING_QUEUE,
}
updatedEpochUnbondingRecord, success := k.RecordsKeeper.AddHostZoneToEpochUnbondingRecord(ctx, epochUnbondingRecord.EpochNumber, chainId, hostZoneUnbonding)
if !success {
errMsg := fmt.Sprintf("Failed to set host zone epoch unbonding record: epochNumber %d, chainId %s, hostZoneUnbonding %v. Err: %s",
epochUnbondingRecord.EpochNumber, chainId, hostZoneUnbonding, err.Error())
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrEpochNotFound, errMsg)
}
k.RecordsKeeper.SetEpochUnbondingRecord(ctx, *updatedEpochUnbondingRecord)
// create an empty deposit record for the host zone
strideEpochTracker, found := k.GetEpochTracker(ctx, epochtypes.STRIDE_EPOCH)
if !found {
return nil, errorsmod.Wrapf(types.ErrEpochNotFound, "epoch tracker (%s) not found", epochtypes.STRIDE_EPOCH)
}
depositRecord := recordstypes.DepositRecord{
Id: 0,
Amount: sdkmath.ZeroInt(),
Denom: zone.HostDenom,
HostZoneId: zone.ChainId,
Status: recordstypes.DepositRecord_TRANSFER_QUEUE,
DepositEpochNumber: strideEpochTracker.EpochNumber,
}
k.RecordsKeeper.AppendDepositRecord(ctx, depositRecord)
// register stToken to consumer reward denom whitelist so that
// stToken rewards can be distributed to provider validators
err = k.RegisterStTokenDenomsToWhitelist(ctx, []string{types.StAssetDenomFromHostZoneDenom(zone.HostDenom)})
if err != nil {
errMsg := fmt.Sprintf("unable to register reward denom, err: %s", err.Error())
k.Logger(ctx).Error(errMsg)
return nil, errorsmod.Wrapf(types.ErrFailedToRegisterHostZone, errMsg)
}
// emit events
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.AttributeValueCategory),
),
)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypeRegisterZone,
sdk.NewAttribute(types.AttributeKeyConnectionId, msg.ConnectionId),
sdk.NewAttribute(types.AttributeKeyRecipientChain, chainId),
),
)
return &types.MsgRegisterHostZoneResponse{}, nil
}