Skip to content

Commit

Permalink
chore: simplify account properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuditi committed Mar 2, 2023
1 parent 0aac3d6 commit 5528a2b
Show file tree
Hide file tree
Showing 12 changed files with 28 additions and 23 deletions.
Expand Up @@ -24,9 +24,7 @@
$: asset = $visibleSelectedAccountAssets?.baseCoin
$: votingPower = parseInt($selectedAccount?.votingPower, 10)
$: hasTransactionInProgress =
$selectedAccount?.hasVotingPowerTransactionInProgress ||
$selectedAccount?.hasVotingTransactionInProgress ||
$selectedAccount?.isTransferring
$selectedAccount?.processingGovernanceTransactionType !== null || $selectedAccount?.isTransferring
$: amount, hasTransactionInProgress, setConfirmDisabled()
function setConfirmDisabled(): void {
Expand Down Expand Up @@ -57,7 +55,7 @@
await checkActiveProfileAuth(
async () => {
await setVotingPower(rawAmount, isVoting)
await setVotingPower(rawAmount)
},
{ stronghold: true, ledger: false }
)
Expand Down
3 changes: 1 addition & 2 deletions packages/desktop/components/popups/RevotePopup.svelte
Expand Up @@ -6,8 +6,7 @@
import { checkActiveProfileAuth } from '@core/profile/actions'
import { vote } from '@contexts/governance/actions'
$: hasGovernanceTransactionInProgress =
$selectedAccount?.hasVotingPowerTransactionInProgress || $selectedAccount?.hasVotingTransactionInProgress
$: hasGovernanceTransactionInProgress = $selectedAccount?.processingGovernanceTransactionType !== null
async function onSubmit(): Promise<void> {
await checkActiveProfileAuth(async () => {
Expand Down
3 changes: 1 addition & 2 deletions packages/desktop/components/popups/StopVotingPopup.svelte
Expand Up @@ -8,8 +8,7 @@
import { selectedAccount } from '@core/account/stores'
import { checkActiveProfileAuth } from '@core/profile/actions'
$: hasGovernanceTransactionInProgress =
$selectedAccount?.hasVotingPowerTransactionInProgress || $selectedAccount?.hasVotingTransactionInProgress
$: hasGovernanceTransactionInProgress = $selectedAccount?.processingGovernanceTransactionType !== null
function onCancelClick(): void {
closePopup()
Expand Down
Expand Up @@ -21,8 +21,7 @@
)
$: hasVotingPower = Number($selectedAccount?.votingPower) > 0
$: hasGovernanceTransactionInProgress =
$selectedAccount?.hasVotingPowerTransactionInProgress || $selectedAccount?.hasVotingTransactionInProgress
$: hasGovernanceTransactionInProgress = $selectedAccount?.processingGovernanceTransactionType !== null
$: numberOfAbstainedQuestions =
selectedAnswerValues?.filter((answerValue) => answerValue === ABSTAIN_VOTE_VALUE).length ?? 0
Expand Down
Expand Up @@ -12,8 +12,7 @@
const ZERO_VOTING_POWER = '0'
$: isTransferring =
$selectedAccount?.hasVotingPowerTransactionInProgress || $selectedAccount?.hasVotingTransactionInProgress
$: isTransferring = $selectedAccount?.processingGovernanceTransactionType !== null
function onCancelClick(): void {
closePopup()
Expand All @@ -29,7 +28,7 @@
id: PopupId.ManageVotingPower,
props: { newVotingPower: ZERO_VOTING_POWER },
})
await setVotingPower(ZERO_VOTING_POWER, true)
await setVotingPower(ZERO_VOTING_POWER)
})
} catch (err) {
handleError(err)
Expand Down
Expand Up @@ -81,8 +81,7 @@
!isProposalVotable($selectedProposal?.status) ||
!hasChangedAnswers(selectedAnswerValues) ||
hasSelectedNoAnswers(selectedAnswerValues)
$: hasGovernanceTransactionInProgress =
$selectedAccount?.hasVotingPowerTransactionInProgress || $selectedAccount?.hasVotingTransactionInProgress
$: hasGovernanceTransactionInProgress = $selectedAccount.processingGovernanceTransactionType !== null
$: $selectedParticipationEventStatus, (textHintString = getTextHintString())
function hasSelectedNoAnswers(_selectedAnswerValues: number[]): boolean {
Expand Down
4 changes: 1 addition & 3 deletions packages/shared/components/VotingPower.svelte
Expand Up @@ -13,9 +13,7 @@
$: formattedVotingPower = formatTokenAmountBestMatch(votingPower, asset?.metadata)
$: formattedMaxVotingPower = formatTokenAmountBestMatch(maxVotingPower, asset?.metadata)
$: hasTransactionInProgress =
$selectedAccount?.hasVotingPowerTransactionInProgress ||
$selectedAccount?.hasVotingTransactionInProgress ||
$selectedAccount?.isTransferring
$selectedAccount?.processingGovernanceTransactionType !== null || $selectedAccount?.isTransferring
function handleManageVotingPower(): void {
openPopup({
Expand Down
Expand Up @@ -3,14 +3,18 @@ import { Transaction } from '@iota/wallet/out/types'
import { selectedAccount, updateSelectedAccount } from '@core/account/stores'
import { processAndAddToActivities } from '@core/wallet/utils'
import { handleError } from '@core/error/handlers'
import { GovernanceTransactionType } from '@contexts/governance/enums'

export async function setVotingPower(rawAmount: string): Promise<void> {
const account = get(selectedAccount)
try {
const votingPower = parseInt(account.votingPower, 10)
const amount = parseInt(rawAmount, 10)

updateSelectedAccount({ hasVotingPowerTransactionInProgress: true, isTransferring: true })
updateSelectedAccount({
processingGovernanceTransactionType: GovernanceTransactionType.VotingPower,
isTransferring: true,
})

let transaction: Transaction
if (amount > votingPower) {
Expand All @@ -23,6 +27,9 @@ export async function setVotingPower(rawAmount: string): Promise<void> {
await processAndAddToActivities(transaction, account)
} catch (err) {
handleError(err)
updateSelectedAccount({ hasVotingPowerTransactionInProgress: false, isTransferring: false })
updateSelectedAccount({
processingGovernanceTransactionType: GovernanceTransactionType.VotingPower,
isTransferring: false,
})
}
}
@@ -0,0 +1,4 @@
export enum GovernanceTransactionType {
VotingPower,
Vote,
}
1 change: 1 addition & 0 deletions packages/shared/lib/contexts/governance/enums/index.ts
@@ -1,3 +1,4 @@
export * from './governance-transaction-type.enum'
export * from './proposal-order-option.enum'
export * from './proposal-status.enum'
export * from './proposal-type.enum'
Expand Up @@ -36,6 +36,7 @@ export async function buildAccountState(account: IAccount, metadata: IAccountMet
...account,
...metadata,
depositAddress,
processingGovernanceTransactionType: null,
balances,
isTransferring: false,
votingPower,
Expand Down
Expand Up @@ -16,6 +16,7 @@ import { PopupId } from '@auxiliary/popup'
import { activeAccounts, updateActiveAccount } from '@core/profile/stores'
import { updateActiveAccountMetadata } from '@core/profile/actions'
import { isAccountVoting } from '@contexts/governance/utils/isAccountVoting'
import { GovernanceTransactionType } from '@contexts/governance/enums'

export function handleTransactionInclusionEvent(error: Error, rawEvent: string): void {
const { accountIndex, payload } = validateWalletApiEvent(error, rawEvent, WalletApiEvent.TransactionInclusion)
Expand Down Expand Up @@ -61,16 +62,16 @@ function handleGovernanceTransactionInclusionEvent(
closePopup(true)

const account = get(activeAccounts)?.find((_account) => _account.index === accountIndex)
if (account.hasVotingPowerTransactionInProgress) {
updateActiveAccount(accountIndex, { hasVotingPowerTransactionInProgress: false })
if (account.processingGovernanceTransactionType === GovernanceTransactionType.VotingPower) {
if (isAccountVoting(accountIndex) && activity.votingPower !== 0) {
updateActiveAccountMetadata(accountIndex, { shouldRevote: true })
openPopup({ id: PopupId.Revote })
}
} else {
updateActiveAccount(accountIndex, { hasVotingTransactionInProgress: false })
updateActiveAccountMetadata(accountIndex, { shouldRevote: false })
}

updateActiveAccount(accountIndex, { processingGovernanceTransactionType: null })
void updateParticipationOverview(accountIndex)
}
syncVotingPower(accountIndex)
Expand Down

0 comments on commit 5528a2b

Please sign in to comment.