@@ -5,8 +5,8 @@ import { FeeOption, FeeQuote, OperationStatus, Relayer } from './relayer.js'
55type GenericProviderTransactionReceipt = 'success' | 'failed' | 'unknown'
66
77export interface GenericProvider {
8- sendTransaction ( args : { to : string ; data : string } ) : Promise < string >
9- getTransactionReceipt ( txHash : string ) : Promise < GenericProviderTransactionReceipt >
8+ sendTransaction ( args : { to : string ; data : string } , chainId : bigint ) : Promise < string >
9+ getTransactionReceipt ( txHash : string , chainId : bigint ) : Promise < GenericProviderTransactionReceipt >
1010}
1111
1212export class LocalRelayer implements Relayer {
@@ -21,15 +21,33 @@ export class LocalRelayer implements Relayer {
2121 return undefined
2222 }
2323
24+ const trySwitchChain = async ( chainId : bigint ) => {
25+ try {
26+ await eth . request ( {
27+ method : 'wallet_switchEthereumChain' ,
28+ params : [
29+ {
30+ chainId : `0x${ chainId . toString ( 16 ) } ` ,
31+ } ,
32+ ] ,
33+ } )
34+ } catch ( error ) {
35+ // Log and continue
36+ console . error ( 'Error switching chain' , error )
37+ }
38+ }
39+
2440 return new LocalRelayer ( {
25- sendTransaction : async ( args ) => {
41+ sendTransaction : async ( args , chainId ) => {
2642 const accounts : string [ ] = await eth . request ( { method : 'eth_requestAccounts' } )
2743 const from = accounts [ 0 ]
2844 if ( ! from ) {
2945 console . warn ( 'No account selected, skipping local relayer' )
3046 return undefined
3147 }
3248
49+ await trySwitchChain ( chainId )
50+
3351 const tx = await eth . request ( {
3452 method : 'eth_sendTransaction' ,
3553 params : [
@@ -42,7 +60,9 @@ export class LocalRelayer implements Relayer {
4260 } )
4361 return tx
4462 } ,
45- getTransactionReceipt : async ( txHash ) => {
63+ getTransactionReceipt : async ( txHash , chainId ) => {
64+ await trySwitchChain ( chainId )
65+
4666 const rpcReceipt = await eth . request ( { method : 'eth_getTransactionReceipt' , params : [ txHash ] } )
4767 if ( rpcReceipt ) {
4868 const receipt = TransactionReceipt . fromRpc ( rpcReceipt )
@@ -80,17 +100,20 @@ export class LocalRelayer implements Relayer {
80100 }
81101
82102 async relay ( to : Address . Address , data : Hex . Hex , chainId : bigint , _ ?: FeeQuote ) : Promise < { opHash : Hex . Hex } > {
83- const txHash = await this . provider . sendTransaction ( {
84- to,
85- data,
86- } )
103+ const txHash = await this . provider . sendTransaction (
104+ {
105+ to,
106+ data,
107+ } ,
108+ chainId ,
109+ )
87110 Hex . assert ( txHash )
88111
89112 return { opHash : txHash }
90113 }
91114
92115 async status ( opHash : Hex . Hex , chainId : bigint ) : Promise < OperationStatus > {
93- const receipt = await this . provider . getTransactionReceipt ( opHash )
116+ const receipt = await this . provider . getTransactionReceipt ( opHash , chainId )
94117 if ( receipt === 'unknown' ) {
95118 // Could be pending but we don't know
96119 return { status : 'unknown' }
0 commit comments