Skip to content

Commit

Permalink
Persist derived airnode wallet in state (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Siegrift authored Oct 24, 2023
1 parent 0b6edd4 commit 38ac42e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
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

0 comments on commit 38ac42e

Please sign in to comment.