Skip to content

Commit

Permalink
Add utils
Browse files Browse the repository at this point in the history
  • Loading branch information
leonthuongto committed Feb 2, 2025
1 parent 9eaa0f0 commit 5e5844c
Show file tree
Hide file tree
Showing 93 changed files with 4,720 additions and 71 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
"formik": "^2.2.9",
"https-browserify": "^1.0.0",
"inter-ui": "^3.13.1",
"json-to-graphql-query": "^2.0.0",
"lightweight-charts": "^3.3.0",
"lodash.flatmap": "^4.5.0",
"lodash.keys": "^4.2.0",
Expand Down Expand Up @@ -181,6 +182,7 @@
"@sentry/cli": "^2.25.0",
"@sentry/react": "^7.93.0",
"@tanstack/react-query": "^5.51.23",
"@tomfrench/ens-avatar-resolver": "^0.1.2",
"async-wait-until": "^2.0.7",
"big.js": "^6.2.1",
"bignumber.js": "^9.0.1",
Expand All @@ -194,6 +196,7 @@
"react-cookie": "^4.1.1",
"react-csv": "^2.2.2",
"react-helmet": "^6.1.0",
"react-jazzicon": "^1.0.4",
"react-lazy-load-image-component": "^1.5.1",
"react-phone-input-2": "^2.15.0",
"react-select": "^5.2.2",
Expand Down
272 changes: 272 additions & 0 deletions src/constants/dexV2/queryKeys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
import { Network } from 'lib/config/types';
import { TransactionReceipt } from '@ethersproject/abstract-provider';

import { SubgraphGauge } from 'services/dexV2/gauges/types';
import { NativeAsset, TokenInfo } from 'types/TokenList';
import { GaugeShare } from 'hooks/dex-v2/queries/useUserGaugeSharesQuery';
import { TokenPrices } from 'hooks/dex-v2//queries/useTokenPricesQuery';
import { MerkleOrchardVersion } from 'services/claim/claim.service';
import { PoolFilterOptions } from 'types/pools';
export const POOLS_ROOT_KEY = 'pools';
export const BALANCES_ROOT_KEY = 'accountBalances';
export const CLAIMS_ROOT_KEY = 'claims';
export const QUERY_EXIT_ROOT_KEY = [POOLS_ROOT_KEY, 'query', 'exit'];
export const QUERY_JOIN_ROOT_KEY = [POOLS_ROOT_KEY, 'query', 'join'];

