Skip to content

Commit df64403

Browse files
authored
Merge pull request #1171 from oraidex/feat/connect-tron
Feat/connect tron
2 parents 3f3deff + b17c1c7 commit df64403

File tree

3 files changed

+25669
-18827
lines changed

3 files changed

+25669
-18827
lines changed

.yarnrc.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodeLinker: node-modules

src/helper/index.tsx

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,31 @@ export interface InfoError {
5050
}
5151

5252
export type DecimalLike = string | number | bigint | BigDecimal;
53-
export const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
54-
export const EVM_CHAIN_ID: NetworkChainId[] = evmChains.map((c) => c.chainId);
53+
export const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
54+
export const EVM_CHAIN_ID: NetworkChainId[] = evmChains.map(c => c.chainId);
5555
export const networks = chainInfosWithIcon.filter(
56-
(c) => c.chainId !== ChainIdEnum.OraiBridge && c.chainId !== ('oraibtc-mainnet-1' as any) && c.chainId !== '0x1ae6'
56+
c => c.chainId !== ChainIdEnum.OraiBridge && c.chainId !== ('oraibtc-mainnet-1' as any) && c.chainId !== '0x1ae6'
5757
);
5858
export const cosmosNetworks = chainInfos.filter(
59-
(c) =>
60-
c.networkType === 'cosmos' && c.chainId !== ChainIdEnum.OraiBridge && c.chainId !== ('oraibtc-mainnet-1' as any)
59+
c => c.networkType === 'cosmos' && c.chainId !== ChainIdEnum.OraiBridge && c.chainId !== ('oraibtc-mainnet-1' as any)
6160
);
6261

63-
export const bitcoinNetworks = chainInfos.filter((c) => c.chainId === bitcoinChainId);
62+
export const bitcoinNetworks = chainInfos.filter(c => c.chainId === bitcoinChainId);
6463
export const cosmosNetworksWithIcon = chainInfos.filter(
65-
(c) =>
66-
c.networkType === 'cosmos' && c.chainId !== ChainIdEnum.OraiBridge && c.chainId !== ('oraibtc-mainnet-1' as any)
64+
c => c.networkType === 'cosmos' && c.chainId !== ChainIdEnum.OraiBridge && c.chainId !== ('oraibtc-mainnet-1' as any)
6765
);
6866

69-
export const evmNetworksWithoutTron = chainInfos.filter((c) => c.networkType === 'evm' && c.chainId !== '0x2b6653dc');
67+
export const evmNetworksWithoutTron = chainInfos.filter(c => c.networkType === 'evm' && c.chainId !== '0x2b6653dc');
7068
export const evmNetworksIconWithoutTron = chainInfosWithIcon.filter(
71-
(c) => c.networkType === 'evm' && c.chainId !== '0x2b6653dc'
69+
c => c.networkType === 'evm' && c.chainId !== '0x2b6653dc'
7270
);
7371

7472
// export const bitcoinNetworks = chainInfos.filter((c) => c.chainId === ChainIdEnum.Bitcoin);
75-
export const tronNetworks = chainInfos.filter((c) => c.chainId === '0x2b6653dc');
76-
export const tronNetworksWithIcon = chainInfosWithIcon.filter((c) => c.chainId === '0x2b6653dc');
77-
export const btcNetworksWithIcon = chainInfosWithIcon.filter((c) => c.chainId === bitcoinChainId);
78-
export const solanaNetworksWithIcon = chainInfosWithIcon.filter((c) => c.chainId === solChainId);
79-
export const tonNetworksWithIcon = chainInfosWithIcon.filter((c) => c.chainId === TonChainId);
73+
export const tronNetworks = chainInfos.filter(c => c.chainId === '0x2b6653dc');
74+
export const tronNetworksWithIcon = chainInfosWithIcon.filter(c => c.chainId === '0x2b6653dc');
75+
export const btcNetworksWithIcon = chainInfosWithIcon.filter(c => c.chainId === bitcoinChainId);
76+
export const solanaNetworksWithIcon = chainInfosWithIcon.filter(c => c.chainId === solChainId);
77+
export const tonNetworksWithIcon = chainInfosWithIcon.filter(c => c.chainId === TonChainId);
8078

