From 4ff6ac3bea0b69436cb59a3f29e74eb98c41260d Mon Sep 17 00:00:00 2001 From: John Letey Date: Thu, 6 Jul 2023 15:00:57 +0200 Subject: [PATCH] fix: track investor delegation (#96) Co-authored-by: Troy Kessler <43882936+troykessler@users.noreply.github.com> Co-authored-by: mbreithecker --- CHANGELOG.md | 4 +++ app/app.go | 3 ++ app/upgrades/v1_3/constants.go | 39 +++++++++++++++++++++++ app/upgrades/v1_3/upgrade.go | 57 ++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ed289345..f3870ee0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,10 @@ - (`x/pool`) [#74](https://github.com/KYVENetwork/chain/pull/74) Improve parameter validation in pool proposals. - (`x/stakers`) [#46](https://github.com/KYVENetwork/chain/pull/46) Allow protocol validator commission rewards to be claimed. +### Bug Fixes + +- [#96](https://github.com/KYVENetwork/chain/pull/96) Track investor delegation inside auth module. + ### Client Breaking - (`x/stakers`) [#46](https://github.com/KYVENetwork/chain/pull/46) Include `MsgClaimCommissionRewards` for claiming commission rewards. diff --git a/app/app.go b/app/app.go index 62c8f6e6..0932d390 100644 --- a/app/app.go +++ b/app/app.go @@ -803,6 +803,9 @@ func NewKYVEApp( v1p3.CreateUpgradeHandler( app.mm, app.configurator, + app.AccountKeeper, + app.BankKeeper, + app.StakingKeeper, ), ) diff --git a/app/upgrades/v1_3/constants.go b/app/upgrades/v1_3/constants.go index ac193c84..0f2ec299 100644 --- a/app/upgrades/v1_3/constants.go +++ b/app/upgrades/v1_3/constants.go @@ -8,3 +8,42 @@ const TestnetChainID = "kaon-1" // MainnetChainID is the Chain ID of the KYVE mainnet. const MainnetChainID = "kyve-1" + +// InvestorAccounts is a list of accounts from our second funding round. +var InvestorAccounts = []string{ + "kyve1lmmvd34rn05uuxyzyajyht5eykksvas9gpz9ln", + "kyve10ucfmct7epngs7k3y77qrfpvjlsgfv2qqjytm4", + "kyve10we97qv6nujhx4eyzskw9p23s0qfnr5rq6uadp", + "kyve1z2d4fvcv8gf72zvdp23x6ryswywh0ptrjrkdhp", + "kyve1hae0u8jqfedpm7u5ng6k464xsxx32tvluqus8t", + "kyve1gcz2tjuel4w6q3v4ec92eu4nvey6fkgnyfcavm", + "kyve1matxpxlw4cna4xr77jaqqmst74j35puy0nf87f", + "kyve1946pdjdg6dqxfd5et4g3k6glkkvt8l9r5zqmp9", + "kyve1ag2mctnvgwqea7au0lh92xd4ncfysw8gs94r6v", + "kyve1e7rmpdrrnjeuypn733c9lt2vg2cvkj788pvtvr", + "kyve1ewy2yfxr8aw3v9jv2kjgt4z6tj9ved3a9qyjh2", + "kyve1u26sz6kydnmpr5zq6nureav4xhasak8nu5q8d8", + "kyve194rvm2rjxhe0gfnv2095z6k5tnyszxh9sdyzhy", + "kyve1j7p7jg4cuw9ww6zyjf2gvpjxgseffjuhaj4qcq", + "kyve1kcmdhddxnwvmnekrsejnr4n87xqyxq9tlqlyqn", + "kyve13agpsm0r2sr3wl0yqqfxdncl7mxla0nh6jteum", + "kyve1jp7tv4g2ks855spj4cppmdnx4jtx26h5u5y3x2", + "kyve1hlsaz3fegeze2mk6a9f7mh93y72edfmxuq2as7", + "kyve1a9zlytywun0sq8gxjkqhsc4knzm8zw3tprq4x8", + "kyve1uqm9n8a769nvwagz7gmpx5x52nmqn3z05gdrqe", + "kyve1tmjvtgv6a447dewwhd4j2ehqsm8x4d2ttm053n", + "kyve1xlg7pgptqujg72csewpx7rytr4f0sgngqrj3gu", + "kyve1th6v7yygmxsfm2x2uwsp582eagnrcrjywwaf7f", + "kyve159ufwz58rzkcvjk94wkxv5uyz877pwpf6h7xy2", + "kyve1z73v6kqhutr5fq5eem2256l5a45empuk0xkxxm", + "kyve17rdl2fadf82qx4r0wd2ehtqyy3qsazeqyun9dd", + "kyve1tp5uz49gvvl5kys795re80e3mf9w9up0yzhzq7", + "kyve18d8t7nj9jqyyrzr76wea38u7er3ljzzqqys5nl", + "kyve14l2zxt75ylgnumjevxxans20uapctnke698vq2", + "kyve17zfy84an63jqdmm632th7sp2tj2tmh82hrysn9", + "kyve1m297tfaystezangz7wrm9j5s55a3j8xz7g5nkp", + "kyve1yxt5u93an2as4csf2xqv8sw96rgm2y3rrzmj2g", + "kyve1eddmugm8k5eyaa96v7e3rwrw97040q8awmkwdy", + "kyve1uxarsjawzcu3xjvydnmlgmceq3eldx2yas2djf", + "kyve1rtx3akgca36lrevmzal050ehxpxnwtn9xv7taz", +} diff --git a/app/upgrades/v1_3/upgrade.go b/app/upgrades/v1_3/upgrade.go index 5f422140..147d0d50 100644 --- a/app/upgrades/v1_3/upgrade.go +++ b/app/upgrades/v1_3/upgrade.go @@ -1,16 +1,73 @@ package v1_3 import ( + "fmt" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" + bankKeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper" + "github.com/tendermint/tendermint/libs/log" + + // Auth + authKeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" + vestingExported "github.com/cosmos/cosmos-sdk/x/auth/vesting/exported" + // Staking + stakingKeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" + // Upgrade upgradeTypes "github.com/cosmos/cosmos-sdk/x/upgrade/types" ) func CreateUpgradeHandler( mm *module.Manager, configurator module.Configurator, + accountKeeper authKeeper.AccountKeeper, + bankKeeper bankKeeper.Keeper, + stakingKeeper stakingKeeper.Keeper, ) upgradeTypes.UpgradeHandler { return func(ctx sdk.Context, _ upgradeTypes.Plan, vm module.VersionMap) (module.VersionMap, error) { + logger := ctx.Logger().With("upgrade", UpgradeName) + + if ctx.ChainID() == MainnetChainID { + for _, address := range InvestorAccounts { + TrackInvestorDelegation(ctx, logger, sdk.MustAccAddressFromBech32(address), accountKeeper, bankKeeper, stakingKeeper) + } + } + return mm.RunMigrations(ctx, configurator, vm) } } + +// TrackInvestorDelegation performs a correction of the delegation tracking inside the vesting account. +// The correction is done by performing a full untracking and then tracking the actual total delegated amount +// (including slashed amounts). +func TrackInvestorDelegation(ctx sdk.Context, logger log.Logger, address sdk.AccAddress, ak authKeeper.AccountKeeper, bk bankKeeper.Keeper, sk stakingKeeper.Keeper) { + denom := sk.BondDenom(ctx) + account, _ := ak.GetAccount(ctx, address).(vestingExported.VestingAccount) + + // Obtain total delegation of address + totalDelegation := sdk.NewInt(0) + for _, delegation := range sk.GetAllDelegatorDelegations(ctx, address) { + // We take the shares as the total delegation as this is the amount which is + // tracked inside the vesting account. (slashes are ignored, which is correct) + totalDelegation = totalDelegation.Add(delegation.GetShares().TruncateInt()) + } + + // Fetch current balance. + balanceCoin := bk.GetBalance(ctx, address, denom) + + // This is the balance a user would have if all tokens are unbonded (even the ones which got slashed). + maxPossibleBalance := balanceCoin.Amount.Add(totalDelegation) + maxPossibleBalanceCoins := sdk.NewCoins().Add(sdk.NewCoin(denom, maxPossibleBalance)) + + if totalDelegation.GT(sdk.ZeroInt()) { + + // Untrack entire vesting delegation using maximum amount. This will set both `delegated_free` + // and `delegated_vesting` back to zero. + account.TrackUndelegation(sdk.NewCoins(sdk.NewCoin("ukyve", maxPossibleBalance))) + + // Track the delegation using the total delegation + account.TrackDelegation(ctx.BlockTime(), maxPossibleBalanceCoins, sdk.NewCoins(sdk.NewCoin("ukyve", totalDelegation))) + + logger.Info(fmt.Sprintf("tracked delegation of %s with %s", address.String(), totalDelegation.String())) + ak.SetAccount(ctx, account) + } +}