const QUERY_KEYS = {
Pools: {
All: (networkId: Network, filterOptions: PoolFilterOptions) => [
POOLS_ROOT_KEY,
'all',
{
networkId,
filterOptions,
},
],
User: (
networkId: Network,
account: string,
gaugeAddresses: string[]
) => [POOLS_ROOT_KEY, 'user', { networkId, account, gaugeAddresses }],
Current: (id: string) => [POOLS_ROOT_KEY, 'current', { id }],
APR: (networkId: Network, id: string) => [
POOLS_ROOT_KEY,
'apr',
{ networkId, id },
],
Snapshot: (networkId: Network, id: string) => [
POOLS_ROOT_KEY,
'snapshot',
{ networkId, id },
],
Activities: (networkId: Network, id: string) => [
POOLS_ROOT_KEY,
'activities',
'all',
{ networkId, id },
],
UserActivities: (
networkId: Network,
id: string,
account: string
) => [POOLS_ROOT_KEY, 'activities', 'user', { networkId, account, id }],
Swaps: (
networkId: Network,
id: string,
subgraphQuery: Record<string, any>
) => [POOLS_ROOT_KEY, 'swaps', { networkId, id, subgraphQuery }],
UserSwaps: (networkId: Network, id: string, account: string) => [
POOLS_ROOT_KEY,
'swaps',
'user',
{ networkId, account, id },
],
HistoricalPrices: (networkId: Network, id: string) => [
POOLS_ROOT_KEY,
'historicalPrices',
{ networkId, id },
],
Joins: {
QueryJoin: (amountsIn: unknown, isSingleAssetJoin: unknown) => [
...QUERY_JOIN_ROOT_KEY,
{
amountsIn,
isSingleAssetJoin,
},
],
},
Exits: {
QueryExit: (
account: string,
bptIn: unknown,
isSingleAssetExit: unknown,
singleAmountOut: unknown,
relayerSignature: string | undefined
) => [
...QUERY_EXIT_ROOT_KEY,
{
account,
bptIn,
isSingleAssetExit,
singleAmountOut,
relayerSignature,
},
],
SingleAssetMax: (
bptBalance: string,
isSingleAssetExit: unknown,
singleAmountOut: unknown
) => [
POOLS_ROOT_KEY,
'singleAssetMax',
{
bptBalance,
isSingleAssetExit,
singleAmountOut,
},
],
},
Gauges: (poolAddresses: string[] | undefined) => [
'pools',
'gauges',
{ poolAddresses },
],
},
Pool: {
Gauges: (poolAddress: string | undefined) => [
'pool',
'gauges',
{ poolAddress },
],
Decorated: (poolId: string | undefined) => [
'pool',
'decorated',
{ poolId },
],
},
User: {
Pool: {
StakedShares: (
userGaugeShares: GaugeShare[] | undefined,
account: string
) => ['user', 'pool', 'stakedShares', { userGaugeShares, account }],
},
Pools: (account: string) => ['user', 'pools', { account }],
Gauges: (account: string, poolAddress: string | undefined) => [
'user',
'gauges',
{ account, poolAddress },
],
Boosts: (
account: string,
userGaugeShares: undefined | GaugeShare[]
) => ['user', 'boosts', { account, userGaugeShares }],
},
TokenLists: {
All: (networkId: Network) => ['tokenLists', 'all', { networkId }],
},
Claims: {
All: (
networkId: Network,
account: string,
merkleOrchardVersion: MerkleOrchardVersion
) => [CLAIMS_ROOT_KEY, { networkId, account, merkleOrchardVersion }],
Protocol: (networkId: Network, account: string) => [
CLAIMS_ROOT_KEY,
'protocol',
{ networkId, account },
],
GaugePools: (poolIds: string[]) => [
CLAIMS_ROOT_KEY,
'gaugePools',
{ poolIds },
],
},
Tokens: {
PairPriceData: (
tokenInAddress: string,
tokenOutAddress: string,
activeTimespan: { option: string; value: number },
userNetworkId: number,
nativeAsset: NativeAsset,
wrappedNativeAsset: TokenInfo
) => [
'pairPriceData',
{
tokenInAddress,
tokenOutAddress,
activeTimespan,
userNetworkId,
nativeAsset,
wrappedNativeAsset,
},
],
Prices: (networkId: Network, pricesToInject: TokenPrices) => [
'tokens',
'prices',
{ networkId, pricesToInject },
],
AllPrices: ['tokens', 'prices'],
VeBAL: (networkId: Network, account: string) => [
'tokens',
'veBAL',
{ networkId, account },
],
},
Account: {
Balances: (
networkId: Network,
account: string,
tokens: string[]
) => ['account', 'balances', { networkId, account, tokens }],
Allowances: (
networkId: Network,
account: string,
contractAddresses: string[],
tokens: string[]
) => [
'account',
'allowances',
{ networkId, account, contractAddresses, tokens },
],
RelayerApprovals: (
networkId: Network,
account: string,
relayer: string
) => ['account', 'relayer', { networkId, account, relayer }],
Profile: (
networkId: Network,
account: string,
chainId: number | undefined
) => ['account', 'profile', { networkId, account, chainId }],
},
Gauges: {
All: {
Static: () => ['gauges', 'all', 'static'],
Onchain: (
gauges: SubgraphGauge[] | undefined,
account: string,
networkId: Network
) => ['gauges', 'all', 'onchain', { gauges, account, networkId }],
},
Expired: (gauges: string[] | undefined, networkId: Network) => [
'gauges',
'expired',
{ gauges, networkId },
],
VotingEscrowLocks: (lockedAmount?: string) => [
'votingEscrowLocks',
lockedAmount,
],
VotingEscrowLocksByNetworkId: (
networkId: Network,
account: string,
providedUser: string | undefined
) => ['votingEscrowLocksByNetworkId', { networkId, account, providedUser }],
OmniEscrowLocks: (networkId: Network, account: string) => [
'omniEscrowLocks',
{ account, networkId },
],
Voting: (account: string) => ['gauges', 'voting', { account }],
},
Transaction: {
ConfirmationDate: (receipt: TransactionReceipt) => [
'tx',
'confirmation',
'date',
{ receipt },
],
},
Locks: {
Historical: (networkId: Network, account: string) => [
'historical-locks',
{ networkId, account },
],
UserRank: (networkId: Network, account: string) => [
'user-rank',
{ networkId, account },
],
},
};

