Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds voting power management (#5337)
* feat: draft manage voting power popup * feat: add slider input and text hint * feat: adds voting power management functionality * fixes from review and test * fix: adds new account methods to account mock Co-authored-by: Tuditi <daviddetroch@pm.me>
- Loading branch information
1 parent
83999bf
commit feb79a4
Showing
16 changed files
with
169 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,40 @@ | ||
<script lang="typescript"> | ||
import { Text, Button } from 'shared/components' | ||
import { localize } from '@core/i18n' | ||
import { ButtonSize, FontWeight, TextType } from './enums' | ||
import { selectedAccount } from '@core/account' | ||
import { localize } from '@core/i18n' | ||
import { formatTokenAmountBestMatch, visibleSelectedAccountAssets } from '@core/wallet' | ||
import { openPopup } from '@auxiliary/popup' | ||
const asset = $visibleSelectedAccountAssets?.baseCoin | ||
const votingPowerBalance: string = '4821 SMR' | ||
const maximalVotingPower: string = '4821 SMR' | ||
$: votingPower = parseInt($selectedAccount?.votingPower, 10) | ||
$: maxVotingPower = parseInt($selectedAccount?.balances?.baseCoin?.available) | ||
$: formattedVotingPower = formatTokenAmountBestMatch(votingPower, asset?.metadata) | ||
$: formattedMaxVotingPower = formatTokenAmountBestMatch(maxVotingPower, asset?.metadata) | ||
function handleManageVotingPower(): void { | ||
return | ||
openPopup({ | ||
type: 'manageVotingPower', | ||
}) | ||
} | ||
</script> | ||
|
||
<voting-power> | ||
<Text fontSize="14" fontWeight={FontWeight.semibold} classes="mb-4"> | ||
{localize('views.governance.votingPower.title')} | ||
</Text> | ||
<Text type={TextType.h1}>{votingPowerBalance}</Text> | ||
<Text type={TextType.h1}>{formattedVotingPower}</Text> | ||
<Text fontWeight={FontWeight.medium} overrideColor classes="mb-4 text-gray-600"> | ||
{localize('views.governance.votingPower.maximal', { values: { value: maximalVotingPower } })} | ||
{localize('views.governance.votingPower.maximal', { values: { value: formattedMaxVotingPower } })} | ||
</Text> | ||
<Button size={ButtonSize.Medium} onClick={handleManageVotingPower} classes="w-full"> | ||
<Button | ||
size={ButtonSize.Medium} | ||
onClick={handleManageVotingPower} | ||
classes="w-full" | ||
disabled={$selectedAccount.isTransferring} | ||
isBusy={$selectedAccount.isTransferring} | ||
> | ||
{localize('views.governance.votingPower.manage')} | ||
</Button> | ||
</voting-power> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/shared/components/popups/ManageVotingPowerPopup.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<script lang="typescript"> | ||
import { TextType } from 'shared/components/enums' | ||
import { Button, Text, TextHint, AssetAmountInput } from 'shared/components' | ||
import { selectedAccount } from '@core/account' | ||
import { handleError } from '@core/error/handlers/handleError' | ||
import { setVotingPower } from '@core/governance/actions' | ||
import { localize } from '@core/i18n' | ||
import { checkActiveProfileAuth } from '@core/profile' | ||
import { visibleSelectedAccountAssets } from '@core/wallet' | ||
import { closePopup } from '@auxiliary/popup' | ||
const asset = $visibleSelectedAccountAssets?.baseCoin | ||
let assetAmountInput: AssetAmountInput | ||
let rawAmount = $selectedAccount?.votingPower | ||
$: isTransferring = $selectedAccount?.isTransferring | ||
function handleBack(): void { | ||
closePopup() | ||
} | ||
async function handleConfirm(): Promise<void> { | ||
try { | ||
await assetAmountInput?.validate() | ||
await checkActiveProfileAuth(async () => { | ||
await setVotingPower(rawAmount) | ||
closePopup() | ||
}) | ||
} catch (err) { | ||
handleError(err) | ||
closePopup() | ||
} | ||
} | ||
</script> | ||
|
||
<Text type={TextType.h4} classes="mb-3">{localize('popups.manageVotingPower.title')}</Text> | ||
<Text type={TextType.p} secondary classes="mb-5">{localize('popups.manageVotingPower.body')}</Text> | ||
<div class="space-y-4 mb-6"> | ||
<AssetAmountInput bind:this={assetAmountInput} bind:rawAmount {asset} containsSlider disableAssetSelection /> | ||
<TextHint info text={localize('popups.manageVotingPower.hint')} /> | ||
</div> | ||
<div class="flex flex-row flex-nowrap w-full space-x-4"> | ||
<Button outline classes="w-full" disabled={isTransferring} onClick={handleBack}> | ||
{localize('actions.back')} | ||
</Button> | ||
<Button | ||
classes="w-full" | ||
disabled={$selectedAccount.isTransferring} | ||
onClick={handleConfirm} | ||
isBusy={$selectedAccount.isTransferring} | ||
> | ||
{localize('actions.confirm')} | ||
</Button> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
packages/shared/lib/core/account/actions/syncVotingPower.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { get } from 'svelte/store' | ||
import { selectedAccount, updateSelectedAccount } from '../stores' | ||
import { getVotingPower } from '../api/getVotingPower' | ||
import { updateActiveAccount } from '@core/profile' | ||
|
||
export async function syncVotingPower(accountIndex: number): Promise<void> { | ||
const votingPower = await getVotingPower(accountIndex) | ||
if (get(selectedAccount)?.index === accountIndex) { | ||
updateSelectedAccount({ votingPower }) | ||
} else { | ||
updateActiveAccount(accountIndex, { votingPower }) | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { getAccount } from '@core/profile-manager' | ||
|
||
export async function getVotingPower(index?: number): Promise<string> { | ||
return (await getAccount(index))?.getVotingPower() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from './createAliasIfNecessary' | ||
export * from './getBalance' | ||
export * from './getVotingPower' | ||
export * from './prepareOutput' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './setVotingPower' |
36 changes: 36 additions & 0 deletions
36
packages/shared/lib/core/governance/actions/setVotingPower.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { get } from 'svelte/store' | ||
import { selectedAccount, updateSelectedAccount } from '@core/account' | ||
import { | ||
addActivityToAccountActivitiesInAllAccountActivities, | ||
generateActivity, | ||
preprocessTransaction, | ||
} from '@core/wallet' | ||
import { Transaction } from '@iota/wallet' | ||
|
||
export async function setVotingPower(rawAmount: string): Promise<void> { | ||
try { | ||
const account = get(selectedAccount) | ||
updateSelectedAccount({ isTransferring: true }) | ||
const votingPower = parseInt(account.votingPower, 10) | ||
const amount = parseInt(rawAmount, 10) | ||
if (amount > votingPower) { | ||
const amountToIncrease = amount - votingPower | ||
const transaction = await account.increaseVotingPower(amountToIncrease.toString()) | ||
await processAndAddToActivities(transaction) | ||
} else if (amount < votingPower) { | ||
const amountToDecrease = votingPower - amount | ||
const transaction = await account.decreaseVotingPower(amountToDecrease.toString()) | ||
await processAndAddToActivities(transaction) | ||
} | ||
updateSelectedAccount({ isTransferring: false }) | ||
} catch (err) { | ||
updateSelectedAccount({ isTransferring: false }) | ||
} | ||
} | ||
|
||
async function processAndAddToActivities(transaction: Transaction): Promise<void> { | ||
const account = get(selectedAccount) | ||
const preprocessedTransaction = await preprocessTransaction(transaction, account) | ||
const activity = generateActivity(preprocessedTransaction, account) | ||
addActivityToAccountActivitiesInAllAccountActivities(account.index, activity) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './actions' | ||
export * from './stores' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters