refactor tokens; move logic from libstatus back to status lib (#25)
refactor tokens; move logic from libstatus back to status lib fix references
This commit is contained in:
parent
132df2b5b4
commit
7e4875a63f
|
@ -36,59 +36,6 @@ proc getCustomTokens*(useCached: bool = true): seq[Erc20Contract] =
|
|||
customTokens = result
|
||||
customTokensInited = true
|
||||
|
||||
proc visibleTokensSNTDefault(): JsonNode =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let SNT = if currentNetwork == NetworkType.Mainnet: "SNT" else: "STT"
|
||||
let response = getSetting[string](Setting.VisibleTokens, "{}").parseJSON
|
||||
|
||||
if not response.hasKey($currentNetwork):
|
||||
# Set STT/SNT visible by default
|
||||
response[$currentNetwork] = %* [SNT]
|
||||
|
||||
return response
|
||||
|
||||
proc convertStringSeqToERC20ContractSeq*(stringSeq: seq[string]): seq[Erc20Contract] =
|
||||
result = @[]
|
||||
for v in stringSeq:
|
||||
let t = getErc20Contract(v)
|
||||
if t != nil: result.add t
|
||||
let ct = customTokens.getErc20ContractBySymbol(v)
|
||||
if ct != nil: result.add ct
|
||||
|
||||
proc toggleAsset*(symbol: string): seq[Erc20Contract] =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let visibleTokens = visibleTokensSNTDefault()
|
||||
var visibleTokenList = visibleTokens[$currentNetwork].to(seq[string])
|
||||
let symbolIdx = visibleTokenList.find(symbol)
|
||||
if symbolIdx > -1:
|
||||
visibleTokenList.del(symbolIdx)
|
||||
else:
|
||||
visibleTokenList.add symbol
|
||||
visibleTokens[$currentNetwork] = newJArray()
|
||||
visibleTokens[$currentNetwork] = %* visibleTokenList
|
||||
let saved = saveSetting(Setting.VisibleTokens, $visibleTokens)
|
||||
|
||||
convertStringSeqToERC20ContractSeq(visibleTokenList)
|
||||
|
||||
proc hideAsset*(symbol: string) =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let visibleTokens = visibleTokensSNTDefault()
|
||||
var visibleTokenList = visibleTokens[$currentNetwork].to(seq[string])
|
||||
var symbolIdx = visibleTokenList.find(symbol)
|
||||
if symbolIdx > -1:
|
||||
visibleTokenList.del(symbolIdx)
|
||||
visibleTokens[$currentNetwork] = newJArray()
|
||||
visibleTokens[$currentNetwork] = %* visibleTokenList
|
||||
discard saveSetting(Setting.VisibleTokens, $visibleTokens)
|
||||
|
||||
proc getVisibleTokens*(): seq[Erc20Contract] =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let visibleTokens = visibleTokensSNTDefault()
|
||||
var visibleTokenList = visibleTokens[$currentNetwork].to(seq[string])
|
||||
let customTokens = getCustomTokens()
|
||||
|
||||
result = convertStringSeqToERC20ContractSeq(visibleTokenList)
|
||||
|
||||
proc addCustomToken*(address: string, name: string, symbol: string, decimals: int, color: string) =
|
||||
let payload = %* [{"address": address, "name": name, "symbol": symbol, "decimals": decimals, "color": color}]
|
||||
discard callPrivateRPC("wallet_addCustomToken", payload)
|
||||
|
@ -99,74 +46,10 @@ proc removeCustomToken*(address: string) =
|
|||
echo callPrivateRPC("wallet_deleteCustomToken", payload)
|
||||
dirty.store(true)
|
||||
|
||||
proc getTokensBalances*(accounts: openArray[string], tokens: openArray[string]): JsonNode =
|
||||
let payload = %* [accounts, tokens]
|
||||
let response = callPrivateRPC("wallet_getTokensBalances", payload).parseJson
|
||||
if response["result"].kind == JNull:
|
||||
return %* {}
|
||||
response["result"]
|
||||
|
||||
proc getToken*(tokenAddress: string): Erc20Contract =
|
||||
getErc20Contracts().concat(getCustomTokens()).getErc20ContractByAddress(tokenAddress.parseAddress)
|
||||
|
||||
proc getTokenBalance*(tokenAddress: string, account: string): string =
|
||||
var postfixedAccount: string = account
|
||||
postfixedAccount.removePrefix("0x")
|
||||
let payload = %* [{
|
||||
"to": tokenAddress, "from": account, "data": fmt"0x70a08231000000000000000000000000{postfixedAccount}"
|
||||
}, "latest"]
|
||||
let response = callPrivateRPC("eth_call", payload)
|
||||
let balance = response.parseJson["result"].getStr
|
||||
|
||||
var decimals = 18
|
||||
let address = parseAddress(tokenAddress)
|
||||
let t = getErc20Contract(address)
|
||||
let ct = getCustomTokens().getErc20ContractByAddress(address)
|
||||
if t != nil:
|
||||
decimals = t.decimals
|
||||
elif ct != nil:
|
||||
decimals = ct.decimals
|
||||
|
||||
result = $hex2Token(balance, decimals)
|
||||
|
||||
proc getSNTAddress*(): string =
|
||||
let snt = contracts.getSntContract()
|
||||
result = $snt.address
|
||||
|
||||
proc getSNTBalance*(account: string): string =
|
||||
let snt = contracts.getSntContract()
|
||||
result = getTokenBalance($snt.address, account)
|
||||
|
||||
proc getTokenString*(contract: Contract, methodName: string): string =
|
||||
let payload = %* [{
|
||||
"to": $contract.address,
|
||||
"data": contract.methods[methodName].encodeAbi()
|
||||
}, "latest"]
|
||||
|
||||
let responseStr = callPrivateRPC("eth_call", payload)
|
||||
let response = Json.decode(responseStr, RpcResponse)
|
||||
if not response.error.isNil:
|
||||
raise newException(RpcException, "Error getting token string - " & methodName & ": " & response.error.message)
|
||||
if response.result == "0x":
|
||||
return ""
|
||||
|
||||
let size = fromHex(Stuint[256], response.result[66..129]).truncate(int)
|
||||
result = response.result[130..129+size*2].parseHexStr
|
||||
|
||||
proc tokenName*(contract: Contract): string = getTokenString(contract, "name")
|
||||
|
||||
proc tokenSymbol*(contract: Contract): string = getTokenString(contract, "symbol")
|
||||
|
||||
proc tokenDecimals*(contract: Contract): int =
|
||||
let payload = %* [{
|
||||
"to": $contract.address,
|
||||
"data": contract.methods["decimals"].encodeAbi()
|
||||
}, "latest"]
|
||||
|
||||
let responseStr = callPrivateRPC("eth_call", payload)
|
||||
let response = Json.decode(responseStr, RpcResponse)
|
||||
if not response.error.isNil:
|
||||
raise newException(RpcException, "Error getting token decimals: " & response.error.message)
|
||||
if response.result == "0x":
|
||||
return 0
|
||||
result = parseHexInt(response.result)
|
||||
# doesnt seem to be used
|
||||
# proc getTokensBalances*(accounts: openArray[string], tokens: openArray[string]): JsonNode =
|
||||
# let payload = %* [accounts, tokens]
|
||||
# let response = callPrivateRPC("wallet_getTokensBalances", payload).parseJson
|
||||
# if response["result"].kind == JNull:
|
||||
# return %* {}
|
||||
# response["result"]
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import libstatus/tokens as status_tokens
|
||||
import libstatus/tokens as libstatus_tokens
|
||||
import tokens_backend as status_tokens
|
||||
import eth/contracts
|
||||
import ../eventemitter
|
||||
|
||||
|
@ -14,10 +15,10 @@ proc getSNTAddress*(): string =
|
|||
result = status_tokens.getSNTAddress()
|
||||
|
||||
proc getCustomTokens*(self: TokensModel, useCached: bool = true): seq[Erc20Contract] =
|
||||
result = status_tokens.getCustomTokens(useCached)
|
||||
result = libstatus_tokens.getCustomTokens(useCached)
|
||||
|
||||
proc removeCustomToken*(self: TokensModel, address: string) =
|
||||
status_tokens.removeCustomToken(address)
|
||||
libstatus_tokens.removeCustomToken(address)
|
||||
|
||||
proc getSNTBalance*(account: string): string =
|
||||
result = status_tokens.getSNTBalance(account)
|
||||
|
|
|
@ -0,0 +1,134 @@
|
|||
# used to be libstatus, should be merged with tokens
|
||||
import
|
||||
json, chronicles, strformat, stint, strutils, sequtils, tables, atomics
|
||||
|
||||
import
|
||||
web3/[ethtypes, conversions], json_serialization
|
||||
|
||||
import
|
||||
./libstatus/settings, ./libstatus/core, ./libstatus/wallet, eth/contracts,
|
||||
types/[setting, network_type, rpc_response]
|
||||
from utils import parseAddress
|
||||
|
||||
import libstatus/tokens as libstatus_tokens
|
||||
|
||||
var
|
||||
customTokens {.threadvar.}: seq[Erc20Contract]
|
||||
|
||||
proc visibleTokensSNTDefault(): JsonNode =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let SNT = if currentNetwork == NetworkType.Mainnet: "SNT" else: "STT"
|
||||
let response = getSetting[string](Setting.VisibleTokens, "{}").parseJSON
|
||||
|
||||
if not response.hasKey($currentNetwork):
|
||||
# Set STT/SNT visible by default
|
||||
response[$currentNetwork] = %* [SNT]
|
||||
|
||||
return response
|
||||
|
||||
proc convertStringSeqToERC20ContractSeq*(stringSeq: seq[string]): seq[Erc20Contract] =
|
||||
result = @[]
|
||||
for v in stringSeq:
|
||||
let t = getErc20Contract(v)
|
||||
if t != nil: result.add t
|
||||
let ct = customTokens.getErc20ContractBySymbol(v)
|
||||
if ct != nil: result.add ct
|
||||
|
||||
proc toggleAsset*(symbol: string): seq[Erc20Contract] =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let visibleTokens = visibleTokensSNTDefault()
|
||||
var visibleTokenList = visibleTokens[$currentNetwork].to(seq[string])
|
||||
let symbolIdx = visibleTokenList.find(symbol)
|
||||
if symbolIdx > -1:
|
||||
visibleTokenList.del(symbolIdx)
|
||||
else:
|
||||
visibleTokenList.add symbol
|
||||
visibleTokens[$currentNetwork] = newJArray()
|
||||
visibleTokens[$currentNetwork] = %* visibleTokenList
|
||||
let saved = saveSetting(Setting.VisibleTokens, $visibleTokens)
|
||||
|
||||
convertStringSeqToERC20ContractSeq(visibleTokenList)
|
||||
|
||||
proc hideAsset*(symbol: string) =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let visibleTokens = visibleTokensSNTDefault()
|
||||
var visibleTokenList = visibleTokens[$currentNetwork].to(seq[string])
|
||||
var symbolIdx = visibleTokenList.find(symbol)
|
||||
if symbolIdx > -1:
|
||||
visibleTokenList.del(symbolIdx)
|
||||
visibleTokens[$currentNetwork] = newJArray()
|
||||
visibleTokens[$currentNetwork] = %* visibleTokenList
|
||||
discard saveSetting(Setting.VisibleTokens, $visibleTokens)
|
||||
|
||||
proc getVisibleTokens*(): seq[Erc20Contract] =
|
||||
let currentNetwork = getCurrentNetwork()
|
||||
let visibleTokens = visibleTokensSNTDefault()
|
||||
var visibleTokenList = visibleTokens[$currentNetwork].to(seq[string])
|
||||
let customTokens = libstatus_tokens.getCustomTokens()
|
||||
|
||||
result = convertStringSeqToERC20ContractSeq(visibleTokenList)
|
||||
|
||||
proc getToken*(tokenAddress: string): Erc20Contract =
|
||||
getErc20Contracts().concat(libstatus_tokens.getCustomTokens()).getErc20ContractByAddress(tokenAddress.parseAddress)
|
||||
|
||||
proc getTokenBalance*(tokenAddress: string, account: string): string =
|
||||
var postfixedAccount: string = account
|
||||
postfixedAccount.removePrefix("0x")
|
||||
let payload = %* [{
|
||||
"to": tokenAddress, "from": account, "data": fmt"0x70a08231000000000000000000000000{postfixedAccount}"
|
||||
}, "latest"]
|
||||
let response = callPrivateRPC("eth_call", payload)
|
||||
let balance = response.parseJson["result"].getStr
|
||||
|
||||
var decimals = 18
|
||||
let address = parseAddress(tokenAddress)
|
||||
let t = getErc20Contract(address)
|
||||
let ct = libstatus_tokens.getCustomTokens().getErc20ContractByAddress(address)
|
||||
if t != nil:
|
||||
decimals = t.decimals
|
||||
elif ct != nil:
|
||||
decimals = ct.decimals
|
||||
|
||||
result = $hex2Token(balance, decimals)
|
||||
|
||||
proc getSNTAddress*(): string =
|
||||
let snt = contracts.getSntContract()
|
||||
result = $snt.address
|
||||
|
||||
proc getSNTBalance*(account: string): string =
|
||||
let snt = contracts.getSntContract()
|
||||
result = getTokenBalance($snt.address, account)
|
||||
|
||||
proc getTokenString*(contract: Contract, methodName: string): string =
|
||||
let payload = %* [{
|
||||
"to": $contract.address,
|
||||
"data": contract.methods[methodName].encodeAbi()
|
||||
}, "latest"]
|
||||
|
||||
let responseStr = callPrivateRPC("eth_call", payload)
|
||||
let response = Json.decode(responseStr, RpcResponse)
|
||||
if not response.error.isNil:
|
||||
raise newException(RpcException, "Error getting token string - " & methodName & ": " & response.error.message)
|
||||
if response.result == "0x":
|
||||
return ""
|
||||
|
||||
let size = fromHex(Stuint[256], response.result[66..129]).truncate(int)
|
||||
result = response.result[130..129+size*2].parseHexStr
|
||||
|
||||
proc tokenName*(contract: Contract): string = getTokenString(contract, "name")
|
||||
|
||||
proc tokenSymbol*(contract: Contract): string = getTokenString(contract, "symbol")
|
||||
|
||||
proc tokenDecimals*(contract: Contract): int =
|
||||
let payload = %* [{
|
||||
"to": $contract.address,
|
||||
"data": contract.methods["decimals"].encodeAbi()
|
||||
}, "latest"]
|
||||
|
||||
let responseStr = callPrivateRPC("eth_call", payload)
|
||||
let response = Json.decode(responseStr, RpcResponse)
|
||||
if not response.error.isNil:
|
||||
raise newException(RpcException, "Error getting token decimals: " & response.error.message)
|
||||
if response.result == "0x":
|
||||
return 0
|
||||
result = parseHexInt(response.result)
|
|
@ -4,7 +4,8 @@ from web3/ethtypes import Address, Quantity
|
|||
from web3/conversions import `$`
|
||||
from libstatus/core import getBlockByNumber
|
||||
import libstatus/accounts as status_accounts
|
||||
import libstatus/tokens as status_tokens
|
||||
import tokens_backend as status_tokens
|
||||
import libstatus/tokens as libstatus_tokens
|
||||
import libstatus/settings as status_settings
|
||||
import libstatus/wallet as status_wallet
|
||||
import libstatus/accounts/constants as constants
|
||||
|
@ -75,7 +76,7 @@ proc buildTokenTransaction(source, to, assetAddress: Address, value: float, tran
|
|||
transactions.buildTokenTransaction(source, assetAddress, gas, gasPrice, isEIP1559Enabled, maxPriorityFeePerGas, maxFeePerGas)
|
||||
|
||||
proc getKnownTokenContract*(self: WalletModel, address: Address): Erc20Contract =
|
||||
getErc20Contracts().concat(getCustomTokens()).getErc20ContractByAddress(address)
|
||||
getErc20Contracts().concat(libstatus_tokens.getCustomTokens()).getErc20ContractByAddress(address)
|
||||
|
||||
proc estimateGas*(self: WalletModel, source, to, value, data: string, success: var bool): string =
|
||||
var tx = transactions.buildTransaction(
|
||||
|
@ -383,7 +384,7 @@ proc hideAsset*(self: WalletModel, symbol: string) =
|
|||
self.events.emit("assetChanged", Args())
|
||||
|
||||
proc addCustomToken*(self: WalletModel, symbol: string, enable: bool, address: string, name: string, decimals: int, color: string) =
|
||||
addCustomToken(address, name, symbol, decimals, color)
|
||||
libstatus_tokens.addCustomToken(address, name, symbol, decimals, color)
|
||||
|
||||
proc getTransfersByAddress*(self: WalletModel, address: string, toBlock: Uint256, limit: int, loadMore: bool): seq[Transaction] =
|
||||
result = status_wallet.getTransfersByAddress(address, toBlock, limit, loadMore)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import strformat, strutils, stint, httpclient, json, chronicles, net
|
||||
import ../libstatus/wallet as status_wallet
|
||||
import ../libstatus/tokens as status_tokens
|
||||
import ../tokens_backend as status_tokens
|
||||
import ../types/[rpc_response]
|
||||
import ../utils/cache
|
||||
import account
|
||||
|
|
|
@ -2,7 +2,7 @@ import json, strformat, options, chronicles, sugar, sequtils, strutils
|
|||
|
||||
import libstatus/accounts as status_accounts
|
||||
import libstatus/accounts/constants as constants
|
||||
import libstatus/tokens as status_tokens
|
||||
import tokens_backend as status_tokens
|
||||
import libstatus/wallet as status_wallet
|
||||
import libstatus/network as status_network
|
||||
import libstatus/settings as status_settings
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import strformat, strutils, stint, httpclient, json, chronicles, net
|
||||
import ../libstatus/wallet as status_wallet
|
||||
import ../libstatus/tokens as status_tokens
|
||||
import ../tokens_backend as status_tokens
|
||||
import ../types/[rpc_response]
|
||||
import ../utils/cache
|
||||
import account
|
||||
|
|
Loading…
Reference in New Issue