Skip to content

Commit

Permalink
feat: update foundational accounts for handling tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyasbhat0 committed Sep 18, 2024
1 parent e4ffd8a commit 8f4954f
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 76 deletions.
33 changes: 17 additions & 16 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,22 +200,22 @@ var (

// module account permissions
maccPerms = map[string][]string{
authtypes.FeeCollectorName: nil,
distrtypes.ModuleName: nil,
icatypes.ModuleName: nil,
minttypes.ModuleName: {authtypes.Minter},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
claimmoduletypes.ModuleName: {authtypes.Minter},
arkeomoduletypes.ModuleName: {},
arkeomoduletypes.ReserveName: {},
arkeomoduletypes.ProviderName: {},
arkeomoduletypes.ContractName: {},
arkeomoduletypes.DevFundPool: {authtypes.Minter},
arkeomoduletypes.CommunityPool: {authtypes.Minter},
arkeomoduletypes.GrantPool: {authtypes.Minter},
authtypes.FeeCollectorName: nil,
distrtypes.ModuleName: nil,
icatypes.ModuleName: nil,
minttypes.ModuleName: {authtypes.Minter},
stakingtypes.BondedPoolName: {authtypes.Burner, authtypes.Staking},
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
claimmoduletypes.ModuleName: {authtypes.Minter},
arkeomoduletypes.ModuleName: {},
arkeomoduletypes.ReserveName: {},
arkeomoduletypes.ProviderName: {},
arkeomoduletypes.ContractName: {},
arkeomoduletypes.FoundationDevAccount: {authtypes.Minter},
arkeomoduletypes.FoundationGrantsAccount: {authtypes.Minter},
arkeomoduletypes.FoundationCommunityAccount: {authtypes.Minter},
}
)

Expand Down Expand Up @@ -598,6 +598,7 @@ func NewArkeoApp(
govModuleAddr,
logger,
app.Keepers.MintKeeper,
app.Keepers.DistrKeeper,
)

/**** Module Options ****/
Expand Down
3 changes: 3 additions & 0 deletions scripts/genesis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ if [ ! -f ~/.arkeo/config/genesis.json ]; then
if [ "$NET" = "mocknet" ] || [ "$NET" = "testnet" ]; then
add_module tarkeo1d0m97ywk2y4vq58ud6q5e0r3q9khj9e3unfe4t $TOKEN 10000000000000000 'arkeo-reserve' # reserve, 10m
add_module tarkeo14tmx70mvve3u7hfmd45vle49kvylk6s2wllxny $TOKEN 10000000000000000 'claimarkeo'
add_module tarkeo10sav33v67743s6cl2cvjmmua7c5arysw3txz9r $TOKEN 10000000000000000 'devpool'
add_module tarkeo1v50hrsxx0mxar4653aujcnqyjft07w0npcxrjx $TOKEN 10000000000000000 'communitypool'
add_module tarkeo16k3k0erkwaanqnup20dxxenpd6wh058nh4pgup $TOKEN 10000000000000000 'grantspool'

echo "shoulder heavy loyal save patient deposit crew bag pull club escape eyebrow hip verify border into wire start pact faint fame festival solve shop" | arkeod keys add alice --keyring-backend test --recover
ALICE=$(arkeod keys show alice -a --keyring-backend test)
Expand Down
14 changes: 14 additions & 0 deletions testutil/keeper/arkeo.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
authcodec "github.com/cosmos/cosmos-sdk/x/auth/codec"
distkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
disttypes "github.com/cosmos/cosmos-sdk/x/distribution/types"

"github.com/arkeonetwork/arkeo/common"
"github.com/arkeonetwork/arkeo/common/cosmos"
Expand Down Expand Up @@ -46,6 +48,7 @@ func ArkeoKeeper(t testing.TB) (cosmos.Context, keeper.Keeper) {
tkeyParams := cosmos.NewTransientStoreKey(paramstypes.TStoreKey)
memStoreKey := storetypes.NewMemoryStoreKey(types.MemStoreKey)
keyMint := cosmos.NewKVStoreKey(minttypes.StoreKey)
keydist := cosmos.NewKVStoreKey(disttypes.StoreKey)
logger := log.NewNopLogger()

db := tmdb.NewMemDB()
Expand Down Expand Up @@ -105,6 +108,16 @@ func ArkeoKeeper(t testing.TB) (cosmos.Context, keeper.Keeper) {
authcodec.NewBech32Codec(sdk.GetConfig().GetBech32ConsensusAddrPrefix()),
)

dk := distkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keydist),
ak,
bk,
sk,
authtypes.FeeCollectorName,
govModuleAddr,
)

