Skip to content

Commit 1ecb9b0

Browse files
committed
Transfer funds from account to account with self spend
1 parent 2f8c6e1 commit 1ecb9b0

File tree

7 files changed

+128
-41
lines changed

7 files changed

+128
-41
lines changed

state/k_AccountTransfer.cpp

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ AccountTransfer::AccountTransfer( StateContext * context) :
3131

3232
// using static connection. Lock flag transferInProgress will be used to switch the processing
3333

34-
connect( context->wallet, &wallet::Wallet::onSetReceiveAccount, this, &AccountTransfer::onSetReceiveAccount, Qt::QueuedConnection );
3534
connect( context->wallet, &wallet::Wallet::onSend, this, &AccountTransfer::onSend, Qt::QueuedConnection );
3635
connect( context->wallet, &wallet::Wallet::onWalletBalanceUpdated, this, &AccountTransfer::onWalletBalanceUpdated, Qt::QueuedConnection );
3736
connect( context->wallet, &wallet::Wallet::onNodeStatus, this, &AccountTransfer::onNodeStatus, Qt::QueuedConnection);
@@ -109,15 +108,6 @@ bool AccountTransfer::transferFunds(const QString & from,
109108
return false;
110109
}
111110

112-
myAddress = context->wallet->getMqsAddress();
113-
114-
// mwc mq expected to be online, we will use it for slate exchange
115-
if (myAddress.isEmpty() || !context->wallet->getListenerStatus().mqs) {
116-
for (auto b : bridge::getBridgeManager()->getAccountTransfer())
117-
b->showTransferResults(false, "Please turn on MWC MQS listener. We can't transfer funds in offline mode");
118-
return false;
119-
}
120-
121111
QStringList outputs; // empty is valid value. Empty - mwc713 will use default algorithm.
122112
uint64_t txnFee = 0; // not used here yet
123113
// nanoCoins < 0 - All
@@ -156,9 +146,10 @@ bool AccountTransfer::transferFunds(const QString & from,
156146
trNanoCoins = nanoCoins;
157147
outputs2use = outputs;
158148

159-
transferState = 0;
160-
recieveAccount = context->wallet->getReceiveAccount();
161-
context->wallet->setReceiveAccount( trAccountTo );
149+
transferState=1;
150+
151+
bool fluff = context->appContext->isFluffSet();
152+
context->wallet->selfSend( trAccountFrom, trAccountTo, trNanoCoins, outputs2use, fluff );
162153

163154
return true;
164155
}
@@ -167,27 +158,6 @@ void AccountTransfer::goBack() {
167158
context->stateMachine->setActionWindow( STATE::ACCOUNTS );
168159
}
169160

