Skip to content

Commit

Permalink
feat: claim rewards from delegation outputs (#8136)
Browse files Browse the repository at this point in the history
* feat: claim rewards from delegation outputs

* feat: update logic to claim delegation rewards

* fix: improve error to handle onClick button action

* feat: add a TODO comment with new task to fix the claim rewards

* fix: use computeAccountId to generate delegationId

* feat: add computeDelegationId

* Update packages/shared/lib/core/api/interfaces/api.interface.ts

Co-authored-by: Marc Espin <[email protected]>

* feat: improve delegationId and add the rewards con confirmation popup

* feat: add Mana to description of claimDelegationRewards

---------

Co-authored-by: Marc Espin <[email protected]>
  • Loading branch information
cpl121 and marc2332 authored Mar 12, 2024
1 parent 6887d69 commit 8abbc92
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
46 changes: 37 additions & 9 deletions packages/desktop/views/dashboard/delegation/Delegation.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,24 @@
ButtonSize,
CopyableBox,
BoxedIconWithText,
TextHintVariant,
} from '@ui'
import { activeProfile } from '@core/profile'
import { activeProfile, checkActiveProfileAuth } from '@core/profile'
import {
formatTokenAmountBestMatch,
AddressConverter,
getDefaultTransactionOptions,
selectedWalletAssets,
EMPTY_HEX_ID,
getOutputRewards,
getCommitteeInfo,
} from '@core/wallet'
import { truncateString } from '@core/utils'
import { Icon as IconEnum } from '@auxiliary/icon'
import { OutputType, DelegationOutput, OutputData } from '@iota/sdk/out/types'
import { PopupId, openPopup } from '@auxiliary/popup'
import { OutputType, DelegationOutput, OutputData, DelegationId } from '@iota/sdk/out/types'
import { PopupId, closePopup, openPopup } from '@auxiliary/popup'
import features from '@features/features'
import { api } from '@core/api'
import { DEFAULT_MANA } from '@core/network'
let delegationData: IDelegationTable[] = []
Expand Down Expand Up @@ -71,13 +75,18 @@
const delegationOutput = output.output as DelegationOutput
// Until the first epoch in which it was delegated ends, no rewards are obtained
const epochsDelegating = currentEpoch - delegationOutput.startEpoch
let delegationId: DelegationId = delegationOutput.delegationId
if (delegationId === EMPTY_HEX_ID) {
delegationId = api.computeDelegationId(output.outputId)
}
const rewards = await getOutputRewards(output.outputId)
return {
[Header.DelegationId]: delegationOutput.delegationId,
[Header.DelegationId]: delegationId,
[Header.DelegatedFunds]: Number(delegationOutput.delegatedAmount),
[Header.Rewards]: await getOutputRewards(output.outputId),
[Header.Rewards]: rewards,
[Header.Epochs]: epochsDelegating > 0 ? epochsDelegating : 0,
[Header.DelegatedAddress]: AddressConverter.addressToBech32(delegationOutput.validatorAddress),
[Header.Action]: handleClaimRewards,
[Header.Action]: () => handleClaimRewards(delegationId, rewards),
}
}) || []
delegationData = await Promise.all(result)
Expand All @@ -94,8 +103,27 @@
})
}
function handleClaimRewards(): void {
// TODO: add logic to claim reward
function handleClaimRewards(delegationId: string, rewards: number): void {
openPopup({
id: PopupId.Confirmation,
props: {
title: localize('popups.claimDelegationRewards.title'),
description: localize('popups.claimDelegationRewards.description', {
values: { rewards, delegationId },
}),
confirmText: localize('popups.claimDelegationRewards.confirmButton'),
variant: TextHintVariant.Success,
onConfirm: async () => {
await checkActiveProfileAuth(
async () => {
await $selectedWallet.burn({ delegations: [delegationId] }, getDefaultTransactionOptions())
closePopup()
},
{ stronghold: true }
)
},
},
})
}
function renderCellValue(value: any, header: string): { component: any; props: any; text?: string } {
Expand Down Expand Up @@ -160,7 +188,7 @@
return {
component: Button,
props: { size: ButtonSize.Small, onClick: value, outline: true },
text: 'Claim Rewards',
text: localize('popups.claimDelegationRewards.title'),
}
default:
return {
Expand Down
2 changes: 2 additions & 0 deletions packages/shared/lib/core/api/interfaces/api.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
Bech32Address,
ProtocolParameters,
DecayedMana,
DelegationId,
} from '@iota/sdk/out/types'
import { IWallet } from '@core/profile/interfaces'

Expand Down Expand Up @@ -45,5 +46,6 @@ export interface IApi {
computeFoundryId(accountId: AccountId, serialNumber: number, tokenSchemeType: number): FoundryId
computeNftId(outputId: string): NftId
computeOutputId(id: TransactionId, index: number): OutputId
computeDelegationId(outputId: OutputId): DelegationId
outputHexBytes(output: Output): HexEncodedString
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ export function getDefaultTransactionOptions(
value: new AccountAddress(accountId),
},
allowMicroAmount: true,
allowAllottingFromAccountMana: true,
}
}
5 changes: 5 additions & 0 deletions packages/shared/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,11 @@
"title": "Account Address",
"description": "Account Address for the validator"
}
},
"claimDelegationRewards": {
"title": "Claim Rewards",
"description": "Claim all the rewards, {rewards} Mana, of this delegation output {delegationId}",
"confirmButton": "Claim"
}
},
"actions": {
Expand Down

0 comments on commit 8abbc92

Please sign in to comment.