|
9 | 9 | getDefaultTransactionOptions, |
10 | 10 | AddressConverter, |
11 | 11 | hasWalletMainAccountNegativeBIC, |
| 12 | + convertToRawAmount, |
| 13 | + getUnitFromTokenMetadata, |
12 | 14 | } from '@core/wallet' |
13 | 15 | import { activeProfile, checkActiveProfileAuth, getNetworkHrp } from '@core/profile' |
14 | 16 | import { ITransactionInfoToCalculateManaCost } from '@core/network' |
|
17 | 19 | import Big from 'big.js' |
18 | 20 | import { validateBech32Address } from '@core/utils' |
19 | 21 | import { AddressType } from '@iota/sdk/out/types' |
| 22 | + import { debounce } from '@core/utils' |
20 | 23 |
|
21 | 24 | export let _onMount: (..._: any[]) => Promise<void> = async () => {} |
22 | 25 | export let rawAmount: string = '0' |
|
27 | 30 | let amount: string |
28 | 31 | let assetAmountInput: AssetAmountInput |
29 | 32 |
|
| 33 | + const transactionUpdater = debounce(updateTransactionInfo, 500) |
30 | 34 | const transactionInfo: ITransactionInfoToCalculateManaCost = {} |
31 | 35 | let hasEnoughMana = false |
32 | 36 |
|
33 | 37 | $: hasTransactionInProgress = |
34 | 38 | $selectedWallet?.hasConsolidatingOutputsTransactionInProgress || $selectedWallet?.isTransferring |
35 | 39 | $: asset = $visibleSelectedWalletAssets[$activeProfile?.network?.id].mana |
36 | 40 | $: validAmount = Big(rawAmount ?? 0)?.gt(0) |
37 | | - $: accountAddress, validAmount, void preparedOutput() |
| 41 | + $: accountAddress, validAmount, amount, void preparedAllotMana() |
38 | 42 | $: accountAddress, void validateAddress() |
39 | 43 |
|
40 | 44 | $: sendAllowed = |
|
75 | 79 | } |
76 | 80 | } |
77 | 81 |
|
78 | | - async function preparedOutput() { |
79 | | - if (!accountAddress || !rawAmount || !validAmount) { |
| 82 | + let updatingTransactionInfo = false |
| 83 | + function preparedAllotMana(): void { |
| 84 | + updatingTransactionInfo = true |
| 85 | + if (!updatingTransactionInfo) { |
80 | 86 | transactionInfo.preparedTransaction = undefined |
81 | 87 | transactionInfo.preparedTransactionError = undefined |
82 | | - return |
83 | 88 | } |
| 89 | + transactionUpdater() |
| 90 | + } |
| 91 | +
|
| 92 | + async function updateTransactionInfo(): Promise<void> { |
84 | 93 | try { |
85 | 94 | 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([], { |
94 | 101 | ...getDefaultTransactionOptions(), |
95 | | - manaAllotments: { [accountId]: Number(rawAmount) }, |
| 102 | + manaAllotments: { [accountId]: Number(_amount) }, |
96 | 103 | }) |
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 |
99 | 111 | } |
100 | 112 | } |
101 | 113 |
|
102 | 114 | async function allotMana(): Promise<void> { |
103 | 115 | try { |
104 | 116 | 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([], { |
111 | 123 | ...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 |
113 | 125 | }) |
114 | 126 | closePopup() |
115 | 127 | } catch (err) { |
|
124 | 136 | onMount(async () => { |
125 | 137 | try { |
126 | 138 | await _onMount() |
127 | | - await preparedOutput() |
128 | 139 | } catch (err) { |
129 | 140 | error = err.message |
130 | 141 | handleError(err.error) |
|
159 | 170 | <Text error>{error}</Text> |
160 | 171 | {/if} |
161 | 172 | {#if displayManaBox} |
162 | | - <ManaBox {transactionInfo} bind:hasEnoughMana refreshTransactionInfo={preparedOutput} /> |
| 173 | + <ManaBox {transactionInfo} bind:hasEnoughMana refreshTransactionInfo={updateTransactionInfo} /> |
163 | 174 | {/if} |
164 | 175 | </div> |
165 | 176 | <popup-buttons class="flex flex-row flex-nowrap w-full space-x-4"> |
|
0 commit comments