support adding a custom token

support adding a custom token

cleanup
This commit is contained in:
Iuri Matias 2020-06-11 15:52:54 -04:00
parent eaad59f690
commit 36b2ae5a66
10 changed files with 101 additions and 1342 deletions

View File

@ -160,3 +160,6 @@ QtObject:
self.accountListChanged() self.accountListChanged()
self.accounts.forceUpdate() self.accounts.forceUpdate()
self.setCurrentAssetList(self.currentAccount.account.assetList) self.setCurrentAssetList(self.currentAccount.account.assetList)
proc addCustomToken*(self: WalletView, address: string, name: string, symbol: string, decimals: string) {.slot.} =
self.status.wallet.toggleAsset(symbol, true, address, name, parseInt(decimals), "")

View File

@ -57,7 +57,7 @@ proc generateAccountConfiguredAssets*(self: WalletModel, accountAddress: string)
assets.add(asset) assets.add(asset)
for token in self.tokens: for token in self.tokens:
var symbol = token["symbol"].getStr var symbol = token["symbol"].getStr
var existingToken = Asset(name: token["name"].getStr, symbol: symbol, value: fmt"0.0", fiatValue: "$0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg", hasIcon: true, accountAddress: accountAddress) var existingToken = Asset(name: token["name"].getStr, symbol: symbol, value: fmt"0.0", fiatValue: "$0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg", hasIcon: true, accountAddress: accountAddress, address: token["address"].getStr)
assets.add(existingToken) assets.add(existingToken)
assets assets

View File

@ -4,7 +4,7 @@ type CurrencyArgs* = ref object of Args
currency*: string currency*: string
type Asset* = ref object type Asset* = ref object
name*, symbol*, value*, fiatValue*, image*, accountAddress*: string name*, symbol*, value*, fiatValue*, image*, accountAddress*, address*: string
hasIcon*: bool hasIcon*: bool
type WalletAccount* = ref object type WalletAccount* = ref object

View File

