Skip to content

Commit

Permalink
chore(bex): Move constants / inits / defaults out of CreatePoolConten…
Browse files Browse the repository at this point in the history
…t and into utils/constants
  • Loading branch information
PaulMcInnis committed Dec 23, 2024
1 parent 12888ba commit e58f590
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 108 deletions.
2 changes: 1 addition & 1 deletion apps/hub/src/app/pools/components/parameters-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import { InputWithLabel } from "@bera/ui/input";
import { PoolType } from "@berachain-foundation/berancer-sdk";
import { Address } from "viem";

import { ParameterPreset } from "~/utils/constants";
import BeraTooltip from "~/components/bera-tooltip";
import { ParameterPreset } from "../create/CreatePageContent";
import { AmplificationInput } from "./amplification-parameter";
import { SwapFeeInput } from "./swap-fee";

Expand Down
126 changes: 19 additions & 107 deletions apps/hub/src/app/pools/create/CreatePageContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {
type Token,
type TokenInput as TokenInputType,
} from "@bera/berajs";
import { getTokenCurrentPrices } from "@bera/berajs/actions";
import {
balancerDelegatedOwnershipAddress,
balancerVaultAddress,
Expand Down Expand Up @@ -47,6 +46,24 @@ import {
import { Address, decodeEventLog, isAddress, zeroAddress } from "viem";
import { usePublicClient } from "wagmi";

import {
DEFAULT_ALGORITHMIC_AMPLIFICATION,
DEFAULT_AMPLIFICATION,
DEFAULT_LIQUIDITY,
DEFAULT_ORACLES,
DEFAULT_OWNER,
DEFAULT_OWNERSHIP_TYPE,
DEFAULT_PARAMETER_PRESET,
DEFAULT_POOL_TYPE,
DEFAULT_TOKENS,
DEFAULT_USD_BACKED_AMPLIFICATION,
DEFAULT_WEIGHTS,
LAST_FORM_STEP_NUM,
ParameterPreset,
emptyOracle,
emptyToken,
emptyTokenInput,
} from "~/utils/constants";
import { isBera, isBeratoken } from "~/utils/isBeraToken";
import { usePoolWeights } from "~/b-sdk/usePoolWeights";
import useMultipleTokenApprovalsWithSlippage from "~/hooks/useMultipleTokenApprovalsWithSlippage";
Expand All @@ -59,112 +76,6 @@ import PoolTypeSelector from "../components/pool-type-selector";
import ProcessSteps, { VerifiedSteps } from "../components/process-steps";
import { getPoolUrl } from "../fetchPools";

export enum ParameterPreset {
USDBACKED = "USD-Backed Stablecoin",
ALGORITHMIC = "Algorithmic Stablecoin",
}

/**
* Default pool amplification factor (responsiveness to price fluctuations) for USD-backed stablecoins.
* @constant {number}
*/
const DEFAULT_USD_BACKED_AMPLIFICATION = 2500;

/**
* Default pool amplification factor (responsiveness to price fluctuations) for algorithmic stablecoins.
* @constant {number}
*/
const DEFAULT_ALGORITHMIC_AMPLIFICATION = 200;

/**
* Default pool type for pools is a composable stable pool, which can be referred to as a 'stable pool'.
* @constant {PoolType}
*/
const DEFAULT_POOL_TYPE = PoolType.ComposableStable;

/**
* Default amplification factor for pools is a higher value for USD-backed stablecoin pools (max 5k).
* @constant {number}
*/
const DEFAULT_AMPLIFICATION = DEFAULT_USD_BACKED_AMPLIFICATION;

/**
* Default owner for pools is fixed type, which is the 0x0 address.
* @constant {Address}
*/
const DEFAULT_OWNER = ZERO_ADDRESS;

/**
* Default ownership type for pools is Fixed which yields the 0x0 owner address.
* @constant {OwnershipType}
*/
const DEFAULT_OWNERSHIP_TYPE = OwnershipType.Fixed;

/**
* Default weights for pools is an event split since we default to two tokens.
* @constant {bigint[]}
*/
const DEFAULT_WEIGHTS = [500000000000000000n, 500000000000000000n];

/**
* Default parameter preset for pools is USD-backed stablecoin preset.
* @constant {ParameterPreset}
*/
const DEFAULT_PARAMETER_PRESET = ParameterPreset.USDBACKED;

/**
* Last form step number.
* @constant {number}
*/
const LAST_FORM_STEP_NUM = 4; // NOTE: in the future we might consider making this more dynamic/strongly typed via enums.

const emptyTokenInput: TokenInputType = {
address: "" as `0x${string}`,
amount: "0",
decimals: 18,
exceeding: false,
name: "",
symbol: "",
};
const emptyToken: Token = {
address: "" as `0x${string}`,
decimals: 18,
name: "",
symbol: "",
};

/**
* Default tokens for pools is two empty tokens.
* NOTE: if in the future we streamline the token selection process, we might consider tying this closer to TokenInputs
* @constant {Token[]}
*/
const DEFAULT_TOKENS = [emptyToken, emptyToken];

/**
* Default liquidity for pools is two empty tokens.
* @constant {TokenInputType[]}
*/
const DEFAULT_LIQUIDITY = [emptyTokenInput, emptyTokenInput];

/**
* If a rate provider (oracle) is provided, this is the default update interval in seconds (using block.timestamp internally).
* @constant {number}
*/
const DEFAULT_ORACLE_CACHE_DURATION = 100;

const emptyOracle: Oracle = {
mode: OracleMode.None,
address: ZERO_ADDRESS,
tokenAddress: "",
cacheDuration: DEFAULT_ORACLE_CACHE_DURATION, // NOTE: even if we dont use an oracle, we pass a safe value for this to pool create
};

/**
* Default oracles for pools is two empty oracles.
* @constant {Oracle[]}
*/
const DEFAULT_ORACLES: Oracle[] = [emptyOracle, emptyOracle];

export default function CreatePageContent() {
const router = useRouter();
const { captureException, track } = useAnalytics();
Expand Down Expand Up @@ -254,6 +165,7 @@ export default function CreatePageContent() {
return null;
}

// @ts-ignore
return event.args.poolId ?? null;
} catch (error) {
console.error(error);
Expand Down
116 changes: 116 additions & 0 deletions apps/hub/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import {
Oracle,
OracleMode,
type Token,
type TokenInput as TokenInputType,
} from "@bera/berajs";
import { PoolType, ZERO_ADDRESS } from "@berachain-foundation/berancer-sdk";

import { OwnershipType } from "~/app/pools/components/parameters-input";

export enum LOCAL_STORAGE_KEYS {
CONNECTOR_ID = "CONNECTOR_ID",
SLIPPAGE_TOLERANCE = "SLIPPAGE_TOLERANCE",
Expand Down Expand Up @@ -194,3 +204,109 @@ export enum POLLING {
NORMAL = 20000,
SLOW = 200000,
}

export enum ParameterPreset {
USDBACKED = "USD-Backed Stablecoin",
ALGORITHMIC = "Algorithmic Stablecoin",
}

/**
* Default pool amplification factor (responsiveness to price fluctuations) for USD-backed stablecoins.
* @constant {number}
*/
export const DEFAULT_USD_BACKED_AMPLIFICATION = 2500;

/**
* Default pool amplification factor (responsiveness to price fluctuations) for algorithmic stablecoins.
* @constant {number}
*/
export const DEFAULT_ALGORITHMIC_AMPLIFICATION = 200;

/**
* Default pool type for pools is a composable stable pool, which can be referred to as a 'stable pool'.
* @constant {PoolType}
*/
export const DEFAULT_POOL_TYPE = PoolType.ComposableStable;

/**
* Default amplification factor for pools is a higher value for USD-backed stablecoin pools (max 5k).
* @constant {number}
*/
export const DEFAULT_AMPLIFICATION = DEFAULT_USD_BACKED_AMPLIFICATION;

/**
* Default owner for pools is fixed type, which is the 0x0 address.
* @constant {Address}
*/
export const DEFAULT_OWNER = ZERO_ADDRESS;

/**
* Default ownership type for pools is Fixed which yields the 0x0 owner address.
* @constant {OwnershipType}
*/
export const DEFAULT_OWNERSHIP_TYPE = OwnershipType.Fixed;

/**
* Default weights for pools is an event split since we default to two tokens.
* @constant {bigint[]}
*/
export const DEFAULT_WEIGHTS = [500000000000000000n, 500000000000000000n];

/**
* Default parameter preset for pools is USD-backed stablecoin preset.
* @constant {ParameterPreset}
*/
export const DEFAULT_PARAMETER_PRESET = ParameterPreset.USDBACKED;

/**
* Last form step number.
* @constant {number}
*/
export const LAST_FORM_STEP_NUM = 4; // NOTE: in the future we might consider making this more dynamic/strongly typed via enums.

export const emptyTokenInput: TokenInputType = {
address: "" as `0x${string}`,
amount: "0",
decimals: 18,
exceeding: false,
name: "",
symbol: "",
};
export const emptyToken: Token = {
address: "" as `0x${string}`,
decimals: 18,
name: "",
symbol: "",
};

/**
* Default tokens for pools is two empty tokens.
* NOTE: if in the future we streamline the token selection process, we might consider tying this closer to TokenInputs
* @constant {Token[]}
*/
export const DEFAULT_TOKENS = [emptyToken, emptyToken];

/**
* Default liquidity for pools is two empty tokens.
* @constant {TokenInputType[]}
*/
export const DEFAULT_LIQUIDITY = [emptyTokenInput, emptyTokenInput];

/**
* If a rate provider (oracle) is provided, this is the default update interval in seconds (using block.timestamp internally).
* @constant {number}
*/
export const DEFAULT_ORACLE_CACHE_DURATION = 100;

export const emptyOracle: Oracle = {
mode: OracleMode.None,
address: ZERO_ADDRESS,
tokenAddress: "",
cacheDuration: DEFAULT_ORACLE_CACHE_DURATION, // NOTE: even if we dont use an oracle, we pass a safe value for this to pool create
};

/**
* Default oracles for pools is two empty oracles.
* @constant {Oracle[]}
*/
export const DEFAULT_ORACLES: Oracle[] = [emptyOracle, emptyOracle];

0 comments on commit e58f590

Please sign in to comment.