@@ -19,7 +19,7 @@ import accounts, {
19
19
} from '../accounts'
20
20
import Chains , { Chain } from '../chains'
21
21
import { getSignerType , Type as SignerType } from '../../resources/domain/signer'
22
- import { TransactionData } from '../../resources/domain/transaction'
22
+ import { normalizeChainId , TransactionData } from '../../resources/domain/transaction'
23
23
import { populate as populateTransaction , maxFee , classifyTransaction } from '../transaction'
24
24
import FrameAccount from '../accounts/Account'
25
25
import { capitalize } from '../../resources/utils'
@@ -449,14 +449,23 @@ export class Provider extends EventEmitter {
449
449
}
450
450
451
451
async fillTransaction ( newTx : RPC . SendTransaction . TxParams , cb : Callback < TransactionMetadata > ) {
452
- if ( ! newTx ) return cb ( new Error ( 'No transaction data' ) )
452
+ if ( ! newTx ) {
453
+ return cb ( new Error ( 'No transaction data' ) )
454
+ }
455
+
456
+ const connection = this . connection . connections [ 'ethereum' ] [ parseInt ( newTx . chainId , 16 ) ]
457
+ const chainConnected = connection && ( connection . primary ?. connected || connection . secondary ?. connected )
458
+
459
+ if ( ! chainConnected ) {
460
+ return cb ( new Error ( `Chain ${ newTx . chainId } not connected` ) )
461
+ }
453
462
454
463
try {
455
464
const approvals : RequiredApproval [ ] = [ ]
456
465
const accountId = ( accounts . current ( ) || { } ) . id
457
466
const rawTx = getRawTx ( newTx , accountId )
458
467
const gas = gasFees ( rawTx )
459
- const chainConfig = this . connection . connections [ 'ethereum' ] [ parseInt ( rawTx . chainId ) ] . chainConfig
468
+ const { chainConfig } = connection
460
469
461
470
const estimateGasLimit = async ( ) => {
462
471
try {
@@ -497,69 +506,65 @@ export class Provider extends EventEmitter {
497
506
}
498
507
499
508
sendTransaction ( payload : RPC . SendTransaction . Request , res : RPCRequestCallback , targetChain : Chain ) {
500
- const txParams = payload . params [ 0 ]
501
- const payloadChain = payload . chainId
502
- const txChain = txParams . chainId
503
-
504
- if ( payloadChain && txChain && parseInt ( payloadChain , 16 ) !== parseInt ( txChain , 16 ) ) {
505
- return resError (
506
- `Chain for transaction (${ txChain } ) does not match request target chain (${ payloadChain } )` ,
507
- payload ,
508
- res
509
- )
510
- }
509
+ try {
510
+ const txParams = payload . params [ 0 ]
511
+ const payloadChain = payload . chainId
511
512
512
- const newTx = {
513
- ...txParams ,
514
- chainId : txChain || payloadChain || addHexPrefix ( targetChain . id . toString ( 16 ) )
515
- }
513
+ const normalizedTx = normalizeChainId ( txParams , payloadChain ? parseInt ( payloadChain , 16 ) : undefined )
514
+ const tx = {
515
+ ...normalizedTx ,
516
+ chainId : normalizedTx . chainId || payloadChain || addHexPrefix ( targetChain . id . toString ( 16 ) )
517
+ }
516
518
517
- const currentAccount = accounts . current ( )
519
+ const currentAccount = accounts . current ( )
518
520
519
- log . verbose ( `sendTransaction(${ JSON . stringify ( newTx ) } ` )
521
+ log . verbose ( `sendTransaction(${ JSON . stringify ( tx ) } ` )
520
522
521
- this . fillTransaction ( newTx , ( err , transactionMetadata ) => {
522
- if ( err ) {
523
- resError ( err , payload , res )
524
- } else {
525
- const txMetadata = transactionMetadata as TransactionMetadata
526
- const from = txMetadata . tx . from
523
+ this . fillTransaction ( tx , ( err , transactionMetadata ) => {
524
+ if ( err ) {
525
+ resError ( err , payload , res )
526
+ } else {
527
+ const txMetadata = transactionMetadata as TransactionMetadata
528
+ const from = txMetadata . tx . from
527
529
528
- if ( ! currentAccount || ! hasAddress ( currentAccount , from ) ) {
529
- return resError ( 'Transaction is not from currently selected account' , payload , res )
530
- }
530
+ if ( ! currentAccount || ! hasAddress ( currentAccount , from ) ) {
531
+ return resError ( 'Transaction is not from currently selected account' , payload , res )
532
+ }
531
533
532
- const handlerId = this . addRequestHandler ( res )
534
+ const handlerId = this . addRequestHandler ( res )
533
535
534
- const { feesUpdated, recipientType, ...data } = txMetadata . tx
536
+ const { feesUpdated, recipientType, ...data } = txMetadata . tx
535
537
536
- const unclassifiedReq = {
537
- handlerId,
538
- type : 'transaction' ,
539
- data,
540
- payload,
541
- account : ( currentAccount as FrameAccount ) . id ,
542
- origin : payload . _origin ,
543
- approvals : [ ] ,
544
- feesUpdatedByUser : false ,
545
- recipientType,
546
- recognizedActions : [ ]
547
- } as Omit < TransactionRequest , 'classification' >
538
+ const unclassifiedReq = {
539
+ handlerId,
540
+ type : 'transaction' ,
541
+ data,
542
+ payload,
543
+ account : ( currentAccount as FrameAccount ) . id ,
544
+ origin : payload . _origin ,
545
+ approvals : [ ] ,
546
+ feesUpdatedByUser : false ,
547
+ recipientType,
548
+ recognizedActions : [ ]
549
+ } as Omit < TransactionRequest , 'classification' >
548
550
549
- const classification = classifyTransaction ( unclassifiedReq )
551
+ const classification = classifyTransaction ( unclassifiedReq )
550
552
551
- const req = {
552
- ...unclassifiedReq ,
553
- classification
554
- }
553
+ const req = {
554
+ ...unclassifiedReq ,
555
+ classification
556
+ }
555
557
556
- accounts . addRequest ( req , res )
558
+ accounts . addRequest ( req , res )
557
559
558
- txMetadata . approvals . forEach ( ( approval ) => {
559
- currentAccount ?. addRequiredApproval ( req , approval . type , approval . data )
560
- } )
561
- }
562
- } )
560
+ txMetadata . approvals . forEach ( ( approval ) => {
561
+ currentAccount ?. addRequiredApproval ( req , approval . type , approval . data )
562
+ } )
563
+ }
564
+ } )
565
+ } catch ( e ) {
566
+ resError ( ( e as Error ) . message , payload , res )
567
+ }
563
568
}
564
569
565
570
getTransactionByHash ( payload : RPCRequestPayload , cb : RPCRequestCallback , targetChain : Chain ) {
0 commit comments