Skip to content

Commit 1db4551

Browse files
authored
Merge pull request #2338 from AmbireTech/fix/transfer-infinite-loop
fix / infinite loop in the transfer ctrl
2 parents 63d0856 + 5756e54 commit 1db4551

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

src/controllers/transfer/transfer.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,23 @@ export class TransferController extends EventEmitter implements ITransferControl
493493
this.emitUpdate()
494494
}
495495

496+
#fetchRecipientAccountStateIfNeeded() {
497+
if (!this.isInitialized) return
498+
499+
const recipientAcc = this.#accounts.accounts.find((a) => a.addr === this.recipientAddress)
500+
if (recipientAcc && this.selectedToken?.chainId) {
501+
const state =
502+
this.#accounts.accountStates[recipientAcc.addr]?.[this.selectedToken.chainId.toString()]
503+
if (!state) {
504+
this.#accounts
505+
.getOrFetchAccountOnChainState(recipientAcc.addr, this.selectedToken.chainId)
506+
.catch((e) => {
507+
console.log('Failed to get the account on chain state:', e)
508+
})
509+
}
510+
}
511+
}
512+
496513
get validationFormMsgs() {
497514
if (!this.isInitialized) return DEFAULT_VALIDATION_FORM_MSGS
498515

@@ -504,17 +521,6 @@ export class TransferController extends EventEmitter implements ITransferControl
504521
// so that we could validate the account properly
505522
// example: Safe accounts may not be deployed on certain networks
506523
const recipientAcc = this.#accounts.accounts.find((a) => a.addr === this.recipientAddress)
507-
if (recipientAcc && this.selectedToken?.chainId) {
508-
const state =
509-
this.#accounts.accountStates[recipientAcc.addr]?.[this.selectedToken.chainId.toString()]
510-
if (!state) {
511-
this.#accounts
512-
.getOrFetchAccountOnChainState(recipientAcc.addr, this.selectedToken.chainId)
513-
.catch((e) => {
514-
console.log('Failed to get the account on chain state:', e)
515-
})
516-
}
517-
}
518524

519525
validationFormMsgsNew.recipientAddress = validateSendTransferAddress(
520526
this.recipientAddress,
@@ -595,6 +601,7 @@ export class TransferController extends EventEmitter implements ITransferControl
595601
}
596602

597603
this.selectedToken = selectedToken
604+
this.#fetchRecipientAccountStateIfNeeded()
598605
}
599606
// If we do a regular check the value won't update if it's '' or '0'
600607
if (typeof amount === 'string') {
@@ -696,6 +703,7 @@ export class TransferController extends EventEmitter implements ITransferControl
696703

697704
this.checkIsRecipientAddressViewOnly()
698705
this.checkIsRecipientAddressUnknown()
706+
this.#fetchRecipientAccountStateIfNeeded()
699707
}
700708

701709
#setAmountAndNotifyUI(amount: string) {

0 commit comments

Comments
 (0)