feat: Init tokens2

This commit is contained in:
Anthony Laibe 2021-09-27 11:58:56 +02:00
parent b8c729b804
commit fe72f3b01c
5 changed files with 59 additions and 28 deletions

View File

@ -1,12 +1,11 @@
import statusgo_backend/accounts as statusgo_backend_accounts
import statusgo_backend/core as statusgo_backend_core
import statusgo_backend/settings as statusgo_backend_settings
import chat, accounts, wallet, wallet2, node, network, messages, contacts, profile, stickers, permissions, fleet, settings, mailservers, browser, tokens, provider
import chat, accounts, wallet, wallet2, node, network, messages, contacts, profile, stickers, permissions, fleet, settings, mailservers, browser, tokens, tokens2, provider
import notifications/os_notifications
import ../eventemitter
import bitops, stew/byteutils, chronicles
import ./types/[setting]
import ../backends/backend
export chat, accounts, node, messages, contacts, profile, network, permissions, fleet, eventemitter
@ -30,6 +29,7 @@ type Status* = ref object
mailservers*: MailserversModel
browser*: BrowserModel
tokens*: TokensModel
tokens2*: Tokens2Model
provider*: ProviderModel
osnotifications*: OsNotifications
@ -55,6 +55,7 @@ proc newStatusInstance*(fleetConfig: string, backendName:string = "statusgo"): S
result.mailservers = mailservers.newMailserversModel(result.events)
result.browser = browser.newBrowserModel(result.events, result.backend)
result.tokens = tokens.newTokensModel(result.events)
result.tokens2 = tokens2.newTokens2Model(result.events)
result.provider = provider.newProviderModel(result.events, result.permissions, result.wallet)
result.osnotifications = newOsNotifications(result.events)

View File

@ -1,13 +1,13 @@
import
json, chronicles, strformat, stint, strutils, sequtils, tables, atomics
json, chronicles, stint, atomics
import
web3/[ethtypes, conversions], json_serialization
import
./settings, ./core, ./wallet, ../eth/contracts,
./core,
../eth/contracts,
../types/[setting, network_type, rpc_response]
from ../utils import parseAddress
logScope:
topics = "wallet"
@ -46,10 +46,9 @@ proc removeCustomToken*(address: string) =
echo callPrivateRPC("wallet_deleteCustomToken", payload)
dirty.store(true)
# 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"]
proc getTokensBalancesForChainIDs*(chainIds: seq[int], accounts: seq[string], tokens: seq[string]): JsonNode =
let payload = %* [chainIds, accounts, tokens]
let response = callPrivateRPC("wallet_getTokensBalancesForChainIDs", payload).parseJson
if response["result"].kind == JNull:
return %* {}
response["result"]

44
status/tokens2.nim Normal file
View File

@ -0,0 +1,44 @@
from sequtils import concat, map
import sugar, json
from web3/conversions import `$`
import statusgo_backend/network as statusgo_backend_network
import statusgo_backend/tokens as statusgo_backend_tokens
import eth/tokens as status_tokens
import eth/contracts
import types/[network_type, token]
import ../eventemitter
type
Tokens2Model* = ref object
events*: EventEmitter
proc newTokens2Model*(events: EventEmitter): Tokens2Model =
result = Tokens2Model()
result.events = events
proc fetchErc20Contracts(self: Tokens2Model): seq[Erc20Contract] =
for network in statusgo_backend_network.getNetworks():
if not network.enabled:
continue
result = concat(result, status_tokens.getVisibleTokens(network))
proc fetchCustomErc20Contracts(self: Tokens2Model, useCached: bool = true): seq[Erc20Contract] =
result = statusgo_backend_tokens.getCustomTokens(useCached)
proc loadTokensForAddress*(self: Tokens2Model, accountAddress: string): seq[Token] =
let contracts = concat(self.fetchErc20Contracts(), self.fetchCustomErc20Contracts())
let contractAddresses = contracts.map(contract => $contract.address)
let chainIds = contracts.map(contract => contract.chainId)
let tokenBalances = statusgo_backend_tokens.getTokensBalancesForChainIDs(chainIds, @[accountAddress], contractAddresses)
for contract in contracts:
let balance = tokenBalances{accountAddress}{$contract.address}.getStr
result.add(Token(
name: contract.name,
symbol: contract.symbol,
balance: balance,
fiatBalance: "0", # TODO: fetch balance
address: $contract.address
))

2
status/types/token.nim Normal file
View File

@ -0,0 +1,2 @@
type Token* = ref object
name*, symbol*, balance*, fiatBalance*, address*: string

View File

@ -5,9 +5,7 @@ import statusgo_backend/accounts/constants as constants
import statusgo_backend/wallet as status_wallet
import statusgo_backend/network as status_network
import statusgo_backend/settings as status_settings
import eth/contracts
import wallet2/balance_manager
import eth/tokens as tokens_backend
import wallet2/account as wallet_account
import ./types/[account, transaction, network, network_type, setting, gas_prediction, rpc_response]
import ../eventemitter
@ -28,7 +26,6 @@ type
events: EventEmitter
accounts: seq[WalletAccount]
networks*: seq[Network]
tokens: seq[Erc20Contract]
totalBalance*: float
# Forward declarations
@ -40,7 +37,6 @@ proc calculateTotalFiatBalance*(self: Wallet2Model)
proc setup(self: Wallet2Model, events: EventEmitter) =
self.events = events
self.accounts = @[]
self.tokens = @[]
self.networks = @[]
self.totalBalance = 0.0
self.initEvents()
@ -52,10 +48,6 @@ proc newWallet2Model*(events: EventEmitter): Wallet2Model =
result = Wallet2Model()
result.setup(events)
proc initTokens(self: Wallet2Model) =
let network = status_settings.getCurrentNetwork().toNetwork()
self.tokens = tokens_backend.getVisibleTokens(network)
proc initNetworks(self: Wallet2Model) =
self.networks = status_network.getNetworks()
@ -68,7 +60,6 @@ proc initAccounts(self: Wallet2Model) =
self.accounts.add(walletAccount)
proc init*(self: Wallet2Model) =
self.initTokens()
self.initNetworks()
self.initAccounts()
@ -93,13 +84,7 @@ proc generateAccountConfiguredAssets*(self: Wallet2Model,
var asset = Asset(name:"Ethereum", symbol: "ETH", value: "0.0",
fiatBalanceDisplay: "0.0", accountAddress: accountAddress)
assets.add(asset)
for token in self.tokens:
var symbol = token.symbol
var existingToken = Asset(name: token.name, symbol: symbol,
value: fmt"0.0", fiatBalanceDisplay: "$0.0", accountAddress: accountAddress,
address: $token.address)
assets.add(existingToken)
assets
return assets
proc calculateTotalFiatBalance*(self: Wallet2Model) =
self.totalBalance = 0.0