Skip to content

Commit

Permalink
feat: preConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
solidovic committed Feb 28, 2024
1 parent 328f842 commit a2ef6a0
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 30 deletions.
6 changes: 5 additions & 1 deletion config/csp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { AppProps } from 'next/app';
import { withSecureHeaders } from 'next-secure-headers';
import type { ContentSecurityPolicyOption } from 'next-secure-headers/lib/rules';

import { getConfig } from 'config';
// Not use absolute import here!
// code'''
// import { getConfig } from 'config';
// '''
import { getConfig } from '../get-config';
const {
cspTrustedHosts,
cspReportOnly,
Expand Down
31 changes: 4 additions & 27 deletions config/get-config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
import { AddressZero } from '@ethersproject/constants';

import getConfigNext from 'next/config';
const { serverRuntimeConfig, publicRuntimeConfig } = getConfigNext();

import { default as dynamics } from './dynamics';
import { getPreConfig } from './get-preconfig';
import * as cache from './groups/cache';
import * as estimate from './groups/estimate';
import * as ipfs from './groups/ipfs';
import * as locale from './groups/locale';
import * as stake from './groups/stake';
import * as withdrawalQueueEstimate from './groups/withdrawal-queue-estimate';

// TODO: type serverRuntimeConfig?
// TODO: type
// type ConfigType = { isClientSide: boolean, isServerSide: boolean }
// & typeof cache
// & typeof estimate
Expand All @@ -20,23 +15,12 @@ import * as withdrawalQueueEstimate from './groups/withdrawal-queue-estimate';
// & typeof rateLimit
// & typeof rateLimit
// & typeof stake
// & typeof withdrawalQueueEstimate
// & typeof dynamics;
// & typeof withdrawalQueueEstimate;

export const getConfig = (): any => {
const isClientSide = typeof window !== 'undefined';
const isServerSide = typeof window === 'undefined';

// TODO: another place
const STAKE_FALLBACK_REFERRAL_ADDRESS = dynamics.ipfsMode
? ipfs.IPFS_REFERRAL_ADDRESS
: AddressZero;

// TODO: another place
const BASE_PATH_ASSET = dynamics.ipfsMode
? '.'
: serverRuntimeConfig.basePath || publicRuntimeConfig.basePath;

return {
isClientSide,
isServerSide,
Expand All @@ -49,14 +33,7 @@ export const getConfig = (): any => {
...stake,
...withdrawalQueueEstimate,

STAKE_FALLBACK_REFERRAL_ADDRESS,
BASE_PATH_ASSET,

// highest priority
...publicRuntimeConfig,
...(typeof window !== 'undefined' ? window.__env__ : dynamics),
...serverRuntimeConfig,
...getPreConfig(),
};
};

// TODO getServerConfig for secret envs?
26 changes: 26 additions & 0 deletions config/get-preconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import getConfigNext from 'next/config';
import { default as dynamics } from './dynamics';

const { serverRuntimeConfig, publicRuntimeConfig } = getConfigNext();

// TODO: type
// type PreConfigType = typeof publicRuntimeConfig & typeof serverRuntimeConfig & typeof dynamics;

// getPreConfig() needs for external internal in 'config/groups/*'
// Not use getPreConfig() outside of 'config/groups/*'
export const getPreConfig = (): any => {
const BASE_PATH_ASSET = dynamics.ipfsMode
? '.'
: serverRuntimeConfig.basePath || publicRuntimeConfig.basePath;

return {
BASE_PATH_ASSET,

...publicRuntimeConfig,

...(typeof window !== 'undefined' ? window.__env__ : dynamics),

// TODO getServerConfig for secret envs?
...serverRuntimeConfig,
};
};
1 change: 1 addition & 0 deletions config/groups/estimate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parseEther } from '@ethersproject/units';

// account for gas estimation
// will always have >=0.001 ether, >=0.001 stETH, >=0.001 wstETH
// on Mainnet, Goerli, Holesky
Expand Down
12 changes: 12 additions & 0 deletions config/groups/stake.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { BigNumber } from 'ethers';
import { AddressZero } from '@ethersproject/constants';
import { parseEther } from '@ethersproject/units';

import { IPFS_REFERRAL_ADDRESS } from './ipfs';

// Not use getConfig() here!!!
// Use getPreConfig() only here!!!
import { getPreConfig } from '../get-preconfig';
const { ipfsMode } = getPreConfig();

export const PRECISION = 10 ** 6;

// how much to leave out on user balance when max is pressed
Expand All @@ -15,3 +23,7 @@ export const STAKE_GASLIMIT_FALLBACK = BigNumber.from(
STETH_SUBMIT_GAS_LIMIT_DEFAULT * SUBMIT_EXTRA_GAS_TRANSACTION_RATIO,
),
);

export const STAKE_FALLBACK_REFERRAL_ADDRESS = ipfsMode
? IPFS_REFERRAL_ADDRESS
: AddressZero;
1 change: 1 addition & 0 deletions config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from './get-config';
export * from './provider';
export * from './use-config';

// TODO: look likes this give more problems than benefits
export * from './csp/index';
export * from './rpc/index';
export * from './user-config/index';
4 changes: 4 additions & 0 deletions config/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { useSDK } from '@lido-sdk/react';

import { CHAINS } from 'consts/chains';

// Not use absolute import here!
// code'''
// import { getConfig } from 'config';
// '''
import { getConfig } from '../get-config';
const { ipfsMode } = getConfig();

Expand Down
9 changes: 7 additions & 2 deletions config/user-config/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// import { UserConfigDefaultType } from 'config/user-config/types'; // TODO: or better use 'config/user-config/types'?
import { getConfig, UserConfigDefaultType } from 'config';
// Not use absolute import here!
// code'''
// import { getConfig } from 'config';
// '''
import { getConfig } from '../get-config';
import { UserConfigDefaultType } from './types';

const {
defaultChain,
supportedChains,
Expand Down

0 comments on commit a2ef6a0

Please sign in to comment.