-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathhelpers.ts
101 lines (90 loc) · 3.14 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import {
ChainAddress,
ChainContext,
DEFAULT_TASK_TIMEOUT,
Network,
Platform,
PlatformToChains,
Signer,
TokenTransfer,
TransferState,
TxHash,
Wormhole,
api,
tasks,
} from "@wormhole-foundation/connect-sdk";
// Importing from src so we dont have to rebuild to see debug stuff in signer
import { getAlgorandSigner } from "@wormhole-foundation/connect-sdk-algorand/src/testing";
import { getCosmwasmSigner } from "@wormhole-foundation/connect-sdk-cosmwasm/src/testing";
import { getEvmSigner } from "@wormhole-foundation/connect-sdk-evm/src/testing";
import { getSolanaSignAndSendSigner } from "@wormhole-foundation/connect-sdk-solana/src/testing";
// 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
// Read in from `.env`
require("dotenv").config();
function getEnv(key: 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) throw new Error(`Missing env var ${key}, did you forget to set valies in '.env'?`);
return val;
}
export interface TransferStuff<
N extends Network,
P extends Platform,
C extends PlatformToChains<P> = PlatformToChains<P>,
> {
chain: ChainContext<N, P, C>;
signer: Signer<N, C>;
address: ChainAddress<C>;
}
export async function getStuff<
N extends Network,
P extends Platform,
C extends PlatformToChains<P>,
>(chain: ChainContext<N, P, C>): Promise<TransferStuff<N, P, C>> {
let signer: Signer;
const platform = chain.platform.utils()._platform;
switch (platform) {
case "Solana":
signer = await getSolanaSignAndSendSigner(await chain.getRpc(), getEnv("SOL_PRIVATE_KEY"));
break;
case "Cosmwasm":
signer = await getCosmwasmSigner(await chain.getRpc(), getEnv("COSMOS_MNEMONIC"));
break;
case "Evm":
signer = await getEvmSigner(await chain.getRpc(), getEnv("ETH_PRIVATE_KEY"));
break;
case "Algorand":
signer = await getAlgorandSigner(await chain.getRpc(), getEnv("ALGORAND_MNEMONIC"));
break;
default:
throw new Error("Unrecognized platform: " + platform);
}
return {
chain,
signer: signer as Signer<N, C>,
address: Wormhole.chainAddress(chain.chain, signer.address()),
};
}
export async function waitLog<N extends Network = Network>(
wh: Wormhole<N>,
xfer: TokenTransfer<N>,
tag: string = "WaitLog",
timeout: number = DEFAULT_TASK_TIMEOUT,
) {
const tracker = TokenTransfer.track(wh, TokenTransfer.getReceipt(xfer), timeout);
let receipt;
for await (receipt of tracker) {
console.log(`${tag}: Current trasfer state: `, TransferState[receipt.state]);
}
return receipt;
}
// Note: This API may change but it is currently the best place to pull
// the relay status from
export async function waitForRelay(txid: TxHash): Promise<api.RelayData | null> {
const relayerApi = "https://relayer.dev.stable.io";
const task = () => api.getRelayStatus(relayerApi, txid);
return tasks.retry<api.RelayData>(task, 5000, 60 * 1000, "Wormhole:GetRelayStatus");
}