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

Immer state #59

Merged
merged 6 commits into from
Nov 2, 2023
Merged
Changes from 1 commit
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
Next Next commit
Use timestamp to check for pending txs
vponline committed Oct 30, 2023

Verified

This commit was signed with the committer’s verified signature.
serban300 Serban Iorga
commit 83e61279381d911ddd176b402c4c6b67d2f9d312
102 changes: 58 additions & 44 deletions src/gas-price/gas-price.test.ts
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@ import { getState, setState } from '../state';
import {
getAirseekerRecommendedGasPrice,
multiplyGasPrice,
setLastOnChainDatafeedValues,
setSponsorLastUpdateTimestampMs,
setStoreGasPrices,
updateGasPriceStore,
clearLastOnChainDatafeedValue,
clearSponsorLastUpdateTimestampMs,
clearExpiredStoreGasPrices,
initializeGasStore,
gasPriceCollector,
@@ -28,6 +28,7 @@ const gasSettings = {
};
const timestampMock = 1_696_930_907_351;
const gasPriceMock = ethers.utils.parseUnits('10', 'gwei');
const sponsorWalletAddress = '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266';

describe('gas price', () => {
beforeAll(() => {
@@ -59,7 +60,7 @@ describe('gas price', () => {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], lastOnChainDataFeedValues: {} },
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
});
@@ -109,7 +110,7 @@ describe('gas price', () => {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], lastOnChainDataFeedValues: {} },
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
});
@@ -140,7 +141,7 @@ describe('gas price', () => {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], lastOnChainDataFeedValues: {} },
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
});
@@ -194,34 +195,26 @@ describe('gas price', () => {
});
});

describe(setLastOnChainDatafeedValues.name, () => {
describe(setSponsorLastUpdateTimestampMs.name, () => {
it('sets last datafeed values details', () => {
const dataFeedId = '0x91be0acf2d58a15c7cf687edabe4e255fdb27fbb77eba2a52f3bb3b46c99ec04';
const dataFeedValue = {
value: ethers.BigNumber.from(1),
timestampMs: 1_697_546_898_352,
};
jest.spyOn(Date, 'now').mockReturnValue(timestampMock);
setLastOnChainDatafeedValues(chainId, providerName, dataFeedId, dataFeedValue);
setSponsorLastUpdateTimestampMs(chainId, providerName, sponsorWalletAddress);

expect(getState().gasPriceStore[chainId]![providerName]!.lastOnChainDataFeedValues[dataFeedId]).toStrictEqual(
dataFeedValue
);
expect(
getState().gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs[sponsorWalletAddress]
).toStrictEqual(timestampMock);
});
});

describe(clearLastOnChainDatafeedValue.name, () => {
describe(clearSponsorLastUpdateTimestampMs.name, () => {
it('clears last datafeed value details', () => {
const dataFeedId = '0x91be0acf2d58a15c7cf687edabe4e255fdb27fbb77eba2a52f3bb3b46c99ec04';
const dataFeedValue = {
value: ethers.BigNumber.from(1),
timestampMs: 1_697_546_898_352,
};
jest.spyOn(Date, 'now').mockReturnValue(timestampMock);
setLastOnChainDatafeedValues(chainId, providerName, dataFeedId, dataFeedValue);
clearLastOnChainDatafeedValue(chainId, providerName, dataFeedId);
setSponsorLastUpdateTimestampMs(chainId, providerName, sponsorWalletAddress);
clearSponsorLastUpdateTimestampMs(chainId, providerName, sponsorWalletAddress);

expect(getState().gasPriceStore[chainId]![providerName]!.lastOnChainDataFeedValues[dataFeedId]).toBeUndefined();
expect(
getState().gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs[sponsorWalletAddress]
).toBeUndefined();
});
});

@@ -236,7 +229,7 @@ describe('gas price', () => {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], lastOnChainDataFeedValues: {} },
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
});
@@ -285,7 +278,7 @@ describe('gas price', () => {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], lastOnChainDataFeedValues: {} },
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
});
@@ -297,7 +290,13 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const gasPrice = await getAirseekerRecommendedGasPrice(chainId, providerName, rpcUrl, gasSettings);
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
providerName,
rpcUrl,
gasSettings,
sponsorWalletAddress
);

expect(gasPrice).toStrictEqual(multiplyGasPrice(gasPriceMock, gasSettings.recommendedGasPriceMultiplier));
expect(getState().gasPriceStore[chainId]![providerName]!.gasPrices).toStrictEqual([
@@ -329,7 +328,13 @@ describe('gas price', () => {
},
},
});
const gasPrice = await getAirseekerRecommendedGasPrice(chainId, providerName, rpcUrl, gasSettings);
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
providerName,
rpcUrl,
gasSettings,
sponsorWalletAddress
);

