Skip to content

Commit

Permalink
Update personal_getBalance to return a string.
Browse files Browse the repository at this point in the history
Oversight on my end. Also adds missing documentation. Another such 
oversight.
  • Loading branch information
kayabaNerve committed May 29, 2021
1 parent b48ccf0 commit cece8c2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
6 changes: 6 additions & 0 deletions docs/RPC/Personal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
28 changes: 16 additions & 12 deletions e2e/Tests/RPC/Personal/WatchWalletTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand All @@ -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.")

Expand Down
6 changes: 4 additions & 2 deletions src/Interfaces/RPC/Modules/PersonalModule.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit cece8c2

Please sign in to comment.