Skip to content

Commit 467256a

Browse files
authored
fix: adjust balance for the smallest input when creating the bundle (#94)
1 parent 0c6cef7 commit 467256a

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

packages/shared/components/popups/LedgerLegacyTransaction.svelte

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@
1010
1111
export let transfer: Transfer
1212
export let inputs: Input[]
13+
let balanceToAdd: number = 0
14+
let smallestBalanceItem: Input | undefined
1315
14-
const outputAmount: number = inputs.reduce(
15-
(acc, input) => acc + (input.balance < MINIMUM_MIGRATABLE_AMOUNT ? MINIMUM_MIGRATABLE_AMOUNT : input.balance),
16-
0
17-
)
16+
if (transfer.value < MINIMUM_MIGRATABLE_AMOUNT) {
17+
balanceToAdd = MINIMUM_MIGRATABLE_AMOUNT - transfer.value
18+
19+
smallestBalanceItem = inputs.reduce((minItem, currentItem) =>
20+
currentItem.balance < minItem.balance ? currentItem : minItem
21+
)
22+
}
1823
1924
// Hardcoded strings because Ledger does not translate them
2025
const checksumString = (checksum): string => `Chk: ${checksum}`
@@ -24,7 +29,7 @@
2429

2530
<Text type="h4" classes="mb-6">{locale('popups.ledgerTransaction.transaction.title')}</Text>
2631
<Text type="p" classes="mb-6" secondary>{locale('popups.ledgerTransaction.transaction.info')}</Text>
27-
{#if outputAmount !== transfer.value}
32+
{#if smallestBalanceItem}
2833
<Text type="p" error classes="mb-6" secondary>{locale('popups.ledgerTransaction.transaction.warning')}</Text>
2934
{/if}
3035

@@ -40,8 +45,8 @@
4045
<div class="transaction flex flex-col space-y-4 scrollable-y">
4146
<div class="rounded-lg bg-gray-50 dark:bg-gray-800 p-5 text-center">
4247
<Text type="h5" highlighted classes="mb-2">{outputString}</Text>
43-
<Text type="pre">{formatUnitBestMatch(outputAmount)}</Text>
44-
{#if outputAmount !== transfer.value}
48+
<Text type="pre">{formatUnitBestMatch(transfer.value + balanceToAdd)}</Text>
49+
{#if smallestBalanceItem}
4550
<Text error type="pre">
4651
{locale('popups.ledgerTransaction.transaction.inputAmount', {
4752
values: { amount: formatUnitBestMatch(transfer.value) },
@@ -57,9 +62,9 @@
5762
<div class="rounded-lg bg-gray-50 dark:bg-gray-800 p-5 text-center">
5863
<Text type="h5" highlighted classes="mb-2">{inputString(index)}</Text>
5964
<Text type="pre"
60-
>{formatUnitBestMatch(balance < MINIMUM_MIGRATABLE_AMOUNT ? MINIMUM_MIGRATABLE_AMOUNT : balance)}</Text
65+
>{formatUnitBestMatch(smallestBalanceItem?.index === index ? balance + balanceToAdd : balance)}</Text
6166
>
62-
{#if balance < MINIMUM_MIGRATABLE_AMOUNT}
67+
{#if smallestBalanceItem?.index === index}
6368
<Text error type="pre"
6469
>{locale('popups.ledgerTransaction.transaction.inputAmount', {
6570
values: { amount: formatUnitBestMatch(balance) },

packages/shared/lib/migration.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -695,36 +695,44 @@ export const createLedgerMigrationBundle = (
695695
): Promise<MigrationBundle> => {
696696
const bundle = findMigrationBundle(bundleIndex)
697697

698+
let totalBalance = bundle.inputs.reduce((acc, input) => acc + input.balance, 0)
699+
let balanceToAdd: number = 0
700+
let smallestBalanceItem: Input | undefined
701+
698702
const transferForConfirmation = {
699703
address: migrationAddress.trytes,
700-
value: bundle.inputs.reduce((acc, input) => acc + input.balance, 0),
704+
value: totalBalance,
701705
tag: 'U'.repeat(27),
702706
}
703707

704708
openLedgerLegacyTransactionPopup(transferForConfirmation, bundle.inputs)
705709

710+
// Adjust totalBalance if its less than MINIMUM_MIGRATABLE_AMOUNT to bypass legacy validation tool in smart contract which doesnt allow migrating less than MINIMUM_MIGRATABLE_AMOUNT.
711+
// The ISC only cares about the addresses in the bundle, it internaly resolves the balances and does NOT depend on the balances sent by migration tool.
712+
// If the amount for migration, resolved by ISC, is less than the Min required storage deposit on stardust the receipt will contain the error messgage
713+
// ex. "not enough base tokens for storage deposit: available 211188 < required 239500 base tokens"
714+
if (totalBalance < MINIMUM_MIGRATABLE_AMOUNT) {
715+
balanceToAdd = MINIMUM_MIGRATABLE_AMOUNT - totalBalance
716+
totalBalance += balanceToAdd
717+
718+
smallestBalanceItem = bundle.inputs.reduce((minItem, currentItem) =>
719+
currentItem.balance < minItem.balance ? currentItem : minItem
720+
)
721+
}
722+
706723
const transfers = [
707724
{
708725
address: migrationAddress.trytes,
709-
value: bundle.inputs.reduce(
710-
(acc, input) =>
711-
acc + (input.balance < MINIMUM_MIGRATABLE_AMOUNT ? MINIMUM_MIGRATABLE_AMOUNT : input.balance),
712-
0
713-
),
726+
value: totalBalance,
714727
tag: 'U'.repeat(27),
715728
},
716729
]
717730

718-
// The correct amount for migration is tracked in totalBalance and is diplayed to the user.
719-
// If the totalBalance is less than the Min required storage deposit on stardust the receipt will contain the error messgage
720-
// ex. "not enough base tokens for storage deposit: available 211188 < required 239500 base tokens"
721-
// Hardcode MINIMUM_MIGRATABLE_AMOUNT for every input so we bypass legacy validation tool in contract which doesnt allow migrating less than MINIMUM_MIGRATABLE_AMOUNT.
722-
// The ISC only cares about the addresses in the bundle, it internaly resolves the balances and does NOT depend on the amounts hardcoded here.
723731
const inputsForTransfer: any[] = bundle.inputs.map((input) => ({
724732
address: input.address,
725733
keyIndex: input.index,
726734
security: input.securityLevel,
727-
balance: input.balance < MINIMUM_MIGRATABLE_AMOUNT ? MINIMUM_MIGRATABLE_AMOUNT : input.balance, // hardcoded amount
735+
balance: smallestBalanceItem?.index === input.index ? input.balance + balanceToAdd : input.balance,
728736
}))
729737

730738
return prepareTransfersFn(transfers, inputsForTransfer).then((trytes) => {
@@ -811,22 +819,34 @@ export const createMigrationBundle = async (bundle: Bundle, migrationAddress: Mi
811819

812820
const prepareTransfers = createPrepareTransfers()
813821

822+
let totalBalance = bundle.inputs.reduce((acc, input) => acc + input.balance, 0)
823+
let balanceToAdd: number = 0
824+
let smallestBalanceItem: Input | undefined
825+
826+
// Adjust totalBalance if its less than MINIMUM_MIGRATABLE_AMOUNT to bypass legacy validation tool in smart contract which doesnt allow migrating less than MINIMUM_MIGRATABLE_AMOUNT.
827+
// The ISC only cares about the addresses in the bundle, it internaly resolves the balances and does NOT depend on the balances sent by migration tool.
828+
// If the amount for migration, resolved by ISC, is less than the Min required storage deposit on stardust the receipt will contain the error messgage
829+
// ex. "not enough base tokens for storage deposit: available 211188 < required 239500 base tokens"
830+
if (totalBalance < MINIMUM_MIGRATABLE_AMOUNT) {
831+
balanceToAdd = MINIMUM_MIGRATABLE_AMOUNT - totalBalance
832+
totalBalance += balanceToAdd
833+
834+
smallestBalanceItem = bundle.inputs.reduce((minItem, currentItem) =>
835+
currentItem.balance < minItem.balance ? currentItem : minItem
836+
)
837+
}
814838
const transfers = [
815839
{
816-
value: bundle.inputs.length * MINIMUM_MIGRATABLE_AMOUNT, // hardcoded amount
840+
value: totalBalance,
817841
address: removeAddressChecksum(migrationAddress.trytes),
818842
},
819843
]
820-
// The correct amount for migration is tracked in totalBalance and is diplayed to the user.
821-
// If the totalBalance is less than the Min required storage deposit on stardust the receipt will contain the error messgage
822-
// ex. "not enough base tokens for storage deposit: available 211188 < required 239500 base tokens"
823-
// Hardcode MINIMUM_MIGRATABLE_AMOUNT for every input so we bypass legacy validation tool in contract which doesnt allow migrating less than MINIMUM_MIGRATABLE_AMOUNT.
824-
// The ISC only cares about the addresses in the bundle, it internaly resolves the balances and does NOT depend on the amounts hardcoded here.
844+
825845
const inputsForTransfer: any[] = bundle.inputs.map((input) => ({
826846
address: input.address,
827847
keyIndex: input.index,
828848
security: input.securityLevel,
829-
balance: MINIMUM_MIGRATABLE_AMOUNT, // hardcoded amount
849+
balance: smallestBalanceItem?.index === input.index ? input.balance + balanceToAdd : input.balance,
830850
}))
831851

832852
try {

0 commit comments

Comments
 (0)