/
SendRouter.svelte
147 lines (132 loc) · 5.6 KB
/
SendRouter.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<script lang="typescript">
import { prepareOutput, selectedAccount } from '@core/account'
import { handleError } from '@core/error/handlers/handleError'
import { ledgerPreparedOutput } from '@core/ledger'
import { isActiveLedgerProfile } from '@core/profile'
import { isStrongholdUnlocked } from '@core/profile-manager'
import { ExpirationTime } from '@core/utils'
import {
DEFAULT_TRANSACTION_OPTIONS,
getAssetById,
getOutputOptions,
newTransactionDetails,
NewTransactionType,
Output,
sendOutput,
updateNewTransactionDetails,
validateSendConfirmation,
} from '@core/wallet'
import { getStorageDepositFromOutput } from '@core/wallet/utils/generateActivity/helper'
import type { OutputOptions } from '@iota/wallet'
import { get } from 'svelte/store'
import { StrongholdUnlock } from '../../../../components'
import { sendRoute, SendRoute, sendRouter } from '../../../../lib/routers'
import { AmountView, Expiration, RecipientView, ReferenceView, ReviewView, TokenView } from './views'
$: ({ recipient, expirationDate, giftStorageDeposit, surplus } = $newTransactionDetails)
let storageDeposit = 0
let preparedOutput: Output
let outputOptions: OutputOptions
let initialExpirationDate: ExpirationTime = getInitialExpirationDate()
let triggerSendOnMount: boolean = false
$: transactionDetails = get(newTransactionDetails)
$: recipientAddress = recipient?.type === 'account' ? recipient?.account?.depositAddress : recipient?.address
$: asset =
transactionDetails.type === NewTransactionType.TokenTransfer
? getAssetById(transactionDetails.assetId)
: undefined
$: expirationDate, giftStorageDeposit, refreshSendConfirmationState()
async function sendTransaction(): Promise<void> {
triggerSendOnMount = false
const isUnlocked = await isStrongholdUnlocked()
if (isUnlocked) {
try {
await prepareTransactionOutput()
validateSendConfirmation(outputOptions, preparedOutput)
updateNewTransactionDetails({
type: $newTransactionDetails.type,
expirationDate,
giftStorageDeposit,
surplus,
})
if ($isActiveLedgerProfile) {
ledgerPreparedOutput.set(preparedOutput)
}
await sendOutput(preparedOutput)
$sendRouter.next()
} catch (err) {
handleError(err)
throw new Error(err)
}
} else {
$sendRouter.next({ needsUnlock: true })
}
}
async function prepareTransactionOutput(): Promise<void> {
// TODO: move arguments into transactionDetails object
outputOptions = getOutputOptions(
expirationDate,
recipientAddress,
$newTransactionDetails.type === NewTransactionType.TokenTransfer ? $newTransactionDetails.rawAmount : '0',
$newTransactionDetails.metadata,
$newTransactionDetails.tag,
asset,
giftStorageDeposit,
$newTransactionDetails.surplus,
$newTransactionDetails.layer2Parameters,
$newTransactionDetails.type === NewTransactionType.NftTransfer ? $newTransactionDetails.nftId : undefined
)
preparedOutput = await prepareOutput($selectedAccount.index, outputOptions, DEFAULT_TRANSACTION_OPTIONS)
setStorageDeposit(preparedOutput, Number(surplus))
if (!initialExpirationDate) {
initialExpirationDate = getInitialExpirationDate()
}
}
function setStorageDeposit(preparedOutput: Output, surplus?: number): void {
const { storageDeposit: _storageDeposit, giftedStorageDeposit: _giftedStorageDeposit } =
getStorageDepositFromOutput(preparedOutput)
if (giftStorageDeposit) {
// Only giftedStorageDeposit needs adjusting, since that is derived
// from the amount property instead of the unlock condition
if (!surplus) {
storageDeposit = _giftedStorageDeposit
} else if (surplus >= _giftedStorageDeposit) {
storageDeposit = 0
} else {
storageDeposit = _giftedStorageDeposit - surplus
}
} else {
storageDeposit = _storageDeposit
}
}
function refreshSendConfirmationState(): void {
void prepareTransactionOutput()
}
function getInitialExpirationDate(): ExpirationTime {
if (expirationDate) {
return ExpirationTime.Custom
} else if (storageDeposit && !giftStorageDeposit) {
return ExpirationTime.OneDay
} else {
return ExpirationTime.None
}
}
function onUnlockSuccess(): void {
triggerSendOnMount = true
$sendRouter.next()
}
</script>
{#if $sendRoute === SendRoute.Token}
<TokenView />
{:else if $sendRoute === SendRoute.Recipient}
<RecipientView />
{:else if $sendRoute === SendRoute.Amount}
<AmountView />
{:else if $sendRoute === SendRoute.Reference}
<ReferenceView />
{:else if $sendRoute === SendRoute.Review}
<ReviewView {sendTransaction} {triggerSendOnMount} {storageDeposit} {initialExpirationDate} bind:expirationDate />
{:else if $sendRoute === SendRoute.Expiration}
<Expiration onCancel={() => $sendRouter.previous()} />
{:else if $sendRoute === SendRoute.Password}
<StrongholdUnlock onSuccess={onUnlockSuccess} onCancel={() => $sendRouter.previous()} />
{/if}