mirror of
https://github.com/status-im/status-desktop.git
synced 2025-01-11 06:47:01 +00:00
fix: token decimals and returning no collectibles when there's no contract for a collectible on the current network
Fixes #773
This commit is contained in:
parent
cc4127df03
commit
9ef82a2d7a
@ -1151,4 +1151,16 @@ proc getDefaultTokens*(): JsonNode =
|
||||
"decimals": 18,
|
||||
"hasIcon": true
|
||||
})
|
||||
|
||||
|
||||
proc getTokenBySymbol*(symbol: string): JsonNode =
|
||||
for defToken in getDefaultTokens().getElems():
|
||||
if defToken["symbol"].getStr == symbol:
|
||||
return defToken
|
||||
return newJNull()
|
||||
|
||||
proc getTokenByAddress*(address: string): JsonNode =
|
||||
for defToken in getDefaultTokens().getElems():
|
||||
if defToken["address"].getStr == address:
|
||||
return defToken
|
||||
|
||||
return newJNull()
|
@ -45,11 +45,10 @@ proc getVisibleTokens*(): JsonNode =
|
||||
let customTokens = getCustomTokens()
|
||||
|
||||
result = newJArray()
|
||||
for defToken in getDefaultTokens().getElems():
|
||||
for v in visibleTokenList:
|
||||
if defToken["symbol"].getStr == v:
|
||||
result.elems.add(defToken)
|
||||
break
|
||||
|
||||
for v in visibleTokenList:
|
||||
let t = getTokenBySymbol(v)
|
||||
if t.kind != JNull: result.elems.add(t)
|
||||
|
||||
for custToken in customTokens.getElems():
|
||||
for v in visibleTokenList:
|
||||
@ -80,7 +79,11 @@ proc getTokenBalance*(tokenAddress: string, account: string): string =
|
||||
}, "latest"]
|
||||
let response = callPrivateRPC("eth_call", payload)
|
||||
let balance = response.parseJson["result"].getStr
|
||||
result = $hex2Eth(balance)
|
||||
|
||||
let t = getTokenByAddress(tokenAddress)
|
||||
var decimals = 18
|
||||
if t.kind != JNull: decimals = t["decimals"].getInt
|
||||
result = $hex2Token(balance, decimals)
|
||||
|
||||
proc getSNTAddress*(): string =
|
||||
let snt = contracts.getContract("snt")
|
||||
|
@ -87,4 +87,13 @@ proc validateMnemonic*(mnemonic: string): string =
|
||||
proc startWallet*() =
|
||||
discard nim_status.startWallet()
|
||||
|
||||
proc hex2Token*(input: string, decimals: int): string =
|
||||
var value = fromHex(Stuint[256], input)
|
||||
var p = u256(10).pow(decimals)
|
||||
var i = value.div(p)
|
||||
var r = value.mod(p)
|
||||
var leading_zeros = "0".repeat(decimals - ($r).len)
|
||||
var d = fmt"{leading_zeros}{$r}"
|
||||
result = $i
|
||||
if(r > 0): result = fmt"{result}.{d}"
|
||||
|
@ -34,7 +34,7 @@ proc getPrice(crypto: string, fiat: string): string =
|
||||
|
||||
proc getEthBalance(address: string): string =
|
||||
var balance = status_wallet.getBalance(address)
|
||||
result = status_wallet.hex2Eth(balance)
|
||||
result = status_wallet.hex2token(balance, 18)
|
||||
|
||||
proc getBalance*(symbol: string, accountAddress: string, tokenAddress: string): string =
|
||||
let cacheKey = fmt"{symbol}-{accountAddress}-{tokenAddress}"
|
||||
|
@ -107,7 +107,7 @@ proc getEthermons*(address: EthAddress): string =
|
||||
var ethermons: seq[Collectible]
|
||||
ethermons = @[]
|
||||
let contract = getContract("ethermon")
|
||||
if contract == nil: return
|
||||
if contract == nil: return $(%*ethermons)
|
||||
|
||||
let tokens = tokensOfOwnerByIndex(contract, address)
|
||||
|
||||
@ -146,7 +146,7 @@ proc getKudos*(address: EthAddress): string =
|
||||
var kudos: seq[Collectible]
|
||||
kudos = @[]
|
||||
let contract = getContract("kudos")
|
||||
if contract == nil: return
|
||||
if contract == nil: return $(%*kudos)
|
||||
|
||||
let tokens = tokensOfOwnerByIndex(contract, address)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user