Skip to content

Commit a3193d8

Browse files
fix: custom mana allotment should send to itself (#8528)
* feat: custom mana allotment should send itself * fix: allot mana without unneccesary basic outputs * fix: improve reactivity when changing the amount to allot mana * fix: calculate correct amount to allot mana * feat: add debounce in allot mana popup * fix: solve merge error * fix: error --------- Co-authored-by: Begoña Álvarez de la Cruz <[email protected]>
1 parent 3efbd4e commit a3193d8

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

packages/desktop/components/popups/AllotManaPopup.svelte

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
getDefaultTransactionOptions,
1010
AddressConverter,
1111
hasWalletMainAccountNegativeBIC,
12+
convertToRawAmount,
13+
getUnitFromTokenMetadata,
1214
} from '@core/wallet'
1315
import { activeProfile, checkActiveProfileAuth, getNetworkHrp } from '@core/profile'
1416
import { ITransactionInfoToCalculateManaCost } from '@core/network'
@@ -17,6 +19,7 @@
1719
import Big from 'big.js'
1820
import { validateBech32Address } from '@core/utils'
1921
import { AddressType } from '@iota/sdk/out/types'
22+
import { debounce } from '@core/utils'
2023
2124
export let _onMount: (..._: any[]) => Promise<void> = async () => {}
2225
export let rawAmount: string = '0'
@@ -27,14 +30,15 @@
2730
let amount: string
2831
let assetAmountInput: AssetAmountInput
2932
33+
const transactionUpdater = debounce(updateTransactionInfo, 500)
3034
const transactionInfo: ITransactionInfoToCalculateManaCost = {}
3135
let hasEnoughMana = false
3236
3337
$: hasTransactionInProgress =
3438
$selectedWallet?.hasConsolidatingOutputsTransactionInProgress || $selectedWallet?.isTransferring
3539
$: asset = $visibleSelectedWalletAssets[$activeProfile?.network?.id].mana
3640
$: validAmount = Big(rawAmount ?? 0)?.gt(0)
37-
$: accountAddress, validAmount, void preparedOutput()
41+
$: accountAddress, validAmount, amount, void preparedAllotMana()
3842
$: accountAddress, void validateAddress()
3943
4044
$: sendAllowed =
@@ -75,41 +79,49 @@
7579
}
7680
}
7781
78-
async function preparedOutput() {
79-
if (!accountAddress || !rawAmount || !validAmount) {
82+
let updatingTransactionInfo = false
83+
function preparedAllotMana(): void {
84+
updatingTransactionInfo = true
85+
if (!updatingTransactionInfo) {
8086
transactionInfo.preparedTransaction = undefined
8187
transactionInfo.preparedTransactionError = undefined
82-
return
8388
}
89+
transactionUpdater()
90+
}
91+
92+
async function updateTransactionInfo(): Promise<void> {
8493
try {
8594
const accountId = AddressConverter.parseBech32Address(accountAddress)
86-
const prepareOutput = await $selectedWallet.prepareOutput(
87-
{
88-
recipientAddress: $selectedWallet.depositAddress,
89-
amount: '0',
90-
},
91-
getDefaultTransactionOptions()
92-
)
93-
transactionInfo.preparedTransaction = await $selectedWallet.prepareSendOutputs([prepareOutput], {
95+
const _amount = convertToRawAmount(
96+
amount,
97+
asset?.metadata,
98+
getUnitFromTokenMetadata(asset?.metadata)
99+
)?.toString()
100+
transactionInfo.preparedTransaction = await $selectedWallet.prepareSendOutputs([], {
94101
...getDefaultTransactionOptions(),
95-
manaAllotments: { [accountId]: Number(rawAmount) },
102+
manaAllotments: { [accountId]: Number(_amount) },
96103
})
97-
} catch (err) {
98-
transactionInfo.preparedTransactionError = err
104+
transactionInfo.preparedTransactionError = undefined
105+
} catch (error) {
106+
console.error(error)
107+
transactionInfo.preparedTransaction = undefined
108+
transactionInfo.preparedTransactionError = error
109+
} finally {
110+
updatingTransactionInfo = false
99111
}
100112
}
101113
102114
async function allotMana(): Promise<void> {
103115
try {
104116
const accountId = AddressConverter.parseBech32Address(accountAddress)
105-
// Send 0 amount transaction to accountAddress with amount in the allotMana
106-
const prepareOutput = await $selectedWallet.prepareOutput(
107-
{ recipientAddress: accountAddress, amount: '0' },
108-
getDefaultTransactionOptions()
109-
)
110-
await $selectedWallet.sendOutputs([prepareOutput], {
117+
const _amount = convertToRawAmount(
118+
amount,
119+
asset?.metadata,
120+
getUnitFromTokenMetadata(asset?.metadata)
121+
)?.toString()
122+
await $selectedWallet.sendOutputs([], {
111123
...getDefaultTransactionOptions(),
112-
manaAllotments: { [accountId]: Number(rawAmount) }, // if manaAllotments amount passed as bigint it is transformed to string in the sdk
124+
manaAllotments: { [accountId]: Number(_amount) }, // if manaAllotments amount passed as bigint it is transformed to string in the sdk
113125
})
114126
closePopup()
115127
} catch (err) {
@@ -124,7 +136,6 @@
124136
onMount(async () => {
125137
try {
126138
await _onMount()
127-
await preparedOutput()
128139
} catch (err) {
129140
error = err.message
130141
handleError(err.error)
@@ -159,7 +170,7 @@
159170
<Text error>{error}</Text>
160171
{/if}
161172
{#if displayManaBox}
162-
<ManaBox {transactionInfo} bind:hasEnoughMana refreshTransactionInfo={preparedOutput} />
173+
<ManaBox {transactionInfo} bind:hasEnoughMana refreshTransactionInfo={updateTransactionInfo} />
163174
{/if}
164175
</div>
165176
<popup-buttons class="flex flex-row flex-nowrap w-full space-x-4">

0 commit comments

Comments
 (0)