HANDS ON
Monitor node balance

Monitor node balance with a script

Bundlr's upfront funding simplifies the uploading process by allowing you to pre-fund your uploads. This eliminates the need for individual funding calls for each upload as the loaded balance is automatically deducted from with each upload. To keep track of your loaded balance, users commonly create scripts that periodically check their balance and provide notifications when it approaches zero.

This recipe offers examples of how to monitor your balance using JavaScript, our Command Line Interface (CLI). These techniques are all equally effective options for balance monitoring, choose the method that aligns with your unique architecture and requirements.

JavaScript

import Bundlr from "@bundlr-network/client";
 
const checkBalance = async () => {
	// Your EVM or Solana private key
	const privateKey = "";
 
	// Initailze the bundlr SDK
	// Funding is node-specefic, connect to the node you funded
	const bundlr = new Bundlr("http://devnet.bundlr.network", "matic", privateKey, {
		providerUrl: "https://rpc-mumbai.maticvigil.com",
	});
	// Get loaded balance in atomic units
	const atomicBalance = await bundlr.getLoadedBalance();
	// Convert balance to standard units
	const convertedBalance = bundlr.utils.fromAtomic(atomicBalance);
	return convertedBalance;
};
 
const checkAndPrintBalance = async () => {
	const balance = await checkBalance();
	const threshold = 0.1; // 10% threshold
 
	if (Math.abs(balance) <= threshold) {
		console.log(`Balance ${balance} is within 10% of 0, please fund.`);
	} else {
		console.log(`Balance ${balance} funding not yet needed.`);
	}
};
 
// Call the function immediately
checkAndPrintBalance();
 
// Then repeat every 30 minutes
setInterval(checkAndPrintBalance, 30 * 60 * 1000);

CLI

You can achieve the same thing using our CLI and a bash script. The advantage to doing it this way is you’re not connecting to a Bundlr node, which means you don’t need to provide your private key. Using your wallet’s public address, script calls to our CLI’s [bundlr balance](/developer-docs/cli/getting-node-balance) command, parse the output and test if it’s close to 0.

#!/bin/bash
 
# Define your variables
address="0xaC568a981B1370B2e1bAA8cE30BD5AC9E28C572D" # Public wallet address
provider_url="https://rpc-mumbai.maticvigil.com"
node_address="https://devnet.bundlr.network"
currency="matic"
balance_output="";
 
# Check if node_address contains "devnet"
# When using devnet, you must also include a provider-url
if [[ $node_address == *"devnet"* ]]; then
   balance_output=$(bundlr balance $address --provider-url $provider_url -h $node_address -c $currency)
else
   balance_output=$(bundlr balance $address -h $node_address -c $currency)
fi
 
# Use regex to parse the output and assign the parsed value to a variable
parsed_balance=$(echo $balance_output | awk -F'[()]' '{split($2,a," "); print a[1]}')
 # Define your threshold
threshold=0.1
 
# Check if parsed_balance is within threshold of 0
is_close_to_zero=$(echo "$parsed_balance < $threshold" | bc -l)
 
if [ $is_close_to_zero -eq 1 ]; then
   echo "Balance ${parsed_balance} is within $(echo "$threshold*100" | bc -l)% of 0, please fund."
else
   echo "Balance ${parsed_balance} funding not yet needed"
fi

To run a bash script periodically, use cron. Assuming you saved the above script as a file checkBalance.sh, open up your crontab file using crontab -e and then add an entry to call the script periodically. To call it every 30 minutes, you’d add the following:

*/30 * * * * /path/to/your/script/checkBalance.sh

cURL

A third option is to make cURL request using an URL with the following format https://<node-address>/account/balance/<currency>?address=<address>. For example to check the Devnet MATIC balance of the wallet with address 0xaC568a981B1370B2e1bAA8cE30BD5AC9E28C572D, you would use the command https://devnet.bundlr.network/account/balance/matic?address=0xaC568a981B1370B2e1bAA8cE30BD5AC9E28C572D

Just as with our CLI, you don’t need to to provide your private key. This version is also more streamlined than the CLI version as you don’t need to include the provider URL when checking the Devnet balances.

#!/bin/bash
 
# Define your variables
address="0xaC568a981B1370B2e1bAA8cE30BD5AC9E28C572D" # Public wallet address
node_address="https://devnet.bundlr.network"
currency="matic"
balance_output="";
 
# Create the API endpoint URL
balance_check_url="${node_address}/account/balance/${currency}?address=${address}"
 
# Make the cURL request and capture the response
balance_output=$(curl -s "$balance_check_url")
 
# Parse the balance from the response using awk
parsed_balance=$(echo "$balance_output" | awk -F'"' '{print $4}')
 
# Define the decimal factor for conversion (this works for MATIC and others with 18 decimals)
decimal_factor=1000000000000000000
 
# For Solana currencies with 9 decimals, use
# decimal_factor=1000000000
 
# Convert parsed_balance to standard units
balance_in_standard_units=$(awk -v parsed_balance="$parsed_balance" -v decimal_factor="$decimal_factor" 'BEGIN{printf "%.18f", parsed_balance/decimal_factor}')
 
# Define your threshold in standard units
threshold=0.1
 
# Check if balance_in_standard_units is within threshold of 0
is_close_to_zero=$(awk -v balance="$balance_in_standard_units" -v threshold="$threshold" 'BEGIN{if(balance < threshold) print 1; else print 0}')
 
if [ $is_close_to_zero -eq 1 ]; then
   echo "Balance ${balance_in_standard_units} is within $(echo "$threshold*100" | bc -l)% of 0, please fund."
else
   echo "Balance ${balance_in_standard_units} funding not yet needed"
fi

To run a bash script periodically, use cron. Assuming you saved the above script as a file checkBalance.sh, open up your crontab file using crontab -e and then add an entry to call the script periodically. To call it every 30 minutes, you’d add the following:

*/30 * * * * /path/to/your/script/checkBalance.sh