Skip to content

Commit

Permalink
Merge pull request #3668 from aura-nw/hotfix/usdt
Browse files Browse the repository at this point in the history
[dev] fix read error usdt contract
  • Loading branch information
TranTrungTien authored Aug 12, 2024
2 parents 703f0b9 + a95171c commit 298bce1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
31 changes: 18 additions & 13 deletions src/app/core/data-services/api-cw20-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { CW20_TOKENS_TEMPLATE, ERC20_TOKENS_TEMPLATE } from './template';
import { getEthersProvider } from '../utils/ethers';
import { ERC20_ABI } from 'src/app/pages/account/account-detail/token-table/ABI/erc20-abi';
import { Contract } from 'ethers';
import { isContract } from '../utils/ethers/utils';

export interface IAsset {
name: string;
Expand Down Expand Up @@ -120,20 +121,24 @@ export class ApiCw20TokenService {
if (!address) {
return null;
}
const contract = this.createContract();
const balance = await contract.balanceOf(address);
const name = await contract.name();
const symbol = await contract.symbol();
const decimals = await contract.decimals();

return {
...USDC_TOKEN,
tokenUrl: USDC_ADDRESS,
name: name?.toString(),
symbol: symbol?.toString(),
balance: Number(balance?.toString()),
decimals: Number(decimals?.toString()),
};
if (await isContract(USDC_ADDRESS, this.env.etherJsonRpc)) {
const contract = this.createContract();
const balance = await contract.balanceOf(address);
const name = await contract.name();
const symbol = await contract.symbol();
const decimals = await contract.decimals();

return {
...USDC_TOKEN,
tokenUrl: USDC_ADDRESS,
name: name?.toString(),
symbol: symbol?.toString(),
balance: Number(balance?.toString()),
decimals: Number(decimals?.toString()),
};
}
return null;
}

parseUSDCToken(token, coinsMarkets) {
Expand Down
35 changes: 23 additions & 12 deletions src/app/core/utils/ethers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getEthersProvider } from './ethers';

export function getMetamask() {
return (window as any).ethereum;
}
Expand All @@ -16,6 +18,16 @@ export async function checkNetwork(targetNetworkId: string) {
return false;
}

export async function isContract(address: string, rpc: string): Promise<boolean> {
try {
const provider = getEthersProvider(rpc);
const result = await provider.getCode(address);
return result !== '0x';
} catch (error) {
return false;
}
}

export async function addNetwork(chain) {
const metamask = getMetamask();

Expand All @@ -42,35 +54,34 @@ export async function addNetwork(chain) {
}

export const hex2a = (hex: string) => {
if(!hex && typeof hex != 'string') return "";
if (!hex && typeof hex != 'string') return '';
const data = hex.toString();
let str = '';
for (let i = 0; i < data.length; i += 2)
str += String.fromCharCode(parseInt(data.substr(i, 2), 16));
for (let i = 0; i < data.length; i += 2) str += String.fromCharCode(parseInt(data.substr(i, 2), 16));
return str;
}
};

export const getValueOfKeyInObject = (obj: object, key: string) => {
let result = null;
if(obj instanceof Array) {
for(const element of obj) {
if (obj instanceof Array) {
for (const element of obj) {
result = getValueOfKeyInObject(element, key);
if (result) {
break;
}
}
}
} else {
for(let prop in obj) {
if(prop === key) {
for (let prop in obj) {
if (prop === key) {
return obj[key] || {};
}
if(obj[prop] instanceof Object || obj[prop] instanceof Array) {
if (obj[prop] instanceof Object || obj[prop] instanceof Array) {
result = getValueOfKeyInObject(obj[prop], key);
if (result) {
break;
}
}
}
}
}
return result;
}
};

0 comments on commit 298bce1

Please sign in to comment.