Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persist derived airnode wallet in state #93

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/pusher/src/api-requests/signed-api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { deriveBeaconId } from '@api3/airnode-node';
import { go } from '@api3/promise-utils';
import axios, { type AxiosError } from 'axios';
import { ethers } from 'ethers';
import { isEmpty, isNil, pick } from 'lodash';

import { logger } from '../logger';
Expand All @@ -11,13 +10,14 @@ import { type SignedApiPayload, signedApiResponseSchema } from '../validation/sc

export const postSignedApiData = async (group: SignedApiNameUpdateDelayGroup) => {
const {
config: { signedApis, airnodeWalletMnemonic },
config: { signedApis },
templateValues,
airnodeWallet,
} = getState();
const { signedApiName, templateIds, updateDelay } = group;
const logContext = { signedApiName, updateDelay };

const airnode = ethers.Wallet.fromMnemonic(airnodeWalletMnemonic).address;
const airnode = airnodeWallet.address;
const batchPayloadOrNull = templateIds.map((templateId): SignedApiPayload | null => {
// Calculate the reference timestamp based on the current time and update delay.
const referenceTimestamp = Date.now() / 1000 - updateDelay;
Expand Down
6 changes: 3 additions & 3 deletions packages/pusher/src/sign-template-data.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ExtractedAndEncodedResponse } from '@api3/airnode-adapter';
import { go } from '@api3/promise-utils';
import { ethers } from 'ethers';
import { isNil } from 'lodash';

import { logger } from './logger';
Expand All @@ -19,8 +18,9 @@ export const signTemplateResponses = async (templateResponses: TemplateResponse[
const { encodedValue } = response;
const timestamp = Math.floor(Date.now() / 1000).toString();

const wallet = ethers.Wallet.fromMnemonic(getState().config.airnodeWalletMnemonic);
const goSignWithTemplateId = await go(async () => signWithTemplateId(wallet, templateId, timestamp, encodedValue));
const goSignWithTemplateId = await go(async () =>
signWithTemplateId(getState().airnodeWallet, templateId, timestamp, encodedValue)
);
if (!goSignWithTemplateId.success) {
const message = `Failed to sign response. Error: "${goSignWithTemplateId.error.message}"`;
logger.warn(message, {
Expand Down
4 changes: 4 additions & 0 deletions packages/pusher/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Bottleneck from 'bottleneck';
import { ethers } from 'ethers';
import { last } from 'lodash';

import { OIS_MAX_CONCURRENCY_DEFAULT, OIS_MIN_TIME_DEFAULT_MS } from './constants';
Expand All @@ -11,6 +12,8 @@ export interface State {
config: Config;
templateValues: TemplateValueStorage;
apiLimiters: Record<string, Bottleneck | undefined>;
// We persist the derived Airnode wallet in memory as a performance optimization.
airnodeWallet: ethers.Wallet;
}

let state: State;
Expand Down Expand Up @@ -81,6 +84,7 @@ export const getInitialState = (config: Config): State => {
config,
templateValues: buildTemplateStorages(config),
apiLimiters: buildApiLimiters(config),
airnodeWallet: ethers.Wallet.fromMnemonic(config.airnodeWalletMnemonic),
};
};

Expand Down