-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to allot mana to account (#8231)
* feat: add allot mana popup and button * feat: allow to allot mana to account WIP * fixes * feat: update manaAllotments into prepare transaction instead of preparing output * feat: improve literals * copy: update texts --------- Co-authored-by: cpl121 <[email protected]> Co-authored-by: cpl121 <[email protected]> Co-authored-by: Begoña Alvarez <[email protected]>
- Loading branch information
1 parent
cff7a2e
commit 1e1c3ec
Showing
10 changed files
with
190 additions
and
1 deletion.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
packages/desktop/components/buttons/popup-buttons/AllotManaButton.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,26 @@ | ||
<script lang="ts"> | ||
import { OnboardingButton } from '@ui' | ||
import { localize } from '@core/i18n' | ||
import { closePopup, openPopup, PopupId } from '@auxiliary/popup' | ||
function allotMana(): void { | ||
closePopup() | ||
} | ||
function onAllotManaClick(): void { | ||
openPopup({ | ||
id: PopupId.AllotMana, | ||
props: { | ||
title: localize('actions.allotMana'), | ||
confirmText: localize('actions.send'), | ||
onConfirm: allotMana, | ||
}, | ||
}) | ||
} | ||
</script> | ||
|
||
<OnboardingButton | ||
primaryText={localize('actions.allotMana')} | ||
secondaryText={localize('general.allotManaDescription')} | ||
onClick={onAllotManaClick} | ||
/> |
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
145 changes: 145 additions & 0 deletions
145
packages/desktop/components/popups/AllotManaPopup.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,145 @@ | ||
<script lang="ts"> | ||
import { localize } from '@core/i18n' | ||
import { closePopup, updatePopupProps } from '@auxiliary/popup' | ||
import { Button, Text, FontWeight, TextType, TextInput, AssetAmountInput } from '@ui' | ||
import { handleError } from '@core/error/handlers' | ||
import { | ||
selectedWallet, | ||
visibleSelectedWalletAssets, | ||
convertToRawAmount, | ||
getDefaultTransactionOptions, | ||
AddressConverter, | ||
} from '@core/wallet' | ||
import { activeProfile, checkActiveProfileAuth } from '@core/profile' | ||
import { ITransactionInfoToCalculateManaCost, getManaBalance } from '@core/network' | ||
import { onMount } from 'svelte' | ||
import { ManaBox } from '@components' | ||
export let _onMount: (..._: any[]) => Promise<void> = async () => {} | ||
export let rawAmount: string = getManaBalance($selectedWallet?.balances?.mana?.available)?.toString() | ||
export let accountAddress: string | ||
let isBusy = false | ||
let error: string | ||
let amount: string | ||
let assetAmountInput: AssetAmountInput | ||
let confirmDisabled = false | ||
const transactionInfo: ITransactionInfoToCalculateManaCost = {} | ||
let hasEnoughMana = false | ||
$: hasTransactionInProgress = | ||
$selectedWallet?.hasConsolidatingOutputsTransactionInProgress || $selectedWallet?.isTransferring | ||
$: amount, accountAddress, hasTransactionInProgress, setConfirmDisabled() | ||
$: asset = $visibleSelectedWalletAssets[$activeProfile?.network?.id].mana | ||
function setConfirmDisabled(): void { | ||
if (!amount || !accountAddress) { | ||
confirmDisabled = true | ||
return | ||
} | ||
const convertedSliderAmount = convertToRawAmount(amount, asset?.metadata)?.toString() | ||
confirmDisabled = convertedSliderAmount === rawAmount || hasTransactionInProgress || !hasEnoughMana | ||
} | ||
async function onSubmit(): Promise<void> { | ||
try { | ||
await assetAmountInput?.validate(true) | ||
if (!rawAmount || !accountAddress) return | ||
updatePopupProps({ rawAmount, accountAddress }) | ||
await checkActiveProfileAuth(allotMana, { stronghold: true, ledger: false }) | ||
} catch (err) { | ||
error = err.error | ||
handleError(err) | ||
} finally { | ||
isBusy = false | ||
} | ||
} | ||
async function preparedOutput() { | ||
try { | ||
const prepareOutput = await $selectedWallet.prepareOutput( | ||
{ | ||
recipientAddress: $selectedWallet.depositAddress, | ||
amount: '0', | ||
}, | ||
getDefaultTransactionOptions() | ||
) | ||
transactionInfo.preparedTransaction = await $selectedWallet.prepareSendOutputs( | ||
[prepareOutput], | ||
getDefaultTransactionOptions() | ||
) | ||
} catch (error) { | ||
transactionInfo.preparedTransactionError = error | ||
} | ||
} | ||
async function allotMana(): Promise<void> { | ||
try { | ||
const accountId = AddressConverter.parseBech32Address(accountAddress) | ||
// Send 0 amount transaction to accountAddress with amount in the allotMana | ||
const prepareOutput = await $selectedWallet.prepareOutput( | ||
{ recipientAddress: accountAddress, amount: '0' }, | ||
getDefaultTransactionOptions() | ||
) | ||
await $selectedWallet.sendOutputs([prepareOutput], { | ||
...getDefaultTransactionOptions(accountId), | ||
manaAllotments: { [accountId]: Number(rawAmount) }, // if manaAllotments amount passed as bigint it is transformed to string in the sdk | ||
}) | ||
closePopup() | ||
} catch (err) { | ||
handleError(err) | ||
} | ||
} | ||
function onBackClick(): void { | ||
closePopup() | ||
} | ||
onMount(async () => { | ||
try { | ||
await _onMount() | ||
await preparedOutput() | ||
} catch (err) { | ||
handleError(err.error) | ||
} | ||
}) | ||
</script> | ||
|
||
<allot-mana-popup class="w-full h-full space-y-6 flex flex-auto flex-col shrink-0"> | ||
<Text type={TextType.h3} fontWeight={FontWeight.semibold} classes="text-left"> | ||
{localize('popups.allotMana.title')} | ||
</Text> | ||
<div class="w-full flex-col space-y-4"> | ||
<form id="allot-mana" on:submit|preventDefault={onSubmit} class="flex flex-col space-y-5"> | ||
<div class="space-y-4"> | ||
<AssetAmountInput | ||
bind:this={assetAmountInput} | ||
bind:rawAmount | ||
bind:amount | ||
bind:asset | ||
containsSlider | ||
disableAssetSelection | ||
disabled={hasTransactionInProgress} | ||
/> | ||
<TextInput | ||
{error} | ||
bind:value={accountAddress} | ||
placeholder={localize('general.accountAddress')} | ||
label={localize('popups.allotMana.body')} | ||
submitHandler={onSubmit} | ||
disabled={isBusy} | ||
/> | ||
<ManaBox {transactionInfo} bind:hasEnoughMana /> | ||
</div> | ||
<popup-buttons class="flex flex-row flex-nowrap w-full space-x-4"> | ||
<Button classes="w-full" outline onClick={onBackClick} disabled={isBusy} | ||
>{localize('actions.back')}</Button | ||
> | ||
<Button classes="w-full" onClick={onSubmit} disabled={isBusy} {isBusy}> | ||
{localize('actions.send')} | ||
</Button> | ||
</popup-buttons> | ||
</form> | ||
</div> | ||
</allot-mana-popup> |
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
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
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