Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: track investor delegation #96

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,9 @@ func NewKYVEApp(
v1p3.CreateUpgradeHandler(
app.mm,
app.configurator,
app.AccountKeeper,
app.BankKeeper,
app.StakingKeeper,
),
)

Expand Down
39 changes: 39 additions & 0 deletions app/upgrades/v1_3/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
57 changes: 57 additions & 0 deletions app/upgrades/v1_3/upgrade.go
Original file line number Diff line number Diff line change
@@ -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)
}
}