-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
86 lines (75 loc) · 2.49 KB
/
utils.ts
File metadata and controls
86 lines (75 loc) · 2.49 KB
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,
amount,
encoding,
} from "@wormhole-foundation/sdk";
import evm from "@wormhole-foundation/sdk/platforms/evm";
import solana from "@wormhole-foundation/sdk/platforms/solana";
// 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;
}
export interface SignerStuff<N extends Network, C extends Chain = Chain> {
chain: ChainContext<N, C>;
signer: Signer<N, C>;
address: ChainAddress<C>;
}
const DEVNET_SOL_PRIVATE_KEY = encoding.b58.encode(
new Uint8Array([
72,52, // rest of the private key
])
);
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 = chain.platform.utils()._platform;
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"), {
debug: true,
maxGasLimit: amount.units(amount.parse("0.01", 18)),
// overrides is a Partial<TransactionRequest>, so any fields can be overriden
//overrides: {
// maxFeePerGas: amount.units(amount.parse("1.5", 9)),
// maxPriorityFeePerGas: amount.units(amount.parse("0.1", 9)),
//},
});
break;
// case "Sui":
// signer = await sui.getSigner(await chain.getRpc(), getEnv("SUI_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()),
};
}