Skip to content

Commit

Permalink
Hardcode platform in ChainContext (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin authored Sep 21, 2023
1 parent e8f51a0 commit 2f40acf
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 52 deletions.
10 changes: 8 additions & 2 deletions core/definitions/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,20 @@ import { NativeAddress } from "./address";

export abstract class ChainContext<P extends PlatformName> {
// Cached Protocol clients
protected rpc?: RpcConnection<P>;
protected tokenBridge?: TokenBridge<P>;
protected autoTokenBridge?: AutomaticTokenBridge<P>;
protected circleBridge?: CircleBridge<P>;
protected autoCircleBridge?: AutomaticCircleBridge<P>;

constructor(readonly platform: Platform<P>, readonly chain: ChainName) {}
abstract platform: Platform<P>;

abstract getRpc(): RpcConnection<P>;
constructor(readonly chain: ChainName) {}

getRpc(): RpcConnection<P> {
this.rpc = this.rpc ? this.rpc : this.platform.getRpc(this.chain);
return this.rpc;
}

// Get the number of decimals for a token
async getDecimals(token: TokenId | "native"): Promise<bigint> {
Expand Down
12 changes: 6 additions & 6 deletions core/definitions/src/testing/mocks/chain.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import { ChainName, PlatformName } from "@wormhole-foundation/sdk-base";
import { RpcConnection, ChainContext, Platform } from "../..";
import { mockPlatformFactory } from "./platform";

export function chainFactory<P extends PlatformName>(
p: Platform<P>,
chain: ChainName
): ChainContext<P> {
return new MockChain<P>(p, chain);
return new MockChain<P>(p.platform, chain);
}

export class MockChain<P extends PlatformName> extends ChainContext<P> {
private rpc?: RpcConnection<P>;

getRpc(): RpcConnection<P> {
this.rpc = this.rpc ? this.rpc : this.platform.getRpc(this.chain);
return this.rpc!;
readonly platform: Platform<P>;
constructor(platform: PlatformName, readonly chain: ChainName) {
super(chain);
this.platform = mockPlatformFactory<P>(platform as P, {});
}
}
13 changes: 6 additions & 7 deletions core/definitions/src/testing/mocks/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import { MockChain } from "./chain";
import { MockTokenBridge } from "./tokenBridge";
import { WormholeCore } from "../../protocols/core";

export function mockPlatformFactory(
p: PlatformName,
export function mockPlatformFactory<P extends PlatformName>(
p: P,
config: ChainsConfig
): Platform<PlatformName> {
class ConcreteMockPlatform extends MockPlatform<typeof p> {
static _platform: typeof p = p;
readonly platform = ConcreteMockPlatform._platform;
): Platform<P> {
class ConcreteMockPlatform extends MockPlatform<P> {
readonly platform = p;
}
return new ConcreteMockPlatform(config);
}
Expand Down Expand Up @@ -65,7 +64,7 @@ export class MockPlatform<P extends PlatformName> implements Platform<P> {
}

getChain(chain: ChainName): ChainContext<P> {
return new MockChain<P>(this, chain);
return new MockChain<P>(this.platform, chain);
}
getRpc(chain: ChainName): RpcConnection<P> {
// @ts-ignore
Expand Down
24 changes: 2 additions & 22 deletions platforms/evm/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,8 @@ import {
RpcConnection,
ChainName,
} from '@wormhole-foundation/connect-sdk';
import { EvmPlatform } from './platform';

export class EvmChain extends ChainContext<'Evm'> {
readonly chain: ChainName;
readonly platform: Platform<'Evm'>;
readonly conf: ChainConfig;

// Cached objects
private provider?: RpcConnection<'Evm'>;

constructor(platform: Platform<'Evm'>, chain: ChainName) {
super(platform, chain);

this.chain = chain;
this.conf = platform.conf[chain]!;
this.platform = platform;
}

getRpc(): RpcConnection<'Evm'> {
this.provider = this.provider
? this.provider
: this.platform.getRpc(this.chain);

return this.provider!;
}
readonly platform: Platform<'Evm'> = EvmPlatform;
}
2 changes: 1 addition & 1 deletion platforms/evm/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export module EvmPlatform {
}

export function getChain(chain: ChainName): EvmChain {
return new EvmChain(EvmPlatform, chain);
return new EvmChain(chain);
}

export function getWormholeCore(
Expand Down
15 changes: 2 additions & 13 deletions platforms/solana/src/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,10 @@ import {
Platform,
} from '@wormhole-foundation/connect-sdk';
import { getAssociatedTokenAddress } from '@solana/spl-token';
import { SolanaPlatform } from './platform';

export class SolanaChain extends ChainContext<'Solana'> {
// Cached objects
private connection?: RpcConnection<'Solana'>;

constructor(platform: Platform<'Solana'>, chain: ChainName) {
super(platform, chain);
}

getRpc(): RpcConnection<'Solana'> {
this.connection = this.connection
? this.connection
: this.platform.getRpc(this.chain);
return this.connection!;
}
readonly platform: Platform<'Solana'> = SolanaPlatform;

async getTokenAccount(
token: UniversalOrNative<'Solana'> | 'native',
Expand Down
2 changes: 1 addition & 1 deletion platforms/solana/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export module SolanaPlatform {
}

export function getChain(chain: ChainName): ChainContext<'Solana'> {
return new SolanaChain(SolanaPlatform, chain);
return new SolanaChain(chain);
}

export async function getDecimals(
Expand Down

0 comments on commit 2f40acf

Please sign in to comment.