170-
// set receive account name results
171-
void AccountTransfer::onSetReceiveAccount( bool ok, QString AccountOrMessage ) {
172-
if (transferState!=0)
173-
return;
174-
175-
if (!ok) {
176-
for (auto b : bridge::getBridgeManager()->getAccountTransfer())
177-
b->showTransferResults(false, "Failed to set receive account. " + AccountOrMessage);
178-
transferState = -1;
179-
return;
180-
}
181-
182-
transferState=1;
183-
184-
core::SendCoinsParams prms = context->appContext->getSendCoinsParams();
185-
bool fluff = context->appContext->isFluffSet();
186-
context->wallet->sendTo( trAccountFrom, trNanoCoins, util::fullFormalAddress( util::ADDRESS_TYPE::MWC_MQ, myAddress), "", "",
187-
prms.inputConfirmationNumber, prms.changeOutputs, outputs2use, fluff, -1, false, "" );
188-
}
189-
190-
191161
void AccountTransfer::onSend( bool success, QStringList errors, QString address, int64_t txid, QString slate, QString mwc ) {
192162
Q_UNUSED(txid);
193163
Q_UNUSED(slate);
@@ -197,13 +167,11 @@ void AccountTransfer::onSend( bool success, QStringList errors, QString address,
197167
if (transferState!=1)
198168
return;
199169

200-
if (!recieveAccount.isEmpty())
201-
context->wallet->setReceiveAccount( recieveAccount );
202170
context->wallet->updateWalletBalance(true, true);
203171

204172
if (!success) {
205173
for (auto b : bridge::getBridgeManager()->getAccountTransfer())
206-
b->showTransferResults(false, "Failed to send the funds. " + util::formatErrorMessages(errors) );
174+
b->showTransferResults(false, "Failed to transfer the funds. " + util::formatErrorMessages(errors) );
207175
transferState = -1;
208176
return;
209177
}
@@ -221,6 +189,8 @@ void AccountTransfer::onWalletBalanceUpdated() {
221189

222190
// Done, finally
223191
transferState = -1;
192+
193+
context->stateMachine->setActionWindow( STATE::ACCOUNTS );
224194
}
225195

226196
void AccountTransfer::onNodeStatus( bool online, QString errMsg, int nodeHeight, int peerHeight, int64_t totalDifficulty, int connections ) {

state/k_AccountTransfer.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Q_OBJECT
4242

4343
private slots:
4444
// set receive account name results
45-
void onSetReceiveAccount( bool ok, QString AccountOrMessage );
4645
void onSend( bool success, QStringList errors, QString address, int64_t txid, QString slate, QString mwc );
4746
void onWalletBalanceUpdated();
4847

@@ -52,9 +51,7 @@ private slots:
5251
int transferState = -1;
5352
bool nodeIsHealthy = false;
5453

55-
QString recieveAccount;
5654
// Single transfer context
57-
QString myAddress;
5855
QString trAccountFrom;
5956
QString trAccountTo;
6057
int64_t trNanoCoins = 0;

wallet/mwc713.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,19 @@ void MWC713::sendTo(const QString &account, int64_t coinNano, const QString &add
764764
eventCollector->addTask(TASK_PRIORITY::TASK_NORMAL, taskGroup);
765765
}
766766

767+
// Self seld for accounts transfres
768+
// Check signal: onSend
769+
void MWC713::selfSend( const QString &accountFrom, const QString &accountTo, int64_t coinNano, const QStringList & outputs, bool fluff ) {
770+
QVector<QPair<Mwc713Task *, int64_t>> taskGroup{
771+
TSK(new TaskAccountSwitch(this, accountFrom), TaskAccountSwitch::TIMEOUT),
772+
TSK(new TaskSelfSendMwc( this, accountTo, coinNano, outputs,fluff ), TaskSelfSendMwc::TIMEOUT)
773+
};
774+
if (accountFrom != currentAccount)
775+
taskGroup.push_back(TSK(new TaskAccountSwitch(this, currentAccount), TaskAccountSwitch::TIMEOUT));
776+
777+
eventCollector->addTask(TASK_PRIORITY::TASK_NORMAL, taskGroup);
778+
}
779+
767780

768781
// Init send transaction with file output
769782
// Check signal: onSendFile
@@ -2520,7 +2533,7 @@ void MWC713::setWalletOutputs(const QString &account, const QVector<wallet::Wall
25202533
void MWC713::timerEvent(QTimerEvent *event) {
25212534
Q_UNUSED(event)
25222535
if (isWalletRunningAndLoggedIn()) {
2523-
if (torStarted) {
2536+
if (torStarted && torOnline) {
25242537
// Checking tor connection
25252538
eventCollector->addTask(TASK_PRIORITY::TASK_NORMAL,
25262539
{TSK(new TaskCheckTorConnection(this), TaskCheckTorConnection::TIMEOUT)});

wallet/mwc713.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,10 @@ class MWC713 : public Wallet
256256
QString message, int inputConfirmationNumber, int changeOutputs,
257257
const QStringList & outputs, bool fluff, int ttl_blocks, bool generateProof, QString expectedproofAddress ) override;
258258

259+
// Self seld for accounts transfres
260+
// Check signal: onSend
261+
virtual void selfSend( const QString &accountFrom, const QString &accountTo, int64_t coinNano, const QStringList & outputs, bool fluff ) override;
262+
259263
// Show outputs for the wallet
260264
// Check Signal: onOutputs( QString account, int64_t height, QVector<WalletOutput> outputs)
261265
virtual void getOutputs(QString account, bool show_spent, bool enforceSync) override;

wallet/tasks/TaskSend.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,84 @@ QString TaskSendMwc::buildCommand( int64_t coinNano, const QString & address, co
188188
return cmd;
189189
}
190190

191+
// ------------------------ TaskSelfSendMwc --------------------------------
192+
193+
bool TaskSelfSendMwc::processTask(const QVector<WEvent> &events) {
194+
195+
// slate [b9c559a8-e134-4af4-a77a-a2118ed74a90] received from [http listener] for [0.012345000] MWCs.
196+
197+
QString slate;
198+
QString mwc;
199+
200+
{
201+
QVector< WEvent > lns = filterEvents(events, WALLET_EVENTS::S_LINE );
202+
// Parsing for txId - index of transaction that was created
203+
for (auto &ln : lns) {
204+
205+
if (ln.message.startsWith("slate") && ln.message.contains("received from") ) {
206+
int idx1 = ln.message.indexOf('[');
207+
int idx2 = ln.message.indexOf(']', idx1+1);
208+
if (idx1>0 && idx2>0)
209+
slate = ln.message.mid(idx1+1, idx2-idx1-1);
210+
211+
idx1 = ln.message.indexOf("for [");
212+
idx2 = ln.message.indexOf(']', idx1+1);
213+
if (idx1>0 && idx2>0) {
214+
idx1 += int(strlen("for ["));
215+
mwc = ln.message.mid(idx1, idx2 - idx1);
216+
mwc = util::zeroDbl2Dbl(mwc);
217+
}
218+
}
219+
}
220+
}
221+
222+
if ( !slate.isEmpty() && !mwc.isEmpty() ) {
223+
wallet713->setSendResults(true, QStringList(), "", -1, slate, mwc);
224+
return true;
225+
}
226+
227+
QStringList errMsgs;
228+
QVector< WEvent > errs = filterEvents(events, WALLET_EVENTS::S_GENERIC_ERROR );
229+
230+
for (WEvent & evt : errs) {
231+
errMsgs.push_back(evt.message);
232+
}
233+
234+
if (errMsgs.isEmpty())
235+
errMsgs.push_back("Not found expected output from mwc713");
236+
237+
wallet713->setSendResults( false, errMsgs, "", -1, "", "" );
238+
return true;
239+
}
240+
241+
QString TaskSelfSendMwc::buildCommand(const QString & accountTo, int64_t coinNano, const QStringList & outputs, bool fluff) const {
242+
243+
QString cmd = "send --self ";// + util::nano2one(coinNano);
244+
if (coinNano>0)
245+
cmd += util::nano2one(coinNano);
246+
247+
if (!outputs.isEmpty()) {
248+
cmd += " --confirmations 1 --strategy custom --outputs " + outputs.join(",");
249+
}
250+
else {
251+
cmd += " --confirmations 1";
252+
}
253+
254+
// So far documentation doesn't specify difference between protocols
255+
cmd += " --to " + util::toMwc713input(accountTo);
256+
257+
if (fluff) {
258+
cmd += " --fluff";
259+
}
260+
261+
if (coinNano<0)
262+
cmd += " ALL";
263+
264+
qDebug() << "sendCommand: '" << cmd << "'";
265+
266+
return cmd;
267+
}
268+
191269
// ----------------------- TaskSendFile --------------------------
192270

193271
QString TaskSendFile::buildCommand( int64_t coinNano, QString message, QString fileTx, int inputConfirmationNumber, int changeOutputs, const QStringList & outputs, int ttl_blocks, bool generateProof) const {

wallet/tasks/TaskSend.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ class TaskSendMwc : public QObject, public Mwc713Task {
7979
int64_t sendMwcNano;
8080
};
8181

82+
class TaskSelfSendMwc : public QObject, public Mwc713Task {
83+
Q_OBJECT
84+
public:
85+
const static int64_t TIMEOUT = 1000*30; // No transports are involved, might take longer because fo bad node connection
86+
87+
// coinNano == -1 - mean All
88+
TaskSelfSendMwc( MWC713 *wallet713, const QString & accountTo, int64_t coinNano, const QStringList & outputs, bool fluff ) :
89+
Mwc713Task("TaskSendMwc", "Sending coins to self...",
90+
buildCommand( accountTo, coinNano, outputs, fluff), wallet713, "") {}
91+
92+
virtual ~TaskSelfSendMwc() override {}
93+
94+
virtual bool processTask(const QVector<WEvent> &events) override;
95+
96+
virtual QSet<WALLET_EVENTS> getReadyEvents() override {return QSet<WALLET_EVENTS>{ WALLET_EVENTS::S_READY };}
97+
private:
98+
// coinNano == -1 - mean All
99+
QString buildCommand(const QString & accountTo, int64_t coinNano, const QStringList & outputs, bool fluff) const;
100+
};
101+
82102

83103
class TaskSendFile : public Mwc713Task {
84104
public:

wallet/wallet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,11 @@ class Wallet : public QObject
710710
virtual void sendTo( const QString &account, int64_t coinNano, const QString & address, const QString & apiSecret,
711711
QString message, int inputConfirmationNumber, int changeOutputs, const QStringList & outputs, bool fluff, int ttl_blocks, bool generateProof, QString expectedproofAddress ) = 0;
712712

713+
// Self seld for accounts transfres
714+
// Check signal: onSend
715+
virtual void selfSend( const QString &accountFrom, const QString &accountTo, int64_t coinNano, const QStringList & outputs, bool fluff ) = 0;
716+
717+
713718
// Airdrop special. Generating the next Public key for transaction
714719
// wallet713> getnextkey --amount 1000000
715720
// "Identifier(0300000000000000000000000600000000), PublicKey(38abad70a72fba1fab4b4d72061f220c0d2b4dafcc8144e778376098575c965f5526b57e1c34624da2dc20dde2312696e7cf8da676e33376aefcc4742ed9cb79)"

0 commit comments

Comments
 (0)