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

Refactor code reported by Sonarqube #299

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 14 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
22 changes: 22 additions & 0 deletions .github/workflows/reusable-sonar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Reusable SonarQube

on:
workflow_dispatch:
pull_request:
push:
branches:
- "feat/ODEX-974"

jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
21 changes: 0 additions & 21 deletions .github/workflows/reuseable-sonar.yml

This file was deleted.

3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"typescript.tsdk": "node_modules/typescript/lib",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,32 @@ export default function TVChartContainer({
};
if (isMobile) widgetOptions.disabled_features.push(...disabledFeaturesOnMobile);

function handleChartReady() {
setChartReady(true);
tvWidgetRef.current!.applyOverrides({
"paneProperties.background": theme === "dark" ? DARK_BACKGROUND_CHART : LIGHT_BACKGROUND_CHART,
"paneProperties.backgroundType": "solid"
});

const activeChart = tvWidgetRef.current!.activeChart();

activeChart.onIntervalChanged().subscribe(null, (interval) => {
if (SUPPORTED_RESOLUTIONS[interval]) {
const period = SUPPORTED_RESOLUTIONS[interval];
setPeriod(period);
}
});

// create indicator
activeChart.createStudy("Volume");
}

const script = document.createElement("script");
script.async = true;
script.src = libraryUrl;
script.onload = () => {
tvWidgetRef.current = new window.TradingView.widget(widgetOptions as any as ChartingLibraryWidgetOptions);
tvWidgetRef.current.onChartReady(function () {
setChartReady(true);
tvWidgetRef.current!.applyOverrides({
"paneProperties.background": theme === "dark" ? DARK_BACKGROUND_CHART : LIGHT_BACKGROUND_CHART,
"paneProperties.backgroundType": "solid"
});

const activeChart = tvWidgetRef.current!.activeChart();
activeChart.onIntervalChanged().subscribe(null, (interval) => {
if (SUPPORTED_RESOLUTIONS[interval]) {
const period = SUPPORTED_RESOLUTIONS[interval];
setPeriod(period);
}
});

// create indicator
activeChart.createStudy("Volume");
});
tvWidgetRef.current.onChartReady(handleChartReady);
};

document.body.appendChild(script);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Props = {
dataProvider?: TVDataProvider;
currentPair: PairToken;
setChartDataLength: any;
pairsChart: PairToken[]
pairsChart: PairToken[];
};

export const EXCHANGE_NAME = "OraiDEX";
Expand Down Expand Up @@ -128,14 +128,6 @@ export default function useTVDatafeed({ dataProvider, currentPair, setChartDataL
onResetCacheNeededCallback: () => void
) {
console.log("[subscribeBars]: Method call with subscriberUID:", subscribeUID);
// subscribeOnStream(
// symbolInfo,
// resolution,
// onRealtimeCallback,
// subscribeUID,
// onResetCacheNeededCallback,
// lastBarsCache.get(symbolInfo.full_name)
// );
},
unsubscribeBars: () => {
if (intervalRef.current) clearInterval(intervalRef.current);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, expect, it } from "vitest";
import { Bar } from "./types";
import {
fillBarGaps,
Expand All @@ -7,7 +8,6 @@ import {
// parseChannelFromPair,
parseFullSymbol
} from "./utils";
import { afterAll, beforeAll, describe, expect, it } from "vitest";

describe("test-utils-func", () => {
it.each([
Expand Down Expand Up @@ -110,15 +110,4 @@ describe("test-utils-func", () => {
const result = parseFullSymbol(fullSymbol);
expect(result).toEqual(expectedResult);
});

// it.each([
// ["invalid-pair", undefined],
// ["orai-orai12hzjxfh77wl572gdzct2fxv2arxcwh6gykc7qh", "ORAI/USDT"]
// ])(
// "test-parseChannelFromPair-with-%s-should-return-correctly-channel-name",
// (pair: string, expectedResult: string) => {
// const result = parseChannelFromPair(pair);
// expect(result).toBe(expectedResult);
// }
// );
});
28 changes: 14 additions & 14 deletions packages/oraidex-common/src/helper.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { ExecuteInstruction, JsonObject, fromBinary, toBinary, wasmTypes } from "@cosmjs/cosmwasm-stargate";
import { fromAscii, toUtf8 } from "@cosmjs/encoding";
import { Coin, EncodeObject, Registry, decodeTxRaw } from "@cosmjs/proto-signing";
import { Event, Attribute } from "@cosmjs/tendermint-rpc/build/tendermint37";
import { defaultRegistryTypes as defaultStargateTypes, logs } from "@cosmjs/stargate";
import { Attribute, Event } from "@cosmjs/tendermint-rpc/build/tendermint37";
import { AssetInfo, Uint128 } from "@oraichain/oraidex-contracts-sdk";
import { TokenInfoResponse } from "@oraichain/oraidex-contracts-sdk/build/OraiswapToken.types";
import bech32 from "bech32";
import { TextProposal } from "cosmjs-types/cosmos/gov/v1beta1/gov";
import { Tx as CosmosTx } from "cosmjs-types/cosmos/tx/v1beta1/tx";
import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
import { ethers } from "ethers";
import Long from "long";
import { BigDecimal } from "./bigdecimal";
import {
AVERAGE_COSMOS_GAS_PRICE,
CW20_DECIMALS,
GAS_ESTIMATION_BRIDGE_DEFAULT,
MULTIPLIER,
WRAP_BNB_CONTRACT,
WRAP_ETH_CONTRACT,
atomic,
truncDecimals,
GAS_ESTIMATION_BRIDGE_DEFAULT,
MULTIPLIER,
CW20_DECIMALS
truncDecimals
} from "./constant";
import { CoinGeckoId, NetworkChainId, cosmosChains } from "./network";
import {
AmountDetails,
CoinGeckoPrices,
TokenInfo,
TokenItemType,
cosmosTokens,
flattenTokens,
oraichainTokens,
CoinGeckoPrices,
tokenMap
} from "./token";
import { StargateMsg, Tx } from "./tx";
import { BigDecimal } from "./bigdecimal";
import { TextProposal } from "cosmjs-types/cosmos/gov/v1beta1/gov";
import { defaultRegistryTypes as defaultStargateTypes, IndexedTx, logs, StargateClient } from "@cosmjs/stargate";

export const getEvmAddress = (bech32Address: string) => {
if (!bech32Address) throw new Error("bech32 address is empty");
Expand Down Expand Up @@ -160,7 +160,7 @@ export const buildMultipleExecuteMessages = (
...preMessages: ExecuteInstruction[]
): ExecuteInstruction[] => {
try {
var messages: ExecuteInstruction[] = mainMsg ? mainMsg : [];
const messages: ExecuteInstruction[] = mainMsg ?? [];
messages.unshift(...preMessages.flat(1));
return messages;
} catch (error) {
Expand Down Expand Up @@ -287,11 +287,11 @@ export const parseTxToMsgExecuteContractMsgs = (tx: Tx): MsgExecuteContract[] =>
const cosmosTx = CosmosTx.decode(tx.tx);
if (!cosmosTx.body) return [];
const msgs: MsgExecuteContract[] = [];
for (let i = 0; i < cosmosTx.body.messages.length; i++) {
const msg = cosmosTx.body.messages[i];

for (const msg of cosmosTx.body.messages) {
if (msg.typeUrl === "/cosmwasm.wasm.v1.MsgExecuteContract") {
const msgExecuteContract = MsgExecuteContract.decode(msg.value);
// TODO: this is an assumption that the log order is the same as the message order.
// this is an assumption that the log order is the same as the message order.
msgs.push({ ...msgExecuteContract });
}
}
Expand Down Expand Up @@ -448,7 +448,7 @@ export function parseAssetInfoOnlyDenom(info: AssetInfo): string {
}

export const decodeProto = (value: JsonObject) => {
if (!value) throw "value is not defined";
if (!value) throw new Error("value is not defined");

const typeUrl = value.type_url || value.typeUrl;
if (typeUrl) {
Expand Down
19 changes: 9 additions & 10 deletions packages/oraidex-common/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Bech32Config, ChainInfo, Currency, FeeCurrency } from "@keplr-wallet/types";
import { listOsmosisToken } from "./alpha-network";
import {
AIRI_BSC_CONTRACT,
AIRI_CONTRACT,
ATOM_ORAICHAIN_DENOM,
BTC_CONTRACT,
CONVERTER_CONTRACT,
FACTORY_CONTRACT,
FACTORY_V2_CONTRACT,
Expand All @@ -20,10 +22,15 @@ import {
MILKY_ERC_CONTRACT,
MILKY_SUB_NETWORK_DENOM,
MULTICALL_CONTRACT,
NEUTARO_ORAICHAIN_DENOM,
OCH_CONTRACT,
OCH_ETH_CONTRACT,
ORACLE_CONTRACT,
ORAIDEX_BID_POOL_CONTRACT,
ORAIDEX_LISTING_CONTRACT,
ORAIIBC_INJECTIVE_DENOM,
ORAIX_CONTRACT,
ORAIX_ETH_CONTRACT,
ORAI_BRIDGE_EVM_DENOM_PREFIX,
ORAI_BRIDGE_EVM_ETH_DENOM_PREFIX,
ORAI_BRIDGE_EVM_TRON_DENOM_PREFIX,
Expand All @@ -40,20 +47,13 @@ import {
USDC_ETH_CONTRACT,
USDT_BSC_CONTRACT,
USDT_CONTRACT,
USDT_ETH_CONTRACT,
USDT_TRON_CONTRACT,
WETH_CONTRACT,
WRAP_BNB_CONTRACT,
WRAP_ETH_CONTRACT,
WRAP_TRON_TRX_CONTRACT,
USDT_ETH_CONTRACT,
BTC_CONTRACT,
NEUTARO_ORAICHAIN_DENOM,
OCH_ETH_CONTRACT,
OCH_CONTRACT,
ORAIDEX_BID_POOL_CONTRACT,
ORAIX_ETH_CONTRACT
WRAP_TRON_TRX_CONTRACT
} from "./constant";
import { listOsmosisToken } from "./alpha-network";

export type NetworkName =
| "Oraichain"
Expand Down Expand Up @@ -99,7 +99,6 @@ export type CoinGeckoId =
| "kawaii-islands"
| "milky-token"
| "scorai"
| "oraidex"
| "usd-coin"
| "tron"
| "weth"
Expand Down
14 changes: 6 additions & 8 deletions packages/oraidex-common/tests/helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Coin } from "@cosmjs/amino";
import { toBinary } from "@cosmjs/cosmwasm-stargate";
import { StargateClient } from "@cosmjs/stargate";
import { Event } from "@cosmjs/tendermint-rpc/build/tendermint37";
import { AssetInfo } from "@oraichain/oraidex-contracts-sdk";
import fs from "fs";
import path from "path";
import { fileURLToPath } from "url";
import { describe, expect, it } from "vitest";
import { AIRI_CONTRACT, AVERAGE_COSMOS_GAS_PRICE, BTC_CONTRACT, MILKYBSC_ORAICHAIN_DENOM, ORAI } from "../src/constant";
import {
calculateMinReceive,
Expand All @@ -20,6 +23,7 @@ import {
isEthAddress,
marshalEncodeObjsToStargateMsgs,
parseAssetInfo,
parseAssetInfoFromContractAddrOrDenom,
parseTokenInfo,
parseTokenInfoRawDenom,
parseTxToMsgsAndEvents,
Expand All @@ -33,16 +37,11 @@ import {
validateAndIdentifyCosmosAddress,
validateEvmAddress,
validateNumber,
validateTronAddress,
parseAssetInfoFromContractAddrOrDenom
validateTronAddress
} from "../src/helper";
import { CoinGeckoId, NetworkChainId } from "../src/network";
import { isFactoryV1 } from "../src/pairs";
import { AmountDetails, TokenItemType, cosmosTokens, flattenTokens, oraichainTokens } from "../src/token";
import fs from "fs";
import path from "path";
import { expect, afterAll, beforeAll, describe, it } from "vitest";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
console.log("__filename: ", __filename);
const __dirname = path.dirname(__filename);
Expand Down Expand Up @@ -393,7 +392,6 @@ describe("should helper functions in helper run exactly", () => {
expect(getCosmosGasPrice()).toEqual(AVERAGE_COSMOS_GAS_PRICE);
});

// TODO: add more tests for this func
it("test-parseTxToMsgsAndEvents", async () => {
// case 1: undefined input
const reuslt = parseTxToMsgsAndEvents(undefined as any);
Expand Down
Loading
Loading