-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhandler.ts
54 lines (45 loc) · 2.2 KB
/
handler.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
import { LogLevel, LogOptions, logger, setLogOptions } from '@api3/airnode-utilities';
import { Context, ScheduledEvent, ScheduledHandler } from 'aws-lambda';
import { ethers } from 'ethers';
import * as references from '../deployments/references.json';
import { loadConfig } from './config';
import { MerkleFunder__factory } from './contracts';
import { fundChainRecipients } from './merkle-funder';
const getMerkleFunderContract = (funderMnemonic: string, providerUrl: string, chainId: string) => {
// Read MerkleFunder address from deployments/references.json
const merkleFunderAddress = (references.MerkleFunder as Record<string, string>)[chainId];
if (!merkleFunderAddress) {
throw new Error(`No MerkleFunder address found for chain ID: ${chainId}`);
}
// Connect to the network and get the signer
const provider = new ethers.providers.JsonRpcProvider(providerUrl);
const signer = ethers.Wallet.fromMnemonic(funderMnemonic).connect(provider);
// Return the merkleFunder contract
return MerkleFunder__factory.connect(merkleFunderAddress, signer);
};
export const run: ScheduledHandler = async (_event: ScheduledEvent, _context: Context): Promise<void> => {
const consoleTimeLabel = 'Scheduled task finished running. Time elapsed';
console.time(consoleTimeLabel);
const config = loadConfig();
const baseLogOptions: LogOptions = {
format: 'plain',
level: (process.env.LOG_LEVEL as LogLevel) || 'INFO',
};
setLogOptions(baseLogOptions);
const chainFundingResults = await Promise.allSettled(
Object.entries(config).flatMap(([chainId, { providers, funderMnemonic, ...chainConfig }]) =>
Object.entries(providers).map(async ([providerName, provider]) => {
const logOptions = { ...baseLogOptions, meta: { 'CHAIN-ID': chainId, PROVIDER: providerName } };
const merkleFunderContract = getMerkleFunderContract(funderMnemonic, provider.url, chainId);
await fundChainRecipients(chainId, chainConfig, merkleFunderContract, logOptions);
})
)
);
chainFundingResults.forEach((result) => {
if (result.status === 'rejected') {
logger.error(result.reason.message);
logger.debug(result.reason.stack);
}
});
console.timeEnd(consoleTimeLabel);
};