Skip to content

Commit

Permalink
Add DAO module + Upgrade logic (#320)
Browse files Browse the repository at this point in the history
* feat: add dao module + upgrade
  • Loading branch information
kioqq authored Jun 8, 2024
1 parent 6bc8a4e commit 6c2cce7
Show file tree
Hide file tree
Showing 52 changed files with 7,449 additions and 72 deletions.
1 change: 1 addition & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,4 @@ ignore:
- "types/*.pb.go"
- "x/**/*.pb.gw.go"
- "scripts/"
- "x/dao/**"
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DIFF_TAG=$(shell git rev-list --tags="v*" --max-count=1 --not $(shell git rev-li
DEFAULT_TAG=$(shell git rev-list --tags="v*" --max-count=1)
# VERSION ?= $(shell echo $(shell git describe --tags $(or $(DIFF_TAG), $(DEFAULT_TAG))) | sed 's/^v//')

VERSION := "1.7.5"
VERSION := "1.7.6"
CBFTVERSION := $(shell go list -m github.com/cometbft/cometbft | sed 's:.* ::')
COMMIT := $(shell git log -1 --format='%H')
LEDGER_ENABLED ?= true
Expand Down
31 changes: 30 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,9 @@ import (
"github.com/haqq-network/haqq/x/coinomics"
coinomicskeeper "github.com/haqq-network/haqq/x/coinomics/keeper"
coinomicstypes "github.com/haqq-network/haqq/x/coinomics/types"
"github.com/haqq-network/haqq/x/dao"
daokeeper "github.com/haqq-network/haqq/x/dao/keeper"
daotypes "github.com/haqq-network/haqq/x/dao/types"
"github.com/haqq-network/haqq/x/epochs"
epochskeeper "github.com/haqq-network/haqq/x/epochs/keeper"
epochstypes "github.com/haqq-network/haqq/x/epochs/types"
Expand All @@ -168,6 +171,7 @@ import (
v173 "github.com/haqq-network/haqq/app/upgrades/v1.7.3"
v174 "github.com/haqq-network/haqq/app/upgrades/v1.7.4"
v175 "github.com/haqq-network/haqq/app/upgrades/v1.7.5"
v176 "github.com/haqq-network/haqq/app/upgrades/v1.7.6"

// NOTE: override ICS20 keeper to support IBC transfers of ERC20 tokens
"github.com/haqq-network/haqq/x/ibc/transfer"
Expand Down Expand Up @@ -245,6 +249,7 @@ var (
epochs.AppModuleBasic{},
consensus.AppModuleBasic{},
liquidvesting.AppModuleBasic{},
dao.AppModuleBasic{},
)

// module account permissions
Expand All @@ -261,6 +266,7 @@ var (
coinomicstypes.ModuleName: {authtypes.Minter},
vestingtypes.ModuleName: nil, // Add vesting module account
liquidvestingtypes.ModuleName: {authtypes.Minter, authtypes.Burner},
daotypes.ModuleName: nil,
}

// module accounts that are allowed to receive tokens
Expand Down Expand Up @@ -329,6 +335,7 @@ type Haqq struct {

// Haqq keepers
CoinomicsKeeper coinomicskeeper.Keeper
DaoKeeper daokeeper.Keeper

// the module manager
mm *module.Manager
Expand Down Expand Up @@ -402,6 +409,7 @@ func NewHaqq(
// haqq keys
coinomicstypes.StoreKey,
liquidvestingtypes.StoreKey,
daotypes.StoreKey,
)

// Add the EVM transient store key
Expand Down Expand Up @@ -547,10 +555,14 @@ func NewHaqq(
)

app.LiquidVestingKeeper = liquidvestingkeeper.NewKeeper(
keys[vestingtypes.StoreKey], appCodec, app.GetSubspace(liquidvestingtypes.ModuleName),
keys[liquidvestingtypes.StoreKey], appCodec, app.GetSubspace(liquidvestingtypes.ModuleName),
app.AccountKeeper, app.BankKeeper, app.Erc20Keeper, app.VestingKeeper,
)

app.DaoKeeper = daokeeper.NewBaseKeeper(
appCodec, keys[daotypes.StoreKey], app.AccountKeeper, app.BankKeeper, authAddr,
)

epochsKeeper := epochskeeper.NewKeeper(appCodec, keys[epochstypes.StoreKey])
app.EpochsKeeper = *epochsKeeper.SetHooks(
epochskeeper.NewMultiEpochHooks(
Expand Down Expand Up @@ -699,6 +711,7 @@ func NewHaqq(

// Haqq app modules
coinomics.NewAppModule(app.CoinomicsKeeper, app.AccountKeeper, app.StakingKeeper),
dao.NewAppModule(appCodec, app.DaoKeeper, app.GetSubspace(daotypes.ModuleName)),
)

// During begin block slashing happens after distr.BeginBlocker so that
Expand Down Expand Up @@ -736,6 +749,7 @@ func NewHaqq(
coinomicstypes.ModuleName,
consensusparamtypes.ModuleName,
liquidvestingtypes.ModuleName,
daotypes.ModuleName,
)

// NOTE: fee market module must go last in order to retrieve the block gas used.
Expand Down Expand Up @@ -772,6 +786,7 @@ func NewHaqq(
coinomicstypes.ModuleName,
consensusparamtypes.ModuleName,
liquidvestingtypes.ModuleName,
daotypes.ModuleName,
)

// NOTE: The genutils module must occur after staking so that pools are
Expand Down Expand Up @@ -811,6 +826,7 @@ func NewHaqq(
coinomicstypes.ModuleName,
erc20types.ModuleName,
epochstypes.ModuleName,
daotypes.ModuleName,
// NOTE: crisis module must go at the end to check for invariants on each module
crisistypes.ModuleName,
consensusparamtypes.ModuleName,
Expand Down Expand Up @@ -1173,6 +1189,7 @@ func initParamsKeeper(
// haqq subspaces
paramsKeeper.Subspace(coinomicstypes.ModuleName)
paramsKeeper.Subspace(liquidvestingtypes.ModuleName)
paramsKeeper.Subspace(daotypes.ModuleName)

return paramsKeeper
}
Expand Down Expand Up @@ -1275,6 +1292,12 @@ func (app *Haqq) setupUpgradeHandlers() {
v175.CreateUpgradeHandler(app.mm, app.configurator, app.BankKeeper, app.LiquidVestingKeeper, app.Erc20Keeper, *app.EvmKeeper, app.AccountKeeper),
)

// v1.7.6 Turn off liquid vesting
app.UpgradeKeeper.SetUpgradeHandler(
v176.UpgradeName,
v176.CreateUpgradeHandler(app.mm, app.configurator, app.AccountKeeper, app.BankKeeper, app.StakingKeeper, app.DaoKeeper, app.LiquidVestingKeeper, app.Erc20Keeper),
)

// When a planned update height is reached, the old binary will panic
// writing on disk the height and name of the update that triggered it
// This will read that value, and execute the preparations for the upgrade.
Expand Down Expand Up @@ -1315,6 +1338,12 @@ func (app *Haqq) setupUpgradeHandlers() {
packetforwardtypes.ModuleName,
},
}
case v176.UpgradeName:
storeUpgrades = &storetypes.StoreUpgrades{
Added: []string{
daotypes.ModuleName,
},
}
}

if storeUpgrades != nil {
Expand Down
12 changes: 12 additions & 0 deletions app/upgrades/v1.7.6/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package v176

const (
// UpgradeName is the shared upgrade plan name for network
UpgradeName = "v1.7.6"

// LockupLengthThreshold is the threshold for lockup periods length
LockupLengthThreshold = 1717043774

// EndTimeForCheck is the threshold for end time of lockup periods
EndTimeForCheck = 1767213372
)
68 changes: 68 additions & 0 deletions app/upgrades/v1.7.6/fix_periods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package v176

import (
"time"

"cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
sdkvestingtypes "github.com/cosmos/cosmos-sdk/x/auth/vesting/types"

vestingtypes "github.com/haqq-network/haqq/x/vesting/types"
)

func FixLockupPeriods(ctx sdk.Context, ak authkeeper.AccountKeeper) error {
logger := ctx.Logger()
logger.Info("Start turning on DAO module")

ak.IterateAccounts(ctx, func(acc authtypes.AccountI) (stop bool) {
// Check if acc is vesting account
vacc, ok := acc.(*vestingtypes.ClawbackVestingAccount)
if !ok {
return false
}

firstPeriod := vacc.LockupPeriods[0]
firstPeriodTime := time.Unix(vacc.StartTime.Unix()+firstPeriod.Length, 0)

if firstPeriodTime.After(time.Unix(LockupLengthThreshold, 0)) && vacc.GetEndTime() > EndTimeForCheck {
unixBlockTime := ctx.BlockTime().Unix()

sumOfLockupPeriods := math.NewInt(0)
elapsedTime := vacc.GetStartTime()
countPeriods := 0

newLookupPeriods := make(sdkvestingtypes.Periods, 0, len(vacc.LockupPeriods))

for _, period := range vacc.LockupPeriods {
elapsedTime += period.Length

if elapsedTime >= unixBlockTime {
countPeriods += 1
sumOfLockupPeriods = sumOfLockupPeriods.Add(period.Amount[0].Amount)
}
}

sumPerPeriod := sumOfLockupPeriods.QuoRaw(int64(countPeriods))
diff := sumOfLockupPeriods.Sub(sumPerPeriod.Mul(math.NewIntFromUint64(uint64(countPeriods))))

for index, period := range vacc.LockupPeriods {
period.Amount[0].Amount = sumPerPeriod

if index == len(vacc.LockupPeriods)-1 {
period.Amount[0].Amount = period.Amount[0].Amount.Add(diff)
}

newLookupPeriods = append(newLookupPeriods, period)
}

vacc.LockupPeriods = newLookupPeriods
ak.SetAccount(ctx, vacc)
}

return false
})

return nil
}
Loading

0 comments on commit 6c2cce7

Please sign in to comment.