Skip to content

Commit

Permalink
Merge withdraw-commission into release-v0.5.18
Browse files Browse the repository at this point in the history
  • Loading branch information
tombeynon committed Mar 28, 2022
2 parents 1cb72ad + 71d5341 commit 9930d5d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
31 changes: 24 additions & 7 deletions src/components/ClaimRewards.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Coins from './Coins'
import {
coin
} from '@cosmjs/stargate'
import { MsgWithdrawDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { MsgWithdrawDelegatorReward, MsgWithdrawValidatorCommission } from "cosmjs-types/cosmos/distribution/v1beta1/tx";
import { MsgDelegate } from "cosmjs-types/cosmos/staking/v1beta1/tx";

import {
Expand Down Expand Up @@ -41,7 +41,7 @@ function ClaimRewards(props) {

signAndBroadcast = props.stargateClient.signAndBroadcastWithoutBalanceCheck

if(adjustedValidatorRewards.some(validatorReward => validatorReward.reward <= 0)) {
if(!props.commission && adjustedValidatorRewards.some(validatorReward => validatorReward.reward <= 0)) {
props.setLoading(false)
props.setError('Reward is too low')
return
Expand Down Expand Up @@ -80,6 +80,15 @@ function ClaimRewards(props) {
validatorAddress: validatorReward.validatorAddress
})
})

if (props.commission) {
valMessages.push({
typeUrl: "/cosmos.distribution.v1beta1.MsgWithdrawValidatorCommission",
value: MsgWithdrawValidatorCommission.fromPartial({
validatorAddress: validatorReward.validatorAddress
})
})
}

if(props.restake){
valMessages.push({
Expand All @@ -96,13 +105,21 @@ function ClaimRewards(props) {
}).flat()
}

function buttonText() {
if(props.restake){
return 'Manual Compound'
}else if(props.commission){
return 'Claim Commission'
}else{
return 'Claim Rewards'
}
}

return (
<>
{props.validatorRewards.length > 0 && (
<Dropdown.Item onClick={() => claim()}>
{props.restake ? 'Manual Compound' : 'Claim Rewards'}
</Dropdown.Item>
)}
<Dropdown.Item onClick={() => claim()}>
{buttonText()}
</Dropdown.Item>
</>
)
}
Expand Down
29 changes: 29 additions & 0 deletions src/components/Delegations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import _ from "lodash";
import { Bech32 } from '@cosmjs/encoding'
import AlertMessage from "./AlertMessage";
import Coins from "./Coins";
import ClaimRewards from "./ClaimRewards";
Expand Down Expand Up @@ -350,6 +351,14 @@ class Delegations extends React.Component {
);
}

isValidatorOperator(validator) {
if(!this.props.address || !validator || !window.atob) return false;

const prefix = this.props.network.prefix
const validatorOperator = Bech32.encode(prefix, Bech32.decode(validator.operator_address).data)
return validatorOperator === this.props.address
}

totalRewards(validators) {
if (!this.state.rewards) return;

Expand Down Expand Up @@ -399,6 +408,7 @@ class Delegations extends React.Component {

renderValidator(validatorAddress, delegation) {
const validator = this.props.validators[validatorAddress];
const isValidatorOperator = this.isValidatorOperator(validator)
if (validator) {
const rewards =
this.state.rewards && this.state.rewards[validatorAddress];
Expand All @@ -411,6 +421,8 @@ class Delegations extends React.Component {
: "table-warning"
: undefined;

if(isValidatorOperator) rowVariant = 'table-info'

const delegationBalance = (delegation && delegation.balance) || {
amount: 0,
denom: this.props.network.denom,
Expand Down Expand Up @@ -592,6 +604,23 @@ class Delegations extends React.Component {
}
setError={this.setError}
/>
{isValidatorOperator && (
<>
<hr />
<ClaimRewards
commission={true}
network={this.props.network}
address={this.props.address}
validatorRewards={this.validatorRewards([validatorAddress])}
stargateClient={this.props.stargateClient}
onClaimRewards={this.onClaimRewards}
setLoading={(loading) =>
this.setValidatorLoading(validatorAddress, loading)
}
setError={this.setError}
/>
</>
)}
<hr />
<Delegate
network={this.props.network}
Expand Down

0 comments on commit 9930d5d

Please sign in to comment.