Skip to content

Commit c32fb52

Browse files
Yurist-85kioqq
authored andcommitted
fix: delegations tracking on vesting accounts
1 parent 56919f8 commit c32fb52

File tree

5 files changed

+87
-7
lines changed

5 files changed

+87
-7
lines changed

app/app.go

+7
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ import (
174174
v177 "github.com/haqq-network/haqq/app/upgrades/v1.7.7"
175175
v178 "github.com/haqq-network/haqq/app/upgrades/v1.7.8"
176176
v180 "github.com/haqq-network/haqq/app/upgrades/v1.8.0"
177+
v181 "github.com/haqq-network/haqq/app/upgrades/v1.8.1"
177178

178179
// NOTE: override ICS20 keeper to support IBC transfers of ERC20 tokens
179180
"github.com/haqq-network/haqq/x/ibc/transfer"
@@ -1281,6 +1282,12 @@ func (app *Haqq) setupUpgradeHandlers(keys map[string]*storetypes.KVStoreKey) {
12811282
v180.CreateUpgradeHandler(app.mm, app.configurator, *app.EvmKeeper, app.BankKeeper, app.DaoKeeper, keys[ucdaotypes.StoreKey]),
12821283
)
12831284

1285+
// v1.8.1 Fix delegations tracking on Vesting accounts
1286+
app.UpgradeKeeper.SetUpgradeHandler(
1287+
v181.UpgradeName,
1288+
v181.CreateUpgradeHandler(app.mm, app.configurator, app.AccountKeeper, *app.StakingKeeper.Keeper),
1289+
)
1290+
12841291
// When a planned update height is reached, the old binary will panic
12851292
// writing on disk the height and name of the update that triggered it
12861293
// This will read that value, and execute the preparations for the upgrade.

app/upgrades/v1.8.1/constants.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package v181
2+
3+
const (
4+
// UpgradeName is the shared upgrade plan name for mainnet
5+
UpgradeName = "v1.8.1"
6+
)

app/upgrades/v1.8.1/upgrades.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package v181
2+
3+
import (
4+
"fmt"
5+
6+
sdk "github.com/cosmos/cosmos-sdk/types"
7+
"github.com/cosmos/cosmos-sdk/types/module"
8+
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
9+
"github.com/cosmos/cosmos-sdk/x/auth/types"
10+
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
11+
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
12+
13+
vestingtypes "github.com/haqq-network/haqq/x/vesting/types"
14+
)
15+
16+
// CreateUpgradeHandler creates an SDK upgrade handler for v1.8.1
17+
func CreateUpgradeHandler(
18+
mm *module.Manager,
19+
configurator module.Configurator,
20+
ak authkeeper.AccountKeeper,
21+
sk stakingkeeper.Keeper,
22+
) upgradetypes.UpgradeHandler {
23+
return func(ctx sdk.Context, _ upgradetypes.Plan, vm module.VersionMap) (module.VersionMap, error) {
24+
logger := ctx.Logger().With("upgrade", UpgradeName)
25+
26+
logger.Info("migrate vesting accounts")
27+
migrateVestingAccounts(ctx, ak, sk)
28+
29+
// Leave modules are as-is to avoid running InitGenesis.
30+
logger.Debug("running module migrations ...")
31+
return mm.RunMigrations(ctx, configurator, vm)
32+
}
33+
}
34+
35+
func migrateVestingAccounts(ctx sdk.Context, ak authkeeper.AccountKeeper, sk stakingkeeper.Keeper) {
36+
logger := ctx.Logger().With("upgrade", UpgradeName)
37+
bondDenom := sk.BondDenom(ctx)
38+
var vaCount int64
39+
40+
ak.IterateAccounts(ctx, func(acc types.AccountI) bool {
41+
va, ok := acc.(*vestingtypes.ClawbackVestingAccount)
42+
if !ok {
43+
return false
44+
}
45+
46+
bondedAmt := sk.GetDelegatorBonded(ctx, va.GetAddress())
47+
unbondingAmt := sk.GetDelegatorUnbonding(ctx, va.GetAddress())
48+
delegatedAmt := bondedAmt.Add(unbondingAmt)
49+
delegated := sdk.NewCoins(sdk.NewCoin(bondDenom, delegatedAmt))
50+
zeroCoins := sdk.NewCoins()
51+
52+
logger.Info(fmt.Sprintf("update vesting account: %s", va.GetAddress()))
53+
logger.Info(fmt.Sprintf(" - set DelegatedVesting: %s -> %s", va.DelegatedVesting.String(), zeroCoins.String()))
54+
logger.Info(fmt.Sprintf(" - set DelegatedFree: %s -> %s", va.DelegatedFree.String(), delegated.String()))
55+
56+
va.DelegatedVesting = sdk.NewCoins()
57+
va.DelegatedFree = delegated
58+
59+
logger.Info("---")
60+
ak.SetAccount(ctx, va)
61+
vaCount++
62+
return false
63+
})
64+
65+
logger.Info(fmt.Sprintf("Updated Clawback Vesting Accounts: %d", vaCount))
66+
}

x/vesting/keeper/msg_server.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,6 @@ func (k Keeper) addGrant(
438438
bondedAmt := k.stakingKeeper.GetDelegatorBonded(ctx, vestingAddr)
439439
unbondingAmt := k.stakingKeeper.GetDelegatorUnbonding(ctx, vestingAddr)
440440
delegatedAmt := bondedAmt.Add(unbondingAmt)
441-
delegated := sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), delegatedAmt))
442441

443442
// modify schedules for the new grant
444443
accStartTime := va.GetStartTime()
@@ -459,10 +458,9 @@ func (k Keeper) addGrant(
459458
va.VestingPeriods = newVestingPeriods
460459
va.OriginalVesting = va.OriginalVesting.Add(grantCoins...)
461460

462-
// cap DV at the current unvested amount, DF rounds out to current delegated
463-
unvested := va.GetVestingCoins(ctx.BlockTime())
464-
va.DelegatedVesting = delegated.Min(unvested)
465-
va.DelegatedFree = delegated.Sub(va.DelegatedVesting...)
461+
// DF rounds out to current delegated
462+
va.DelegatedVesting = sdk.NewCoins()
463+
va.DelegatedFree = sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), delegatedAmt))
466464
return nil
467465
}
468466

x/vesting/keeper/schedule.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,11 @@ func (k Keeper) ApplyVestingSchedule(
6969
vestingPeriods,
7070
&codeHash,
7171
)
72-
acc := k.accountKeeper.NewAccount(ctx, vestingAcc)
73-
k.accountKeeper.SetAccount(ctx, acc)
72+
bondedAmt := k.stakingKeeper.GetDelegatorBonded(ctx, vestingAcc.GetAddress())
73+
unbondingAmt := k.stakingKeeper.GetDelegatorUnbonding(ctx, vestingAcc.GetAddress())
74+
delegatedAmt := bondedAmt.Add(unbondingAmt)
75+
vestingAcc.DelegatedFree = sdk.NewCoins(sdk.NewCoin(k.stakingKeeper.BondDenom(ctx), delegatedAmt))
76+
k.accountKeeper.SetAccount(ctx, vestingAcc)
7477
return vestingAcc, false, false, nil
7578
case isClawback && merge:
7679
if funder.String() != vestingAcc.FunderAddress {

0 commit comments

Comments
 (0)