@@ -5,8 +5,18 @@ import { useAddressStore } from './Address';
5
5
export type StakingState = {
6
6
validators : Record < string , Validator > ,
7
7
stakeByAddress : Record < string , Stake > ,
8
+ stakingEventsByAddress : Record < string , StakingEvent [ ] > ,
8
9
}
9
10
11
+ export type AddStakeEvent = {
12
+ sender_address :string , // eslint-disable-line camelcase
13
+ date : string ,
14
+ value : number ,
15
+ type : number ,
16
+ }
17
+
18
+ export type StakingEvent = AddStakeEvent ;
19
+
10
20
export type Stake = {
11
21
address : string ,
12
22
activeBalance : number , // activeBalance (does not include inactiveBalance)
@@ -58,6 +68,7 @@ export const useStakingStore = createStore({
58
68
state : ( ) => ( {
59
69
validators : { } ,
60
70
stakeByAddress : { } ,
71
+ stakingEventsByAddress : { } ,
61
72
} as StakingState ) ,
62
73
getters : {
63
74
validatorsList : ( state ) : Readonly < Validator [ ] > => Object . values ( state . validators ) ,
@@ -156,6 +167,21 @@ export const useStakingStore = createStore({
156
167
active : false ,
157
168
} ;
158
169
} ,
170
+
171
+ stakingEventsByAddress : ( state ) : Readonly < Record < string , StakingEvent [ ] > > => state . stakingEventsByAddress ,
172
+ stakingEvents : ( state ) : Readonly < StakingEvent [ ] | null > => {
173
+ const { activeAddress } = useAddressStore ( ) ;
174
+ if ( ! activeAddress . value ) return null ;
175
+
176
+ return state . stakingEventsByAddress [ activeAddress . value ] || null ;
177
+ } ,
178
+ restakingRewards : ( state , { stakingEvents } ) : Readonly < number | null > => {
179
+ const events = stakingEvents . value as StakingEvent [ ] | null ;
180
+ if ( ! events ) return null ;
181
+
182
+ const addStakeEvents : AddStakeEvent [ ] = events . filter ( ( event ) => event . type === 6 ) ;
183
+ return addStakeEvents . reduce ( ( sum , event ) => sum + event . value , 0 ) ;
184
+ } ,
159
185
} ,
160
186
actions : {
161
187
setStake ( stake : Stake ) {
@@ -205,5 +231,13 @@ export const useStakingStore = createStore({
205
231
206
232
this . state . validators = newValidators ;
207
233
} ,
234
+ setStakingEvents ( address : string , events : StakingEvent [ ] ) {
235
+ // Need to assign whole object for change detection of new addresses.
236
+ // TODO: Simply set new stake in Vue 3.
237
+ this . state . stakingEventsByAddress = {
238
+ ...this . state . stakingEventsByAddress ,
239
+ [ address ] : events ,
240
+ } ;
241
+ } ,
208
242
} ,
209
243
} ) ;
0 commit comments