-
-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathawait-dependencies.ts
More file actions
93 lines (76 loc) · 2.48 KB
/
Copy pathawait-dependencies.ts
File metadata and controls
93 lines (76 loc) · 2.48 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
87
88
89
90
91
92
93
import {
http as httpTransport,
createClient,
createWalletClient,
http,
createPublicClient,
} from 'viem';
import { nodeUrl, bundlerUrl, paymasterUrl, chain, deployPk } from './config';
import { privateKeyToAccount } from 'viem/accounts';
import { deployDeleGatorEnvironment } from '@metamask/delegation-toolkit/utils';
import { ENTRYPOINT_ADDRESS_V07 } from 'permissionless';
import { writeFile } from 'fs/promises';
const POLL_INTERVAL_MS = 1_000;
// timeout is 5 minutes, which is sufficiently long to never trigger a false positive
const TIMEOUT_MS = 5 * 60_000;
let hasTimedOut = false;
const waitFor = async (name: string, url: string) => {
let isAvailable: boolean | undefined = undefined;
console.log(`Waiting for ${name}, at ${url}`);
const transport = httpTransport(url);
const client = createClient({
transport,
});
do {
if (isAvailable !== undefined) {
// Only add a delay if it's not the first time
await new Promise((resolve) => setTimeout(resolve, POLL_INTERVAL_MS));
}
await client
.request({ method: 'web3_clientVersion' })
.then(() => (isAvailable = true))
// as soon as the node is responding (even if it's MethodNotFoundRpcError)
.catch((e) => (isAvailable = (e as Error).name !== 'HttpRequestError'));
} while (!isAvailable && !hasTimedOut);
if (isAvailable) {
console.log(`${name} is available`);
}
};
const deployEnvironment = async () => {
const gatorEnvironment = await deployDeleGatorEnvironment(
createWalletClient({
account: privateKeyToAccount(deployPk),
chain,
transport: http(nodeUrl),
}),
createPublicClient({
chain,
transport: http(nodeUrl),
}),
chain,
{
EntryPoint: ENTRYPOINT_ADDRESS_V07,
},
);
return gatorEnvironment;
};
(async () => {
const startTime = Date.now();
const timeoutRef = setTimeout(() => (hasTimedOut = true), TIMEOUT_MS);
await waitFor('Blockchain node', nodeUrl);
const waitingForDependencies = Promise.all([
waitFor('Bundler', bundlerUrl),
waitFor('Mock paymaster', paymasterUrl),
]);
const environment = await deployEnvironment();
await writeFile('./.gator-env.json', JSON.stringify(environment, null, 2));
await waitingForDependencies;
clearTimeout(timeoutRef);
if (hasTimedOut) {
console.error('Timed out waiting for dependencies');
process.exitCode = 1;
} else {
const duration = Date.now() - startTime;
console.log(`Dependencies ready in ${duration}ms`);
}
})();