From 724badde0c86b83b3b371ec12b08d2d5fdb0f3cd Mon Sep 17 00:00:00 2001 From: SDargarh Date: Mon, 22 Jul 2024 18:21:00 +0530 Subject: [PATCH] Added get balance method to fetch SOL balance of an account --- CHANGELOG.md | 3 ++- src/index.js | 17 ++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d16828..956ffa8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,4 +6,5 @@ - Added getAccounts() method to fetch list of generated accounts - Added importWallet() to import account using privateKey - Added initial test -- Added functionality to sign message \ No newline at end of file +- Added functionality to sign message +- Added get balance method to fetch SOL balance of an account \ No newline at end of file diff --git a/src/index.js b/src/index.js index 6d39fb5..88286ba 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,8 @@ -const ObservableStore = require("obs-store"); const bs58 = require("bs58"); -const helper = require("./helper"); const nacl = require('tweetnacl'); +const helper = require("./helper"); +const ObservableStore = require("obs-store"); +const solanaWeb3 = require('@solana/web3.js'); const { solana: { HD_PATH }, solana_connection: { MAINNET }} = require('./config') @@ -83,5 +84,15 @@ class KeyringController { } } +const getBalance = async (address, network) => { + try { + const _network = helper.getNetwork(network) + const connection = new solanaWeb3.Connection(_network, "confirmed") + const accInfo = await connection.getAccountInfo(new solanaWeb3.PublicKey(address), 'confirmed') + return { balance: accInfo ? accInfo.lamports : 0 } + } catch (err) { + throw err + } +} -module.exports = { KeyringController }; +module.exports = { KeyringController, getBalance };