|
| 1 | +<script lang="ts"> |
| 2 | + import { PopupId, closePopup, openPopup } from '@auxiliary/popup' |
| 3 | + import { getSelectedAccount } from '@core/account' |
| 4 | + import { localize } from '@core/i18n' |
| 5 | + import { getArchivedBaseTokens } from '@core/layer-2/helpers/getArchivedBaseTokens' |
| 6 | + import { getBaseToken, getCoinType, activeProfile, isActiveLedgerProfile, isSoftwareProfile } from '@core/profile' |
| 7 | + import { truncateString } from '@core/utils' |
| 8 | + import { formatTokenAmountPrecise, getRequiredStorageDepositForMinimalBasicOutput } from '@core/wallet' |
| 9 | + import { Button, FontWeight, KeyValueBox, Spinner, Text, TextType } from 'shared/components' |
| 10 | + import { onMount } from 'svelte' |
| 11 | + import { WithdrawRequest, getLayer2WithdrawRequest } from '@core/layer-2/utils' |
| 12 | + import { withdrawL2Funds } from '@core/layer-2/helpers/widthdrawL2Funds' |
| 13 | + import { getL2ReceiptByRequestId } from '@core/layer-2/helpers/getL2ReceiptByRequestId' |
| 14 | + import { showAppNotification } from '@auxiliary/notification' |
| 15 | + import { Bip44 } from '@iota/sdk/out/types' |
| 16 | + import { displayNotificationForLedgerProfile, ledgerNanoStatus } from '@core/ledger' |
| 17 | + import { getEstimatedGasForOffLedgerRequest, getNonceForWithdrawRequest } from '@core/layer-2/helpers' |
| 18 | +
|
| 19 | + export let withdrawOnLoad = false |
| 20 | + export let withdrawableAmount: number |
| 21 | + const WASP_ISC_OPTIMIZATION_AMOUNT = 1 |
| 22 | + const WASP_ISC_MOCK_GAS_AMOUNT = 1000 |
| 23 | +
|
| 24 | + const bip44Chain: Bip44 = { |
| 25 | + coinType: Number(getCoinType()), |
| 26 | + account: getSelectedAccount().index, |
| 27 | + change: 0, |
| 28 | + addressIndex: 0, |
| 29 | + } |
| 30 | +
|
| 31 | + let error = '' |
| 32 | + let address: string | undefined = undefined |
| 33 | + let isWithdrawing = false |
| 34 | + const { isStrongholdLocked } = $activeProfile |
| 35 | +
|
| 36 | + $: withdrawOnLoad && address && !$isStrongholdLocked && withdrawFromL2() |
| 37 | +
|
| 38 | + function onCancelClick(): void { |
| 39 | + closePopup() |
| 40 | + } |
| 41 | +
|
| 42 | + async function onWithdrawFromL2Click(): Promise<void> { |
| 43 | + if ($isSoftwareProfile && $isStrongholdLocked) { |
| 44 | + openUnlockStrongholdPopup() |
| 45 | + } else { |
| 46 | + await handleAction(withdrawFromL2) |
| 47 | + } |
| 48 | + } |
| 49 | +
|
| 50 | + async function handleAction(callback: () => Promise<void>): Promise<void> { |
| 51 | + try { |
| 52 | + error = '' |
| 53 | +
|
| 54 | + if ($isActiveLedgerProfile && !$ledgerNanoStatus.connected) { |
| 55 | + displayNotificationForLedgerProfile('warning') |
| 56 | + return |
| 57 | + } |
| 58 | +
|
| 59 | + await callback() |
| 60 | + } catch (err) { |
| 61 | + error = localize(err.error) |
| 62 | +
|
| 63 | + if ($isActiveLedgerProfile) { |
| 64 | + displayNotificationForLedgerProfile('error', true, true, err) |
| 65 | + } else { |
| 66 | + showAppNotification({ |
| 67 | + type: 'error', |
| 68 | + message: localize(err.error), |
| 69 | + }) |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +
|
| 74 | + async function withdrawFromL2(): Promise<void> { |
| 75 | + isWithdrawing = true |
| 76 | + if ($isActiveLedgerProfile && !$ledgerNanoStatus.connected) { |
| 77 | + isWithdrawing = false |
| 78 | + displayNotificationForLedgerProfile('warning') |
| 79 | + return |
| 80 | + } |
| 81 | + try { |
| 82 | + const nonce = await getNonceForWithdrawRequest(address) |
| 83 | + if (!nonce) { |
| 84 | + isWithdrawing = false |
| 85 | + displayNotificationForLedgerProfile('warning') |
| 86 | + return |
| 87 | + } |
| 88 | +
|
| 89 | + const minRequiredStorageDeposit: number = Number(await getRequiredStorageDepositForMinimalBasicOutput()) |
| 90 | + let withdrawAmount = |
| 91 | + withdrawableAmount < minRequiredStorageDeposit + WASP_ISC_MOCK_GAS_AMOUNT |
| 92 | + ? withdrawableAmount |
| 93 | + : withdrawableAmount - WASP_ISC_MOCK_GAS_AMOUNT |
| 94 | +
|
| 95 | + // create withdraw request for gas estimations with hardcoded gasBudget |
| 96 | + let withdrawRequest: WithdrawRequest | undefined |
| 97 | + withdrawRequest = await getLayer2WithdrawRequest(withdrawAmount, nonce, bip44Chain) |
| 98 | + const gasEstimatePayload = await getEstimatedGasForOffLedgerRequest(withdrawRequest.request) |
| 99 | +
|
| 100 | + // adjust withdrawAmount to use estimated gas fee charged |
| 101 | + withdrawAmount = withdrawableAmount - gasEstimatePayload.gasFeeCharged |
| 102 | + // calculate gas |
| 103 | + const gasBudget = gasEstimatePayload.gasBurned + WASP_ISC_OPTIMIZATION_AMOUNT |
| 104 | +
|
| 105 | + if (withdrawableAmount > Number(minRequiredStorageDeposit) + Number(gasBudget)) { |
| 106 | + // Create new withdraw request with correct gas budget and withdraw amount |
| 107 | + withdrawRequest = await getLayer2WithdrawRequest(withdrawAmount, nonce, bip44Chain, gasBudget) |
| 108 | + } else { |
| 109 | + isWithdrawing = false |
| 110 | + showErrorNotification(localize('error.send.notEnoughBalance')) |
| 111 | + return |
| 112 | + } |
| 113 | +
|
| 114 | + await withdrawL2Funds(withdrawRequest.request) |
| 115 | + const receipt = await getL2ReceiptByRequestId(withdrawRequest.requestId) |
| 116 | +
|
| 117 | + isWithdrawing = false |
| 118 | + if (receipt?.errorMessage) { |
| 119 | + // if withdawing fails refresh the withdrawable amount because gas was used for the withdraw attempt |
| 120 | + withdrawableAmount = await getArchivedBaseTokens(address) |
| 121 | + showErrorNotification(receipt?.errorMessage) |
| 122 | + } else { |
| 123 | + closePopup() |
| 124 | + } |
| 125 | + } catch (err) { |
| 126 | + // if withdawing fails refresh the withdrawable amount because gas was used for the withdraw attempt (withdrawL2Funds()) |
| 127 | + withdrawableAmount = await getArchivedBaseTokens(address) |
| 128 | + let error = err |
| 129 | + // TODO: check error object in real ledger device when user cancels transaction. (In simulator the returned object is a string) |
| 130 | + // parse the error because ledger simulator returns error as a string. |
| 131 | + if (typeof err === 'string') { |
| 132 | + try { |
| 133 | + const parsedError = JSON.parse(err) |
| 134 | + error = parsedError?.payload ? parsedError.payload : parsedError |
| 135 | + } catch (e) { |
| 136 | + console.error(e) |
| 137 | + } |
| 138 | + } |
| 139 | + isWithdrawing = false |
| 140 | + showErrorNotification(error) |
| 141 | + } |
| 142 | + } |
| 143 | +
|
| 144 | + function openUnlockStrongholdPopup(): void { |
| 145 | + openPopup({ |
| 146 | + id: PopupId.UnlockStronghold, |
| 147 | + props: { |
| 148 | + onSuccess: () => { |
| 149 | + openPopup({ |
| 150 | + id: PopupId.WithdrawFromL2, |
| 151 | + props: { |
| 152 | + withdrawOnLoad: true, |
| 153 | + withdrawableAmount, |
| 154 | + }, |
| 155 | + }) |
| 156 | + }, |
| 157 | + onCancelled: () => { |
| 158 | + openPopup({ |
| 159 | + id: PopupId.WithdrawFromL2, |
| 160 | + props: { |
| 161 | + withdrawableAmount, |
| 162 | + }, |
| 163 | + }) |
| 164 | + }, |
| 165 | + subtitle: localize('popups.password.backup'), |
| 166 | + }, |
| 167 | + }) |
| 168 | + } |
| 169 | + function showErrorNotification(error): void { |
| 170 | + if ($isActiveLedgerProfile) { |
| 171 | + displayNotificationForLedgerProfile('error', true, false, error) |
| 172 | + } else { |
| 173 | + showAppNotification({ |
| 174 | + type: 'error', |
| 175 | + message: error, |
| 176 | + alert: true, |
| 177 | + }) |
| 178 | + } |
| 179 | + } |
| 180 | +
|
| 181 | + onMount(async () => { |
| 182 | + address = getSelectedAccount().depositAddress |
| 183 | + if (!withdrawableAmount) { |
| 184 | + withdrawableAmount = await getArchivedBaseTokens(address) |
| 185 | + } |
| 186 | + }) |
| 187 | +</script> |
| 188 | + |
| 189 | +<div class="flex flex-col space-y-6"> |
| 190 | + <Text type={TextType.h3} fontWeight={FontWeight.semibold} lineHeight="6"> |
| 191 | + {localize('popups.withdrawFromL2.title')} |
| 192 | + </Text> |
| 193 | + <Text fontSize="15" color="gray-700" classes="text-left">{localize('popups.withdrawFromL2.body')}</Text> |
| 194 | + {#if address} |
| 195 | + <KeyValueBox |
| 196 | + classes="flex items-center w-full py-4" |
| 197 | + keyText={truncateString(address, 15, 15)} |
| 198 | + valueText={formatTokenAmountPrecise(withdrawableAmount, getBaseToken())} |
| 199 | + /> |
| 200 | + {:else} |
| 201 | + <div class="flex items-center justify-center"> |
| 202 | + <Spinner /> |
| 203 | + </div> |
| 204 | + {/if} |
| 205 | + <div class="flex flex-row flex-nowrap w-full space-x-4 mt-6"> |
| 206 | + <Button classes="w-full" outline onClick={onCancelClick} disabled={isWithdrawing}> |
| 207 | + {localize('actions.cancel')} |
| 208 | + </Button> |
| 209 | + <Button |
| 210 | + classes="w-full" |
| 211 | + onClick={onWithdrawFromL2Click} |
| 212 | + disabled={!withdrawableAmount || Number(withdrawableAmount) === 0 || isWithdrawing} |
| 213 | + isBusy={isWithdrawing} |
| 214 | + busyMessage={localize('popups.withdrawFromL2.withdrawing')} |
| 215 | + > |
| 216 | + {localize('popups.withdrawFromL2.withdraw')} |
| 217 | + </Button> |
| 218 | + </div> |
| 219 | +</div> |
0 commit comments