8179
export const filterChainBridge = (token: Tokens | TokenItemType, item: CustomChainInfo) => {
8280
const tokenCanBridgeTo = token.bridgeTo ?? ['Oraichain'];
@@ -95,7 +93,7 @@ export const getDenomEvm = (): EvmDenom => {
9593
};
9694

9795
export const getSpecialCoingecko = (fromCoingecko: string, toCoingecko: string) => {
98-
const isSpecialCoingecko = (coinGeckoId) =>
96+
const isSpecialCoingecko = coinGeckoId =>
9997
['kawaii-islands', 'milky-token', 'injective-protocol'].includes(coinGeckoId);
10098
const isSpecialFromCoingecko = isSpecialCoingecko(fromCoingecko);
10199
const isSpecialToCoingecko = isSpecialCoingecko(toCoingecko);
@@ -138,7 +136,7 @@ export const getAccountUrl = (account: string) => {
138136
export const getNetworkGasPrice = async (chainId): Promise<number> => {
139137
try {
140138
const chainInfosWithoutEndpoints = await window.Keplr?.getChainInfosWithoutEndpoints(chainId);
141-
const findToken = chainInfosWithoutEndpoints.find((e) => e.chainId === chainId);
139+
const findToken = chainInfosWithoutEndpoints.find(e => e.chainId === chainId);
142140
if (findToken) {
143141
return findToken.feeCurrencies[0].gasPriceStep.average;
144142
}
@@ -186,11 +184,11 @@ export const handleCheckWallet = async () => {
186184
}
187185
};
188186

189-
export const handleCheckChainEvmWallet = async (fromChainId) => {
187+
export const handleCheckChainEvmWallet = async fromChainId => {
190188
const supportedChainIds = ['0x01', '0x38'];
191189

192190
if (supportedChainIds.includes(fromChainId)) {
193-
const fromChainInfo = evmChainInfos.find((evm) => Number(evm.chainId) === Number(fromChainId));
191+
const fromChainInfo = evmChainInfos.find(evm => Number(evm.chainId) === Number(fromChainId));
194192
if (fromChainInfo) {
195193
try {
196194
await window.ethereumDapp.request({
@@ -242,7 +240,7 @@ const transferMsgError = (message: string, info?: InfoError) => {
242240
return `Your previous transaction has not been included in a block. Please wait until it is included before creating a new transaction!`;
243241

244242
const network = info?.chainName
245-
? [...evmChains, ...cosmosChains].find((evm) => evm.chainId === info.chainName)?.chainName
243+
? [...evmChains, ...cosmosChains].find(evm => evm.chainId === info.chainName)?.chainName
246244
: '';
247245
if (message.includes('Insufficient funds to redeem voucher') || message.includes('checking balance channel ibc'))
248246
return `Insufficient ${info?.tokenName ?? ''} liquidity on ${network} Bridge`;
@@ -371,7 +369,11 @@ export const switchWalletTron = async (walletType: WalletType) => {
371369
} else {
372370
const { code, message = 'Tronlink is not ready' } = res;
373371
if (code !== 200) throw new Error(message);
374-
tronAddress = window.tronWeb?.defaultAddress?.base58;
372+
if (res?.base58) {
373+
tronAddress = res.base58;
374+
} else {
375+
tronAddress = window.tronWeb?.defaultAddress?.base58;
376+
}
375377
}
376378

377379
if (!tronAddress) throw new Error(res?.message ?? 'Error get Tron address!');
@@ -396,10 +398,10 @@ export const isConnectSpecificNetwork = (status: string | null) => {
396398
export const getAddressTransferForEvm = async (walletByNetworks: WalletsByNetwork, network: CustomChainInfo) => {
397399
let address = '';
398400
if (network.chainId === EVM_CHAIN_ID_COMMON.TRON_CHAIN_ID) {
399-
if (window.tronLinkDapp?.isOwallet) {
400-
const accountTron: interfaceRequestTron = await window.tronLinkDapp.request({
401-
method: 'tron_requestAccounts'
402-
});
401+
const accountTron: interfaceRequestTron = await window.tronLinkDapp.request({
402+
method: 'tron_requestAccounts'
403+
});
404+
if (accountTron && accountTron.code === 200 && accountTron.base58) {
403405
address = accountTron.base58;
404406
} else {
405407
address = window?.tronWebDapp?.defaultAddress?.base58;
@@ -423,6 +425,7 @@ export const getAddressTransfer = async (network: CustomChainInfo, walletByNetwo
423425
toUserFriendlyAddress(window.Ton?.account?.address);
424426
// address = useTonAddress();
425427
} else if (network.networkType === 'evm' && isConnectSpecificNetwork(walletByNetworks.evm)) {
428+
//here
426429
address = await getAddressTransferForEvm(walletByNetworks, network);
427430
} else if (network.networkType == 'svm' && isConnectSpecificNetwork(walletByNetworks.solana)) {
428431
let provider = window?.solana;
@@ -490,7 +493,7 @@ export const getChainSupported = async () => {
490493
}
491494
});
492495
};
493-
export const getAddressBySnap = async (chainId) => {
496+
export const getAddressBySnap = async chainId => {
494497
await window.Keplr.suggestChain(chainId);
495498
const rs = await getChainSupported();
496499
if (rs?.[chainId]) {
@@ -516,7 +519,7 @@ type ChainInfoWithoutIcons = Omit<CustomChainInfo, 'currencies' | 'Icon' | 'Icon
516519
currencies: Array<Omit<CustomChainInfo['currencies'][number], 'Icon' | 'IconLight'>>;
517520
bech32Config: Bech32Config;
518521
};
519-
const checkErrorObj = (info) => {
522+
const checkErrorObj = info => {
520523
if (info?.Icon && info?.IconLight) {
521524
const { Icon, IconLight, ...data } = info;
522525
return data;
@@ -531,16 +534,16 @@ const checkErrorObj = (info) => {
531534
};
532535
export const chainInfoWithoutIcon = (): ChainInfoWithoutIcons[] => {
533536
let chainInfoData = [...chainInfos];
534-
return chainInfoData.map((info) => {
537+
return chainInfoData.map(info => {
535538
const infoWithoutIcon = checkErrorObj(info);
536539

537-
const currenciesWithoutIcons = info.currencies.map((currency) => {
540+
const currenciesWithoutIcons = info.currencies.map(currency => {
538541
const currencyWithoutIcons = checkErrorObj(currency);
539542
return currencyWithoutIcons;
540543
});
541544

542545
const stakeCurrencyyWithoutIcons = checkErrorObj(info.stakeCurrency);
543-
const feeCurrenciesWithoutIcons = info?.feeCurrencies?.map((feeCurrency) => {
546+
const feeCurrenciesWithoutIcons = info?.feeCurrencies?.map(feeCurrency => {
544547
const feeCurrencyyWithoutIcon = checkErrorObj(feeCurrency);
545548

546549
return feeCurrencyyWithoutIcon;
@@ -607,7 +610,7 @@ export interface GetIconInterface {
607610
export const minimize = (priceUsd: string) => {
608611
const regex = /^0\.0*(\d+)/;
609612
const match = priceUsd.match(regex);
610-
const getSubscript = (num) => String.fromCharCode(0x2080 + num);
613+
const getSubscript = num => String.fromCharCode(0x2080 + num);
611614

612615
if (match) {
613616
const leadingZeros = match[0].length - match[1].length - 2;
@@ -649,7 +652,7 @@ export function formatMoney(num) {
649652

650653
export const getIcon = ({ isLightTheme, type, chainId, coinGeckoId, width, height }: GetIconInterface) => {
651654
if (type === 'token') {
652-
const tokenIcon = flattenTokens.find((token) => token.coinGeckoId === coinGeckoId);
655+
const tokenIcon = flattenTokens.find(token => token.coinGeckoId === coinGeckoId);
653656
if (tokenIcon) {
654657
return isLightTheme ? (
655658
<img src={tokenIcon.iconLight} alt="" width={width} height={height} />
@@ -660,7 +663,7 @@ export const getIcon = ({ isLightTheme, type, chainId, coinGeckoId, width, heigh
660663

661664
return <DefaultIcon />;
662665
} else {
663-
const networkIcon = chainInfos.find((chain) => chain.chainId === chainId);
666+
const networkIcon = chainInfos.find(chain => chain.chainId === chainId);
664667
if (networkIcon) {
665668
return isLightTheme ? (
666669
<img src={networkIcon.chainSymbolImageUrl} alt="" width={width} height={height} />
@@ -680,7 +683,7 @@ export const getIconWallet = () => {
680683
export const getIconToken = ({ isLightTheme, denom, width = 30, height = 30 }) => {
681684
const storage = store.getState();
682685
const allOraichainTokens = storage.token.allOraichainTokens || [];
683-
const tokenIcon = allOraichainTokens.find((tokenWithIcon) =>
686+
const tokenIcon = allOraichainTokens.find(tokenWithIcon =>
684687
[tokenWithIcon.contractAddress, tokenWithIcon.denom].includes(denom)
685688
);
686689
if (tokenIcon) {
@@ -765,7 +768,7 @@ export const inspectTokenFromOraiCommonApi = async (denoms: string[]): Promise<T
765768
if (response.status !== 200) throw new Error('Failed to fetch token list: ' + response.statusText);
766769

767770
const inspectedTokens = await response.json();
768-
const tokenItemTypes = inspectedTokens.map((info) => onChainTokenToTokenItem(info));
771+
const tokenItemTypes = inspectedTokens.map(info => onChainTokenToTokenItem(info));
769772
return tokenItemTypes;
770773
} catch (error) {
771774
console.log('Error inspectTokenFromOraiCommonApi: ', error);

0 commit comments

Comments
 (0)