Skip to content

Commit 9ec886e

Browse files
committed
Update reawrds fetching and withdrawal
Adjust the dapp to changes from #1567.
1 parent a1d3409 commit 9ec886e

3 files changed

Lines changed: 36 additions & 23 deletions

File tree

solidity/dashboard/src/constants/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const TOKEN_STAKING_CONTRACT_NAME = 'stakingContract'
33
export const TOKEN_GRANT_CONTRACT_NAME = 'grantContract'
44
export const OPERATOR_CONTRACT_NAME = 'keepRandomBeaconOperatorContract'
55
export const REGISTRY_CONTRACT_NAME = 'registryContract'
6+
export const KEEP_OPERATOR_STATISTICS_CONTRACT_NAME = 'keepRandomBeaconOperatorStatistics'
67

78
export const ETHERSCAN_DEFAULT_URL = 'https://ropsten.etherscan.io/address/'
89

solidity/dashboard/src/contracts.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import KeepRandomBeaconOperator from '@keep-network/keep-core/artifacts/KeepRand
55
import Registry from '@keep-network/keep-core/artifacts/Registry.json'
66
import GuaranteedMinimumStakingPolicy from '@keep-network/keep-core/artifacts/GuaranteedMinimumStakingPolicy.json'
77
import PermissiveStakingPolicy from '@keep-network/keep-core/artifacts/PermissiveStakingPolicy.json'
8+
import KeepRandomBeaconOperatorStatistics from '@keep-network/keep-core/artifacts/KeepRandomBeaconOperatorStatistics.json'
89
import {
910
KEEP_TOKEN_CONTRACT_NAME,
1011
TOKEN_STAKING_CONTRACT_NAME,
1112
TOKEN_GRANT_CONTRACT_NAME,
1213
OPERATOR_CONTRACT_NAME,
1314
REGISTRY_CONTRACT_NAME,
15+
KEEP_OPERATOR_STATISTICS_CONTRACT_NAME,
1416
} from './constants/constants'
1517

1618
export const CONTRACT_DEPLOY_BLOCK_NUMBER = {
@@ -19,6 +21,7 @@ export const CONTRACT_DEPLOY_BLOCK_NUMBER = {
1921
[OPERATOR_CONTRACT_NAME]: 0,
2022
[TOKEN_STAKING_CONTRACT_NAME]: 0,
2123
[REGISTRY_CONTRACT_NAME]: 0,
24+
[KEEP_OPERATOR_STATISTICS_CONTRACT_NAME]: 0,
2225
}
2326

2427
export async function getKeepToken(web3) {
@@ -41,6 +44,10 @@ export async function getRegistry(web3) {
4144
return getContract(web3, Registry, REGISTRY_CONTRACT_NAME)
4245
}
4346

47+
export async function getKeepRandomBeaconOperatorStatistics(web3) {
48+
return getContract(web3, KeepRandomBeaconOperatorStatistics, KEEP_OPERATOR_STATISTICS_CONTRACT_NAME)
49+
}
50+
4451
export async function getKeepTokenContractDeployerAddress(web3) {
4552
const deployTransactionHash = getTransactionHashOfContractDeploy(KeepToken)
4653
const transaction = await web3.eth.getTransaction(deployTransactionHash)
@@ -62,6 +69,7 @@ export async function getContracts(web3) {
6269
getTokenStaking(web3),
6370
getKeepRandomBeaconOperator(web3),
6471
getRegistry(web3),
72+
getKeepRandomBeaconOperatorStatistics(web3),
6573
])
6674

6775
return {
@@ -70,6 +78,7 @@ export async function getContracts(web3) {
7078
stakingContract: contracts[2],
7179
keepRandomBeaconOperatorContract: contracts[3],
7280
registryContract: contracts[4],
81+
keepRandomBeaconOperatorStatistics: contracts[5]
7382
}
7483
}
7584

solidity/dashboard/src/services/rewards.service.js

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import web3Utils from 'web3-utils'
22
import { formatDate, wait, isSameEthAddress } from '../utils/general.utils'
3-
import { add, mul } from '../utils/arithmetics.utils'
3+
import { add, gt } from '../utils/arithmetics.utils'
44
import { CONTRACT_DEPLOY_BLOCK_NUMBER } from '../contracts'
55
import {
66
OPERATOR_CONTRACT_NAME,
77
} from '../constants/constants'
88

99
const 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

7073
const 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

Comments
 (0)