mk := mintkeeper.NewKeeper(
cdc,
runtime.NewKVStoreService(keyMint),
Expand All @@ -126,6 +139,7 @@ func ArkeoKeeper(t testing.TB) (cosmos.Context, keeper.Keeper) {
govModuleAddr,
logger,
mk,
dk,
)
ctx := sdk.NewContext(stateStore, tmproto.Header{}, false, log.NewNopLogger())

Expand Down
132 changes: 92 additions & 40 deletions x/arkeo/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
distkeeper "github.com/cosmos/cosmos-sdk/x/distribution/keeper"
minttypes "github.com/cosmos/cosmos-sdk/x/mint/keeper"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
Expand Down Expand Up @@ -64,6 +65,7 @@ type Keeper interface {
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)
MoveTokensFromDistributionToFoundationPoolAccount(ctx cosmos.Context) error

// Query
Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error)
Expand Down Expand Up @@ -116,53 +118,49 @@ const (
)

type KVStore struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
coinKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
stakingKeeper stakingkeeper.Keeper
authority string
logger log.Logger
mintKeeper minttypes.Keeper
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
memKey storetypes.StoreKey
paramstore paramtypes.Subspace
coinKeeper bankkeeper.Keeper
accountKeeper authkeeper.AccountKeeper
stakingKeeper stakingkeeper.Keeper
authority string
logger log.Logger
mintKeeper minttypes.Keeper
distributionKeeper distkeeper.Keeper
}

func NewKVStore(
cdc codec.BinaryCodec,
storeKey,
memKey storetypes.StoreKey,
ps paramtypes.Subspace,

/*
accountKeeper keeper.AccountKeeper
bankKeeper keeper.Keeper
distributionKeeper distkeeper.Keeper
stakingKeeper stakingkeeper.Keeper
*/
coinKeeper bankkeeper.Keeper,
accountKeeper authkeeper.AccountKeeper,
stakingKeeper stakingkeeper.Keeper,
authority string,
logger log.Logger,
mintKeeper minttypes.Keeper,
distributionKeeper distkeeper.Keeper,
) *KVStore {
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}

return &KVStore{
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
coinKeeper: coinKeeper,
accountKeeper: accountKeeper,
stakingKeeper: stakingKeeper,
authority: authority,
logger: logger,
mintKeeper: mintKeeper,
cdc: cdc,
storeKey: storeKey,
memKey: memKey,
paramstore: ps,
coinKeeper: coinKeeper,
accountKeeper: accountKeeper,
stakingKeeper: stakingKeeper,
authority: authority,
logger: logger,
mintKeeper: mintKeeper,
distributionKeeper: distributionKeeper,
}
}

