2018-06-21 17:19:14 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
2018-12-11 11:05:49 -08:00
|
|
|
"fmt"
|
2018-09-20 07:53:52 -07:00
|
|
|
|
2018-06-21 17:19:14 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-05-16 08:25:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types"
|
2018-06-21 17:19:14 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
// InitGenesis - store genesis parameters
|
2020-01-30 13:31:16 -08:00
|
|
|
func InitGenesis(ctx sdk.Context, bk types.BankKeeper, supplyKeeper types.SupplyKeeper, k Keeper, data GenesisState) {
|
2019-08-08 12:51:18 -07:00
|
|
|
k.SetProposalID(ctx, data.StartingProposalID)
|
|
|
|
k.SetDepositParams(ctx, data.DepositParams)
|
|
|
|
k.SetVotingParams(ctx, data.VotingParams)
|
|
|
|
k.SetTallyParams(ctx, data.TallyParams)
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
// check if the deposits pool account exists
|
|
|
|
moduleAcc := k.GetGovernanceAccount(ctx)
|
|
|
|
if moduleAcc == nil {
|
|
|
|
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
|
|
|
|
}
|
|
|
|
|
|
|
|
var totalDeposits sdk.Coins
|
2018-11-08 16:28:28 -08:00
|
|
|
for _, deposit := range data.Deposits {
|
2019-08-08 12:51:18 -07:00
|
|
|
k.SetDeposit(ctx, deposit)
|
2020-01-03 12:44:53 -08:00
|
|
|
totalDeposits = totalDeposits.Add(deposit.Amount...)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
for _, vote := range data.Votes {
|
2019-08-08 12:51:18 -07:00
|
|
|
k.SetVote(ctx, vote)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
2019-06-28 13:11:27 -07:00
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
for _, proposal := range data.Proposals {
|
2019-03-15 09:47:47 -07:00
|
|
|
switch proposal.Status {
|
2019-01-07 14:24:04 -08:00
|
|
|
case StatusDepositPeriod:
|
2019-06-04 11:38:11 -07:00
|
|
|
k.InsertInactiveProposalQueue(ctx, proposal.ProposalID, proposal.DepositEndTime)
|
2019-01-07 14:24:04 -08:00
|
|
|
case StatusVotingPeriod:
|
2019-06-04 11:38:11 -07:00
|
|
|
k.InsertActiveProposalQueue(ctx, proposal.ProposalID, proposal.VotingEndTime)
|
2019-01-07 14:24:04 -08:00
|
|
|
}
|
2018-11-08 16:28:28 -08:00
|
|
|
k.SetProposal(ctx, proposal)
|
|
|
|
}
|
2019-06-28 13:11:27 -07:00
|
|
|
|
|
|
|
// add coins if not provided on genesis
|
2020-01-30 13:31:16 -08:00
|
|
|
if bk.GetAllBalances(ctx, moduleAcc.GetAddress()).IsZero() {
|
|
|
|
if err := bk.SetBalances(ctx, moduleAcc.GetAddress(), totalDeposits); err != nil {
|
2019-06-28 13:11:27 -07:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
supplyKeeper.SetModuleAccount(ctx, moduleAcc)
|
|
|
|
}
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
|
2018-11-08 16:28:28 -08:00
|
|
|
// ExportGenesis - output genesis parameters
|
|
|
|
func ExportGenesis(ctx sdk.Context, k Keeper) GenesisState {
|
2019-06-04 11:38:11 -07:00
|
|
|
startingProposalID, _ := k.GetProposalID(ctx)
|
2018-11-06 23:33:18 -08:00
|
|
|
depositParams := k.GetDepositParams(ctx)
|
|
|
|
votingParams := k.GetVotingParams(ctx)
|
|
|
|
tallyParams := k.GetTallyParams(ctx)
|
2019-10-18 09:46:21 -07:00
|
|
|
proposals := k.GetProposals(ctx)
|
2019-06-04 11:38:11 -07:00
|
|
|
|
|
|
|
var proposalsDeposits Deposits
|
|
|
|
var proposalsVotes Votes
|
2018-11-08 16:28:28 -08:00
|
|
|
for _, proposal := range proposals {
|
2019-06-04 11:38:11 -07:00
|
|
|
deposits := k.GetDeposits(ctx, proposal.ProposalID)
|
|
|
|
proposalsDeposits = append(proposalsDeposits, deposits...)
|
|
|
|
|
|
|
|
votes := k.GetVotes(ctx, proposal.ProposalID)
|
|
|
|
proposalsVotes = append(proposalsVotes, votes...)
|
2018-11-08 16:28:28 -08:00
|
|
|
}
|
2018-06-21 17:19:14 -07:00
|
|
|
|
|
|
|
return GenesisState{
|
2018-07-26 18:24:41 -07:00
|
|
|
StartingProposalID: startingProposalID,
|
2019-06-04 11:38:11 -07:00
|
|
|
Deposits: proposalsDeposits,
|
|
|
|
Votes: proposalsVotes,
|
2018-11-08 16:28:28 -08:00
|
|
|
Proposals: proposals,
|
2018-11-06 23:33:18 -08:00
|
|
|
DepositParams: depositParams,
|
|
|
|
VotingParams: votingParams,
|
|
|
|
TallyParams: tallyParams,
|
2018-06-21 17:19:14 -07:00
|
|
|
}
|
|
|
|
}
|