|
|
|
@ -52,6 +52,7 @@ import { |
|
|
|
|
} from '../../../../shared/constants/network'; |
|
|
|
|
import { |
|
|
|
|
determineTransactionAssetType, |
|
|
|
|
determineTransactionContractCode, |
|
|
|
|
determineTransactionType, |
|
|
|
|
isEIP1559Transaction, |
|
|
|
|
} from '../../../../shared/modules/transaction.utils'; |
|
|
|
@ -696,16 +697,54 @@ export default class TransactionController extends EventEmitter { |
|
|
|
|
return this._getTransaction(txId); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async addTransactionGasDefaults(txMeta) { |
|
|
|
|
const contractCode = await determineTransactionContractCode( |
|
|
|
|
txMeta.txParams, |
|
|
|
|
this.query, |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
let updateTxMeta = txMeta; |
|
|
|
|
try { |
|
|
|
|
updateTxMeta = await this.addTxGasDefaults(txMeta, contractCode); |
|
|
|
|
} catch (error) { |
|
|
|
|
log.warn(error); |
|
|
|
|
updateTxMeta = this.txStateManager.getTransaction(txMeta.id); |
|
|
|
|
updateTxMeta.loadingDefaults = false; |
|
|
|
|
this.txStateManager.updateTransaction( |
|
|
|
|
txMeta, |
|
|
|
|
'Failed to calculate gas defaults.', |
|
|
|
|
); |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
updateTxMeta.loadingDefaults = false; |
|
|
|
|
|
|
|
|
|
// The history note used here 'Added new unapproved transaction.' is confusing update call only updated the gas defaults.
|
|
|
|
|
// We need to improve `this.addTransaction` to accept history note and change note here.
|
|
|
|
|
this.txStateManager.updateTransaction( |
|
|
|
|
updateTxMeta, |
|
|
|
|
'Added new unapproved transaction.', |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
return updateTxMeta; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// ====================================================================================================================================================
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Validates and generates a txMeta with defaults and puts it in txStateManager |
|
|
|
|
* store. |
|
|
|
|
* |
|
|
|
|
* actionId is used to uniquely identify a request to create a transaction. |
|
|
|
|
* Only 1 transaction will be created for multiple requests with same actionId. |
|
|
|
|
* actionId is fix used for making this action idempotent to deal with scenario when |
|
|
|
|
* action is invoked multiple times with same parameters in MV3 due to service worker re-activation. |
|
|
|
|
* |
|
|
|
|
* @param txParams |
|
|
|
|
* @param origin |
|
|
|
|
* @param transactionType |
|
|
|
|
* @param sendFlowHistory |
|
|
|
|
* @param actionId |
|
|
|
|
* @returns {txMeta} |
|
|
|
|
*/ |
|
|
|
|
async addUnapprovedTransaction( |
|
|
|
@ -713,6 +752,7 @@ export default class TransactionController extends EventEmitter { |
|
|
|
|
origin, |
|
|
|
|
transactionType, |
|
|
|
|
sendFlowHistory = [], |
|
|
|
|
actionId, |
|
|
|
|
) { |
|
|
|
|
if ( |
|
|
|
|
transactionType !== undefined && |
|
|
|
@ -723,6 +763,17 @@ export default class TransactionController extends EventEmitter { |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// In transaction is found for same action id, do not create a new transaction.
|
|
|
|
|
if (actionId) { |
|
|
|
|
let existingTxMeta = |
|
|
|
|
this.txStateManager.getTransactionWithActionId(actionId); |
|
|
|
|
if (existingTxMeta) { |
|
|
|
|
this.emit('newUnapprovedTx', existingTxMeta); |
|
|
|
|
existingTxMeta = await this.addTransactionGasDefaults(existingTxMeta); |
|
|
|
|
return existingTxMeta; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// validate
|
|
|
|
|
const normalizedTxParams = txUtils.normalizeTxParams(txParams); |
|
|
|
|
const eip1559Compatibility = await this.getEIP1559Compatibility(); |
|
|
|
@ -741,6 +792,12 @@ export default class TransactionController extends EventEmitter { |
|
|
|
|
sendFlowHistory, |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Add actionId to txMeta to check if same actionId is seen again
|
|
|
|
|
// IF request to create transaction with same actionId is submitted again, new transaction will not be added for it.
|
|
|
|
|
if (actionId) { |
|
|
|
|
txMeta.actionId = actionId; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (origin === ORIGIN_METAMASK) { |
|
|
|
|
// Assert the from address is the selected address
|
|
|
|
|
if (normalizedTxParams.from !== this.getSelectedAddress()) { |
|
|
|
@ -762,10 +819,7 @@ export default class TransactionController extends EventEmitter { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const { type, getCodeResponse } = await determineTransactionType( |
|
|
|
|
txParams, |
|
|
|
|
this.query, |
|
|
|
|
); |
|
|
|
|
const { type } = await determineTransactionType(txParams, this.query); |
|
|
|
|
txMeta.type = transactionType || type; |
|
|
|
|
|
|
|
|
|
// ensure value
|
|
|
|
@ -776,25 +830,7 @@ export default class TransactionController extends EventEmitter { |
|
|
|
|
this.addTransaction(txMeta); |
|
|
|
|
this.emit('newUnapprovedTx', txMeta); |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
txMeta = await this.addTxGasDefaults(txMeta, getCodeResponse); |
|
|
|
|
} catch (error) { |
|
|
|
|
log.warn(error); |
|
|
|
|
txMeta = this.txStateManager.getTransaction(txMeta.id); |
|
|
|
|
txMeta.loadingDefaults = false; |
|
|
|
|
this.txStateManager.updateTransaction( |
|
|
|
|
txMeta, |
|
|
|
|
'Failed to calculate gas defaults.', |
|
|
|
|
); |
|
|
|
|
throw error; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
txMeta.loadingDefaults = false; |
|
|
|
|
// save txMeta
|
|
|
|
|
this.txStateManager.updateTransaction( |
|
|
|
|
txMeta, |
|
|
|
|
'Added new unapproved transaction.', |
|
|
|
|
); |
|
|
|
|
txMeta = await this.addTransactionGasDefaults(txMeta); |
|
|
|
|
|
|
|
|
|
return txMeta; |
|
|
|
|
} |
|
|
|
|