expect(gasPrice).toStrictEqual(
multiplyGasPrice(ethers.utils.parseUnits('8', 'gwei'), gasSettings.recommendedGasPriceMultiplier)
@@ -365,7 +370,13 @@ describe('gas price', () => {
},
},
});
const gasPrice = await getAirseekerRecommendedGasPrice(chainId, providerName, rpcUrl, gasSettings);
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
providerName,
rpcUrl,
gasSettings,
sponsorWalletAddress
);

expect(gasPrice).toStrictEqual(multiplyGasPrice(gasPriceMock, gasSettings.recommendedGasPriceMultiplier));
expect(getState().gasPriceStore[chainId]![providerName]!.gasPrices).toStrictEqual([
@@ -399,7 +410,13 @@ describe('gas price', () => {
},
},
});
const gasPrice = await getAirseekerRecommendedGasPrice(chainId, providerName, rpcUrl, gasSettings);
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
providerName,
rpcUrl,
gasSettings,
sponsorWalletAddress
);

expect(gasPrice).toStrictEqual(multiplyGasPrice(oldGasPriceValueMock, gasSettings.recommendedGasPriceMultiplier));
expect(getState().gasPriceStore[chainId]![providerName]!.gasPrices).toStrictEqual([
@@ -408,7 +425,7 @@ describe('gas price', () => {
]);
});

