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
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ module.exports = {
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/no-dynamic-delete': 'off',

// Lodash
'lodash/prefer-immutable-method': 'off',

// Jest
'jest/no-hooks': 'off',
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"axios": "^1.5.1",
"dotenv": "^16.3.1",
"ethers": "^5.7.2",
"immer": "^10.0.3",
"lodash": "^4.17.21",
"zod": "^3.22.3"
}
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

185 changes: 39 additions & 146 deletions src/gas-price/gas-price.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ethers } from 'ethers';

import { init } from '../../test/fixtures/mock-config';
import { getState, setState } from '../state';
import { getState, updateState } from '../state';

import {
getAirseekerRecommendedGasPrice,
Expand Down Expand Up @@ -53,16 +53,9 @@ describe('gas price', () => {
beforeEach(() => {
initializeGasStore(chainId, providerName);
// Reset the gasPriceStore
const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
updateState((draft) => {
draft.gasPriceStore[chainId] = { [providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} } };
return draft;
vponline marked this conversation as resolved.
Show resolved Hide resolved
});
});

Expand All @@ -76,19 +69,9 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: [oldGasPriceMock, ...state.gasPriceStore[chainId]![providerName]!.gasPrices],
},
},
},
updateState((draft) => {
draft.gasPriceStore[chainId]![providerName]!.gasPrices.unshift(oldGasPriceMock);
return draft;
});
clearExpiredStoreGasPrices(chainId, providerName, gasSettings.sanitizationSamplingWindow);
setStoreGasPrices(chainId, providerName, gasPriceMock);
Expand All @@ -103,16 +86,9 @@ describe('gas price', () => {
beforeEach(() => {
initializeGasStore(chainId, providerName);
// Reset the gasPriceStore
const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
updateState((draft) => {
draft.gasPriceStore[chainId] = { [providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} } };
return draft;
});
});

Expand All @@ -134,16 +110,9 @@ describe('gas price', () => {
beforeEach(() => {
initializeGasStore(chainId, providerName);
// Reset the gasPriceStore
const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
updateState((draft) => {
draft.gasPriceStore[chainId] = { [providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} } };
return draft;
});
});

Expand Down Expand Up @@ -171,19 +140,9 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: [oldGasPriceMock, ...state.gasPriceStore[chainId]![providerName]!.gasPrices],
},
},
},
updateState((draft) => {
draft.gasPriceStore[chainId]![providerName]!.gasPrices.unshift(oldGasPriceMock);
return draft;
});
clearExpiredStoreGasPrices(chainId, providerName, gasSettings.sanitizationSamplingWindow);
const gasPrice = await updateGasPriceStore(chainId, providerName, rpcUrl);
Expand Down Expand Up @@ -222,16 +181,9 @@ describe('gas price', () => {
beforeEach(() => {
initializeGasStore(chainId, providerName);
// Reset the gasPriceStore
const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
updateState((draft) => {
draft.gasPriceStore[chainId] = { [providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} } };
return draft;
});
});

Expand All @@ -245,19 +197,9 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: [oldGasPriceMock, ...state.gasPriceStore[chainId]![providerName]!.gasPrices],
},
},
},
updateState((draft) => {
draft.gasPriceStore[chainId]![providerName]!.gasPrices.unshift(oldGasPriceMock);
return draft;
});
await gasPriceCollector(chainId, providerName, rpcUrl, gasSettings.sanitizationSamplingWindow);

Expand All @@ -271,16 +213,9 @@ describe('gas price', () => {
beforeEach(() => {
initializeGasStore(chainId, providerName);
// Reset the gasPriceStore
const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} },
},
},
updateState((draft) => {
draft.gasPriceStore[chainId] = { [providerName]: { gasPrices: [], sponsorLastUpdateTimestampMs: {} } };
return draft;
});
});

Expand Down Expand Up @@ -314,19 +249,9 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: gasPricesMock,
},
},
},
updateState((draft) => {
draft.gasPriceStore[chainId]![providerName]!.gasPrices = gasPricesMock;
return draft;
});
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
Expand Down Expand Up @@ -356,19 +281,9 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: [oldGasPriceMock, ...state.gasPriceStore[chainId]![providerName]!.gasPrices],
},
},
},
updateState((draft) => {
draft.gasPriceStore[chainId]![providerName]!.gasPrices.unshift(oldGasPriceMock);
return draft;
});
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
Expand Down Expand Up @@ -396,19 +311,9 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: [oldGasPriceMock, ...state.gasPriceStore[chainId]![providerName]!.gasPrices],
},
},
},
updateState((draft) => {
draft.gasPriceStore[chainId]![providerName]!.gasPrices.unshift(oldGasPriceMock);
return draft;
});
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
Expand Down Expand Up @@ -436,23 +341,11 @@ describe('gas price', () => {
.spyOn(ethers.providers.StaticJsonRpcProvider.prototype, 'getGasPrice')
.mockResolvedValueOnce(ethers.BigNumber.from(gasPriceMock));

const state = getState();
setState({
...state,
gasPriceStore: {
...state.gasPriceStore,
[chainId]: {
...state.gasPriceStore[chainId],
[providerName]: {
...state.gasPriceStore[chainId]![providerName]!,
gasPrices: [oldGasPriceMock, ...state.gasPriceStore[chainId]![providerName]!.gasPrices],
sponsorLastUpdateTimestampMs: {
...state.gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs,
[sponsorWalletAddress]: timestampMock - gasSettings.scalingWindow * 60 * 1000 - 1,
},
},
},
},
updateState((draft) => {
draft.gasPriceStore[chainId]![providerName]!.gasPrices.unshift(oldGasPriceMock);
draft.gasPriceStore[chainId]![providerName]!.sponsorLastUpdateTimestampMs[sponsorWalletAddress] =
timestampMock - gasSettings.scalingWindow * 60 * 1000 - 1;
return draft;
});
const gasPrice = await getAirseekerRecommendedGasPrice(
chainId,
Expand Down
Loading