diff --git a/docs/RPC/Personal.md b/docs/RPC/Personal.md index 543999af..cbf67eeb 100644 --- a/docs/RPC/Personal.md +++ b/docs/RPC/Personal.md @@ -83,6 +83,12 @@ The result is an array of objects, each as follows: - `hash` (string) - `nonce` (int) +### `getBalance` + +`getBalance` replies with the sum of the value of every UTXO known to the node's Wallet, AKA its balance. If you only want the balance for a specific address, use `transactions_getBalance`. + +The result is a string of the balance. + ### `getTransactionTemplate` `getTransactionTemplate` replies with a signable transaction template usable by a program with the relevant private keys. diff --git a/e2e/Tests/RPC/Personal/WatchWalletTest.py b/e2e/Tests/RPC/Personal/WatchWalletTest.py index 621928dc..91425c32 100644 --- a/e2e/Tests/RPC/Personal/WatchWalletTest.py +++ b/e2e/Tests/RPC/Personal/WatchWalletTest.py @@ -172,12 +172,14 @@ def test() -> None: raise TestError("WatchWallet Meros couldn't get its UTXOs.") #Also test balance getting. - if rpc.call("personal", "getBalance") != sum( - [ - int( - rpc.call("transactions", "getTransaction", {"hash": send.hex()})["outputs"][0]["amount"] - ) for send in sends[:2] - ] + if rpc.call("personal", "getBalance") != str( + sum( + [ + int( + rpc.call("transactions", "getTransaction", {"hash": send.hex()})["outputs"][0]["amount"] + ) for send in sends[:2] + ] + ) ): raise TestError("WatchWallet Meros couldn't get its balance.") @@ -201,12 +203,14 @@ def test() -> None: raise TestError("WatchWallet Meros couldn't get its UTXOs.") #Again test the balance. - if rpc.call("personal", "getBalance") != sum( - [ - int( - rpc.call("transactions", "getTransaction", {"hash": send.hex()})["outputs"][0]["amount"] - ) for send in sends[:3] - ] + if rpc.call("personal", "getBalance") != str( + sum( + [ + int( + rpc.call("transactions", "getTransaction", {"hash": send.hex()})["outputs"][0]["amount"] + ) for send in sends[:3] + ] + ) ): raise TestError("WatchWallet Meros couldn't get its balance.") diff --git a/src/Interfaces/RPC/Modules/PersonalModule.nim b/src/Interfaces/RPC/Modules/PersonalModule.nim index d8c222bd..f1abd7cf 100644 --- a/src/Interfaces/RPC/Modules/PersonalModule.nim +++ b/src/Interfaces/RPC/Modules/PersonalModule.nim @@ -112,12 +112,14 @@ proc module*( "nonce": utxo.utxo.nonce }) - proc getBalance(): uint64 {.requireAuth, forceCheck: [].} = + proc getBalance(): string {.requireAuth, forceCheck: [].} = + var balance: uint64 = 0 for utxo in functions.personal.getUTXOs(): try: - result += functions.transactions.getTransaction(utxo.utxo.hash).outputs[utxo.utxo.nonce].amount + balance += functions.transactions.getTransaction(utxo.utxo.hash).outputs[utxo.utxo.nonce].amount except IndexError as e: panic("Had a UTXO for a non-existent Transaction: " & e.msg) + result = $balance proc getTransactionTemplate( #_JSON is used to distinguish the name from the below variables, not out of necessity due to using a keyword.