export default QUERY_KEYS;
22 changes: 22 additions & 0 deletions src/dependencies/EthersContract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Contract } from '@ethersproject/contracts';
import { handleDependencyError } from '.';

let _contract: typeof Contract | undefined;

/**
* initContract uses the real Ethers Contract instance by default but allows injecting Contract mocks from tests
*/
export function initEthersContract(
contractInstance: typeof Contract = Contract
) {
_contract = contractInstance;
}

export function getEthersContract() {
if (!_contract) {
handleDependencyError('Ethers Contract');
}
return _contract;
}

export type EthersContract = Contract;
21 changes: 21 additions & 0 deletions src/dependencies/Multicaller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// eslint-disable-next-line no-restricted-imports
import { Multicaller } from 'services/multicalls/multicaller';
import { handleDependencyError } from '.';

let _multicaller: typeof Multicaller | undefined;

/**
* initMulticall uses the real multicall instance by default but allows injecting multicall mocks from tests
*/
export function initMulticaller(
multicallerInstance: typeof Multicaller = Multicaller
) {
_multicaller = multicallerInstance;
}

export function getMulticaller() {
if (!_multicaller) {
handleDependencyError('Multicaller');
}
return _multicaller;
}
23 changes: 23 additions & 0 deletions src/dependencies/OldMulticaller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// eslint-disable-next-line no-restricted-imports
import { Multicaller } from 'lib/utils/balancer/contract';
import { handleDependencyError } from '.';

let _multicaller: typeof Multicaller | undefined;

/**
* initMulticall uses the real multicall instance by default but allows injecting multicall mocks from tests
*/
export function initOldMulticaller(
multicallerInstance: typeof Multicaller = Multicaller
) {
_multicaller = multicallerInstance;
}

export function getOldMulticaller() {
if (!_multicaller) {
handleDependencyError('Old Multicaller');
}
return _multicaller;
}

export const OldMulticaller = Multicaller;
26 changes: 26 additions & 0 deletions src/dependencies/PoolsFallbackRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// eslint-disable-next-line no-restricted-imports
import { PoolsFallbackRepository } from '@ixswap1/dex-v2-sdk';
import { handleDependencyError } from '.';

// Initializing this heavy dependency by default we avoid calling initPoolsFallbackRepository in initDependencies from main.ts
// so that we avoid including in in the initial bundle
// as it is only imported when used via getPoolsFallbackRepository
let _repository: typeof PoolsFallbackRepository = PoolsFallbackRepository;

/**
* Uses the real PoolsFallbackRepository instance by default but allows injecting PoolsFallbackRepository mocks from tests
*/
export function initPoolsFallbackRepository(
repository: typeof PoolsFallbackRepository = PoolsFallbackRepository
) {
_repository = repository;
}

export type PoolsFallBackFactoryConstructor = PoolsFallbackRepository;

export function getPoolsFallbackRepository() {
if (!_repository) {
handleDependencyError('PoolsFallbackRepository');
}
return _repository;
}
Loading

0 comments on commit 5e5844c

Please sign in to comment.