From 9c5d75eb3293781530ad86362d5e81a26c3ac5aa Mon Sep 17 00:00:00 2001 From: Tuditi Date: Fri, 2 Dec 2022 14:43:22 +0100 Subject: [PATCH 1/5] feat: draft manage voting power popup --- packages/shared/components/VotingPower.svelte | 5 +++- .../shared/components/popups/Index.svelte | 2 ++ .../popups/ManageVotingPowerPopup.svelte | 29 +++++++++++++++++++ packages/shared/locales/en.json | 4 +++ 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 packages/shared/components/popups/ManageVotingPowerPopup.svelte diff --git a/packages/shared/components/VotingPower.svelte b/packages/shared/components/VotingPower.svelte index b79d744a3ee..ea00e1dce1a 100644 --- a/packages/shared/components/VotingPower.svelte +++ b/packages/shared/components/VotingPower.svelte @@ -2,12 +2,15 @@ import { Text, Button } from 'shared/components' import { localize } from '@core/i18n' import { ButtonSize, FontWeight, TextType } from './enums' + import { openPopup } from '@auxiliary/popup' const votingPowerBalance: string = '4821 SMR' const maximalVotingPower: string = '4821 SMR' function handleManageVotingPower(): void { - return + openPopup({ + type: 'manageVotingPower', + }) } diff --git a/packages/shared/components/popups/Index.svelte b/packages/shared/components/popups/Index.svelte index f9ca1612b9d..5c1a63d8f9e 100644 --- a/packages/shared/components/popups/Index.svelte +++ b/packages/shared/components/popups/Index.svelte @@ -26,6 +26,7 @@ import LedgerConnectionGuidePopup from './LedgerConnectionGuidePopup.svelte' import LegalUpdate from './LegalUpdate.svelte' import ManageAccountPopup from './ManageAccountPopup.svelte' + import ManageVotingPowerPopup from './ManageVotingPowerPopup.svelte' import MintNativeTokenFormPopup from './MintNativeTokenFormPopup.svelte' import MintNativeTokenConfirmationPopup from './MintNativeTokenConfirmationPopup.svelte' import MintNftFormPopup from './MintNftFormPopup.svelte' @@ -119,6 +120,7 @@ faucetRequest: FaucetRequestPopup, enableLedgerBlindSigning: EnableLedgerBlindSigningPopup, testDeepLinkForm: TestDeepLinkFormPopup, + manageVotingPower: ManageVotingPowerPopup, } function onKey(event: KeyboardEvent): void { diff --git a/packages/shared/components/popups/ManageVotingPowerPopup.svelte b/packages/shared/components/popups/ManageVotingPowerPopup.svelte new file mode 100644 index 00000000000..2dcff853ae8 --- /dev/null +++ b/packages/shared/components/popups/ManageVotingPowerPopup.svelte @@ -0,0 +1,29 @@ + + +{localize('popups.manageVotingPower.title')} +{localize('popups.manageVotingPower.body')} +
+ + +
diff --git a/packages/shared/locales/en.json b/packages/shared/locales/en.json index 1f783b769ca..1c3137b6591 100644 --- a/packages/shared/locales/en.json +++ b/packages/shared/locales/en.json @@ -1003,6 +1003,10 @@ "faucetRequest": { "title": "Faucet request", "body": "Are you sure you want to request {token} tokens from the {network} faucet?" + }, + "manageVotingPower": { + "title": "Manage voting power", + "body": "Define your voting power to vote on proposals." } }, "charts": { From 599652d4f241b7be618dec0fbc85fda135e88374 Mon Sep 17 00:00:00 2001 From: Tuditi Date: Fri, 2 Dec 2022 15:00:28 +0100 Subject: [PATCH 2/5] feat: add slider input and text hint --- .../components/popups/ManageVotingPowerPopup.svelte | 12 +++++++++++- packages/shared/locales/en.json | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/packages/shared/components/popups/ManageVotingPowerPopup.svelte b/packages/shared/components/popups/ManageVotingPowerPopup.svelte index 2dcff853ae8..8625665483b 100644 --- a/packages/shared/components/popups/ManageVotingPowerPopup.svelte +++ b/packages/shared/components/popups/ManageVotingPowerPopup.svelte @@ -1,10 +1,16 @@ {localize('popups.manageVotingPower.title')} diff --git a/packages/shared/lib/core/account/actions/buildAccountState.ts b/packages/shared/lib/core/account/actions/buildAccountState.ts index 738c2c343ef..d51c6995e76 100644 --- a/packages/shared/lib/core/account/actions/buildAccountState.ts +++ b/packages/shared/lib/core/account/actions/buildAccountState.ts @@ -18,9 +18,11 @@ export async function buildAccountState(account: IAccount, metadata: IAccountMet aliases: [], } let depositAddress: string + let votingPower: string try { balances = await account.getBalance() depositAddress = await getDepositAddress(account) + votingPower = await account.getVotingPower() } catch (err) { console.error(err) } @@ -31,5 +33,6 @@ export async function buildAccountState(account: IAccount, metadata: IAccountMet depositAddress, balances, isTransferring: false, + votingPower, } } diff --git a/packages/shared/lib/core/account/actions/index.ts b/packages/shared/lib/core/account/actions/index.ts index ee09c9b373a..37ca55e6834 100644 --- a/packages/shared/lib/core/account/actions/index.ts +++ b/packages/shared/lib/core/account/actions/index.ts @@ -6,5 +6,6 @@ export * from './loadAccount' export * from './resetSelectedAccount' export * from './setNextSelectedAccount' export * from './syncBalance' +export * from './syncVotingPower' export * from './tryCreateAdditionalAccount' export * from './tryEditSelectedAccountMetadata' diff --git a/packages/shared/lib/core/account/actions/syncVotingPower.ts b/packages/shared/lib/core/account/actions/syncVotingPower.ts new file mode 100644 index 00000000000..0d3eb715d43 --- /dev/null +++ b/packages/shared/lib/core/account/actions/syncVotingPower.ts @@ -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 { + const votingPower = await getVotingPower(accountIndex) + if (get(selectedAccount)?.index === accountIndex) { + updateSelectedAccount({ votingPower }) + } else { + updateActiveAccount(accountIndex, { votingPower }) + } + return +} diff --git a/packages/shared/lib/core/account/api/getVotingPower.ts b/packages/shared/lib/core/account/api/getVotingPower.ts new file mode 100644 index 00000000000..c6680806b74 --- /dev/null +++ b/packages/shared/lib/core/account/api/getVotingPower.ts @@ -0,0 +1,5 @@ +import { getAccount } from '@core/profile-manager' + +export async function getVotingPower(index?: number): Promise { + return (await getAccount(index))?.getVotingPower() +} diff --git a/packages/shared/lib/core/account/api/index.ts b/packages/shared/lib/core/account/api/index.ts index b7a989c6057..bbad23d5482 100644 --- a/packages/shared/lib/core/account/api/index.ts +++ b/packages/shared/lib/core/account/api/index.ts @@ -1,3 +1,4 @@ export * from './createAliasIfNecessary' export * from './getBalance' +export * from './getVotingPower' export * from './prepareOutput' diff --git a/packages/shared/lib/core/account/interfaces/account-state.interface.ts b/packages/shared/lib/core/account/interfaces/account-state.interface.ts index 7f8c1322f3a..f4398a2a302 100644 --- a/packages/shared/lib/core/account/interfaces/account-state.interface.ts +++ b/packages/shared/lib/core/account/interfaces/account-state.interface.ts @@ -6,4 +6,5 @@ export interface IAccountState extends IAccount, IAccountMetadata { depositAddress: string balances: AccountBalance isTransferring: boolean + votingPower: string } diff --git a/packages/shared/lib/core/account/interfaces/account.interface.ts b/packages/shared/lib/core/account/interfaces/account.interface.ts index 92532a6ff92..c48bbca9e35 100644 --- a/packages/shared/lib/core/account/interfaces/account.interface.ts +++ b/packages/shared/lib/core/account/interfaces/account.interface.ts @@ -62,6 +62,7 @@ export interface IAccount { meltAmount: HexEncodedAmount, transactionOptions?: TransactionOptions ): Promise + decreaseVotingPower(amount: string): Promise destroyAlias(aliasId: string, transactionOptions?: TransactionOptions): Promise destroyFoundry(foundryId: string, transactionOptions?: TransactionOptions): Promise generateAddress(options?: AddressGenerationOptions): Promise
@@ -72,6 +73,7 @@ export interface IAccount { getOutput(outputId: string): Promise getOutputsWithAdditionalUnlockConditions(outputs: OutputsToClaim): Promise getTransaction(transactionId: string): Promise + getVotingPower(): Promise incomingTransactions(): Promise<[string, [ITransactionPayload, IOutputResponse[]]][]> increaseNativeTokenSupply( tokenId: string, @@ -79,6 +81,7 @@ export interface IAccount { increaseNativeTokenSupplyOptions?: IncreaseNativeTokenSupplyOptions, transactionOptions?: TransactionOptions ): Promise + increaseVotingPower(amount: string): Promise minimumRequiredStorageDeposit(outputs: OutputTypes[]): Promise mintNativeToken( nativeTokenOptions: NativeTokenOptions, diff --git a/packages/shared/lib/core/governance/actions/index.ts b/packages/shared/lib/core/governance/actions/index.ts new file mode 100644 index 00000000000..bcb399c0a71 --- /dev/null +++ b/packages/shared/lib/core/governance/actions/index.ts @@ -0,0 +1 @@ +export * from './setVotingPower' diff --git a/packages/shared/lib/core/governance/actions/setVotingPower.ts b/packages/shared/lib/core/governance/actions/setVotingPower.ts new file mode 100644 index 00000000000..a9561add5d7 --- /dev/null +++ b/packages/shared/lib/core/governance/actions/setVotingPower.ts @@ -0,0 +1,30 @@ +import { get } from 'svelte/store' +import { selectedAccount, updateSelectedAccount } from '@core/account' +import { + addActivityToAccountActivitiesInAllAccountActivities, + generateActivity, + preprocessTransaction, +} from '@core/wallet' + +export async function setVotingPower(rawAmount: string): Promise { + 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()) + const activity = generateActivity(preprocessTransaction(transaction, account.depositAddress), account) + addActivityToAccountActivitiesInAllAccountActivities(account.index, activity) + } else if (amount < votingPower) { + const amountToDecrease = votingPower - amount + const transaction = await account.decreaseVotingPower(amountToDecrease.toString()) + const activity = generateActivity(preprocessTransaction(transaction, account.depositAddress), account) + addActivityToAccountActivitiesInAllAccountActivities(account.index, activity) + } + updateSelectedAccount({ isTransferring: false }) + } catch (err) { + updateSelectedAccount({ isTransferring: false }) + } +} diff --git a/packages/shared/lib/core/governance/index.ts b/packages/shared/lib/core/governance/index.ts index 2e6184e1e17..cc7b3ae4cec 100644 --- a/packages/shared/lib/core/governance/index.ts +++ b/packages/shared/lib/core/governance/index.ts @@ -1 +1,2 @@ +export * from './actions' export * from './stores' diff --git a/packages/shared/lib/core/profile-manager/actions/events-handlers/handleTransactionInclusionEvent.ts b/packages/shared/lib/core/profile-manager/actions/events-handlers/handleTransactionInclusionEvent.ts index 5b825628550..0cadac5cb4f 100644 --- a/packages/shared/lib/core/profile-manager/actions/events-handlers/handleTransactionInclusionEvent.ts +++ b/packages/shared/lib/core/profile-manager/actions/events-handlers/handleTransactionInclusionEvent.ts @@ -1,3 +1,4 @@ +import { syncVotingPower } from '@core/account' import { updateNftInAllAccountNfts } from '@core/nfts' import { ActivityDirection, ActivityType } from '@core/wallet' import { updateClaimingTransactionInclusion } from '@core/wallet/actions/activities/updateClaimingTransactionInclusion' @@ -33,5 +34,9 @@ export function handleTransactionInclusionEventInternal( updateNftInAllAccountNfts(accountIndex, activity.nftId, { isSpendable }) } + if (activity.tag === 'PARTICIPATE') { + syncVotingPower(accountIndex) + } + updateClaimingTransactionInclusion(payload.transactionId, payload.inclusionState, accountIndex) } From 336a30d82623cd486187556e38a3d92fdbf05d55 Mon Sep 17 00:00:00 2001 From: Jean Ribeiro Date: Mon, 5 Dec 2022 12:01:03 -0300 Subject: [PATCH 4/5] fixes from review and test --- .../popups/ManageVotingPowerPopup.svelte | 12 +++++++----- .../lib/core/governance/actions/setVotingPower.ts | 14 ++++++++++---- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/packages/shared/components/popups/ManageVotingPowerPopup.svelte b/packages/shared/components/popups/ManageVotingPowerPopup.svelte index 22191ef1c0f..ed9b706dfc3 100644 --- a/packages/shared/components/popups/ManageVotingPowerPopup.svelte +++ b/packages/shared/components/popups/ManageVotingPowerPopup.svelte @@ -1,6 +1,6 @@