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

refactor: CUDOS Init outstanding rewards for manifest #404

Merged
merged 3 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
50 changes: 45 additions & 5 deletions app/upgrade_cudos.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,27 @@ func convertAddressPrefix(addr string, newPrefix string) (string, error) {
return newAddress, nil
}

func convertOperatorAddressToAccount(addr string) (string, error) {
prefix, decodedAddrData, err := bech32.DecodeAndConvert(addr)
if err != nil {
return "", err
}

suffix := "valoper"
if strings.HasSuffix(prefix, suffix) {
prefix = prefix[:len(prefix)-len(suffix)]
} else {
return "", fmt.Errorf("wrong operator address")
}

newAddress, err := bech32.ConvertAndEncode(prefix, decodedAddrData)
if err != nil {
return "", err
}

return newAddress, nil
}

func ensureCudosconvertAddressToRaw(addr string, genesisData *GenesisData) (sdk.AccAddress, error) {
prefix, decodedAddrData, err := bech32.DecodeAndConvert(addr)

Expand Down Expand Up @@ -217,6 +238,19 @@ func writeMovedBalancesToManifest(genesisData *GenesisData, manifest *UpgradeMan
if account, exists := genesisData.Accounts.Get(address); exists {
upgradeBalance.BankBalance = account.Balance
}

// Bonded tokens will be delegated after conversion
if delegations, exists := genesisData.Delegations.Get(address); exists {
bondedBalance := sdk.Coins{}
for i := range delegations.Iterate() {
_, delegatedAmount := i.Key, i.Value
delegatedBalance := sdk.NewCoin(genesisData.BondDenom, delegatedAmount)
bondedBalance = bondedBalance.Add(delegatedBalance)
}
upgradeBalance.BondedStakingBalancesAggr = bondedBalance
upgradeBalance.BankBalance = upgradeBalance.BankBalance.Sub(bondedBalance)
}

upgradeBalances = append(upgradeBalances, upgradeBalance)

}
Expand Down Expand Up @@ -250,7 +284,7 @@ func writeInitialBalancesToManifest(genesisData *GenesisData, manifest *UpgradeM
delegatedBalance := sdk.NewCoin(genesisData.BondDenom, delegatedAmount)
totalBalance = totalBalance.Add(delegatedBalance)
}
upgradeBalance.BondedStakingBalance = totalBalance
upgradeBalance.BondedStakingBalancesAggr = totalBalance
}

// Unbonding tokens
Expand All @@ -261,7 +295,7 @@ func writeInitialBalancesToManifest(genesisData *GenesisData, manifest *UpgradeM
delegatedBalance := sdk.NewCoin(genesisData.BondDenom, delegatedAmount)
totalBalance = totalBalance.Add(delegatedBalance)
}
upgradeBalance.UnbondingStakingBalance = totalBalance
upgradeBalance.UnbondingStakingBalancesAggr = totalBalance
}

// Unbonded tokens
Expand All @@ -272,10 +306,10 @@ func writeInitialBalancesToManifest(genesisData *GenesisData, manifest *UpgradeM
delegatedBalance := sdk.NewCoin(genesisData.BondDenom, delegatedAmount)
totalBalance = totalBalance.Add(delegatedBalance)
}
upgradeBalance.UnbondedStakingBalance = totalBalance
upgradeBalance.UnbondedStakingBalancesAggr = totalBalance
}

// Get distribution module rewards
// Get distribution module delegator rewards
if DelegatorRewards, exists := genesisData.DistributionInfo.Rewards.Get(address); exists {
totalBalance := sdk.Coins{}
for j := range DelegatorRewards.Iterate() {
Expand All @@ -285,7 +319,13 @@ func writeInitialBalancesToManifest(genesisData *GenesisData, manifest *UpgradeM
totalBalance = totalBalance.Add(rewardAmount...)
}
}
upgradeBalance.DistributionRewards = totalBalance
upgradeBalance.DelegatorRewardsAggr = totalBalance
}

