@@ -20,7 +20,7 @@ import {
20
20
} from "./index.js" ;
21
21
import type { PlatformContext } from "./platform.js" ;
22
22
import type { ProtocolInstance , ProtocolInterface , ProtocolName } from "./protocol.js" ;
23
- import { protocolIsRegistered } from "./protocol.js" ;
23
+ import { isVersionedProtocolInitializer , protocolIsRegistered } from "./protocol.js" ;
24
24
import type { AutomaticTokenBridge , TokenBridge } from "./protocols/tokenBridge/tokenBridge.js" ;
25
25
import type { RpcConnection } from "./rpc.js" ;
26
26
import type { ChainConfig , SignedTx , TokenAddress , TokenId } from "./types.js" ;
@@ -208,16 +208,32 @@ export abstract class ChainContext<
208
208
contracts ?: Contracts ,
209
209
rpc ?: RpcConnection < P > ,
210
210
) : Promise < ProtocolInstance < P , PN , N , C > > {
211
+ if ( ! contracts && this . protocols . has ( protocolName ) ) return this . protocols . get ( protocolName ) ! ;
212
+
211
213
const _contracts = contracts
212
214
? { ...this . config . contracts , ...contracts }
213
215
: this . config . contracts ;
214
-
215
- if ( ! contracts && this . protocols . has ( protocolName ) ) return this . protocols . get ( protocolName ) ! ;
216
-
216
+ const _rpc = rpc ?? ( await this . getRpc ( ) ) ;
217
217
const ctor = this . platform . getProtocolInitializer ( protocolName ) ;
218
- const protocol = rpc
219
- ? await this . platform . getProtocol ( protocolName , rpc )
220
- : new ctor ( this . network , this . chain , await this . getRpc ( ) , _contracts ) ;
218
+
219
+ let protocol ;
220
+ if ( rpc ) {
221
+ if ( contracts )
222
+ // TODO: the platforms `getProtocol` does not allow passing contracts
223
+ // it would need to be updated to allow this
224
+ throw new Error (
225
+ "Custom contracts are currently not supported with custom rpc connection. Add the contracts to the base config." ,
226
+ ) ;
227
+ // This ultimately calls fromRpc which _should_ handle version fetching
228
+ protocol = await this . platform . getProtocol ( protocolName , _rpc ) ;
229
+ } else {
230
+ if ( isVersionedProtocolInitializer ( ctor ) ) {
231
+ const version = await ctor . getVersion ( _rpc , _contracts ) ;
232
+ protocol = new ctor ( this . network , this . chain , _rpc , _contracts , version ) ;
233
+ } else {
234
+ protocol = new ctor ( this . network , this . chain , _rpc , _contracts ) ;
235
+ }
236
+ }
221
237
222
238
if ( ! contracts ) this . protocols . set ( protocolName , protocol ) ;
223
239
0 commit comments