Expand Down Expand Up @@ -390,14 +388,25 @@ func (k KVStore) GetCirculatingSupply(ctx cosmos.Context, denom string) (cosmos.
totalSupply := fullTokenSupply.Supply.AmountOf(configs.Denom)
sdkContext.Logger().Error("Current supply of token with denom :", denom, "supply:", totalSupply)

// Get the account addresses whose balances need to be exempted
devAccountAddress := k.getFoundationDevAccountAddress()

communityAccountAddress := k.getFoundationCommunityAccountAddress()

grantAccountAddress := k.getFoundationGrantsAccountAddress()

// Module Address for which the circulating supply should be exempted
modulesToExempt := []string{types.GrantPool, types.CommunityPool, types.DevFundPool}
moduleAddressToExempt := []sdk.AccAddress{
devAccountAddress,
communityAccountAddress,
grantAccountAddress,
}

exemptBalance := cosmos.NewInt(0)

// range over the module and create exempt balances
for _, moduleName := range modulesToExempt {
moduleAddr := k.accountKeeper.GetModuleAddress(moduleName)
moduleBalance := k.coinKeeper.GetBalance(ctx, moduleAddr, denom)
for _, moduleAddress := range moduleAddressToExempt {
moduleBalance := k.coinKeeper.GetBalance(ctx, moduleAddress, denom)
exemptBalance = exemptBalance.Add(moduleBalance.Amount)
}

Expand All @@ -407,26 +416,35 @@ func (k KVStore) GetCirculatingSupply(ctx cosmos.Context, denom string) (cosmos.
return cosmos.NewCoin(denom, circulatingSupply), nil
}

func (k KVStore) MintAndDistributeTokens(ctx cosmos.Context, newlyMinted cosmos.Coin) (cosmos.Coin, error) {
func (k KVStore) MintAndDistributeTokens(ctx cosmos.Context, newlyMinted cosmos.Coin) (sdk.Coin, error) {

params := k.GetParams(ctx)

newlyMintedDec := sdkmath.LegacyNewDecFromInt(newlyMinted.Amount)

// mint newly added tokens to reserve
if err := k.MintToModule(ctx, types.ReserveName, newlyMinted); err != nil {
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), err
}

devFundAmount := newlyMintedDec.Mul(params.DevFundPercentage).TruncateInt()
communityPoolAmount := newlyMintedDec.Mul(params.CommunityPoolPercentage).TruncateInt()
// validatorRewardAmount := newlyMintedDec.Mul(params.ValidatorRewardsPercentage).TruncateInt()
grantFundAmount := newlyMintedDec.Mul(params.GrantFundPercentage).TruncateInt()

if err := k.SendFromModuleToModule(ctx, types.ReserveName, types.FoundationDevAccount, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, devFundAmount))); err != nil {
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
}

if err := k.MintToModule(ctx, types.DevFundPool, cosmos.NewCoin(newlyMinted.Denom, devFundAmount)); err != nil {
return cosmos.NewCoin(configs.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
if err := k.SendFromModuleToModule(ctx, types.ReserveName, types.FoundationCommunityAccount, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, communityPoolAmount))); err != nil {
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
}

if err := k.MintToModule(ctx, types.CommunityPool, cosmos.NewCoin(newlyMinted.Denom, communityPoolAmount)); err != nil {
return cosmos.NewCoin(configs.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
if err := k.SendFromModuleToModule(ctx, types.ReserveName, types.FoundationGrantsAccount, cosmos.NewCoins(cosmos.NewCoin(newlyMinted.Denom, grantFundAmount))); err != nil {
return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(0)), fmt.Errorf("error sending amount to module %s", err)
}
balance := newlyMintedDec.Sub(sdkmath.LegacyDec(devFundAmount)).Sub(sdkmath.LegacyDec(communityPoolAmount))

return cosmos.NewCoin(configs.Denom, balance.RoundInt()), nil
balance := newlyMintedDec.Sub(sdkmath.LegacyDec(devFundAmount)).Sub(sdkmath.LegacyDec(communityPoolAmount)).Sub(sdkmath.LegacyDec(grantFundAmount))

return cosmos.NewCoin(newlyMinted.Denom, sdkmath.NewInt(balance.RoundInt64())), nil

}

Expand All @@ -438,3 +456,37 @@ func (k KVStore) GetInflationRate(ctx cosmos.Context) (math.LegacyDec, error) {

return minter.Inflation, nil
}

// transfer tokens form the Distribution to Foundation Community Pool
func (k KVStore) MoveTokensFromDistributionToFoundationPoolAccount(ctx cosmos.Context) error {

// get pool balance
pool, err := k.distributionKeeper.FeePool.Get(ctx)
if err != nil {
return err
}
amount := pool.CommunityPool.AmountOf(configs.Denom)

communityAccountAddress := k.getFoundationCommunityAccountAddress()

if amount.RoundInt64() > 0 {
err = k.distributionKeeper.DistributeFromFeePool(ctx, cosmos.NewCoins(cosmos.NewCoin(configs.Denom, amount.RoundInt())), communityAccountAddress)
if err != nil {
return err
}

}
return err
}

func (k KVStore) getFoundationDevAccountAddress() cosmos.AccAddress {
return k.GetModuleAccAddress(types.FoundationDevAccount)
}

func (k KVStore) getFoundationCommunityAccountAddress() cosmos.AccAddress {
return k.GetModuleAccAddress(types.FoundationCommunityAccount)
}

func (k KVStore) getFoundationGrantsAccountAddress() cosmos.AccAddress {
return k.GetModuleAccAddress(types.FoundationGrantsAccount)
}
Loading

0 comments on commit 8f4954f

Please sign in to comment.