Fabcoin Core  0.16.2
P2P Digital Currency
sendtokenpage.cpp
Go to the documentation of this file.
1 #include <sendtokenpage.h>
2 #include <ui_sendtokenpage.h>
3 
4 #include <walletmodel.h>
5 #include <clientmodel.h>
6 #include <optionsmodel.h>
7 #include <validation.h>
8 #include <utilmoneystr.h>
9 #include <token.h>
10 #include <fabcoinunits.h>
11 #include <wallet/wallet.h>
12 #include <validation.h>
13 #include <guiutil.h>
14 #include <sendcoinsdialog.h>
16 #include <uint256.h>
17 #include <styleSheet.h>
18 
19 static const CAmount SINGLE_STEP = 0.00000001*COIN;
20 
22  std::string address;
23  std::string sender;
24  std::string symbol;
25  int8_t decimals;
26  std::string balance;
28  decimals(0)
29  {}
30 };
31 
32 SendTokenPage::SendTokenPage(QWidget *parent) :
33  QWidget(parent),
34  ui(new Ui::SendTokenPage),
35  m_model(0),
36  m_clientModel(0),
37  m_tokenABI(0),
38  m_selectedToken(0)
39 {
40  // Setup ui components
41  ui->setupUi(this);
42 
43  // Set stylesheet
44  SetObjectStyleSheet(ui->clearButton, StyleSheetNames::ButtonBlack);
45 
46  ui->labelPayTo->setToolTip(tr("The address that will receive the tokens."));
47  ui->labelAmount->setToolTip(tr("The amount in Token to send."));
48  ui->labelDescription->setToolTip(tr("Optional description for transaction."));
49  m_tokenABI = new Token();
51 
52  // Set defaults
53  ui->lineEditGasPrice->setValue(DEFAULT_GAS_PRICE);
54  ui->lineEditGasPrice->setSingleStep(SINGLE_STEP);
55  ui->lineEditGasLimit->setMaximum(DEFAULT_GAS_LIMIT_OP_SEND);
56  ui->lineEditGasLimit->setValue(DEFAULT_GAS_LIMIT_OP_SEND);
57  ui->confirmButton->setEnabled(false);
58 
59  // Connect signals with slots
60  connect(ui->lineEditPayTo, SIGNAL(textChanged(QString)), SLOT(on_updateConfirmButton()));
61  connect(ui->lineEditAmount, SIGNAL(valueChanged()), SLOT(on_updateConfirmButton()));
62  connect(ui->confirmButton, SIGNAL(clicked()), SLOT(on_confirmClicked()));
63 
65 }
66 
68 {
69  delete ui;
70 
71  if(m_tokenABI)
72  delete m_tokenABI;
73  m_tokenABI = 0;
74 }
75 
77 {
78  m_model = _model;
79 }
80 
82 {
83  m_clientModel = _clientModel;
84 
85  if (m_clientModel)
86  {
87  connect(m_clientModel, SIGNAL(tipChanged()), this, SLOT(on_numBlocksChanged()));
89  }
90 }
91 
93 {
94  ui->lineEditPayTo->setText("");
96  ui->lineEditDescription->setText("");
97  ui->lineEditGasLimit->setValue(DEFAULT_GAS_LIMIT_OP_SEND);
98  ui->lineEditGasPrice->setValue(DEFAULT_GAS_PRICE);
99 }
100 
102 {
104  return ui->lineEditPayTo->isValid();
105 }
106 
108 {
109  bool dataValid = true;
110 
111  if(!isValidAddress())
112  dataValid = false;
113  if(!ui->lineEditAmount->validate())
114  dataValid = false;
115  if(ui->lineEditAmount->value(0) <= 0)
116  {
117  ui->lineEditAmount->setValid(false);
118  dataValid = false;
119  }
120  return dataValid;
121 }
122 
124 {
125  clearAll();
126 }
127 
129 {
130  if(m_clientModel)
131  {
132  uint64_t blockGasLimit = 0;
133  uint64_t minGasPrice = 0;
134  uint64_t nGasPrice = 0;
135  m_clientModel->getGasInfo(blockGasLimit, minGasPrice, nGasPrice);
136 
137  ui->labelGasLimit->setToolTip(tr("Gas limit: Default = %1, Max = %2.").arg(DEFAULT_GAS_LIMIT_OP_SEND).arg(blockGasLimit));
138  ui->labelGasPrice->setToolTip(tr("Gas price: FABCOIN price per gas unit. Default = %1, Min = %2.").arg(QString::fromStdString(FormatMoney(DEFAULT_GAS_PRICE))).arg(QString::fromStdString(FormatMoney(minGasPrice))));
139  ui->lineEditGasPrice->setMinimum(minGasPrice);
140  ui->lineEditGasLimit->setMaximum(blockGasLimit);
141  }
142 }
143 
145 {
146  bool enabled = true;
147  if(ui->lineEditPayTo->text().isEmpty() || ui->lineEditAmount->text().isEmpty())
148  {
149  enabled = false;
150  }
151 
152  ui->confirmButton->setEnabled(enabled);
153 }
154 
156 {
157  if(!isDataValid())
158  return;
159 
161  if(!ctx.isValid())
162  {
163  return;
164  }
165 
167  {
168  int unit = m_model->getOptionsModel()->getDisplayUnit();
169  uint64_t gasLimit = ui->lineEditGasLimit->value();
170  CAmount gasPrice = ui->lineEditGasPrice->value();
171  std::string label = ui->lineEditDescription->text().trimmed().toStdString();
172 
175  m_tokenABI->setGasLimit(QString::number(gasLimit).toStdString());
176  m_tokenABI->setGasPrice(FabcoinUnits::format(unit, gasPrice, false, FabcoinUnits::separatorNever).toStdString());
177 
178  std::string toAddress = ui->lineEditPayTo->text().toStdString();
179  std::string amountToSend = ui->lineEditAmount->text().toStdString();
181 
182  QString questionString = tr("Are you sure you want to send? <br /><br />");
183  questionString.append(tr("<b>%1 %2 </b> to ")
184  .arg(amountFormated).arg(QString::fromStdString(m_selectedToken->symbol)));
185  questionString.append(tr("<br />%3 <br />")
186  .arg(QString::fromStdString(toAddress)));
187 
188  SendConfirmationDialog confirmationDialog(tr("Confirm send token."), questionString, 3, this);
189  confirmationDialog.exec();
190  QMessageBox::StandardButton retval = (QMessageBox::StandardButton)confirmationDialog.result();
191  if(retval == QMessageBox::Yes)
192  {
193  if(m_tokenABI->transfer(toAddress, amountToSend, true))
194  {
195  CTokenTx tokenTx;
198  tokenTx.strReceiverAddress = toAddress;
199  dev::u256 nValue(amountToSend);
200  tokenTx.nValue = u256Touint(nValue);
202  tokenTx.strLabel = label;
203  m_model->addTokenTxEntry(tokenTx);
204  }
205  clearAll();
206  }
207  }
208  else
209  {
210  QString message = tr("To send %1 you need FABCOIN on address <br /> %2.")
211  .arg(QString::fromStdString(m_selectedToken->symbol)).arg(QString::fromStdString(CFabcoinAddress(m_selectedToken->sender).ToString()));
212 
213  QMessageBox::warning(this, tr("Send token"), message);
214  }
215 }
216 
217 void SendTokenPage::setTokenData(std::string address, std::string sender, std::string symbol, int8_t decimals, std::string balance)
218 {
219  // Update data with the current token
220  int decimalDiff = decimals - m_selectedToken->decimals;
222  m_selectedToken->sender = sender;
223  m_selectedToken->symbol = symbol;
224  m_selectedToken->decimals = decimals;
226 
227  // Convert values for different number of decimals
228  int256_t totalSupply(balance);
229  int256_t value(ui->lineEditAmount->value());
230  if(value != 0)
231  {
232  for(int i = 0; i < decimalDiff; i++)
233  {
234  value *= 10;
235  }
236  for(int i = decimalDiff; i < 0; i++)
237  {
238  value /= 10;
239  }
240  }
241  if(value > totalSupply)
242  {
243  value = totalSupply;
244  }
245 
246  // Update the amount field with the current token data
247  ui->lineEditAmount->clear();
248  ui->lineEditAmount->setDecimalUnits(decimals);
249  ui->lineEditAmount->setTotalSupply(totalSupply);
250  if(value != 0)
251  {
252  ui->lineEditAmount->setValue(value);
253  }
254 }
void setDecimalUnits(int value)
std::string symbol
void setTotalSupply(const int256_t &value)
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
std::string strReceiverAddress
Definition: wallet.h:1357
QSpinBox * lineEditGasLimit
std::string address
TokenAmountField * lineEditAmount
SendTokenPage(QWidget *parent=0)
struct evm_uint256be balance(struct evm_env *env, struct evm_uint160be address)
Definition: capi.c:7
SelectedToken * m_selectedToken
Definition: sendtokenpage.h:42
WalletModel * m_model
Definition: sendtokenpage.h:39
UnlockContext requestUnlock()
std::string balance
void setValue(const int256_t &value)
std::string getTxId()
Definition: token.cpp:259
base58-encoded Fabcoin addresses.
Definition: base58.h:104
void setSingleStep(const CAmount &step)
Set single step in satoshis.
void on_confirmClicked()
void on_numBlocksChanged()
Definition: token.h:32
void on_clearButton_clicked()
void clear()
Make field empty and ready for new input.
void setTokenData(std::string address, std::string sender, std::string symbol, int8_t decimals, std::string balance)
void setClientModel(ClientModel *clientModel)
void on_updateConfirmButton()
void setupUi(QWidget *SendTokenPage)
bool validate()
Perform input validation, mark field as invalid if entered value is not valid.
std::string strContractAddress
Definition: wallet.h:1355
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
QLineEdit * lineEditDescription
uint256 uint256S(const char *str)
Definition: uint256.h:153
QPushButton * confirmButton
void setAddress(const std::string &address)
Definition: token.cpp:217
void setValid(bool valid)
Mark current value as invalid in UI.
QLabel * labelDescription
std::string ToString() const
Definition: base58.cpp:193
void setGasLimit(const std::string &gaslimit)
Definition: token.cpp:232
void setGasPrice(const std::string &gasPrice)
Definition: token.cpp:237
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
Definition: Common.h:125
int getDisplayUnit()
Definition: optionsmodel.h:70
FabcoinAmountField * lineEditGasPrice
Model for Fabcoin network client.
Definition: clientmodel.h:38
std::string FormatMoney(const CAmount &n)
Money parsing/formatting utilities.
Address toAddress(Public const &_public)
Convert a public key to address.
Definition: Common.cpp:87
bool transfer(const std::string &_to, const std::string &_value, bool sendTo=false)
Definition: token.cpp:451
std::string strLabel
Definition: wallet.h:1365
static QString formatToken(int decimal_units, const int256_t &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format token as string.
QValidatedLineEdit * lineEditPayTo
bool isUnspentAddress(const std::string &address) const
uint256 u256Touint(const dev::u256 &in)
Definition: uint256.h:191
void setValue(const CAmount &value)
QPushButton * clearButton
ClientModel * m_clientModel
Definition: sendtokenpage.h:40
Interface to Fabcoin wallet from Qt view code.
Definition: walletmodel.h:103
void setCheckValidator(const QValidator *v)
QString text() const
Ui::SendTokenPage * ui
Definition: sendtokenpage.h:38
std::string sender
uint256 nValue
Definition: wallet.h:1358
std::string strSenderAddress
Definition: wallet.h:1356
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
void setModel(WalletModel *_model)
uint256 transactionHash
Definition: wallet.h:1359
void setMinimum(const CAmount &min)
void setSender(const std::string &sender)
Definition: token.cpp:242
Token * m_tokenABI
Definition: sendtokenpage.h:41
Fabcoin address widget validator, checks for a valid fabcoin address.
bool addTokenTxEntry(const CTokenTx &tokenTx, bool fFlushOnClose=true)
OptionsModel * getOptionsModel()
void getGasInfo(uint64_t &blockGasLimit, uint64_t &minGasPrice, uint64_t &nGasPrice) const
Get the information about the needed gas.