@@ -195,6 +195,76 @@ func moveDelegations(ctx sdk.Context, k stakingkeeper.Keeper, oldAddress sdk.Acc
195
195
}
196
196
}
197
197
198
+ func moveSelfDelegation (ctx sdk.Context , keepers * keepers.AppKeepers , oldDelegatorAddress sdk.AccAddress , newDelegatorAddress sdk.AccAddress , validatorAddr sdk.ValAddress ) error {
199
+ stakingKeeper := keepers .StakingKeeper
200
+
201
+ // Get the delegation from the old validator
202
+ delegation , found := stakingKeeper .GetDelegation (ctx , oldDelegatorAddress , validatorAddr )
203
+ if ! found {
204
+ log .Printf ("self delegation not found: %s" , oldDelegatorAddress )
205
+ return fmt .Errorf ("self delegation not found" )
206
+ }
207
+
208
+ // Remove delegation from the old validator
209
+ stakingKeeper .RemoveDelegation (ctx , delegation )
210
+
211
+ balance , err := getBalance (ctx , * keepers .StakingKeeper , keepers .AccountKeeper , keepers .BankKeeper , oldDelegatorAddress )
212
+ if err != nil {
213
+ log .Printf ("Error when retrieving balance for address %s: %s" , oldDelegatorAddress , err )
214
+ return err
215
+ }
216
+
217
+ log .Printf ("Sending coins from %v to %v (%v)" , oldDelegatorAddress , newDelegatorAddress , balance )
218
+
219
+ err = sendCoins (ctx , keepers .BankKeeper , oldDelegatorAddress , newDelegatorAddress , balance )
220
+ if err != nil {
221
+ log .Printf ("Error when sending coins for address %s: %s" , oldDelegatorAddress , err )
222
+ return err
223
+ }
224
+
225
+ // Assuming you've calculated or know the amount to redelegate
226
+ amount := delegation .Shares
227
+
228
+ // Create a new delegation to the new validator
229
+ newDelegation := stakingtypes.Delegation {
230
+ DelegatorAddress : newDelegatorAddress .String (),
231
+ ValidatorAddress : validatorAddr .String (),
232
+ Shares : amount ,
233
+ }
234
+
235
+ err = stakingKeeper .Hooks ().BeforeDelegationCreated (ctx , delegation .GetDelegatorAddr (), validatorAddr )
236
+ if err != nil {
237
+ log .Printf ("Error when running hook before adding delegation %v to %v" , delegation .GetDelegatorAddr (), validatorAddr )
238
+ return err
239
+ }
240
+ // Save the new delegation
241
+ stakingKeeper .SetDelegation (ctx , newDelegation )
242
+
243
+ // Update the old validator's and new validator's tokens and delegator shares
244
+ validator , found := stakingKeeper .GetValidator (ctx , validatorAddr )
245
+ if ! found {
246
+ return fmt .Errorf ("old validator not found" )
247
+ }
248
+
249
+
250
+ // Convert shares to tokens if necessary. This example assumes `amount` is already in tokens.
251
+ // You might need to adjust based on your actual data and the SDK's version.
252
+ // tokens := sdk.TokensFromConsensusPower(int64(amount.TruncateInt64()), sdk.DefaultPowerReduction)
253
+
254
+ // oldValidator.Tokens = oldValidator.Tokens.Sub(tokens)
255
+ // newValidator.Tokens = newValidator.Tokens.Add(tokens)
256
+
257
+ // Ensure to update the validators' power index if their tokens have changed
258
+ stakingKeeper .SetValidator (ctx , validator )
259
+ // stakingKeeper.SetValidator(ctx, newValidator)
260
+
261
+ // This operation should ideally be followed by updates to the pool's bonded tokens
262
+ // and triggering any necessary events or hooks for state consistency
263
+
264
+ return nil
265
+ }
266
+
267
+
198
268
func sendCoins (
199
269
ctx sdk.Context ,
200
270
bankkeeper bankkeeper.Keeper ,
@@ -242,7 +312,7 @@ func fixDefiantLabs(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
242
312
return err
243
313
}
244
314
245
- DanVal , found := keepers .StakingKeeper .GetValidator (ctx , DefiantLabsValAddress )
315
+ DefiantLabsVal , found := keepers .StakingKeeper .GetValidator (ctx , DefiantLabsValAddress )
246
316
if ! found {
247
317
log .Printf ("Validator with %v has not been found" , DefiantLabsValAddress )
248
318
return err
@@ -260,41 +330,47 @@ func fixDefiantLabs(ctx sdk.Context, keepers *keepers.AppKeepers) (error) {
260
330
return err
261
331
}
262
332
263
- // Donating 10K loki to DefiantLab to automate his self-delegation process
264
-
265
- OdinMainnet3DonorAddr , err := sdk .AccAddressFromBech32 (OdinMainnet3OldAccAddress )
333
+ // Moving DefiantLabs self-delegation
334
+ err = moveSelfDelegation (ctx , keepers , DefiantLabsOldAcc , DefiantLabsAcc , DefiantLabsValAddress )
266
335
if err != nil {
267
- log .Printf ("account address is not valid bech32: %s: %s" , OdinMainnet3OldAccAddress , err )
268
336
return err
269
337
}
270
338
271
- log .Printf ("Donating coins for self-delegation %v" , DefiantLabsValAddress )
272
- sendCoins (ctx , keepers .BankKeeper , OdinMainnet3DonorAddr , DefiantLabsAcc , sdk .NewCoins (sdk .NewCoin ("loki" , sdk .NewInt (10000 ))))
339
+ // Donating 10K loki to DefiantLab to automate his self-delegation process
273
340
274
- keepers .DistrKeeper .SetWithdrawAddr (ctx , DefiantLabsAcc , DefiantLabsAcc )
341
+ // OdinMainnet3DonorAddr, err := sdk.AccAddressFromBech32(OdinMainnet3OldAccAddress)
342
+ // if err != nil {
343
+ // log.Printf("account address is not valid bech32: %s: %s", OdinMainnet3OldAccAddress, err)
344
+ // return err
345
+ // }
346
+
347
+ // log.Printf("Donating coins for self-delegation %v", DefiantLabsValAddress)
348
+ // sendCoins(ctx, keepers.BankKeeper, OdinMainnet3DonorAddr, DefiantLabsAcc, sdk.NewCoins(sdk.NewCoin("loki", sdk.NewInt(10000))))
275
349
276
- // sending balances
277
- ctx .Logger ().Info (fmt .Sprintf ("Sending tokens from %s to %s" , DefiantLabOldAccAddress , DefiantLabAccAddress ))
350
+ // keepers.DistrKeeper.SetWithdrawAddr(ctx, DefiantLabsAcc, DefiantLabsAcc)
351
+
352
+ // // sending balances
353
+ // ctx.Logger().Info(fmt.Sprintf("Sending tokens from %s to %s", DefiantLabOldAccAddress, DefiantLabAccAddress))
278
354
279
- balance , err := getBalance (ctx , * keepers .StakingKeeper , keepers .AccountKeeper , keepers .BankKeeper , DefiantLabsOldAcc )
280
- if err != nil {
281
- log .Printf ("Error when retrieving balance for address %s: %s" , DefiantLabOldAccAddress , err )
282
- return err
283
- }
284
- sendCoins (ctx , keepers .BankKeeper , DefiantLabsOldAcc , DefiantLabsAcc , balance )
355
+ // balance, err := getBalance(ctx, *keepers.StakingKeeper, keepers.AccountKeeper, keepers.BankKeeper, DefiantLabsOldAcc)
356
+ // if err != nil {
357
+ // log.Printf("Error when retrieving balance for address %s: %s", DefiantLabOldAccAddress, err)
358
+ // return err
359
+ // }
360
+ // sendCoins(ctx, keepers.BankKeeper, DefiantLabsOldAcc, DefiantLabsAcc, balance)
285
361
286
362
// Moving delegations
287
- moveDelegations (ctx , * keepers .StakingKeeper , DefiantLabsOldAcc , DanVal )
363
+ moveDelegations (ctx , * keepers .StakingKeeper , DefiantLabsOldAcc , DefiantLabsVal )
288
364
289
365
// Self delegating to new validator
290
- selfDel := keepers .StakingKeeper .Delegation (ctx , DefiantLabsAcc , DefiantLabsValAddress )
291
- if selfDel == nil {
292
- err := SelfDelegate (ctx , * keepers .StakingKeeper , keepers .BankKeeper , DefiantLabsAcc , DanVal , sdk .NewCoin ("loki" , math .NewInt (1000 )))
293
- if err != nil {
294
- log .Printf ("Error when self delegating to %v" , DefiantLabsValAddress )
295
- return err
296
- }
297
- }
366
+ // selfDel := keepers.StakingKeeper.Delegation(ctx, DefiantLabsAcc, DefiantLabsValAddress)
367
+ // if selfDel == nil {
368
+ // err := SelfDelegate(ctx, *keepers.StakingKeeper, keepers.BankKeeper, DefiantLabsAcc, DefiantLabsVal , sdk.NewCoin("loki", math.NewInt(1000)))
369
+ // if err != nil {
370
+ // log.Printf("Error when self delegating to %v", DefiantLabsValAddress)
371
+ // return err
372
+ // }
373
+ // }
298
374
return nil
299
375
}
300
376
0 commit comments