11import web3Utils from 'web3-utils'
22import { formatDate , wait , isSameEthAddress } from '../utils/general.utils'
3- import { add , mul } from '../utils/arithmetics.utils'
3+ import { add , gt } from '../utils/arithmetics.utils'
44import { CONTRACT_DEPLOY_BLOCK_NUMBER } from '../contracts'
55import {
66 OPERATOR_CONTRACT_NAME ,
77} from '../constants/constants'
88
99const fetchAvailableRewards = async ( web3Context ) => {
10- const { keepRandomBeaconOperatorContract, stakingContract, yourAddress } = web3Context
10+ const {
11+ keepRandomBeaconOperatorContract,
12+ keepRandomBeaconOperatorStatistics,
13+ stakingContract,
14+ yourAddress
15+ } = web3Context
1116 try {
1217 let totalRewardsBalance = web3Utils . toBN ( 0 )
1318 const expiredGroupsCount = await keepRandomBeaconOperatorContract . methods . getFirstActiveGroupIndex ( ) . call ( )
@@ -25,46 +30,44 @@ const fetchAvailableRewards = async (web3Context) => {
2530 if ( ! isSameEthAddress ( yourAddress , beneficiaryAddressForMember ) ) {
2631 continue
2732 }
28- groupMemberIndices [ groupPublicKey ] [ memberAddress ] = await keepRandomBeaconOperatorContract
33+ const awaitingRewards = await keepRandomBeaconOperatorStatistics
2934 . methods
30- . getGroupMemberIndices ( groupPublicKey , memberAddress )
35+ . awaitingRewards ( memberAddress , groupIndex )
3136 . call ( )
37+
38+ if ( gt ( awaitingRewards , 0 ) ) {
39+ groupMemberIndices [ groupPublicKey ] [ memberAddress ] = awaitingRewards
40+ }
3241 }
3342 if ( Object . keys ( groupMemberIndices [ groupPublicKey ] ) . length === 0 ) {
3443 continue
3544 }
36- const { reward, rewardPerMemberInWei } = await getAvailableRewardFromGroupInEther ( groupPublicKey , groupMemberIndices , web3Context )
45+ const reward = getAvailableRewardForGroup ( groupMemberIndices [ groupPublicKey ] )
3746 const isStale = await keepRandomBeaconOperatorContract . methods . isStaleGroup ( groupPublicKey ) . call ( )
3847
39- totalRewardsBalance = add ( totalRewardsBalance , web3Utils . toWei ( reward , 'ether' ) )
48+ totalRewardsBalance = add ( totalRewardsBalance , reward )
4049 groups . push ( {
4150 groupIndex : groupIndex . toString ( ) ,
4251 groupPublicKey,
4352 membersIndeces : groupMemberIndices [ groupPublicKey ] ,
44- reward,
45- rewardPerMemberInWei,
53+ reward : web3Utils . fromWei ( reward , 'ether' ) ,
4654 isStale,
4755 } )
4856 }
49- return [ groups , web3Utils . fromWei ( totalRewardsBalance . toString ( ) , 'ether' ) ]
57+ return [ groups , web3Utils . fromWei ( totalRewardsBalance , 'ether' ) ]
5058 } catch ( error ) {
5159 throw error
5260 }
5361}
5462
55- const getAvailableRewardFromGroupInEther = async ( groupPublicKey , groupMemberIndices , web3Context ) => {
56- const { keepRandomBeaconOperatorContract } = web3Context
57- const membersInGroup = Object . keys ( groupMemberIndices [ groupPublicKey ] )
58- const rewardsMultiplier = membersInGroup . length === 1 ?
59- groupMemberIndices [ groupPublicKey ] [ membersInGroup [ 0 ] ] . length :
60- membersInGroup . reduce ( ( prev , current , index ) => {
61- const prevValue = index === 1 ? groupMemberIndices [ groupPublicKey ] [ prev ] . length : prev
62- return prevValue + groupMemberIndices [ groupPublicKey ] [ current ] . length
63- } )
64- const groupMemberReward = await keepRandomBeaconOperatorContract . methods . getGroupMemberRewards ( groupPublicKey ) . call ( )
65- const wholeReward = mul ( groupMemberReward , rewardsMultiplier )
66-
67- return { reward : web3Utils . fromWei ( wholeReward , 'ether' ) , rewardPerMemberInWei : groupMemberReward }
63+ const getAvailableRewardForGroup = ( oeratorsAmount ) => {
64+ let wholeReward = 0
65+ for ( const operator in oeratorsAmount ) {
66+ if ( oeratorsAmount . hasOwnProperty ( operator ) ) {
67+ wholeReward = add ( wholeReward , oeratorsAmount [ operator ] )
68+ }
69+ }
70+ return wholeReward
6871}
6972
7073const withdrawRewardFromGroup = async ( groupIndex , groupMembersIndices , web3Context ) => {
@@ -78,7 +81,7 @@ const withdrawRewardFromGroup = async (groupIndex, groupMembersIndices, web3Cont
7881 return new Promise ( ( resolve , reject ) => {
7982 const request = keepRandomBeaconOperatorContract
8083 . methods
81- . withdrawGroupMemberRewards ( memberAddress , groupIndex , groupMembersIndices [ memberAddress ] )
84+ . withdrawGroupMemberRewards ( memberAddress , groupIndex )
8285 . send . request ( { from : yourAddress } , ( error , transactionHash ) => {
8386 if ( error ) {
8487 resolve ( { memberAddress, memberIndices : groupMembersIndices [ memberAddress ] , isError : true , error } )
0 commit comments