Skip to content

Commit

Permalink
adding overrides for configs
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Dec 28, 2023
1 parent 25d633b commit bcb91a1
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
33 changes: 33 additions & 0 deletions connect/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ export type WormholeConfig<N extends Network = Network, P extends Platform = Pla
chains: ChainsConfig<N, P>;
};

type RecursivePartial<T> = {
[P in keyof T]?: T[P] extends (infer U)[]
? RecursivePartial<U>[]
: T[P] extends object | undefined
? RecursivePartial<T[P]>
: T[P];
};

export type ConfigOverrides = RecursivePartial<WormholeConfig>;

export const CONFIG = {
Mainnet: {
api: "https://api.wormholescan.io",
Expand Down Expand Up @@ -41,6 +51,29 @@ export function networkPlatformConfigs<N extends Network, P extends Platform>(
) as ChainsConfig<N, P>;
}

// Apply any overrides to the base config
export function applyOverrides<N extends Network>(
network: N,
overrides?: ConfigOverrides,
): WormholeConfig {
let base = CONFIG[network];
if (!overrides) return base;

// recurse through the overrides and apply them to the base config
function override(base: any, overrides: any) {
for (const [key, value] of Object.entries(overrides)) {
if (typeof value === "object" && !Array.isArray(value)) {
base[key] = override(base[key], value);
} else {
base[key] = value;
}
}
return base;
}

return override(base, overrides);
}

const inNode = typeof process !== "undefined";
export const DEFAULT_NETWORK: Network =
(inNode && (process.env["NETWORK"] as Network)) || "Testnet";
8 changes: 4 additions & 4 deletions connect/src/wormhole.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
Chain,
ChainToPlatform,
Network,
Platform,
PlatformToChains,
chainToPlatform,
circle,
ChainToPlatform,
} from "@wormhole-foundation/sdk-base";
import {
ChainAddress,
Expand All @@ -24,7 +24,7 @@ import {
toNative,
} from "@wormhole-foundation/sdk-definitions";
import { getCircleAttestationWithRetry } from "./circle-api";
import { CONFIG, DEFAULT_TASK_TIMEOUT, WormholeConfig } from "./config";
import { ConfigOverrides, DEFAULT_TASK_TIMEOUT, WormholeConfig, applyOverrides } from "./config";
import { CircleTransfer } from "./protocols/cctpTransfer";
import { TokenTransfer } from "./protocols/tokenTransfer";
import { retry } from "./tasks";
Expand All @@ -49,9 +49,9 @@ export class Wormhole<N extends Network> {

readonly config: WormholeConfig;

constructor(network: N, platforms: PlatformUtils<N, any>[], config?: WormholeConfig) {
constructor(network: N, platforms: PlatformUtils<N, any>[], config?: ConfigOverrides) {
this._network = network;
this.config = config ?? CONFIG[network];
this.config = applyOverrides(network, config);

this._chains = new Map();
this._platforms = new Map();
Expand Down
3 changes: 3 additions & 0 deletions core/base/src/constants/nativeChainIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,9 @@ export function platformNativeChainIdToNetworkChain<
const candidates = nativeChainIdToNetworkChain(
chainId as PlatformToNativeChainIds<P>,
) as readonly (readonly [Network, Chain])[];

console.log(platform, chainId, candidates);

const mustBeSingleton = candidates.filter(([_, chain]) => chainToPlatform(chain) === platform);
if (mustBeSingleton.length !== 1)
throw new Error(`Platform ${platform} has multiple chains with native chain id ${chainId}`);
Expand Down
19 changes: 19 additions & 0 deletions examples/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Wormhole } from "@wormhole-foundation/connect-sdk";
import { SolanaPlatform } from "@wormhole-foundation/connect-sdk-solana";

(async function () {
// Pass a partial WormholeConfig object to override specific
// fields in the default config
const wh = new Wormhole("Testnet", [SolanaPlatform], {
chains: {
Solana: {
contracts: {
coreBridge: "11111111111111111111111111111",
},
rpc: "https://api.devnet.solana.com",
},
},
});

console.log(wh.config.chains.Solana);
})();

0 comments on commit bcb91a1

Please sign in to comment.