Skip to content

Commit

Permalink
Move relayFee to account
Browse files Browse the repository at this point in the history
Save the relay fee with the account. Allow changing of the relay fee in
Account settings as apposed to App settings. Change relay fee input
field to radio buttons with labels low, default, and high. Set
appropriate values for low (4000) and high (114000).
  • Loading branch information
JoeGruffins committed Apr 10, 2020
1 parent c8ade44 commit 4780ecd
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 39 deletions.
6 changes: 5 additions & 1 deletion decred/decred/dcr/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from decred import DecredError
from decred.crypto import crypto, opcode
from decred.dcr import addrlib
from decred.dcr.txscript import DefaultRelayFeePerKb
from decred.util import encode, helpers
from decred.util.encode import BuildyBytes, ByteArray, unblobCheck

Expand Down Expand Up @@ -801,6 +802,7 @@ def __init__(
self.coinID = BIPID
self.netID = netID
self.netParams = nets.parse(netID)
self.relayFee = int(DefaultRelayFeePerKb)
# For external addresses, the cursor can sit on the last seen address,
# so start the lastSeen at the 0th external address. This is necessary
# because the currentAddress method grabs the address at the current
Expand Down Expand Up @@ -855,14 +857,15 @@ def blob(acct):
.addData(encode.intToBytes(acct.cursorExt, signed=True))
.addData(acct.cursorInt)
.addData(acct.gapLimit)
.addData(acct.relayFee)
.b
)

@staticmethod
def unblob(b):
"""Satisfies the encode.Blobber API"""
ver, d = encode.decodeBlob(b)
unblobCheck("Account", ver, len(d), {0: 8})
unblobCheck("Account", ver, len(d), {0: 9})

iFunc = encode.intFromBytes

Expand All @@ -875,6 +878,7 @@ def unblob(b):
acct.cursorExt = iFunc(d[5], signed=True)
acct.cursorInt = iFunc(d[6])
acct.gapLimit = iFunc(d[7])
acct.relayFee = iFunc(d[8])
return acct

@staticmethod
Expand Down
3 changes: 0 additions & 3 deletions tinywallet/tinywallet/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

from decred.dcr import constants as DCR
from decred.dcr.dcrdata import DcrdataBlockchain
from decred.dcr.txscript import DefaultRelayFeePerKb
from decred.util import chains, database, helpers
from decred.wallet.wallet import Wallet
from tinywallet import config, qutilities as Q, screens, ui
Expand Down Expand Up @@ -274,8 +273,6 @@ def loadSettings(self):
self.settings[DB.dcrdata] = config.NetworkDefaults[self.cfg.netParams.Name][
"dcrdata"
].encode()
if DB.relayFee not in self.settings:
self.settings[DB.relayFee] = int(DefaultRelayFeePerKb)

def registerSignal(self, sig, cb, *a, **k):
"""
Expand Down
1 change: 0 additions & 1 deletion tinywallet/tinywallet/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ class DB:
wallet = "wallet".encode()
theme = "theme".encode()
dcrdata = "dcrdata".encode()
relayFee = "relayfee".encode()


tinyConfig = None
Expand Down
107 changes: 73 additions & 34 deletions tinywallet/tinywallet/screens.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from PyQt5 import QtCore, QtGui, QtSvg, QtWidgets

from decred.dcr import constants as DCR, nets
from decred.dcr.txscript import DefaultRelayFeePerKb
from decred.dcr.vsp import VotingServiceProvider
from decred.util import chains, helpers
from decred.wallet.wallet import Wallet
Expand Down Expand Up @@ -461,7 +462,9 @@ def __init__(self, acctMgr, acct, assetScreen):
self.canGoHome = False
self.ticketStats = None
self.balance = None
self.settingsScreen = AccountSettingsScreen(self.saveName)
self.settingsScreen = AccountSettingsScreen(
self.account.relayFee, self.saveName, self.saveRelayFee
)
self.stakeScreen = StakingScreen(acct)
self.wgt.setFixedSize(
TinyDialog.maxWidth * 0.9,
Expand Down Expand Up @@ -702,6 +705,16 @@ def saveName(self, newName):
self.assetScreen.doButtons()
app.home()

def saveRelayFee(self, relayFee):
"""
Changes and saves the relayFee of the account.
Args:
int: The new relayFee.
"""
self.account.relayFee = relayFee
self.acctMgr.saveAccount(self.account.idx)

def stackAndSync(self):
"""
Start syncing the account.
Expand Down Expand Up @@ -1103,16 +1116,6 @@ def __init__(self):
self.layout.addWidget(wgt)
grid.setColumnStretch(0, 1)
row = 0
grid.addWidget(Q.makeLabel("Relay Fee", 14, Q.ALIGN_LEFT), row, 0)
row += 1
self.relayFeeField = QtWidgets.QLineEdit(str(app.settings[DB.relayFee]))
lbl = Q.makeLabel(" atoms / kb ", 14)
wgt, _ = Q.makeRow(self.relayFeeField, lbl)
grid.addWidget(wgt, row, 0)
bttn = app.getButton(SMALL, "change")
bttn.clicked.connect(self.relayFeeChangeClicked)
grid.addWidget(bttn, row, 1)
row += 1
grid.addWidget(Q.makeLabel("Dcrdata URL", 14, Q.ALIGN_LEFT), row, 0)
row += 1
self.dcrdataField = QtWidgets.QLineEdit(app.settings[DB.dcrdata].decode())
Expand All @@ -1123,28 +1126,6 @@ def __init__(self):

