-
Notifications
You must be signed in to change notification settings - Fork 6
/
user_entry.go
210 lines (184 loc) · 6.77 KB
/
user_entry.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package types
import (
"cosmossdk.io/errors"
"fmt"
c4eerrors "github.com/chain4energy/c4e-chain/v2/types/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// Validate checks the userEntry is valid
func (m *UserEntry) Validate() error {
if _, err := sdk.AccAddressFromBech32(m.Address); err != nil {
return err
}
if len(m.ClaimRecords) == 0 {
return fmt.Errorf("at least one campaign record is required")
}
campaignIDMap := make(map[uint64]struct{})
for _, elem := range m.ClaimRecords {
if _, ok := campaignIDMap[elem.CampaignId]; ok {
return fmt.Errorf("duplicated campaign id for completed mission")
}
campaignIDMap[elem.CampaignId] = struct{}{}
}
for i, claimRecord := range m.ClaimRecords {
if err := claimRecord.Validate(); err != nil {
return errors.Wrapf(err, "claim record index %d", i)
}
if !claimRecord.Amount.IsAllPositive() {
return fmt.Errorf("claimable amount must be positive")
}
missionIdMap := make(map[uint64]struct{})
for _, elem := range claimRecord.CompletedMissions {
if _, ok := missionIdMap[elem]; ok {
return fmt.Errorf("duplicated mission id for completed mission")
}
missionIdMap[elem] = struct{}{}
}
}
return nil
}
// IsMissionCompleted checks if the specified mission ID is completed for the claim record
func (m *ClaimRecord) IsMissionCompleted(missionId uint64) bool {
for _, completed := range m.CompletedMissions {
if completed == missionId {
return true
}
}
return false
}
func (m *ClaimRecord) IsMissionClaimed(missionId uint64) bool {
for _, claimed := range m.ClaimedMissions {
if claimed == missionId {
return true
}
}
return false
}
func (m *ClaimRecord) IsInitialMissionClaimed() bool {
for _, claimed := range m.ClaimedMissions {
if claimed == InitialMissionId {
return true
}
}
return false
}
// HasCampaign checks if the specified reccord for campignId ID exists
func (m *UserEntry) HasCampaign(campaignId uint64) bool {
for _, claimRecord := range m.ClaimRecords {
if claimRecord.CampaignId == campaignId {
return true
}
}
return false
}
// CompleteMission checks if the specified mission ID is completed for the claim record
func (m *ClaimRecord) CompleteMission(campaignId uint64, missionId uint64) error {
if m.IsMissionCompleted(missionId) {
return errors.Wrapf(ErrMissionCompleted, "campaignId: %d, missionId: %d", campaignId, missionId)
}
m.CompletedMissions = append(m.CompletedMissions, missionId)
return nil
}
// CalculateInitialClaimClaimableAmount calculates the initial claimable amount for the claim record
func (m *ClaimRecord) CalculateInitialClaimClaimableAmount(weightSum sdk.Dec) sdk.Coins {
allMissionsAmountSum := sdk.NewCoins()
for _, amount := range m.Amount {
allMissionsAmountSum = allMissionsAmountSum.Add(sdk.NewCoin(amount.Denom, weightSum.Mul(sdk.NewDecFromInt(amount.Amount)).TruncateInt()))
}
return m.Amount.Sub(allMissionsAmountSum...)
}
// ClaimMission claims the specified mission ID for the claim record
func (m *ClaimRecord) ClaimMission(campaignId uint64, missionId uint64) error {
if !m.IsMissionCompleted(missionId) {
return errors.Wrapf(ErrMissionNotCompleted, "campaignId: %d, missionId: %d", campaignId, missionId)
}
if m.IsMissionClaimed(missionId) {
return errors.Wrapf(ErrMissionClaimed, "campaignId: %d, missionId: %d", campaignId, missionId)
}
m.ClaimedMissions = append(m.ClaimedMissions, missionId)
return nil
}
// ClaimableFromMission returns the amount claimable for this claim record from the provided mission completion
func (m ClaimRecord) ClaimableFromMission(mission *Mission) (sdk.Coins, error) {
var coinSum sdk.Coins
for _, coin := range m.Amount {
coinSum = coinSum.Add(sdk.NewCoin(coin.Denom, mission.Weight.Mul(sdk.NewDecFromInt(coin.Amount)).TruncateInt()))
}
return coinSum, nil
}
func (cr *UserEntry) GetClaimRecord(campaignId uint64) *ClaimRecord {
for _, claimRecord := range cr.ClaimRecords {
if claimRecord.CampaignId == campaignId {
return claimRecord
}
}
return nil
}
// DeleteClaimRecord deletes the claim record for the specified campaign ID
func (cr *UserEntry) DeleteClaimRecord(campaignId uint64) {
for i, claimRecord := range cr.ClaimRecords {
if claimRecord.CampaignId == campaignId {
cr.ClaimRecords = append(cr.ClaimRecords[:i], cr.ClaimRecords[i+1:]...)
}
}
}
func (cr *UserEntry) MustGetClaimRecord(camapaignId uint64) (*ClaimRecord, error) {
for _, claimRecord := range cr.ClaimRecords {
if claimRecord.CampaignId == camapaignId {
return claimRecord, nil
}
}
return nil, errors.Wrapf(sdkerrors.ErrNotFound, "claim record with campaign id %d not found for address %s", camapaignId, cr.Address)
}
func (claimRecord *ClaimRecord) Validate() error {
if claimRecord.Amount == nil {
return errors.Wrapf(c4eerrors.ErrParam, "claim record amount cannot be nil")
}
if claimRecord.Amount.IsAnyNil() {
return errors.Wrapf(c4eerrors.ErrParam, "claim record amount cannot be nil")
}
if err := claimRecord.Amount.Validate(); err != nil {
return errors.Wrapf(c4eerrors.ErrParam, "wrong claim record amount (%s)", err)
}
return nil
}
func (claimRecordEntry *ClaimRecordEntry) Validate() error {
_, err := sdk.AccAddressFromBech32(claimRecordEntry.UserEntryAddress)
if err != nil {
return errors.Wrapf(c4eerrors.ErrParam, "claim record entry user entry address parsing error (%s)", err)
}
if claimRecordEntry.Amount == nil {
return errors.Wrapf(c4eerrors.ErrParam, "claim record entry amount cannot be nil")
}
if claimRecordEntry.Amount.IsAnyNil() {
return errors.Wrapf(c4eerrors.ErrParam, "claim record entry amount cannot be nil")
}
if err := claimRecordEntry.Amount.Validate(); err != nil {
return errors.Wrapf(c4eerrors.ErrParam, "wrong claim record entry amount (%s)", err)
}
if !claimRecordEntry.Amount.IsAllPositive() {
return errors.Wrapf(c4eerrors.ErrParam, "claim record amount must be positive")
}
return nil
}
func ValidateClaimRecordEntries(claimRecordEntries []*ClaimRecordEntry) error {
for i, claimRecordEntry := range claimRecordEntries {
if err := claimRecordEntry.Validate(); err != nil {
return errors.Wrapf(err, "claim record entry index %d", i)
}
}
return nil
}
func ValidateVestingPoolCampaignClaimRecordEntries(claimRecordEntries []*ClaimRecordEntry, vestingDenom string) error {
for i, claimRecordEntry := range claimRecordEntries {
if err := claimRecordEntry.Validate(); err != nil {
return errors.Wrapf(err, "claim record entry index %d", i)
}
if claimRecordEntry.Amount.Len() > 1 || claimRecordEntry.Amount.AmountOf(vestingDenom).IsZero() {
return errors.Wrapf(c4eerrors.ErrParam, "claim record entry index %d: for vesting pool campaigns,"+
" the claim record entry must have only one coin with the denomination currently used by the cfevesting module (%s)", i, vestingDenom)
}
}
return nil
}