Skip to content

Commit 4aecd5f

Browse files
authored
fix: migration flow, routing and error handling (#76)
1 parent 9389bb0 commit 4aecd5f

File tree

2 files changed

+94
-107
lines changed

2 files changed

+94
-107
lines changed

packages/shared/routes/setup/migrate/views/Migrate.svelte

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
migration,
1616
migrationAddress,
1717
prepareMigrationLog,
18-
removeAddressChecksum,
1918
sendOffLedgerMigrationRequest,
2019
unselectedInputs,
2120
} from 'shared/lib/migration'
@@ -29,9 +28,6 @@
2928
import { AvailableExchangeRates, CurrencyTypes } from 'shared/lib/typings/currency'
3029
import { walletSetupType } from 'shared/lib/wallet'
3130
import { SetupType } from 'shared/lib/typings/setup'
32-
import { MigrationAddress } from '@lib/typings/migration'
33-
import { createPrepareTransfers } from '@iota/core'
34-
import { appRouter } from '@core/router'
3531
3632
export let locale: Locale
3733
@@ -64,7 +60,7 @@
6460
6561
const unsubscribe = confirmedBundles.subscribe((newConfirmedBundles) => {
6662
newConfirmedBundles.forEach((bundle) => {
67-
if (bundle.bundleHash && bundle.bundleHash === singleMigrationBundleHash && bundle.confirmed) {
63+
if ($hasSingleBundle && bundle.confirmed) {
6864
didComplete.set(true)
6965
loading = false
7066
dispatch('next')
@@ -128,7 +124,7 @@
128124
// TODO: Check the bundlehash with software profiles
129125
const reverseTrytesSoftware = trytes.reverse()
130126
prepareMigrationLog('', reverseTrytesSoftware, migratableBalance)
131-
sendOffLedgerMigrationRequest(reverseTrytesSoftware, 0)
127+
return sendOffLedgerMigrationRequest(reverseTrytesSoftware, 0)
132128
})
133129
.then((receipt) => {
134130
// todo: handle receipt data

packages/shared/routes/setup/migrate/views/TransferFragmentedFunds.svelte

Lines changed: 92 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@
137137
function rerunMigration() {
138138
const _unmigratedBundles = $unmigratedBundles
139139
const unmigratedBundleIndexes = _unmigratedBundles.map((_bundle) => _bundle.index)
140-
141140
transactions = transactions.map((item) => {
142141
if (unmigratedBundleIndexes.includes(item.index)) {
143142
return { ...item, status: 0, errorText: null }
@@ -188,106 +187,98 @@
188187
function migrateFunds() {
189188
migratingFundsMessage = locale('views.migrate.migrating')
190189
191-
transactions.forEach((transaction, index) => {
192-
if (legacyLedger) {
193-
Platform.ledger
194-
.selectSeed($hardwareIndexes.accountIndex, $hardwareIndexes.pageIndex, ADDRESS_SECURITY_LEVEL)
195-
.then(({ iota, callback }) => {
196-
closeTransport = callback
197-
return createLedgerMigrationBundle(
198-
transaction.index,
199-
get(migrationAddress),
200-
iota.prepareTransfers,
201-
callback
202-
)
203-
})
204-
.then(({ trytes, bundleHash }) => {
205-
closePopup(true) // close transaction popup
206-
setMigratingTransaction(transaction, 1)
207-
transactions = transactions.map((_transaction, i) => {
208-
if (_transaction.index === transaction.index) {
209-
return { ..._transaction, bundleHash }
210-
}
211-
return _transaction
212-
})
213-
const reverseTrytesLedger = trytes.reverse()
214-
prepareMigrationLog(bundleHash, reverseTrytesLedger, transaction.balance)
215-
return sendOffLedgerMigrationRequest(reverseTrytesLedger, transaction.index)
216-
})
217-
.then((receipt) => {
218-
// todo: handle receipt
219-
if (!hasBroadcastAnyBundle) {
220-
hasBroadcastAnyBundle = true
221-
persistProfile()
222-
}
223-
})
224-
.catch((error) => {
225-
console.error(error)
226-
227-
if (legacyLedger) {
228-
closePopup(true) // close transaction popup
229-
closeTransport()
230-
displayNotificationForLedgerProfile('error', false, true, false, true, error)
231-
}
232-
233-
transactions = transactions.map((_transaction, i) => {
234-
if (_transaction.index === transaction.index) {
235-
return { ..._transaction, status: -1, errorText: 'Migration failed' }
236-
}
237-
238-
return _transaction
239-
})
240-
})
241-
.finally(() => {
242-
if (
243-
!transactions.some((tx) => tx.status === 1) &&
244-
transactions.every((tx) => tx.status !== 0)
245-
) {
246-
migrated = true
247-
busy = false
248-
}
249-
})
250-
} else {
251-
setMigratingTransaction(transaction, 1)
252-
253-
createMigrationBundle(transaction as Bundle, get(migrationAddress))
254-
.then((trytes: string[]) => {
255-
const reverseTrytesSoftware = trytes.reverse()
256-
prepareMigrationLog('', reverseTrytesSoftware, transaction.balance)
257-
sendOffLedgerMigrationRequest(reverseTrytesSoftware, transaction.index)
258-
})
259-
.then((receipt) => {
260-
// todo: handle receipt data
261-
// is this needed?
262-
if (!hasBroadcastAnyBundle) {
263-
hasBroadcastAnyBundle = true
264-
265-
persistProfile()
266-
}
267-
})
268-
.catch((error) => {
269-
showAppNotification({ type: 'error', message: error.message || 'Failed to prepare transfers' })
270-
console.error(error)
271-
272-
transactions = transactions.map((_transaction, i) => {
273-
if (_transaction.index === transaction.index) {
274-
return { ..._transaction, status: -1, errorText: 'Migration failed' }
275-
}
276-
277-
return _transaction
278-
})
279-
})
280-
.finally(() => {
281-
if (
282-
!transactions.some((tx) => tx.status === 1) &&
283-
transactions.every((tx) => tx.status !== 0)
284-
) {
285-
migrated = true
286-
busy = false
287-
}
288-
})
289-
}
290-
})
190+
transactions.reduce(
191+
(promise, transaction, idx) =>
192+
transaction.status === 0
193+
? // @ts-ignore
194+
promise
195+
.then((acc) => {
196+
if (legacyLedger) {
197+
return Platform.ledger
198+
.selectSeed(
199+
$hardwareIndexes.accountIndex,
200+
$hardwareIndexes.pageIndex,
201+
ADDRESS_SECURITY_LEVEL
202+
)
203+
.then(({ iota, callback }) => {
204+
closeTransport = callback
205+
return createLedgerMigrationBundle(
206+
transaction.index,
207+
get(migrationAddress),
208+
iota.prepareTransfers,
209+
callback
210+
)
211+
})
212+
.then(({ trytes, bundleHash }) => {
213+
closePopup(true) // close transaction popup
214+
setMigratingTransaction(transaction, 1)
215+
transactions = transactions.map((_transaction, i) => {
216+
if (_transaction.index === transaction.index) {
217+
return { ..._transaction, bundleHash }
218+
}
219+
return _transaction
220+
})
221+
const reverseTrytesLedger = trytes.reverse()
222+
prepareMigrationLog(bundleHash, reverseTrytesLedger, transaction.balance)
223+
return sendOffLedgerMigrationRequest(reverseTrytesLedger, transaction.index)
224+
})
225+
.then((receipt) => {
226+
// todo: handle receipt
227+
if (!hasBroadcastAnyBundle) {
228+
hasBroadcastAnyBundle = true
229+
persistProfile()
230+
}
231+
})
232+
} else {
233+
setMigratingTransaction(transaction, 1)
234+
235+
return createMigrationBundle(transaction as Bundle, get(migrationAddress))
236+
.then((trytes: string[]) => {
237+
const reverseTrytesSoftware = trytes.reverse()
238+
prepareMigrationLog('', reverseTrytesSoftware, transaction.balance)
239+
return sendOffLedgerMigrationRequest(reverseTrytesSoftware, transaction.index)
240+
})
241+
.then((receipt) => {
242+
// todo: handle receipt data
243+
// is this needed?
244+
if (!hasBroadcastAnyBundle) {
245+
hasBroadcastAnyBundle = true
246+
247+
persistProfile()
248+
}
249+
})
250+
}
251+
})
252+
.catch((error) => {
253+
console.error(error)
254+
255+
if (legacyLedger) {
256+
closePopup(true) // close transaction popup
257+
closeTransport()
258+
displayNotificationForLedgerProfile('error', false, true, false, true, error)
259+
}
260+
showAppNotification({
261+
type: 'error',
262+
message: error.message || 'Failed to prepare transfers',
263+
})
264+
265+
transactions = transactions.map((_transaction, i) => {
266+
if (_transaction.index === transaction.index) {
267+
return { ..._transaction, status: -1, errorText: 'Migration failed' }
268+
}
269+
270+
return _transaction
271+
})
272+
})
273+
.finally(() => {
274+
if (transactions.every((tx) => tx.status !== 0 && tx.status !== 1)) {
275+
migrated = true
276+
busy = false
277+
}
278+
})
279+
: promise,
280+
Promise.resolve([])
281+
)
291282
}
292283
</script>
293284

0 commit comments

Comments
 (0)