diff --git a/controllers/wallet.controller.js b/controllers/wallet.controller.js index 26521003..40d8cfa8 100644 --- a/controllers/wallet.controller.js +++ b/controllers/wallet.controller.js @@ -307,7 +307,7 @@ exports.getBalanceByToken = async (req, res) => { const balanceFormatted = formatTokenBalance(balance, decimals); return responseHandler.makeResponseData(res, 200, 'success', balanceFormatted); } else { - const balance = await getNativeBalance(web3Instance, walletAddress); + const balance = await getNativeBalance(web3Instance, walletAddress, network); return responseHandler.makeResponseData(res, 200, 'success', balance); } } catch (err) { @@ -521,9 +521,9 @@ exports.checkWalletToken = async (req, res) => { const tronWeb = await webTronInstance() const networks = [ - { name: 'bep20', web3: Web3BEP20.eth, abi: Constants.bep20.abi }, + { name: 'bep20', web3: Web3BEP20.eth, abi: Constants.token.abi }, { name: 'erc20', web3: Web3ETH.eth, abi: Constants.token.abi }, - { name: 'polygon', web3: web3MATIC.eth, abi: Constants.bep20.abi }, + { name: 'polygon', web3: web3MATIC.eth, abi: Constants.token.abi }, { name: 'bttc', web3: web3BTT.eth, abi: Constants.token.abi }, // Add more EVM networks here if needed diff --git a/middleware/walletValidator.middleware.js b/middleware/walletValidator.middleware.js index 2ff8a010..e9f6cb4c 100644 --- a/middleware/walletValidator.middleware.js +++ b/middleware/walletValidator.middleware.js @@ -23,7 +23,7 @@ const validatePassword = () => Joi.string().min(8).required().custom((value) => // VALIDATION NETWORK -const validateNetworks = (validNetworks) => Joi.string().required().custom((value) => { +const validateNetworks = (validNetworks) => Joi.string().allow(null).required().custom((value) => { for(let i = 0; i < validNetworks.length ; i++) { if(validNetworks[i] === value.toString().toLowerCase()) { return value; diff --git a/web3/wallets.js b/web3/wallets.js index 4bf4c0b6..a029efd2 100644 --- a/web3/wallets.js +++ b/web3/wallets.js @@ -910,15 +910,28 @@ exports.formatTokenBalance = (balance, decimals) => { }; // Helper function to get native balance (ETH, TRX, etc.) and format it -exports.getNativeBalance = async (web3Instance, walletAddress) => { - const balanceWei = await web3Instance.eth.getBalance(walletAddress); - const balanceFormatted = web3Instance.utils.fromWei(balanceWei, 'ether'); - return balanceFormatted; +exports.getNativeBalance = async (web3Instance, walletAddress, network) => { + if(network === 'tron') { + const account = await web3Instance.trx.getAccount(walletAddress); + if (account && account.balance) { + const balanceFormatted = web3Instance.fromSun(account.balance); + return balanceFormatted; + } + return '0'; + } else { + const balanceWei = await web3Instance.eth.getBalance(walletAddress); + const balanceFormatted = web3Instance.utils.fromWei(balanceWei, 'ether'); + return balanceFormatted; + } + + + }; exports.getTronBalance = async (webTron, token, address, isTrx = false) => { try { if (isTrx) { + console.log({trx: webTron.trx}) let amount = await webTron.trx.getBalance(address) return amount.toString() }