add various methods needed for wallet: get price, balance, convert hex wei to eth

This commit is contained in:
Iuri Matias 2020-05-13 19:02:23 -04:00
parent ed0059d48f
commit 18db5dc145
4 changed files with 55 additions and 3 deletions

View File

@ -1,7 +1,7 @@
SHELL := bash
build:
nim c -L:lib/libstatus.a -L:-lm --outdir:./bin src/nim_status_client.nim
nim c -L:lib/libstatus.a -d:ssl -L:-lm --outdir:./bin src/nim_status_client.nim
build-osx:
nim c -L:lib/libstatus.dylib -L:-lm -L:"-framework Foundation -framework Security -framework IOKit -framework CoreServices" --outdir:./bin src/nim_status_client.nim
nim c -L:lib/libstatus.dylib -d:ssl -L:-lm -L:"-framework Foundation -framework Security -framework IOKit -framework CoreServices" --outdir:./bin src/nim_status_client.nim

View File

@ -7,4 +7,4 @@ srcDir = "src"
bin = "nim_status_client"
[Deps]
Requires: "nim >= 1.0.0, nimqml >= 0.7.0"
Requires: "nim >= 1.0.0, nimqml >= 0.7.0, stint"

View File

@ -4,11 +4,14 @@ import chats
import json
import state
import status/utils
import strformat
import strutils
import status/core as status
import status/chat as status_chat
import status/test as status_test
import status/types as types
import status/wallet as status_wallet
proc mainProc() =
# From QT docs:
@ -34,6 +37,22 @@ proc mainProc() =
discard status_test.addPeer("enode://2c8de3cbb27a3d30cbb5b3e003bc722b126f5aef82e2052aaef032ca94e0c7ad219e533ba88c70585ebd802de206693255335b100307645ab5170e88620d2a81@47.244.221.14:443")
echo status.callPrivateRPC("{\"jsonrpc\":\"2.0\", \"method\":\"wakuext_requestMessages\", \"params\":[{\"topics\": [\"0x7998f3c8\"]}], \"id\": 1}")
# 1. get balance of an address
var balance = status_wallet.getBalance("0x0000000000000000000000000000000000000000")
echo(fmt"balance in hex: {balance}")
# 2. convert balance to eth
var eth_value = status_wallet.hex2Eth(balance)
echo(fmt"balance in eth: {eth_value}")
# 3. get usd price of 1 eth
var usd_eth_price = status_wallet.getPrice("ETH", "USD")
echo(fmt"usd_price: {usd_eth_price}")
# 4. convert balance to usd
var usd_balance = parseFloat(eth_value) * parseFloat(usd_eth_price)
echo(fmt"balance in usd: {usd_balance}")
# result.accountResult = status.queryAccounts()
var sendMessage = proc (msg: string): string =

33
src/status/wallet.nim Normal file
View File

@ -0,0 +1,33 @@
import libstatus
import json
import utils
import httpclient, json
import strformat
import stint
proc getPrice*(crypto: string, fiat: string): string =
var url: string = fmt"https://min-api.cryptocompare.com/data/price?fsym={crypto}&tsyms={fiat}"
let client = newHttpClient()
client.headers = newHttpHeaders({ "Content-Type": "application/json" })
let response = client.request(url)
$parseJson(response.body)["USD"]
proc getBalance*(address: string): string =
let payload = %* {
"jsonrpc": "2.0",
"id": 50,
"method": "eth_getBalance",
"params": [
address,
"latest"
]
}
parseJson($libstatus.callPrivateRPC($payload))["result"].str
proc hex2Eth*(input: string): string =
var value = fromHex(Stuint[256], input)
var one_eth = fromHex(Stuint[256], "DE0B6B3A7640000")
var (eth, remainder) = divmod(value, one_eth)
fmt"{eth}.{remainder}"