@ -2,7 +2,6 @@ import tables, strformat, strutils, stint, httpclient, json
import ../libstatus/wallet as status_wallet import ../libstatus/wallet as status_wallet
import ../libstatus/tokens as status_tokens import ../libstatus/tokens as status_tokens
import account import account
import token_list
type BalanceManager* = ref object type BalanceManager* = ref object
pricePairs: Table[string, string] pricePairs: Table[string, string]
@ -29,20 +28,20 @@ proc getPrice(crypto: string, fiat: string): string =
except Exception as e: except Exception as e:
echo "error getting price" echo "error getting price"
echo e.msg echo e.msg
result = "0.0"
proc getEthBalance(address: string): string = proc getEthBalance(address: string): string =
var balance = status_wallet.getBalance(address) var balance = status_wallet.getBalance(address)
result = status_wallet.hex2Eth(balance) result = status_wallet.hex2Eth(balance)
# balanceManager.tokenBalances["ETH"] = result # balanceManager.tokenBalances["ETH"] = result
proc getBalance*(symbol: string, accountAddress: string): string = proc getBalance*(symbol: string, accountAddress: string, tokenAddress: string): string =
if balanceManager.tokenBalances.hasKey(symbol): if balanceManager.tokenBalances.hasKey(symbol):
return balanceManager.tokenBalances[symbol] return balanceManager.tokenBalances[symbol]
if symbol == "ETH": if symbol == "ETH":
return getEthBalance(accountAddress) return getEthBalance(accountAddress)
var token: AssetConfig = getTokenConfig(symbol) result = $status_tokens.getTokenBalance(tokenAddress, accountAddress)
result = $status_tokens.getTokenBalance(token.address, accountAddress)
# balanceManager.tokenBalances[symbol] = result # balanceManager.tokenBalances[symbol] = result
proc getFiatValue*(crypto_balance: string, crypto_symbol: string, fiat_symbol: string): float = proc getFiatValue*(crypto_balance: string, crypto_symbol: string, fiat_symbol: string): float =
@ -51,13 +50,13 @@ proc getFiatValue*(crypto_balance: string, crypto_symbol: string, fiat_symbol: s
parseFloat(crypto_balance) * parseFloat(fiat_crypto_price) parseFloat(crypto_balance) * parseFloat(fiat_crypto_price)
proc updateBalance*(asset: Asset, currency: string) = proc updateBalance*(asset: Asset, currency: string) =
var token_balance = getBalance(asset.symbol, asset.accountAddress) var token_balance = getBalance(asset.symbol, asset.accountAddress, asset.address)
let fiat_balance = getFiatValue(token_balance, asset.symbol, currency) let fiat_balance = getFiatValue(token_balance, asset.symbol, currency)
asset.value = token_balance asset.value = token_balance
asset.fiatValue = fmt"{fiat_balance:.2f} {currency}" asset.fiatValue = fmt"{fiat_balance:.2f} {currency}"
proc updateBalance*(account: WalletAccount, currency: string) = proc updateBalance*(account: WalletAccount, currency: string) =
let eth_balance = getBalance("ETH", account.address) let eth_balance = getBalance("ETH", account.address, "")
let usd_balance = getFiatValue(eth_balance, "ETH", currency) let usd_balance = getFiatValue(eth_balance, "ETH", currency)
var totalAccountBalance = usd_balance var totalAccountBalance = usd_balance
account.realFiatBalance = totalAccountBalance account.realFiatBalance = totalAccountBalance

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,79 @@
import QtQuick 2.12
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import QtQuick.Dialogs 1.3
import "../../../imports"
import "../../../shared"
ModalPopup {
id: popup
title: qsTr("add custom token")
height: 630
property int marginBetweenInputs: 35
onOpened: {
accountNameInput.forceActiveFocus(Qt.MouseFocusReason)
}
Input {
id: addressInput
placeholderText: qsTr("Enter contract address...")
label: qsTr("Contract address")
}
Input {
id: nameInput
anchors.top: addressInput.bottom
anchors.topMargin: marginBetweenInputs
placeholderText: qsTr("The name of your token...")
label: qsTr("Name")
}
Input {
id: symbolInput
anchors.top: nameInput.bottom
anchors.topMargin: marginBetweenInputs
placeholderText: qsTr("ABC")
label: qsTr("Symbol")
}
Input {
id: decimalsInput
anchors.top: symbolInput.bottom
anchors.topMargin: marginBetweenInputs
label: qsTr("Decimals")
text: "18"
}
footer: Item {
anchors.fill: parent
StyledButton {
id: addBtn
anchors.top: parent.top
anchors.topMargin: Theme.padding
anchors.right: parent.right
anchors.rightMargin: Theme.padding
label: qsTr("Add")
disabled: addressInput.text === "" || nameInput.text === "" || symbolInput.text === "" || decimalsInput.text === ""
onClicked : {
const error = walletModel.addCustomToken(addressInput.text, nameInput.text, symbolInput.text, decimalsInput.text);
if (error) {
changeError.text = error
changeError.open()
return
}
popup.close();
}
}
}
}
/*##^##
Designer {
D{i:0;autoSize:true;height:480;width:640}
}
##^##*/

View File

@ -21,6 +21,11 @@ Item {
anchors.left: parent.left anchors.left: parent.left
anchors.leftMargin: 0 anchors.leftMargin: 0
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onStatusChanged: {
if (assetInfoImage.status == Image.Error) {
assetInfoImage.source = "../../img/tokens/0-native.png"
}
}
} }
Text { Text {
id: assetSymbol id: assetSymbol

View File

@ -134,11 +134,12 @@ Item {
StyledButton { StyledButton {
anchors.right: parent.right anchors.right: parent.right
anchors.rightMargin: Theme.padding anchors.rightMargin: Theme.padding
label: qsTr("Apply to all accounts") label: qsTr("Add custom token")
anchors.bottom: parent.bottom anchors.bottom: parent.bottom
anchors.bottomMargin: Theme.padding anchors.bottomMargin: Theme.padding
onClicked: { onClicked: {
popup.close() popup.close()
addCustomTokenModal.open()
} }
} }
} }

View File

@ -83,6 +83,10 @@ Item {
changeSelectedAccount: walletHeader.changeSelectedAccount changeSelectedAccount: walletHeader.changeSelectedAccount
} }
AddCustomTokenModal {
id: addCustomTokenModal
}
Item { Item {
property int btnMargin: 8 property int btnMargin: 8
property int btnOuterMargin: 32 property int btnOuterMargin: 32

View File

@ -78,6 +78,7 @@ DISTFILES += \
app/AppLayouts/Profile/LeftTab/qmldir \ app/AppLayouts/Profile/LeftTab/qmldir \
app/AppLayouts/Profile/ProfileLayout.qml \ app/AppLayouts/Profile/ProfileLayout.qml \
app/AppLayouts/Wallet/AccountSettingsModal.qml \ app/AppLayouts/Wallet/AccountSettingsModal.qml \
app/AppLayouts/Wallet/AddCustomTokenModal.qml \
app/AppLayouts/Wallet/AssetsTab.qml \ app/AppLayouts/Wallet/AssetsTab.qml \
app/AppLayouts/Wallet/CollectiblesTab.qml \ app/AppLayouts/Wallet/CollectiblesTab.qml \
app/AppLayouts/Wallet/Components/AccountSettingsModal.qml \ app/AppLayouts/Wallet/Components/AccountSettingsModal.qml \