How to handle non-existent currency balances in Numscript
Last updated: February 21, 2025
When using Numscript to query account balances, you may encounter a 500 Internal Server Error if the account doesn't have a balance in the specified currency. This article explains how to handle this situation and provides a workaround.
The Issue
Consider the following Numscript:
vars {
monetary $balance = balance(@my_account, GBP/2)
}This script will cause a 500 Internal Server Error if the account doesn't have a GBP/2 balance. However, it works correctly if the account has a currency balance of [GBP/2 0].
Workaround
To avoid this error, you can seed the account with a zero balance for the required currency before running your Numscript. Here's how to do it:
send [GBP/2 0] (
source = @world
destination = @my_account
)This transaction will create a zero balance for the GBP/2 currency in the specified account, allowing your original Numscript to run without errors.
Future Improvements
The Formance team is aware of this issue and plans to address it in future releases of Numscript. The goal is to interpret no balance as zero, which will eliminate the need for the workaround described above.
Always ensure you're using the latest version of the Formance stack to benefit from bug fixes and improvements.