Skip to content

Commit

Permalink
feat: update validator token allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Sep 27, 2024
1 parent 164e4fd commit 9a15947
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 92 deletions.
20 changes: 18 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
clienthelpers "cosmossdk.io/client/v2/helpers"
"cosmossdk.io/math"
runtimeservices "github.com/cosmos/cosmos-sdk/runtime/services"
"github.com/cosmos/cosmos-sdk/std"
"github.com/cosmos/gogoproto/proto"
Expand Down Expand Up @@ -620,7 +621,7 @@ func NewArkeoApp(
groupmodule.NewAppModule(appCodec, app.Keepers.GroupKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.interfaceRegistry),
crisis.NewAppModule(&app.Keepers.CrisisKeeper, skipGenesisInvariants, app.GetSubspace(crisistypes.ModuleName)),
gov.NewAppModule(appCodec, app.Keepers.GovKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
mint.NewAppModule(appCodec, app.Keepers.MintKeeper, app.Keepers.AccountKeeper, minttypes.DefaultInflationCalculationFn, app.GetSubspace(minttypes.ModuleName)),
mint.NewAppModule(appCodec, app.Keepers.MintKeeper, app.Keepers.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
slashing.NewAppModule(appCodec, app.Keepers.SlashingKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.Keepers.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
distr.NewAppModule(appCodec, app.Keepers.DistrKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.Keepers.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
staking.NewAppModule(appCodec, app.Keepers.StakingKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
Expand Down Expand Up @@ -760,7 +761,7 @@ func NewArkeoApp(
capability.NewAppModule(appCodec, *app.Keepers.CapabilityKeeper, false),
feegrantmodule.NewAppModule(appCodec, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.Keepers.FeeGrantKeeper, app.interfaceRegistry),
gov.NewAppModule(appCodec, app.Keepers.GovKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.GetSubspace(govtypes.ModuleName)),
mint.NewAppModule(appCodec, app.Keepers.MintKeeper, app.Keepers.AccountKeeper, minttypes.DefaultInflationCalculationFn, app.GetSubspace(minttypes.ModuleName)),
mint.NewAppModule(appCodec, app.Keepers.MintKeeper, app.Keepers.AccountKeeper, nil, app.GetSubspace(minttypes.ModuleName)),
staking.NewAppModule(appCodec, app.Keepers.StakingKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.GetSubspace(stakingtypes.ModuleName)),
distr.NewAppModule(appCodec, app.Keepers.DistrKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.Keepers.StakingKeeper, app.GetSubspace(distrtypes.ModuleName)),
slashing.NewAppModule(appCodec, app.Keepers.SlashingKeeper, app.Keepers.AccountKeeper, app.Keepers.BankKeeper, app.Keepers.StakingKeeper, app.GetSubspace(slashingtypes.ModuleName), app.interfaceRegistry),
Expand Down Expand Up @@ -852,6 +853,21 @@ func (app *ArkeoApp) InitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*
if err != nil {
panic(err)
}
mintGen := minttypes.GenesisState{
Minter: minttypes.Minter{
Inflation: math.LegacyMustNewDecFromStr("0.000000000000000000"),
AnnualProvisions: math.LegacyMustNewDecFromStr("0.000000000000000000"),
},
Params: minttypes.Params{
MintDenom: "uarkeo",
InflationRateChange: math.LegacyMustNewDecFromStr("0.000000000000000000"),
InflationMax: math.LegacyMustNewDecFromStr("0.000000000000000000"),
InflationMin: math.LegacyMustNewDecFromStr("0.000000000000000000"),
GoalBonded: math.LegacyNewDec(670000000000000000),
BlocksPerYear: 5256666,
},
}
genesisState[minttypes.ModuleName] = app.cdc.MustMarshalJSON(&mintGen)
return app.mm.InitGenesis(ctx, app.appCodec, genesisState)
}

Expand Down
22 changes: 21 additions & 1 deletion app/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package app
import (
"encoding/json"

"cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/codec"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
)

// The genesis state of the blockchain is represented here as a map of raw json
Expand All @@ -17,5 +19,23 @@ type GenesisState map[string]json.RawMessage

// NewDefaultGenesisState generates the default state for the application.
func NewDefaultGenesisState(cdc codec.JSONCodec) GenesisState {
return ModuleBasics.DefaultGenesis(cdc)
defaultGenesis := ModuleBasics.DefaultGenesis(cdc)
// set mint module params for genesis state
mintGen := minttypes.GenesisState{
Minter: minttypes.Minter{
Inflation: math.LegacyMustNewDecFromStr("0.000000000000000000"),
AnnualProvisions: math.LegacyMustNewDecFromStr("0.000000000000000000"),
},
Params: minttypes.Params{
MintDenom: "uarkeo",
InflationRateChange: math.LegacyMustNewDecFromStr("0.000000000000000000"),
InflationMax: math.LegacyMustNewDecFromStr("0.000000000000000000"),
InflationMin: math.LegacyMustNewDecFromStr("0.000000000000000000"),
GoalBonded: math.LegacyNewDec(670000000000000000),
BlocksPerYear: 5256666,
},
}
defaultGenesis[minttypes.ModuleName] = cdc.MustMarshalJSON(&mintGen)

return defaultGenesis
}
105 changes: 64 additions & 41 deletions x/arkeo/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,12 @@ type Keeper interface {
GetActiveValidators(ctx cosmos.Context) ([]stakingtypes.Validator, error)
GetAccount(ctx cosmos.Context, addr cosmos.AccAddress) cosmos.Account
StakingSetParams(ctx cosmos.Context, params stakingtypes.Params) error
MintAndDistributeTokens(ctx cosmos.Context, newlyMinted cosmos.Coin) (cosmos.Coin, error)
GetCirculatingSupply(ctx cosmos.Context, denom string) (cosmos.Coin, error)
GetInflationRate(ctx cosmos.Context) (math.LegacyDec, error)
MintAndDistributeTokens(ctx cosmos.Context, newlyMinted sdk.DecCoin) (sdk.DecCoin, error)
GetCirculatingSupply(ctx cosmos.Context, denom string) (sdk.DecCoin, error)
GetInflationRate(ctx cosmos.Context) math.LegacyDec
MoveTokensFromDistributionToFoundationPoolAccount(ctx cosmos.Context) error
AllocateTokensToValidator(ctx context.Context, val stakingtypes.ValidatorI, tokens sdk.DecCoins) error
BurnCoins(ctx context.Context, moduleName string, coins sdk.Coins) error

// Query
Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error)
Expand Down Expand Up @@ -382,114 +384,125 @@ func (k KVStore) GetAuthority() string {
return k.authority
}

func (k KVStore) GetCirculatingSupply(ctx cosmos.Context, denom string) (cosmos.Coin, error) {
func (k KVStore) GetCirculatingSupply(ctx cosmos.Context, denom string) (sdk.DecCoin, error) {
sdkContext := sdk.UnwrapSDKContext(ctx)

// Get Total Supply
fullTokenSupply, err := k.coinKeeper.TotalSupply(ctx, &banktypes.QueryTotalSupplyRequest{})
fullTokenSupply, err := k.coinKeeper.SupplyOf(ctx, &banktypes.QuerySupplyOfRequest{Denom: configs.Denom})
if err != nil {
sdkContext.Logger().Error("Failed to get full token supply data", err)
return cosmos.NewCoin(denom, sdkmath.NewInt(0)), err
return sdk.NewDecCoin(denom, sdkmath.NewInt(0)), err
}
totalSupply := fullTokenSupply.Supply.AmountOf(configs.Denom)
totalSupply := fullTokenSupply.GetAmount().Amount

sdkContext.Logger().Info(fmt.Sprintf("TotalSupply %v", totalSupply))

// Get the account addresses whose balances need to be exempted
devAccountAddress, err := k.getFoundationDevAccountAddress()
if err != nil {
return cosmos.NewCoin(denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
return sdk.NewDecCoin(denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
}

communityAccountAddress, err := k.getFoundationCommunityAccountAddress()
if err != nil {
return cosmos.NewCoin(denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
return sdk.NewDecCoin(denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
}

grantAccountAddress, err := k.getFoundationGrantsAccountAddress()
if err != nil {
return cosmos.NewCoin(denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
return sdk.NewDecCoin(denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
}

// Module Address for which the circulating supply should be exempted
moduleAddressToExempt := []sdk.AccAddress{
// Account Address for which the circulating supply should be exempted
addressToExempt := []sdk.AccAddress{
devAccountAddress,
communityAccountAddress,
grantAccountAddress,
k.stakingKeeper.GetBondedPool(ctx).GetAddress(),
k.GetModuleAccAddress(types.ModuleName),
k.GetModuleAccAddress("claimarkeo"),
}

exemptBalance := cosmos.NewInt(0)

// range over the module and create exempt balances
for _, moduleAddress := range moduleAddressToExempt {
moduleBalance := k.coinKeeper.GetBalance(ctx, moduleAddress, denom)
exemptBalance = exemptBalance.Add(moduleBalance.Amount)
sdkContext.Logger().Info("Starting to calculate exempt balances")

// Range over the module accounts to create exempt balances
for _, address := range addressToExempt {
moduleBalance := k.coinKeeper.GetBalance(ctx, address, denom)
sdkContext.Logger().Info(fmt.Sprintf("Module address: %v, Balance: %v %v", address.String(), moduleBalance.Amount, denom))

if !moduleBalance.IsZero() {
exemptBalance = exemptBalance.Add(moduleBalance.Amount)
} else {
sdkContext.Logger().Info(fmt.Sprintf("Module address: %v has zero balance for denom: %v", address.String(), denom))
}
}

// total supply without balances of exempted module
circulatingSupply := totalSupply.Sub(exemptBalance)
sdkContext.Logger().Info(fmt.Sprintf("Total supply %v exempted balance %v, final balance %v", totalSupply, exemptBalance, circulatingSupply))

return cosmos.NewCoin(denom, circulatingSupply), nil
return sdk.NewDecCoin(denom, circulatingSupply), nil
}

func (k KVStore) MintAndDistributeTokens(ctx cosmos.Context, newlyMinted cosmos.Coin) (sdk.Coin, error) {
func (k KVStore) MintAndDistributeTokens(ctx cosmos.Context, newlyMinted sdk.DecCoin) (sdk.DecCoin, error) {
sdkContext := sdk.UnwrapSDKContext(ctx)

params := k.GetParams(ctx)
newlyMintedAmount := newlyMinted.Amount

// mint newly added tokens to reserve
if err := k.MintToModule(ctx, types.ModuleName, newlyMinted); err != nil {
if err := k.MintToModule(ctx, types.ModuleName, sdk.NewCoin(newlyMinted.Denom, newlyMinted.Amount.RoundInt())); err != nil {
sdkContext.Logger().Error(fmt.Sprintf("failed to mint %s", err))
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), err
return sdk.NewDecCoin(newlyMinted.Denom, sdkmath.NewInt(0)), err
}

devFundAmount := newlyMintedAmount.Mul(params.DevFundPercentage).Quo(sdkmath.NewInt(100))
communityPoolAmount := newlyMintedAmount.Mul(params.CommunityPoolPercentage).Quo(sdkmath.NewInt(100))
grantFundAmount := newlyMintedAmount.Mul(params.GrantFundPercentage).Quo(sdkmath.NewInt(100))
devFundAmount := newlyMintedAmount.Mul(params.DevFundPercentage.ToLegacyDec()).Quo(sdkmath.NewInt(100).ToLegacyDec())
communityPoolAmount := newlyMintedAmount.Mul(params.CommunityPoolPercentage.ToLegacyDec()).Quo(sdkmath.NewInt(100).ToLegacyDec())
grantFundAmount := newlyMintedAmount.Mul(params.GrantFundPercentage.ToLegacyDec()).Quo(sdkmath.NewInt(100).ToLegacyDec())

devAccountAddress, err := k.getFoundationDevAccountAddress()
if err != nil {
sdkContext.Logger().Error(fmt.Sprintf("failed to fetch foundational account %s", err))
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
return sdk.NewDecCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
}

communityAccountAddress, err := k.getFoundationCommunityAccountAddress()
if err != nil {
sdkContext.Logger().Error(fmt.Sprintf("failed to fetch foundational account %s", err))
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
return sdk.NewDecCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
}

grantAccountAddress, err := k.getFoundationGrantsAccountAddress()
if err != nil {
sdkContext.Logger().Error(fmt.Sprintf("failed to fetch foundational account %s", err))
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
return sdk.NewDecCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("failed to fetch foundational account %s", err)
}

if err := k.SendFromModuleToAccount(ctx, types.ModuleName, devAccountAddress, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, devFundAmount))); err != nil {
if err := k.SendFromModuleToAccount(ctx, types.ModuleName, devAccountAddress, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, devFundAmount.RoundInt()))); err != nil {
sdkContext.Logger().Error(fmt.Sprintf("failed to send amount to Dev foundational account %s", err))
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
return sdk.NewDecCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
}

if err := k.SendFromModuleToAccount(ctx, types.ModuleName, communityAccountAddress, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, communityPoolAmount))); err != nil {
if err := k.SendFromModuleToAccount(ctx, types.ModuleName, communityAccountAddress, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, communityPoolAmount.RoundInt()))); err != nil {
sdkContext.Logger().Error(fmt.Sprintf("failed to send amount to Community foundational account %s", err))
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
return sdk.NewDecCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
}

if err := k.SendFromModuleToAccount(ctx, types.ModuleName, grantAccountAddress, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, grantFundAmount))); err != nil {
if err := k.SendFromModuleToAccount(ctx, types.ModuleName, grantAccountAddress, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, grantFundAmount.RoundInt()))); err != nil {
sdkContext.Logger().Error(fmt.Sprintf("failed to send amount to Grant foundational account %s", err))
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
return sdk.NewDecCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
}

balance := newlyMintedAmount.Sub(devFundAmount).Sub(communityPoolAmount).Sub(grantFundAmount)
return cosmos.NewCoin(newlyMinted.Denom, balance), nil
return sdk.NewDecCoin(newlyMinted.Denom, balance.RoundInt()), nil
}

func (k KVStore) GetInflationRate(ctx cosmos.Context) (math.LegacyDec, error) {
minter, err := k.mintKeeper.Minter.Get(ctx)
if err != nil {
return math.LegacyNewDec(0), err
}
func (k KVStore) GetInflationRate(ctx cosmos.Context) math.LegacyDec {

params := k.GetParams(ctx)

return minter.Inflation, nil
return params.InflationChangePercentage.ToLegacyDec()
}

// transfer tokens form the Distribution to Foundation Community Pool
Expand All @@ -506,7 +519,7 @@ func (k KVStore) MoveTokensFromDistributionToFoundationPoolAccount(ctx cosmos.Co
return fmt.Errorf("failed to fetch foundational account %s", err)
}

if amount.RoundInt64() > 0 {
if !amount.IsZero() {
if err := k.distributionKeeper.DistributeFromFeePool(ctx, cosmos.NewCoins(cosmos.NewCoin(configs.Denom, amount.RoundInt())), communityAccountAddress); err != nil {
if err.Error() == "community pool does not have sufficient coins to distribute" {
ctx.Logger().Info(fmt.Sprintf("%s", err))
Expand All @@ -516,7 +529,9 @@ func (k KVStore) MoveTokensFromDistributionToFoundationPoolAccount(ctx cosmos.Co
return err
}
}

}

return nil
}

Expand All @@ -531,3 +546,11 @@ func (k KVStore) getFoundationCommunityAccountAddress() (cosmos.AccAddress, erro
func (k KVStore) getFoundationGrantsAccountAddress() (cosmos.AccAddress, error) {
return sdk.AccAddressFromBech32(types.FoundationGrantsAccount)
}

func (k KVStore) AllocateTokensToValidator(ctx context.Context, val stakingtypes.ValidatorI, tokens sdk.DecCoins) error {
return k.distributionKeeper.AllocateTokensToValidator(ctx, val, tokens)
}

func (k KVStore) BurnCoins(ctx context.Context, moduleName string, coins sdk.Coins) error {
return k.coinKeeper.BurnCoins(ctx, moduleName, coins)
}
Loading

0 comments on commit 9a15947

Please sign in to comment.