-
Notifications
You must be signed in to change notification settings - Fork 6
/
account_vesting_pools_utils.go
186 lines (157 loc) · 6.87 KB
/
account_vesting_pools_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package cfevesting
import (
// "math"
"math/rand"
"reflect"
"strconv"
"time"
"cosmossdk.io/math"
"github.com/chain4energy/c4e-chain/x/cfevesting/types"
"github.com/stretchr/testify/require"
testcosmos "github.com/chain4energy/c4e-chain/testutil/cosmossdk"
testenv "github.com/chain4energy/c4e-chain/testutil/env"
)
func AssertAccountVestingPools(t require.TestingT, expected types.AccountVestingPools, actual types.AccountVestingPools) {
numOfFields := reflect.TypeOf(types.AccountVestingPools{}).NumField()
j := 0
require.EqualValues(t, len(expected.VestingPools), len(actual.VestingPools))
j++
require.EqualValues(t, expected.Owner, actual.Owner)
j++
require.EqualValues(t, numOfFields, j)
numOfFields = reflect.TypeOf(types.VestingPool{}).NumField()
for i, expectedVesting := range expected.VestingPools {
actualVesting := actual.VestingPools[i]
j := 0
require.EqualValues(t, expectedVesting.Name, actualVesting.Name)
j++
require.EqualValues(t, expectedVesting.VestingType, actualVesting.VestingType)
j++
require.EqualValues(t, true, expectedVesting.LockStart.Equal(actualVesting.LockStart))
j++
require.EqualValues(t, true, expectedVesting.LockEnd.Equal(actualVesting.LockEnd))
j++
require.EqualValues(t, expectedVesting.InitiallyLocked, actualVesting.InitiallyLocked)
j++
require.EqualValues(t, expectedVesting.Withdrawn, actualVesting.Withdrawn)
j++
require.EqualValues(t, expectedVesting.Sent, actualVesting.Sent)
j++
require.EqualValues(t, expectedVesting.GenesisPool, actualVesting.GenesisPool)
j++
require.EqualValues(t, expectedVesting.Reservations, actualVesting.Reservations)
j++
require.EqualValues(t, numOfFields, j)
}
}
func AssertAccountVestingPoolsArrays(t require.TestingT, expected []*types.AccountVestingPools, actual []*types.AccountVestingPools) {
require.EqualValues(t, len(expected), len(actual))
for _, accVest := range expected {
found := false
for _, accVestExp := range actual {
if accVest.Owner == accVestExp.Owner {
AssertAccountVestingPools(t, *accVest, *accVestExp)
found = true
}
}
require.True(t, found, "not found: "+accVest.Owner)
}
}
func GenerateOneAccountVestingPoolsWithAddressWithRandomVestingPools(numberOfVestingPoolsPerAccount int,
accountId int, vestingStartId int) types.AccountVestingPools {
return *GenerateAccountVestingPoolsWithRandomVestingPools(1, numberOfVestingPoolsPerAccount, accountId, vestingStartId)[0]
}
func GenerateAccountVestingPoolsWithRandomVestingPools(numberOfAccounts int, numberOfVestingPoolsPerAccount int,
accountStartId int, vestingStartId int) []*types.AccountVestingPools {
return generateAccountVestingPools(numberOfAccounts, numberOfVestingPoolsPerAccount,
accountStartId, vestingStartId, generateRandomVestingPool)
}
func GenerateOneAccountVestingPoolsWithAddressWith10BasedVestingPools(numberOfVestingPoolsPerAccount int,
accountId int, vestingStartId int) types.AccountVestingPools {
return *GenerateAccountVestingPoolsWith10BasedVestingPools(1, numberOfVestingPoolsPerAccount, accountId, vestingStartId)[0]
}
func GenerateAccountVestingPoolsWith10BasedVestingPools(numberOfAccounts int, numberOfVestingPoolsPerAccount int,
accountStartId int, vestingStartId int) []*types.AccountVestingPools {
return generateAccountVestingPools(numberOfAccounts, numberOfVestingPoolsPerAccount,
accountStartId, vestingStartId, generate10BasedVestingPool)
}
func generateAccountVestingPools(numberOfAccounts int, numberOfVestingPoolsPerAccount int,
accountStartId int, vestingStartId int, generateVesting func(accuntId int, vestingId int) types.VestingPool) []*types.AccountVestingPools {
accountVestingPoolsArr := []*types.AccountVestingPools{}
accountsAddresses, _ := testcosmos.CreateAccounts(2*numberOfAccounts, 0)
for i := 0; i < numberOfAccounts; i++ {
accountVestingPools := types.AccountVestingPools{}
accountVestingPools.Owner = "test-vesting-account-addr-" + strconv.Itoa(i+accountStartId)
accountVestingPools.Owner = accountsAddresses[i].String()
var vestingPools []*types.VestingPool
for j := 0; j < numberOfVestingPoolsPerAccount; j++ {
vesting := generateVesting(i+accountStartId, j+vestingStartId)
vestingPools = append(vestingPools, &vesting)
}
accountVestingPools.VestingPools = vestingPools
accountVestingPoolsArr = append(accountVestingPoolsArr, &accountVestingPools)
}
return accountVestingPoolsArr
}
func generateRandomVestingPool(accuntId int, vestingId int) types.VestingPool {
rgen := rand.New(rand.NewSource(time.Now().UnixNano()))
initiallyLocked := rgen.Intn(10000000)
withdrawn := rgen.Intn(initiallyLocked)
sent := rgen.Intn(initiallyLocked - withdrawn)
return types.VestingPool{
Name: "test-vesting-account-name" + strconv.Itoa(accuntId) + "-" + strconv.Itoa(vestingId),
VestingType: "test-vesting-account-" + strconv.Itoa(accuntId) + "-" + strconv.Itoa(vestingId),
LockStart: CreateTimeFromNumOfHours(int64(rgen.Intn(100000))),
LockEnd: CreateTimeFromNumOfHours(int64(rgen.Intn(100000))),
InitiallyLocked: math.NewInt(int64(initiallyLocked)),
Withdrawn: math.NewInt(int64(withdrawn)),
Sent: math.NewInt(int64(sent)),
GenesisPool: rgen.Int()%2 == 0,
}
}
func generate10BasedVestingPool(accuntId int, vestingId int) types.VestingPool {
return types.VestingPool{
Name: "test-vesting-account-name" + strconv.Itoa(accuntId) + "-" + strconv.Itoa(vestingId),
VestingType: "test-vesting-account-" + strconv.Itoa(accuntId) + "-" + strconv.Itoa(vestingId),
LockStart: CreateTimeFromNumOfHours(1000),
LockEnd: CreateTimeFromNumOfHours(110000),
InitiallyLocked: math.NewInt(1000000),
Withdrawn: math.ZeroInt(),
Sent: math.ZeroInt(),
GenesisPool: true,
}
}
func ToAccountVestingPoolsPointersArray(src []types.AccountVestingPools) []*types.AccountVestingPools {
result := []*types.AccountVestingPools{}
for i := 0; i < len(src); i++ {
result = append(result, &src[i])
}
return result
}
func GetExpectedWithdrawableForVesting(vestingPool types.VestingPool, current time.Time) math.Int {
result := GetExpectedWithdrawable(vestingPool.LockEnd, current, vestingPool.InitiallyLocked.Sub(vestingPool.Sent).Sub(vestingPool.Withdrawn))
if result.LT(math.ZeroInt()) {
return math.ZeroInt()
}
return result
}
func GetExpectedWithdrawable(lockEnd time.Time, current time.Time, amount math.Int) math.Int {
if current.Equal(lockEnd) || current.After(lockEnd) {
return amount
}
return math.ZeroInt()
}
func CreateTimeFromNumOfHours(numOfHours int64) time.Time {
return testenv.TestEnvTime.Add(time.Hour * time.Duration(numOfHours))
}
func CreateDurationFromNumOfHours(numOfHours int64) time.Duration {
return time.Hour * time.Duration(numOfHours)
}
func GetVestingPoolByName(vps []*types.VestingPool, name string) (vp *types.VestingPool, found bool) {
for _, vPool := range vps {
if vPool.Name == name {
return vPool, true
}
}
return nil, false
}