-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers.ts
86 lines (76 loc) · 2.58 KB
/
helpers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
Chain,
ChainAddress,
ChainContext,
Network,
Signer,
Wormhole,
chainToPlatform,
} from "@wormhole-foundation/sdk";
import evm from "@wormhole-foundation/sdk/platforms/evm";
import solana from "@wormhole-foundation/sdk/platforms/solana";
import { NttContracts, DEVNET_SOL_PRIVATE_KEY, DEVNET_ETH_PRIVATE_KEY, TEST_NTT_TOKENS} from "./const";
import { NttRoute } from "@wormhole-foundation/sdk-route-ntt";
export interface SignerStuff<N extends Network, C extends Chain> {
chain: ChainContext<N, C>;
signer: Signer<N, C>;
address: ChainAddress<C>;
}
export async function getSigner<N extends Network, C extends Chain>(
chain: ChainContext<N, C>
): Promise<SignerStuff<N, C>> {
// Read in from `.env`
(await import("dotenv")).config();
let signer: Signer;
const platform = chainToPlatform(chain.chain);
switch (platform) {
case "Solana":
signer = await solana.getSigner(
await chain.getRpc(),
getEnv("OTHER_SOL_PRIVATE_KEY", DEVNET_SOL_PRIVATE_KEY),
{ debug: false }
);
break;
case "Evm":
signer = await evm.getSigner(
await chain.getRpc(),
getEnv("ETH_PRIVATE_KEY", DEVNET_ETH_PRIVATE_KEY)
);
break;
default:
throw new Error("Unrecognized platform: " + platform);
}
return {
chain,
signer: signer as Signer<N, C>,
address: Wormhole.chainAddress(chain.chain, signer.address()),
};
}
// Use .env.example as a template for your .env file and populate it with secrets
// for funded accounts on the relevant chain+network combos to run the example
function getEnv(key: string, dev?: string): string {
// If we're in the browser, return empty string
if (typeof process === undefined) return "";
// Otherwise, return the env var or error
const val = process.env[key];
if (!val) {
if (dev) return dev;
throw new Error(
`Missing env var ${key}, did you forget to set values in '.env'?`
);
}
return val;
}
// Reformat NTT contracts to fit TokenConfig for Route
function reformat(contracts: NttContracts) {
return Object.entries(TEST_NTT_TOKENS).map(([chain, contracts]) => {
const { token, manager, transceiver: xcvrs } = contracts!;
const transceiver = Object.entries(xcvrs).map(([k, v]) => {
return { type: k as NttRoute.TransceiverType, address: v };
});
return { chain: chain as Chain, token, manager, transceiver };
});
}
export const NttTokens = {
Test: reformat(TEST_NTT_TOKENS),
};