Skip to content

Commit

Permalink
change periodic vesting account to vest tokens linearly during period
Browse files Browse the repository at this point in the history
  • Loading branch information
yun-yeo committed May 26, 2022
1 parent ad9e562 commit eca20f0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions x/auth/vesting/types/vesting_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,11 @@ func (pva PeriodicVestingAccount) GetVestedCoins(blockTime time.Time) sdk.Coins
for _, period := range pva.VestingPeriods {
x := blockTime.Unix() - currentPeriodStartTime
if x < period.Length {
coins, _ := sdk.NewDecCoinsFromCoins(period.Amount...).
MulDec(sdk.NewDec(x).QuoInt64(period.Length)).
TruncateDecimal()
vestedCoins = vestedCoins.Add(coins...)

break
}

Expand Down
12 changes: 6 additions & 6 deletions x/auth/vesting/types/vesting_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,17 +329,17 @@ func TestGetVestedCoinsPeriodicVestingAcc(t *testing.T) {
vestedCoins = pva.GetVestedCoins(endTime)
require.Equal(t, origCoins, vestedCoins)

// require no coins vested during first vesting period
// require 50% of first period coins vested during first vesting period
vestedCoins = pva.GetVestedCoins(now.Add(6 * time.Hour))
require.Nil(t, vestedCoins)
require.Equal(t, vestedCoins, sdk.Coins{sdk.NewInt64Coin(feeDenom, 250), sdk.NewInt64Coin(stakeDenom, 25)})

// require 50% of coins vested after period 1
vestedCoins = pva.GetVestedCoins(now.Add(12 * time.Hour))
require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, vestedCoins)

// require period 2 coins don't vest until period is over
// require 50% of period 2 coins vested until 15 hours
vestedCoins = pva.GetVestedCoins(now.Add(15 * time.Hour))
require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, vestedCoins)
require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 625), sdk.NewInt64Coin(stakeDenom, 62)}, vestedCoins)

// require 75% of coins vested after period 2
vestedCoins = pva.GetVestedCoins(now.Add(18 * time.Hour))
Expand Down Expand Up @@ -376,9 +376,9 @@ func TestGetVestingCoinsPeriodicVestingAcc(t *testing.T) {
vestingCoins = pva.GetVestingCoins(now.Add(12 * time.Hour))
require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, vestingCoins)

// require 50% of coins vesting after period 1, but before period 2 completes.
// require 37.5% of coins vesting after period 1, but before period 2 completes.
vestingCoins = pva.GetVestingCoins(now.Add(15 * time.Hour))
require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 500), sdk.NewInt64Coin(stakeDenom, 50)}, vestingCoins)
require.Equal(t, sdk.Coins{sdk.NewInt64Coin(feeDenom, 375), sdk.NewInt64Coin(stakeDenom, 38)}, vestingCoins)

// require 25% of coins vesting after period 2
vestingCoins = pva.GetVestingCoins(now.Add(18 * time.Hour))
Expand Down

0 comments on commit eca20f0

Please sign in to comment.