Skip to content

Commit 1142d18

Browse files
authored
Add logic to fetch the version of the contract prior to initializing the client (#512)
1 parent cca3056 commit 1142d18

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

core/definitions/src/chain.ts

+23-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from "./index.js";
2121
import type { PlatformContext } from "./platform.js";
2222
import type { ProtocolInstance, ProtocolInterface, ProtocolName } from "./protocol.js";
23-
import { protocolIsRegistered } from "./protocol.js";
23+
import { isVersionedProtocolInitializer, protocolIsRegistered } from "./protocol.js";
2424
import type { AutomaticTokenBridge, TokenBridge } from "./protocols/tokenBridge/tokenBridge.js";
2525
import type { RpcConnection } from "./rpc.js";
2626
import type { ChainConfig, SignedTx, TokenAddress, TokenId } from "./types.js";
@@ -208,16 +208,32 @@ export abstract class ChainContext<
208208
contracts?: Contracts,
209209
rpc?: RpcConnection<P>,
210210
): Promise<ProtocolInstance<P, PN, N, C>> {
211+
if (!contracts && this.protocols.has(protocolName)) return this.protocols.get(protocolName)!;
212+
211213
const _contracts = contracts
212214
? { ...this.config.contracts, ...contracts }
213215
: this.config.contracts;
214-
215-
if (!contracts && this.protocols.has(protocolName)) return this.protocols.get(protocolName)!;
216-
216+
const _rpc = rpc ?? (await this.getRpc());
217217
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+
}
221237

222238
if (!contracts) this.protocols.set(protocolName, protocol);
223239

core/definitions/src/protocol.ts

+15
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export interface ProtocolInitializer<
5555
chain: C,
5656
connection: RpcConnection<P>,
5757
contracts: Contracts,
58+
version?: string,
5859
): ProtocolInterface<PN, N, C>;
5960
/** fromRpc will create a new instance of the Protocol client given the RPC and the config
6061
* @param rpc - the RPC connection to the chain, used to query the chain for its native chain id
@@ -66,6 +67,20 @@ export interface ProtocolInitializer<
6667
): Promise<ProtocolInterface<PN, N, C>>;
6768
}
6869

70+
export interface VersionedProtocolInitializer<
71+
P extends Platform,
72+
PN extends ProtocolName,
73+
N extends Network,
74+
> extends ProtocolInitializer<P, PN, N> {
75+
getVersion(rpc: RpcConnection<P>, Contracts: Contracts): Promise<string>;
76+
}
77+
78+
export function isVersionedProtocolInitializer(
79+
ctr: ProtocolInitializer<Platform, ProtocolName, Network>,
80+
): ctr is VersionedProtocolInitializer<Platform, ProtocolName, Network> {
81+
return "getVersion" in ctr;
82+
}
83+
6984
export type ProtocolInstance<
7085
P extends Platform,
7186
PN extends ProtocolName,

0 commit comments

Comments
 (0)