// Get distribution module validator rewards
if ValidatorDecRewards, exists := genesisData.DistributionInfo.ValidatorRewards.Get(address); exists {
ValidatorRewards, _ := ValidatorDecRewards.TruncateDecimal()
upgradeBalance.ValidatorRewards = ValidatorRewards
}

upgradeBalances = append(upgradeBalances, upgradeBalance)
Expand Down
19 changes: 18 additions & 1 deletion app/upgrade_cudos_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ type DistributionInfo struct {
DistributionModuleAccountAddress string

// Aggregated values
Rewards *OrderedMap[string, *OrderedMap[string, sdk.DecCoins]] // delegator_addr -> validator_addr -> reward
Rewards *OrderedMap[string, *OrderedMap[string, sdk.DecCoins]] // delegator_addr -> validator_addr -> reward
ValidatorRewards *OrderedMap[string, sdk.DecCoins] // validator_addr -> validator_rewards
}

func parseDelegatorStartingInfos(distribution map[string]interface{}) (*OrderedMap[string, *OrderedMap[string, *DelegatorStartingInfo]], error) {
Expand Down Expand Up @@ -251,12 +252,18 @@ func aggregateRewards(distributionInfo *DistributionInfo, validators *OrderedMap
if distributionInfo.Rewards == nil {
distributionInfo.Rewards = NewOrderedMap[string, *OrderedMap[string, sdk.DecCoins]]()
}
if distributionInfo.ValidatorRewards == nil {
distributionInfo.ValidatorRewards = NewOrderedMap[string, sdk.DecCoins]()
}

// Withdraw all delegation rewards
for i := range distributionInfo.DelegatorStartingInfos.Iterate() {
validatorOperatorAddr, delegatorStartInfo := i.Key, i.Value
validator := validators.MustGet(validatorOperatorAddr)

// Get initial outstanding reward
outstandingRewards := distributionInfo.OutstandingRewards.MustGet(validatorOperatorAddr)

endingPeriod := UpdateValidatorData(distributionInfo, validator)

for _, delegatorAddr := range delegatorStartInfo.Keys() {
Expand All @@ -269,8 +276,18 @@ func aggregateRewards(distributionInfo *DistributionInfo, validators *OrderedMap

delegatorRewards, _ := distributionInfo.Rewards.GetOrSetDefault(delegatorAddr, NewOrderedMap[string, sdk.DecCoins]())
delegatorRewards.SetNew(validatorOperatorAddr, rewardsRaw)

outstandingRewards = outstandingRewards.Sub(rewardsRaw.Intersect(outstandingRewards))

}

validatorAccountAddress, err := convertOperatorAddressToAccount(validatorOperatorAddr)
if err != nil {
return err
}

distributionInfo.ValidatorRewards.Set(validatorAccountAddress, outstandingRewards)

}

return nil
Expand Down
23 changes: 9 additions & 14 deletions app/upgrade_v_11_4_manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,15 @@ type UpgradeAccountCreation struct {
}

type UpgradeBalances struct {
Address string `json:"address"`
BankBalance types.Coins `json:"bank_balance"`
VestedBalance types.Coins `json:"vested_balance,omitempty"`
BondedStakingBalance types.Coins `json:"bonded_staking_balance,omitempty"`
UnbondedStakingBalance types.Coins `json:"unbonded_staking_balance,omitempty"`
UnbondingStakingBalance types.Coins `json:"unbonding_staking_balance,omitempty"`
DistributionRewards types.Coins `json:"distribution_rewards,omitempty"`
Memo string `json:"memo,omitempty"`
}

// app.UpgradeKeeper.GetUpgradeInfoPath()
// if upgradeFilePath, err = app.UpgradeKeeper.GetUpgradeInfoPath(); err != nil {
// return "", err
// }
Address string `json:"address"`
BankBalance types.Coins `json:"bank_balance"`
VestedBalance types.Coins `json:"vested_balance,omitempty"`
BondedStakingBalancesAggr types.Coins `json:"bonded_staking_balances_aggr,omitempty"`
UnbondedStakingBalancesAggr types.Coins `json:"unbonded_staking_balances_aggr,omitempty"`
UnbondingStakingBalancesAggr types.Coins `json:"unbonding_staking_balances_aggr,omitempty"`
DelegatorRewardsAggr types.Coins `json:"delegator_rewards_aggr,omitempty"`
ValidatorRewards types.Coins `json:"validator_rewards,omitempty"`
}

func getManifestFilePath(app *App, prefix string) (string, error) {
var upgradeFilePath string
Expand Down
54 changes: 10 additions & 44 deletions cmd/fetchd/cmd/cudos_merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func AddCommandManifestAddressInfo(networkMergeCmd *cobra.Command) {
cmd := &cobra.Command{
Use: "manifest-address-info [manifest_file_path] [address]",
Short: "Extracts balance information for a specific address",
Long: `This command retrieves all balance information for a given address from almanac, including the amount delegated to validators, and rewards.`,
Long: `This command retrieves all balance information for a given address from manifest, including the amount delegated to validators, and rewards.`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := client.GetClientContextFromCmd(cmd)
Expand All @@ -129,7 +129,7 @@ func AddCommandManifestAddressInfo(networkMergeCmd *cobra.Command) {
address := args[1]

// Call a function to extract address info
err := AlmanacAddressInfo(manifestFilePath, address, ctx)
err := ManifestAddressInfo(manifestFilePath, address, ctx)
if err != nil {
return err
}
Expand Down Expand Up @@ -463,55 +463,21 @@ func parseManifestData(manifest *app.UpgradeManifest) (*ManifestData, error) {
}

func printBalancesEntry(upgradeBalances app.UpgradeBalances, ctx client.Context) error {
if !upgradeBalances.BankBalance.IsZero() {
err := ctx.PrintString(fmt.Sprintf("Bank balance: %s\n", upgradeBalances.BankBalance))
if err != nil {
return err
}
}

if !upgradeBalances.VestedBalance.IsZero() {
err := ctx.PrintString(fmt.Sprintf("Vested balance: %s\n", upgradeBalances.VestedBalance))
if err != nil {
return err
}
}

if !upgradeBalances.BondedStakingBalance.IsZero() {
err := ctx.PrintString(fmt.Sprintf("Bonded staking balance: %s\n", upgradeBalances.BondedStakingBalance))
if err != nil {
return err
}
}

if !upgradeBalances.UnbondedStakingBalance.IsZero() {
err := ctx.PrintString(fmt.Sprintf("Unbonded staking balance: %s\n", upgradeBalances.UnbondedStakingBalance))
if err != nil {
return err
}
}

if !upgradeBalances.UnbondingStakingBalance.IsZero() {
err := ctx.PrintString(fmt.Sprintf("Unbonding staking balance: %s\n", upgradeBalances.UnbondingStakingBalance))
if err != nil {
return err
}
data, err := json.MarshalIndent(upgradeBalances, "", " ")
if err != nil {
return err
}

if !upgradeBalances.DistributionRewards.IsZero() {
err := ctx.PrintString(fmt.Sprintf("Distribution rewards: %s\n", upgradeBalances.DistributionRewards))
if err != nil {
return err
}
err = ctx.PrintString(fmt.Sprintf("%s\n", string(data)))
if err != nil {
return err
}

return nil
}

func AlmanacAddressInfo(almanacFilePath string, address string, ctx client.Context) error {
func ManifestAddressInfo(manifestFilePath string, address string, ctx client.Context) error {
manifest := app.NewUpgradeManifest()

manifest, err := app.LoadManifestFromPath(almanacFilePath)
manifest, err := app.LoadManifestFromPath(manifestFilePath)
if err != nil {
return err
}
Expand Down
Loading