Skip to content

Commit

Permalink
feat: refactor asset parsing and validation logic in OraidexCommon to…
Browse files Browse the repository at this point in the history
… ensure OraidexCommon is loaded before processing
  • Loading branch information
trungbach committed Nov 29, 2024
1 parent 80ee846 commit c9d89eb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 50 deletions.
58 changes: 32 additions & 26 deletions packages/oraidex-common/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,24 @@ export const calculateMinReceive = (
).toString();
};

// export const parseAssetInfoFromContractAddrOrDenom = (addressOrDenomToken: string) => {
// if (!addressOrDenomToken) return null;
// const addressOrDenomLowerCase = addressOrDenomToken.toLowerCase();
// const tokenItem = cosmosTokens.find((cosmosToken) => {
// return !cosmosToken.contractAddress
// ? cosmosToken.denom.toLowerCase() === addressOrDenomLowerCase
// : cosmosToken.contractAddress.toLowerCase() === addressOrDenomLowerCase;
// });
// // @ts-ignore
// return tokenItem ? parseTokenInfo(tokenItem).info : null;
// };
export const checkOraidexCommonLoaded = () => {
if (!OraidexCommon.instance) throw new Error("OraidexCommon is not loaded");
};

export const parseAssetInfoFromContractAddrOrDenom = (addressOrDenomToken: string) => {
if (!addressOrDenomToken) return null;
const addressOrDenomLowerCase = addressOrDenomToken.toLowerCase();

checkOraidexCommonLoaded();

const tokenItem = OraidexCommon.instance.cosmosTokens.find((cosmosToken) => {
return !cosmosToken.contractAddress
? cosmosToken.denom.toLowerCase() === addressOrDenomLowerCase
: cosmosToken.contractAddress.toLowerCase() === addressOrDenomLowerCase;
});
// @ts-ignore
return tokenItem ? parseTokenInfo(tokenItem).info : null;
};

export const parseTokenInfo = (tokenInfo: TokenItemType, amount?: string): { fund?: Coin; info: AssetInfo } => {
if (!tokenInfo.contractAddress) {
Expand Down Expand Up @@ -382,16 +389,19 @@ export const calcMaxAmount = ({
return finalAmount;
};

// export const getTotalUsd = (amounts: AmountDetails, prices: CoinGeckoPrices<string>): number => {
// let usd = 0;
// for (const denom in amounts) {
// const tokenInfo = tokenMap[denom];
// if (!tokenInfo) continue;
// const amount = toDisplay(amounts[denom], tokenInfo.decimals);
// usd += amount * (prices[tokenInfo.coinGeckoId] ?? 0);
// }
// return usd;
// };
export const getTotalUsd = (amounts: AmountDetails, prices: CoinGeckoPrices<string>): number => {
let usd = 0;

checkOraidexCommonLoaded();
const tokenMap = OraidexCommon.instance.tokenMap;
for (const denom in amounts) {
const tokenInfo = tokenMap[denom];
if (!tokenInfo) continue;
const amount = toDisplay(amounts[denom], tokenInfo.decimals);
usd += amount * (prices[tokenInfo.coinGeckoId] ?? 0);
}
return usd;
};

// export const toSubDisplay = (amounts: AmountDetails, tokenInfo: TokenItemType): number => {
// const subAmounts = getSubAmountDetails(amounts, tokenInfo);
Expand Down Expand Up @@ -501,11 +511,7 @@ export const parseTxToMsgsAndEvents = (indexedTx: Tx, eventsParser?: (events: re
});
};

export const validateAndIdentifyCosmosAddress = async (
address: string,
network: string,
cosmosChains: CustomChainInfo[]
) => {
export const validateAndIdentifyCosmosAddress = (address: string, network: string, cosmosChains: CustomChainInfo[]) => {
try {
const cosmosAddressRegex = /^[a-z]{1,6}[0-9a-z]{0,64}$/;
if (!cosmosAddressRegex.test(address)) {
Expand Down
26 changes: 2 additions & 24 deletions packages/oraidex-common/src/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export class OraidexCommon {

get oraichainNetwork() {
return this.chainConfig.getSpecificChainInfo("Oraichain");
// TODO: update later
// bech32Config: defaultBech32Config("orai"),
// currencies: oraiCommon.chainInfos.getSpecificChainInfo("Oraichain").currencies.map((currency) => {
// const coingeckoId =
Expand All @@ -127,7 +128,7 @@ export class OraidexCommon {

get chainInfos() {
return this.chainConfig.chainInfos;

// TODO: update later
// .filter((chain) => supportedChainIds.includes(chain.chainId))
// .map((c) => {
// const updatedCurrencies = c.currencies.map((currency) => {
Expand Down Expand Up @@ -192,18 +193,6 @@ export class OraidexCommon {
return this.chainConfig.getSpecificChainInfo("celestia");
}

parseAssetInfoFromContractAddrOrDenom(addressOrDenomToken: string) {
if (!addressOrDenomToken) return null;
const addressOrDenomLowerCase = addressOrDenomToken.toLowerCase();
const tokenItem = this.cosmosTokens.find((cosmosToken) => {
return !cosmosToken.contractAddress
? cosmosToken.denom.toLowerCase() === addressOrDenomLowerCase
: cosmosToken.contractAddress.toLowerCase() === addressOrDenomLowerCase;
});
// @ts-ignore
return tokenItem ? parseTokenInfo(tokenItem).info : null;
}

getTokenOnOraichain(coingeckoId: CoinGeckoId, isNative?: boolean) {
const filterOraichainToken = this.oraichainTokens.filter((orai) => orai.coinGeckoId === coingeckoId);
if (!filterOraichainToken.length) return undefined;
Expand All @@ -213,17 +202,6 @@ export class OraidexCommon {
return oraichainToken;
}

getTotalUsd(amounts: AmountDetails, prices: CoinGeckoPrices<string>) {
let usd = 0;
for (const denom in amounts) {
const tokenInfo = this.tokenMap[denom];
if (!tokenInfo) continue;
const amount = toDisplay(amounts[denom], tokenInfo.decimals);
usd += amount * (prices[tokenInfo.coinGeckoId] ?? 0);
}
return usd;
}

toSumDisplay(amounts: AmountDetails) {
let amount = 0;

Expand Down

0 comments on commit c9d89eb

Please sign in to comment.