self.layout.addStretch(1)

def relayFeeChangeClicked(self, e=None):
"""
Qt slot connected to relay fee change button clicked signal.
Changes the relay fee.
"""

fee = int(self.relayFeeField.text())
if fee <= 0:
app.appWindow.showError("fee cannot be less than or equal to zero")
self.relayFeeField.setText(str(app.settings[DB.relayFee]))
return

highFee = int(1e4 * 10)
if fee > highFee:
app.appWindow.showError(f"fee cannot be higher than {highFee}")
self.relayFeeField.setText(str(app.settings[DB.relayFee]))
return

app.settings[DB.relayFee] = fee
log.info(f"relay fee changed to {fee}")
app.appWindow.showSuccess("fee changed")

def dcrdataChangeClicked(self, e=None):
"""
Qt slot connected to dcrdata URL submission button clicked signal.
Expand Down Expand Up @@ -1219,7 +1200,7 @@ class AccountSettingsScreen(Screen):
Account settings screen.
"""

def __init__(self, saveName):
def __init__(self, relayFee, saveName, saveRelayFee):
"""
Args:
saveName (function): A callback function to be called after the user
Expand All @@ -1233,6 +1214,7 @@ def __init__(self, saveName):
TinyDialog.maxHeight * 0.9 - TinyDialog.topMenuHeight,
)
self.saveName = saveName
self.saveRelayFee = saveRelayFee

gear = SVGWidget("gear", h=20)
lbl = Q.makeLabel("Account Settings", 22)
Expand All @@ -1256,6 +1238,50 @@ def __init__(self, saveName):
bttn.clicked.connect(self.nameChangeClicked)
grid.addWidget(bttn, row, 1)

# RELAY FEE

# Set string constants.
self.lowStr = "low"
self.defaultStr = "default"
self.highStr = "high"

# Set values that are considered low, default, and high.
self.feeRates = {
self.lowStr: 4000,
self.defaultStr: int(DefaultRelayFeePerKb),
self.highStr: 114000,
}
self.feeLvl = ""

# Find the current level. Warn if the current level isn't one of the
# three.
for k, v in self.feeRates.items():
if v == relayFee:
self.feeLvl = k
break
else:
log.warn(f"non standard relay fee set: {relayFee}")

# Helper to check the current value.
def setChecked(btn):
if self.feeLvl == btn.text():
btn.setChecked(True)

row += 1
grid.addWidget(Q.makeLabel("Relay Fee", 14, Q.ALIGN_LEFT), row, 0)
row += 1
btn1 = QtWidgets.QRadioButton(self.lowStr)
setChecked(btn1)
btn1.toggled.connect(lambda: self.relayFeeChangeClicked(self.lowStr))
btn2 = QtWidgets.QRadioButton(self.defaultStr)
setChecked(btn2)
btn2.toggled.connect(lambda: self.relayFeeChangeClicked(self.defaultStr))
btn3 = QtWidgets.QRadioButton(self.highStr)
setChecked(btn3)
btn3.toggled.connect(lambda: self.relayFeeChangeClicked(self.highStr))
wgt, _ = Q.makeRow(btn1, btn2, btn3)
grid.addWidget(wgt, row, 0, 1, -1)

self.layout.addStretch(1)

def nameChangeClicked(self, e):
Expand All @@ -1269,6 +1295,19 @@ def nameChangeClicked(self, e):
self.nameField.setText("")
self.saveName(newName)

def relayFeeChangeClicked(self, feeLvl, e=None):
"""
Qt slot connected to relay fee radio button clicked signal. Changes the
relay fee to feeLvl.
"""
if self.feeLvl == feeLvl:
return
self.feeLvl = feeLvl
fee = self.feeRates[feeLvl]
self.saveRelayFee(fee)
log.info(f"relay fee changed to {fee} atoms/kb")
app.appWindow.showSuccess(f"using {feeLvl} fees")


class NewAccountScreen(Screen):
"""
Expand Down

0 comments on commit 4780ecd

Please sign in to comment.