Page 1 of 1

Get the balance of a Solana crypto wallet

Posted: Fri Sep 19, 2025 1:03 pm
by Axeman
The functions 'GetSolanaWalletBalanceAsLamports( wallet_address.s )' and 'GetSolanaWalletBalanceAsSolana( wallet_address.s )' will return the balance of a specified Solana crypto wallet.

I'm writing a Solana trading bot and needed this fuctionality, so I thought I'd share the code for anyone who needs it.

Code: Select all


; REFERENCE: https://solana.com/docs/rpc/http/getbalance


Global G_error, G_response.s, G_response_pos

Procedure.q GetWrappedInteger( start_text.s )
; Returns the string extracted from 'G_response.s' and updates 'G_response_pos' to the first position past the end text.
; If the 'start_text.s' could not be found then 'G_response_pos' will be zero, the function will return an empty string, and the attempt to parse data from the response should be aborted.

Protected max_pos = Len( G_response.s )
If G_response_pos > max_pos : G_response_pos = 0 : ProcedureReturn 0 : EndIf ; Abort if 'G_response_pos' is past the end of the response string.
G_response_pos = FindString( G_response.s, start_text.s, G_response_pos )
If G_response_pos = 0 : ProcedureReturn 0 : EndIf
G_response_pos + Len( start_text.s ) ; Move 'G_response_pos' past the 'start_text.s' to the start of the string being extracted.
Protected result.s, char.s
Repeat
	If G_response_pos > max_pos : Break : EndIf
	char.s = Mid( G_response.s, G_response_pos, 1 )
	If char.s = " " : G_response_pos + 1 : Continue : EndIf ; Ignore spaces.
	If FindString( "0123456789", char.s ) = 0 : Break : EndIf ; Check if the character is not a number.
	result.s + char.s : G_response_pos + 1
ForEver
ProcedureReturn Val( result.s )
EndProcedure


Procedure.q GetSolanaWalletBalanceAsLamports( wallet_address.s )
; Returns the balance of the specified Solana wallet as a quad, in Lamports. There are one billion Lamports in one Solana.
; If there is an error then a zero will be returned and 'G_error' will be non-zero.
; REFERENCE: https://solana.com/docs/rpc/http/getbalance

G_response.s = "" : G_response_pos = 1 : G_error = 0
Protected NewMap TempHeaderMap.s()
TempHeaderMap( "Content-Type" ) = "application/json"
Protected request_data.s = ~"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"getBalance\",\"params\":[\"" + wallet_address.s + ~"\",{\"commitment\":\"finalized\"}]}"
Protected HttpRequest.i = HTTPRequest( #PB_HTTP_Post, "https://api.mainnet-beta.solana.com", request_data.s, 0, TempHeaderMap() )
If HttpRequest
	Protected status.s =  HTTPInfo( HTTPRequest, #PB_HTTP_StatusCode )
	;Debug status.s
	If status.s = "200"
		G_response.s = HTTPInfo( HTTPRequest, #PB_HTTP_Response )
		Protected lamports.q = GetWrappedInteger( ~"\"value\":" )
		If G_response_pos = 0 : lamports = 0 : G_error = 3 : EndIf
	Else
		;Debug  "STATUS ERROR: " + status.s : End
		G_error = 1
	EndIf
	FinishHTTP( HTTPRequest )
Else
	G_error = 2
EndIf
ProcedureReturn lamports
EndProcedure


Procedure.d GetSolanaWalletBalanceAsSolana( wallet_address.s )
; Returns the balance of the specified Solana wallet as a double, in Solana.
; If there is an error then a zero will be returned and 'G_error' will be non-zero.
; REFERENCE: https://solana.com/docs/rpc/http/getbalance

G_response.s = "" : G_response_pos = 1 : G_error = 0
Protected NewMap TempHeaderMap.s()
TempHeaderMap( "Content-Type" ) = "application/json"
Protected request_data.s = ~"{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"getBalance\",\"params\":[\"" + wallet_address.s + ~"\",{\"commitment\":\"finalized\"}]}"
Protected HttpRequest.i = HTTPRequest( #PB_HTTP_Post, "https://api.mainnet-beta.solana.com", request_data.s, 0, TempHeaderMap() )
If HttpRequest
	Protected status.s =  HTTPInfo( HTTPRequest, #PB_HTTP_StatusCode )
	;Debug status.s
	If status.s = "200"
		G_response.s = HTTPInfo( HTTPRequest, #PB_HTTP_Response )
		Protected lamports.q = GetWrappedInteger( ~"\"value\":" )
		If G_response_pos = 0 : lamports = 0 : G_error = 3 : EndIf
	Else
		;Debug  "STATUS ERROR: " + status.s : End
		G_error = 1
	EndIf
	FinishHTTP( HTTPRequest )
Else
	G_error = 2
EndIf
ProcedureReturn lamports / 1000000000.0 ; There are one billion Lamports in one Solana, so we divide by one billion.
EndProcedure


Procedure.d GetSolanaWalletBalanceAsSolana2( wallet_address.s )
; Returns the balance of the specified Solana wallet as a double, in Solana.
; If there is an error then a zero will be returned and 'G_error' will be non-zero.
; REFERENCE: https://solana.com/docs/rpc/http/getbalance

Protected lamports.q = GetSolanaWalletBalanceAsLamports( wallet_address.s )
If G_error : ProcedureReturn 0 : EndIf
ProcedureReturn lamports / 1000000000.0 ; There are one billion Lamports in one Solana, so we divide by one billion.
EndProcedure


; *** EXAMPLE USAGE ***

; This holds the Solana pubkey (wallet address) of the Solana account to query the balance for. The address used here is an example address published on the solana.com website.
Global G_wallet_address.s = "83astBRguLMdt2h5U1Tpdq5tjFoJ6noeGwaY3mDLVcri"

Debug GetSolanaWalletBalanceAsLamports( G_wallet_address.s )

If G_error : Debug "ERROR: " + Str( G_error ) : EndIf

Delay( 1000 )

Debug GetSolanaWalletBalanceAsSolana( G_wallet_address.s )

If G_error : Debug "ERROR: " + Str( G_error ) : EndIf










Re: Get the balance of a Solana crypto wallet

Posted: Fri Sep 19, 2025 1:29 pm
by jacdelad
Thank god I took a second look, I was just about to report it as spam. :mrgreen:

Re: Get the balance of a Solana crypto wallet

Posted: Sun Sep 21, 2025 4:07 pm
by Caronte3D
Nice!
Do you have the same for: btc, eth, usdt...?