it('applies scaling if past the scaling window and same nonce', async () => {
it('applies scaling if last update timestamp is past the scaling window', async () => {
const oldGasPriceValueMock = ethers.utils.parseUnits('5', 'gwei');
const oldGasPriceMock = {
price: oldGasPriceValueMock,
@@ -419,12 +436,6 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const dataFeedId = '0x91be0acf2d58a15c7cf687edabe4e255fdb27fbb77eba2a52f3bb3b46c99ec04';
const dataFeedValue = {
value: ethers.BigNumber.from(1),
// This sets the timestamp to be 1ms past the scalingWindow
timestampMs: timestampMock - gasSettings.scalingWindow * 60 * 1000 - 1,
};
const state = getState();
setState({
...state,
@@ -435,18 +446,21 @@ describe('gas price', () => {
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: [oldGasPriceMock, ...state.gasPriceStore[chainId]![providerName]!.gasPrices],
lastOnChainDataFeedValues: {
...state.gasPriceStore[chainId]![providerName]!.lastOnChainDataFeedValues,
[dataFeedId]: dataFeedValue,
sponsorLastUpdateTimestampMs: {
...state.gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs,
[sponsorWalletAddress]: timestampMock - gasSettings.scalingWindow * 60 * 1000 - 1,
},
},
},
},
});
const gasPrice = await getAirseekerRecommendedGasPrice(chainId, providerName, rpcUrl, gasSettings, {
dataFeedId,
newDataFeedValue: dataFeedValue,
});
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
providerName,
rpcUrl,
gasSettings,
sponsorWalletAddress
);

expect(gasPrice).toStrictEqual(multiplyGasPrice(gasPriceMock, gasSettings.maxScalingMultiplier));
expect(getState().gasPriceStore[chainId]![providerName]!.gasPrices).toStrictEqual([
59 changes: 27 additions & 32 deletions src/gas-price/gas-price.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers';

import type { GasSettings } from '../config/schema';
import { getState, setState, type DataFeedValue } from '../state';
import { getState, setState } from '../state';

export const initializeGasStore = (chainId: string, providerName: string) => {
const state = getState();
@@ -23,7 +23,7 @@ export const initializeGasStore = (chainId: string, providerName: string) => {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], lastOnChainDataFeedValues: {} },
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
});
@@ -87,16 +87,15 @@ export const clearExpiredStoreGasPrices = (
};

/**
* Saves last transaction details into the store.
* Saves a sponsor wallet's last update timestamp into the store.
* @param chainId
* @param providerName
* @param nonce
* @param sponsorWalletAddress
*/
export const setLastOnChainDatafeedValues = (
export const setSponsorLastUpdateTimestampMs = (
chainId: string,
providerName: string,
dataFeedId: string,
dataFeedValues: { value: ethers.BigNumber; timestampMs: number }
sponsorWalletAddress: string
) => {
initializeGasStore(chainId, providerName);
const state = getState();
@@ -108,9 +107,9 @@ export const setLastOnChainDatafeedValues = (
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
lastOnChainDataFeedValues: {
...state.gasPriceStore[chainId]![providerName]!.lastOnChainDataFeedValues,
[dataFeedId]: dataFeedValues,
sponsorLastUpdateTimestampMs: {
...state.gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs,
[sponsorWalletAddress]: Date.now(),
},
},
},
@@ -119,17 +118,21 @@ export const setLastOnChainDatafeedValues = (
};

/**
* Removes last transaction details from the store.
* Removes a sponsor wallet's last update timestamp from the store.
* @param chainId
* @param providerName
* @param nonce
* @param sponsorWalletAddress
*/
export const clearLastOnChainDatafeedValue = (chainId: string, providerName: string, dataFeedId: string) => {
export const clearSponsorLastUpdateTimestampMs = (
chainId: string,
providerName: string,
sponsorWalletAddress: string
) => {
const state = getState();

if (state.gasPriceStore[chainId]![providerName]!.lastOnChainDataFeedValues[dataFeedId]) {
const { [dataFeedId]: _value, ...lastOnChainDataFeedValues } =
state.gasPriceStore[chainId]![providerName]!.lastOnChainDataFeedValues;
if (state.gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs[sponsorWalletAddress]) {
const { [sponsorWalletAddress]: _value, ...sponsorLastUpdateTimestampMs } =
state.gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs;

setState({
...state,
@@ -139,7 +142,7 @@ export const clearLastOnChainDatafeedValue = (chainId: string, providerName: str
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
lastOnChainDataFeedValues,
sponsorLastUpdateTimestampMs,
},
},
},
@@ -221,18 +224,15 @@ export const gasPriceCollector = async (
* @param providerName
* @param rpcUrl
* @param gasSettings
* @param nonce
* @param sponsorWalletAddress
* @returns {ethers.BigNumber}
*/
export const getAirseekerRecommendedGasPrice = async (
chainId: string,
providerName: string,
rpcUrl: string,
gasSettings: GasSettings,
newDataFeedUpdateOnChainValues?: {
dataFeedId: string;
newDataFeedValue: DataFeedValue;
}
sponsorWalletAddress: string
): Promise<ethers.BigNumber> => {
const {
recommendedGasPriceMultiplier,
@@ -242,7 +242,7 @@ export const getAirseekerRecommendedGasPrice = async (
maxScalingMultiplier,
} = gasSettings;
const state = getState();
const { gasPrices, lastOnChainDataFeedValues } = state.gasPriceStore[chainId]![providerName]!;
const { gasPrices, sponsorLastUpdateTimestampMs } = state.gasPriceStore[chainId]![providerName]!;

// Get the configured percentile of historical gas prices before adding the new price
const percentileGasPrice = getPercentile(
@@ -252,19 +252,14 @@ export const getAirseekerRecommendedGasPrice = async (

const gasPrice = await updateGasPriceStore(chainId, providerName, rpcUrl);

const lastDataFeedValue =
newDataFeedUpdateOnChainValues && lastOnChainDataFeedValues[newDataFeedUpdateOnChainValues.dataFeedId];
const lastUpdateTimestampMs = sponsorLastUpdateTimestampMs[sponsorWalletAddress];

// Check if the next update is a retry of a pending transaction and if it has been pending longer than scalingWindow
if (
lastDataFeedValue &&
newDataFeedUpdateOnChainValues &&
lastDataFeedValue?.value === newDataFeedUpdateOnChainValues.newDataFeedValue.value &&
lastDataFeedValue?.timestampMs < Date.now() - scalingWindow * 60 * 1000
) {
if (lastUpdateTimestampMs && lastUpdateTimestampMs < Date.now() - scalingWindow * 60 * 1000) {
const multiplier = calculateScalingMultiplier(
recommendedGasPriceMultiplier,
maxScalingMultiplier,
(Date.now() - lastDataFeedValue.timestampMs) / (60 * 1000),
(Date.now() - lastUpdateTimestampMs) / (60 * 1000),
scalingWindow
);

6 changes: 1 addition & 5 deletions src/state/state.ts
Original file line number Diff line number Diff line change
@@ -2,13 +2,9 @@ import type { BigNumber } from 'ethers';

import type { Config } from '../config/schema';

export interface DataFeedValue {
value: BigNumber;
timestampMs: number;
}
interface GasState {
gasPrices: { price: BigNumber; timestampMs: number }[];
lastOnChainDataFeedValues: Record<string, DataFeedValue>;
sponsorLastUpdateTimestampMs: Record<string, number>;
}

export interface State {
Loading