Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding overrides for configs #183

Merged
merged 2 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion connect/src/circle-api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import axios from "axios";
import { CIRCLE_RETRY_INTERVAL } from "./config";
import { retry } from "./tasks";

// Note: mostly ripped off from https://github.com/circlefin/cctp-sample-app/blob/master/src/services/attestationService.ts
export const CIRCLE_RETRY_INTERVAL = 2000;

export enum CircleAttestationStatus {
complete = "complete",
Expand Down
36 changes: 33 additions & 3 deletions connect/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,22 @@ import { buildConfig, ChainsConfig } from "@wormhole-foundation/sdk-definitions"

export const DEFAULT_TASK_TIMEOUT = 60 * 1000; // 1 minute in milliseconds

export const CIRCLE_RETRY_INTERVAL = 2000;
export const WHSCAN_RETRY_INTERVAL = 2000;

export type WormholeConfig<N extends Network = Network, P extends Platform = Platform> = {
api: string;
circleAPI: string;
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 +48,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";
7 changes: 3 additions & 4 deletions connect/src/whscan-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
deserialize,
} from "@wormhole-foundation/sdk-definitions";
import axios from "axios";
import { WHSCAN_RETRY_INTERVAL } from "./config";
import { retry } from "./tasks";

export const WHSCAN_RETRY_INTERVAL = 2000;

// TransactionStatus returned by wormholescan
export interface TransactionStatus {
id: string;
Expand Down Expand Up @@ -136,9 +137,7 @@ export async function getVaaBytes(
try {
const {
data: { vaaBytes },
} = await axios.get<{ vaaBytes: string }>(url, {
timeout: 2000,
});
} = await axios.get<{ vaaBytes: string }>(url);
return encoding.b64.decode(vaaBytes);
} catch (error) {
if (!error) return null;
Expand Down
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
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);
})();