Skip to content

Commit

Permalink
add api to get user balance by token (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
hichri-louay authored Aug 11, 2023
2 parents 55349f2 + 14b73fc commit 302347f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
31 changes: 31 additions & 0 deletions controllers/wallet.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const {
getAccountV2,
getChartVariation,
getGlobalCryptoMarket,
getWeb3Instance,
formatTokenBalance,
getNativeBalance
} = require('../web3/wallets')

const {
Expand Down Expand Up @@ -288,6 +291,34 @@ exports.globalCryptoMarketInfo = async (req, res) => {
return responseHandler.makeResponseData(res, 200, 'success', global)
}

exports.getBalanceByToken = async (req, res) => {
try {
const { network, walletAddress, smartContract, isNative } = req.body;

// Initialize the appropriate Web3 instance based on the network
const web3Instance = await getWeb3Instance(network);

if (!isNative) {
const contract = new web3Instance.eth.Contract(Constants.token.abi, smartContract);
const [balance, decimals] = await Promise.all([
contract.methods.balanceOf(walletAddress).call(),
contract.methods.decimals().call(),
]);
const balanceFormatted = formatTokenBalance(balance, decimals);
return responseHandler.makeResponseData(res, 200, 'success', balanceFormatted);
} else {
const balance = await getNativeBalance(web3Instance, walletAddress);
return responseHandler.makeResponseData(res, 200, 'success', balance);
}
} catch (err) {
return responseHandler.makeResponseError(
res,
500,
err.message ? err.message : err.error
);
}
};

exports.totalBalances = async (req, res) => {
try {
if (req.user.hasWallet == true) {
Expand Down
7 changes: 6 additions & 1 deletion routes/wallet.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const {
exportKeyStoreMobile,
cryptoPriceDetails,
globalCryptoMarketInfo,
getNftByAddress
getNftByAddress,
getBalanceByToken
} = require('../controllers/wallet.controller')
const {
verifyAuth,
Expand Down Expand Up @@ -814,4 +815,8 @@ router.post('/export-keystore-mobile', verifyAuth ,exportKeyStoreMobile)

router.get('/nfts/:address', checkEVMValidation,getNftByAddress)



router.post('/getBalance', verifyAuth, getBalanceByToken)

module.exports = router
30 changes: 30 additions & 0 deletions web3/wallets.js
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,36 @@ exports.multicall = async (tokens, addresses, network, web3) => {
}
}

// Helper function to get the Web3 instance based on the network
exports.getWeb3Instance = async (network) => {
switch (network) {
case 'bep20':
return await bep20Connexion();
case 'erc20':
return await erc20Connexion();
case 'polygon':
return await polygonConnexion();
case 'bttc':
return await bttConnexion();
case 'tron':
return await webTronInstance();
default:
throw new Error(`Invalid network: ${network}`);
}
};

// Helper function to format token balance with decimals
exports.formatTokenBalance = (balance, decimals) => {
return balance / 10 ** 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.getTronBalance = async (webTron, token, address, isTrx = false) => {
try {
if (isTrx) {
Expand Down

0 comments on commit 302347f

Please sign in to comment.