Skip to content

Commit

Permalink
Merge branch 'main' into feat/add-chart-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
quangdz1704 committed Apr 15, 2024
2 parents f61deff + 8ff075e commit 6c6a97d
Show file tree
Hide file tree
Showing 16 changed files with 2,375 additions and 1,089 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ jobs:
npm install -g yarn
yarn
# - name: Run nx reset workspace
# run: |
# yarn nx reset
- name: Run nx reset workspace
run: |
yarn nx reset
- name: Run test
run: |
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
// jest.config.js
module.exports = {
transform: {
"^.+\\.ts?$": ["ts-jest", { isolatedModules: true }]
"^.+\\.ts?$": ["@swc/jest"]
},
testEnvironment: "node",
modulePathIgnorePatterns: ["<rootDir>/dist/", "<rootDir>/packages/ibc-routing"],
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@
"@oraichain/common-contracts-sdk": "1.0.31"
},
"devDependencies": {
"@babel/traverse": "7.24.1",
"@cosmjs/encoding": "0.31.3",
"@oraichain/cw-simulate": "^2.8.68",
"@swc/core": "^1.4.11",
"@swc/jest": "^0.2.36",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.0",
"@types/node": "^20.11.30",
Expand All @@ -55,8 +58,7 @@
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"typedoc": "^0.25.12",
"typescript": "5.3.2",
"@babel/traverse": "7.24.1"
"typescript": "5.3.2"
},
"version": "1.0.1"
}
11 changes: 7 additions & 4 deletions packages/oraidex-common-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
"react-use-websocket": "^4.5.0"
},
"devDependencies": {
"@babel/preset-env": "^7.24.4",
"@babel/preset-react": "^7.24.1",
"@babel/preset-typescript": "^7.24.1",
"@storybook/addon-essentials": "7.5.3",
"@storybook/addon-interactions": "7.5.3",
"@storybook/addon-links": "7.5.3",
"@storybook/react": "7.5.3",
"@storybook/react-webpack5": "^7.5.3",
"@types/react": "^18.2.2",
"css-minimizer-webpack-plugin": "^4.1.0",
"@storybook/react-webpack5": "7.5.3",
"@types/react": "18.2.2",
"css-minimizer-webpack-plugin": "6.0.0",
"mini-css-extract-plugin": "^2.8.1",
"storybook": "7.5.3",
"terser-webpack-plugin": "5.3.6",
"terser-webpack-plugin": "5.3.10",
"webpack": "5.89.0",
"webpack-cli": "^5.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/oraidex-common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@oraichain/oraidex-common",
"version": "1.0.81",
"version": "1.0.83",
"main": "build/index.js",
"files": [
"build/"
Expand Down
116 changes: 115 additions & 1 deletion packages/oraidex-common/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
MULTIPLIER,
CW20_DECIMALS
} from "./constant";
import { CoinGeckoId, NetworkChainId } from "./network";
import { CoinGeckoId, NetworkChainId, cosmosChains } from "./network";
import {
AmountDetails,
TokenInfo,
Expand All @@ -34,6 +34,7 @@ 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";
import TronWeb from "tronweb";

export const getEvmAddress = (bech32Address: string) => {
if (!bech32Address) throw new Error("bech32 address is empty");
Expand Down Expand Up @@ -489,3 +490,116 @@ export const parseTxToMsgsAndEvents = (indexedTx: Tx, eventsParser?: (events: re
return { attrs, message: messages[index] };
});
};

export const validateAndIdentifyCosmosAddress = (address: string, network: string) => {
try {
const cosmosAddressRegex = /^[a-z]{1,6}[0-9a-z]{0,64}$/;
if (!cosmosAddressRegex.test(address)) {
throw new Error("Invalid address");
}

const decodedAddress = bech32.decode(address);
const prefix = decodedAddress.prefix;

let chainInfo;
const networkMap = cosmosChains.reduce((acc, cur) => {
if (cur.chainId === network) chainInfo = cur;
return {
...acc,
[cur.bech32Config.bech32PrefixAccAddr]: true
};
}, {});

if (chainInfo && chainInfo.bech32Config.bech32PrefixAccAddr !== prefix) {
throw new Error("Network doesn't match");
}

if (networkMap.hasOwnProperty(prefix)) {
return {
isValid: true,
network
};
} else {
throw new Error("Unsupported address network");
}
} catch (error) {
console.log("error:", error);
return {
isValid: false,
error: error.message
};
}
};

export const validateEvmAddress = (address: string, network: string) => {
try {
const isEvm = ethers.utils.isAddress(address);

if (isEvm) {
return {
isValid: true,
network
};
}

return {
isValid: false
};
} catch (error) {
return {
isValid: false
};
}
};

export const validateTronAddress = (address: string, network: string) => {
try {
if (!/T[a-zA-Z0-9]{32}/.test(address)) {
throw new Error("Invalid tron address");
}

return {
isValid: true,
network
};

// const tronWeb = new TronWeb({
// fullHost: "https://api.trongrid.io"
// });

// tronWeb.trx.getAccount(address).then((isValid) => {
// if (isValid) {
// return {
// isValid: true,
// network: "0x2b6653dc" //"tron"
// };
// } else {
// console.error("Invalid address");

// return {
// isValid: false,
// network: "0x2b6653dc" //"tron"
// };
// }
// });
} catch (error) {
return {
isValid: false
};
}
};

export const checkValidateAddressWithNetwork = (address: string, network: NetworkChainId) => {
switch (network) {
case "0x01":
case "0x38":
return validateEvmAddress(address, network);

// tron
case "0x2b6653dc":
return validateTronAddress(address, network);

default:
return validateAndIdentifyCosmosAddress(address, network);
}
};
125 changes: 119 additions & 6 deletions packages/oraidex-common/tests/helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AIRI_CONTRACT, AVERAGE_COSMOS_GAS_PRICE, MILKYBSC_ORAICHAIN_DENOM, ORAI
import {
calculateMinReceive,
calculateTimeoutTimestamp,
checkValidateAddressWithNetwork,
decodeProto,
ethToTronAddress,
findToTokenOnOraiBridge,
Expand All @@ -29,11 +30,16 @@ import {
toDisplay,
toTokenInfo,
tronToEthAddress,
validateNumber
validateAndIdentifyCosmosAddress,
validateEvmAddress,
validateNumber,
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";

describe("should helper functions in helper run exactly", () => {
const amounts: AmountDetails = {
Expand Down Expand Up @@ -388,11 +394,10 @@ describe("should helper functions in helper run exactly", () => {
expect(reuslt).toEqual([]);

// case 2: real tx with multiple msgs and multiple contract calls
const client = await StargateClient.connect("wss://rpc.orai.io");
const indexedTx = await client.getTx("9B435E4014DEBA5AB80D4BB8F52D766A6C14BFCAC21F821CDB96F4ABB4E29B17");
client.disconnect();

const data = parseTxToMsgsAndEvents(indexedTx!);
// got data from tx hash 9B435E4014DEBA5AB80D4BB8F52D766A6C14BFCAC21F821CDB96F4ABB4E29B17 Oraichain.
const rawLog = fs.readFileSync(path.join(__dirname, "indexed-tx-raw-log.json")).toString();
const tx = Buffer.from(fs.readFileSync(path.join(__dirname, "indexed-tx-tx.json")).toString(), "base64");
const data = parseTxToMsgsAndEvents({ rawLog, tx } as any);
expect(data.length).toEqual(2);
expect(data[0].message).toMatchObject({
sender: "orai16hv74w3eu3ek0muqpgp4fekhrqgpzl3hd3qeqk",
Expand Down Expand Up @@ -540,4 +545,112 @@ describe("should helper functions in helper run exactly", () => {
])("test-parseWasmEvents-with-case: %p", (_case, input, expectedOutput) => {
expect(parseWasmEvents(input)).toEqual(expectedOutput);
});

it.each<[string, NetworkChainId, { isValid: boolean; network?: string; error?: string }]>([
[
"0x1CE09E54A5d7432ecabf3b085BAda7920aeb7dab",
"0x01",
{
isValid: true,
network: "0x01"
}
],
[
"0x1CE09E54A5d7432ecabf3b085BAda7920aeb7dab",
"0x38",
{
isValid: true,
network: "0x38"
}
],
[
"TPF97BNTx2pyNayUhz6B88JSzfdz5SHDbm",
"0x2b6653dc",
{
isValid: true,
network: "0x2b6653dc"
}
],
[
"orai1hvr9d72r5um9lvt0rpkd4r75vrsqtw6yujhqs2",
"Oraichain",
{
isValid: true,
network: "Oraichain"
}
],
[
"osmo1hvr9d72r5um9lvt0rpkd4r75vrsqtw6y86jn8t",
"0x38",
{
isValid: false
}
],
[
"osmo1hvr9d72r5um9lvt0rpkd4r75vrsqtw6y86jn8t",
"Oraichain",
{
isValid: false,
error: "Network doesn't match"
}
],
[
"TPF97BNTx2pyNayUhz6B88JSzfdz5SHDbm",
"Oraichain",
{
isValid: false,
error: "Invalid address"
}
]
])("test-check-validate-address-wallet-with-network", (address, network, expected) => {
const check = checkValidateAddressWithNetwork(address, network);

expect(check).toEqual(expected);
});

it.each([
["0x1CE09E54A5d7432ecabf3b085BAda7920aeb7dab", "0x01", true],
["TEu6u8JLCFs6x1w5s8WosNqYqVx2JMC5hQ", "0x2b6653dc", false],
["TEu6u8JLCFs6x1w5s8WosNqYqVx2JMC5hQ", "0x01", false],
["0x1", "0x38", false],
["", "0x38", false]
])("test-validateEvmAddress", (value, network, expectation) => {
try {
const { isValid } = validateEvmAddress(value, network);
expect(isValid).toEqual(expectation);
} catch (error) {
expect(expectation).toEqual(false);
}
});

it.each([
["TEu6u8JLCFs6x1w5s8WosNqYqVx2JMC5hQ", "0x2b6653dc", true],
["0x1CE09E54A5d7432ecabf3b085BAda7920aeb7dab", "0x01", false],
["TEu6u8JLCFs6x1w5s8WosNqYqVx2JMC5hQ", "0x01", false],
["TE", "0x2b6653dc", false],
["", "0x2b6653dc", false]
])("test-validateTronAddress", (value, network, expectation) => {
try {
const { isValid } = validateTronAddress(value, network);
expect(isValid).toEqual(expectation);
} catch (error) {
expect(expectation).toEqual(false);
}
});

it.each([
["orai12zyu8w93h0q2lcnt50g3fn0w3yqnhy4fvawaqz", "Oraichain", true],
["orai1", "Oraichain", false],
["", "Oraichain", false],
["cosmos12zyu8w93h0q2lcnt50g3fn0w3yqnhy4flwc7p3", "cosmoshub-4", true],
["cosmos12", "cosmoshub-4", false],
["", "cosmoshub-4", false]
])("test-validateTronAddress", (value, network, expectation) => {
try {
const { isValid } = validateAndIdentifyCosmosAddress(value, network);
expect(isValid).toEqual(expectation);
} catch (error) {
expect(expectation).toEqual(false);
}
});
});
Loading

0 comments on commit 6c6a97d

Please sign in to comment.