Skip to content

Commit

Permalink
fix: double supply when add genesis vesting (#108)
Browse files Browse the repository at this point in the history
* fix: double supply when add genesis vesting

* fix: add miss flags
  • Loading branch information
hoanguyenkh authored Sep 13, 2022
1 parent df3fd72 commit 127c750
Showing 1 changed file with 51 additions and 1 deletion.
52 changes: 51 additions & 1 deletion cmd/astrad/genaccounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import (

const (
flagVestingStart = "vesting-start-time"
flagVestingEnd = "vesting-end-time"
flagVestingAmt = "vesting-amount"
)

// AddGenesisAccountCmd returns add-genesis-account cobra Command.
Expand Down Expand Up @@ -91,6 +93,19 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
if err != nil {
return err
}
vestingEnd, err := cmd.Flags().GetInt64(flagVestingEnd)
if err != nil {
return err
}
vestingAmtStr, err := cmd.Flags().GetString(flagVestingAmt)
if err != nil {
return err
}

vestingAmt, err := sdk.ParseCoinsNormalized(vestingAmtStr)
if err != nil {
return fmt.Errorf("failed to parse vesting amount: %w", err)
}

// create concrete account type based on input parameters
var genAccount authtypes.GenesisAccount
Expand All @@ -100,6 +115,7 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa

clawback, _ := cmd.Flags().GetBool(vestingcli.FlagClawback)

vestingCoins := sdk.NewCoins()
// Create ClawbackvestingAccount, sdk.VestingAccount or EthAccount
switch {
case clawback:
Expand Down Expand Up @@ -147,7 +163,6 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
commonStart, _ := vestingtypes.AlignSchedules(lockupStart, vestingStart, lockupPeriods, vestingPeriods)

// Get total lockup and vesting from schedules
vestingCoins := sdk.NewCoins()
for _, period := range vestingPeriods {
vestingCoins = vestingCoins.Add(period.Amount...)
}
Expand Down Expand Up @@ -196,6 +211,25 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
lockupPeriods,
vestingPeriods,
)
// SDK vesting types
case !vestingAmt.IsZero():
baseVestingAccount := authvesting.NewBaseVestingAccount(baseAccount, vestingAmt.Sort(), vestingEnd)

if (balances.Coins.IsZero() && !baseVestingAccount.OriginalVesting.IsZero()) ||
baseVestingAccount.OriginalVesting.IsAnyGT(balances.Coins) {
return fmt.Errorf("vesting amount cannot be greater than total amount")
}

switch {
case vestingStart != 0 && vestingEnd != 0:
genAccount = authvesting.NewContinuousVestingAccountRaw(baseVestingAccount, vestingStart)

case vestingEnd != 0:
genAccount = authvesting.NewDelayedVestingAccountRaw(baseVestingAccount)

default:
return fmt.Errorf("invalid vesting parameters; must supply start and end time or end time")
}
default:
genAccount = &ethermint.EthAccount{
BaseAccount: baseAccount,
Expand Down Expand Up @@ -247,6 +281,20 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
bankGenState.Supply = bankGenState.Supply.Add(balances.Coins...)

if clawback {
funderStr, err := cmd.Flags().GetString(vestingcli.FlagFunder)
if err != nil {
return fmt.Errorf("must specify the clawback vesting account funder with the --funder flag")
}
for i := range bankGenState.Balances {
if bankGenState.Balances[i].Address == funderStr {
bankGenState.Balances[i].Coins[0] = bankGenState.Balances[i].Coins[0].Sub(vestingCoins[0])
}
}
bankGenState.Balances = banktypes.SanitizeGenesisBalances(bankGenState.Balances)
bankGenState.Supply[0] = bankGenState.Supply[0].Sub(vestingCoins[0])
}

bankGenStateBz, err := clientCtx.Codec.MarshalJSON(bankGenState)
if err != nil {
return fmt.Errorf("failed to marshal bank genesis state: %w", err)
Expand All @@ -266,7 +314,9 @@ contain valid denominations. Accounts may optionally be supplied with vesting pa

cmd.Flags().String(flags.FlagHome, defaultNodeHome, "The application home directory")
cmd.Flags().String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test)")
cmd.Flags().String(flagVestingAmt, "", "amount of coins for vesting accounts")
cmd.Flags().Int64(flagVestingStart, 0, "schedule start time (unix epoch) for vesting accounts")
cmd.Flags().Int64(flagVestingEnd, 0, "schedule end time (unix epoch) for vesting accounts")
cmd.Flags().Bool(vestingcli.FlagClawback, false, "create clawback account")
cmd.Flags().String(vestingcli.FlagFunder, "", "funder address for clawback")
cmd.Flags().String(vestingcli.FlagLockup, "", "path to file containing unlocking periods")
Expand Down

0 comments on commit 127c750

Please sign in to comment.