mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-29 22:22:54 +02:00
send and confirm transactions to assigned nodes from DS
This commit is contained in:
parent
908eb5cc4f
commit
1dddabb691
5 changed files with 109 additions and 13 deletions
|
@ -739,15 +739,58 @@ void DomainServer::setupPendingAssignmentCredits() {
|
||||||
}
|
}
|
||||||
|
|
||||||
void DomainServer::sendPendingTransactionsToServer() {
|
void DomainServer::sendPendingTransactionsToServer() {
|
||||||
|
|
||||||
|
AccountManager& accountManager = AccountManager::getInstance();
|
||||||
|
|
||||||
|
if (accountManager.hasValidAccessToken()) {
|
||||||
|
|
||||||
// enumerate the pending transactions and send them to the server to complete payment
|
// enumerate the pending transactions and send them to the server to complete payment
|
||||||
TransactionHash::iterator i = _pendingAssignmentCredits.begin();
|
TransactionHash::iterator i = _pendingAssignmentCredits.begin();
|
||||||
|
|
||||||
|
JSONCallbackParameters transactionCallbackParams;
|
||||||
|
|
||||||
|
transactionCallbackParams.jsonCallbackReceiver = this;
|
||||||
|
transactionCallbackParams.jsonCallbackMethod = "transactionJSONCallback";
|
||||||
|
|
||||||
while (i != _pendingAssignmentCredits.end()) {
|
while (i != _pendingAssignmentCredits.end()) {
|
||||||
|
accountManager.authenticatedRequest("api/v1/transactions", QNetworkAccessManager::PostOperation,
|
||||||
|
transactionCallbackParams, i.value()->postJson().toJson());
|
||||||
|
|
||||||
|
// set this transaction to finalized so we don't add additional credits to it
|
||||||
|
i.value()->setIsFinalized(true);
|
||||||
|
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void DomainServer::transactionJSONCallback(const QJsonObject& data) {
|
||||||
|
// check if this was successful - if so we can remove it from our list of pending
|
||||||
|
if (data.value("status").toString() == "success") {
|
||||||
|
// create a dummy wallet transaction to unpack the JSON to
|
||||||
|
WalletTransaction dummyTransaction;
|
||||||
|
dummyTransaction.loadFromJson(data);
|
||||||
|
|
||||||
|
TransactionHash::iterator i = _pendingAssignmentCredits.find(dummyTransaction.getDestinationUUID());
|
||||||
|
|
||||||
|
while (i != _pendingAssignmentCredits.end() && i.key() == dummyTransaction.getDestinationUUID()) {
|
||||||
|
if (i.value()->getUUID() == dummyTransaction.getUUID()) {
|
||||||
|
// we have a match - we can remove this from the hash of pending credits
|
||||||
|
// and delete it for clean up
|
||||||
|
|
||||||
|
WalletTransaction* matchingTransaction = i.value();
|
||||||
|
_pendingAssignmentCredits.erase(i);
|
||||||
|
delete matchingTransaction;
|
||||||
|
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void DomainServer::processDatagram(const QByteArray& receivedPacket, const HifiSockAddr& senderSockAddr) {
|
void DomainServer::processDatagram(const QByteArray& receivedPacket, const HifiSockAddr& senderSockAddr) {
|
||||||
LimitedNodeList* nodeList = LimitedNodeList::getInstance();
|
LimitedNodeList* nodeList = LimitedNodeList::getInstance();
|
||||||
|
|
||||||
|
@ -907,6 +950,24 @@ bool DomainServer::handleHTTPRequest(HTTPConnection* connection, const QUrl& url
|
||||||
connection->respond(HTTPConnection::StatusCode200, assignmentDocument.toJson(), qPrintable(JSON_MIME_TYPE));
|
connection->respond(HTTPConnection::StatusCode200, assignmentDocument.toJson(), qPrintable(JSON_MIME_TYPE));
|
||||||
|
|
||||||
// we've processed this request
|
// we've processed this request
|
||||||
|
return true;
|
||||||
|
} else if (url.path() == "/transactions.json") {
|
||||||
|
// enumerate our pending transactions and display them in an array
|
||||||
|
QJsonObject rootObject;
|
||||||
|
QJsonArray transactionArray;
|
||||||
|
|
||||||
|
TransactionHash::iterator i = _pendingAssignmentCredits.begin();
|
||||||
|
while (i != _pendingAssignmentCredits.end()) {
|
||||||
|
transactionArray.push_back(i.value()->toJson());
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
|
||||||
|
rootObject["pending_transactions"] = transactionArray;
|
||||||
|
|
||||||
|
// print out the created JSON
|
||||||
|
QJsonDocument transactionsDocument(rootObject);
|
||||||
|
connection->respond(HTTPConnection::StatusCode200, transactionsDocument.toJson(), qPrintable(JSON_MIME_TYPE));
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
} else if (url.path() == QString("%1.json").arg(URI_NODES)) {
|
} else if (url.path() == QString("%1.json").arg(URI_NODES)) {
|
||||||
// setup the JSON
|
// setup the JSON
|
||||||
|
|
|
@ -47,6 +47,8 @@ public slots:
|
||||||
/// Called by NodeList to inform us a node has been killed
|
/// Called by NodeList to inform us a node has been killed
|
||||||
void nodeKilled(SharedNodePointer node);
|
void nodeKilled(SharedNodePointer node);
|
||||||
|
|
||||||
|
void transactionJSONCallback(const QJsonObject& data);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void loginFailed();
|
void loginFailed();
|
||||||
void readAvailableDatagrams();
|
void readAvailableDatagrams();
|
||||||
|
|
|
@ -15,6 +15,15 @@
|
||||||
|
|
||||||
#include "WalletTransaction.h"
|
#include "WalletTransaction.h"
|
||||||
|
|
||||||
|
WalletTransaction::WalletTransaction() :
|
||||||
|
_uuid(),
|
||||||
|
_destinationUUID(),
|
||||||
|
_amount(),
|
||||||
|
_isFinalized(false)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
WalletTransaction::WalletTransaction(const QUuid& destinationUUID, double amount) :
|
WalletTransaction::WalletTransaction(const QUuid& destinationUUID, double amount) :
|
||||||
_uuid(QUuid::createUuid()),
|
_uuid(QUuid::createUuid()),
|
||||||
_destinationUUID(destinationUUID),
|
_destinationUUID(destinationUUID),
|
||||||
|
@ -24,20 +33,35 @@ WalletTransaction::WalletTransaction(const QUuid& destinationUUID, double amount
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QJsonDocument WalletTransaction::postJson() {
|
const QString TRANSACTION_ID_KEY = "id";
|
||||||
QJsonObject rootObject;
|
|
||||||
QJsonObject transactionObject;
|
|
||||||
|
|
||||||
const QString TRANSCATION_ID_KEY = "id";
|
|
||||||
const QString TRANSACTION_DESTINATION_WALLET_ID_KEY = "destination_wallet_id";
|
const QString TRANSACTION_DESTINATION_WALLET_ID_KEY = "destination_wallet_id";
|
||||||
const QString TRANSACTION_AMOUNT_KEY = "amount";
|
const QString TRANSACTION_AMOUNT_KEY = "amount";
|
||||||
|
|
||||||
transactionObject.insert(TRANSCATION_ID_KEY, uuidStringWithoutCurlyBraces(_uuid));
|
|
||||||
transactionObject.insert(TRANSACTION_DESTINATION_WALLET_ID_KEY, uuidStringWithoutCurlyBraces(_destinationUUID));
|
|
||||||
transactionObject.insert(TRANSACTION_AMOUNT_KEY, _amount);
|
|
||||||
|
|
||||||
const QString ROOT_OBJECT_TRANSACTION_KEY = "transaction";
|
const QString ROOT_OBJECT_TRANSACTION_KEY = "transaction";
|
||||||
rootObject.insert(ROOT_OBJECT_TRANSACTION_KEY, transactionObject);
|
|
||||||
|
QJsonDocument WalletTransaction::postJson() {
|
||||||
|
QJsonObject rootObject;
|
||||||
|
|
||||||
|
rootObject.insert(ROOT_OBJECT_TRANSACTION_KEY, toJson());
|
||||||
|
|
||||||
return QJsonDocument(rootObject);
|
return QJsonDocument(rootObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject WalletTransaction::toJson() {
|
||||||
|
QJsonObject transactionObject;
|
||||||
|
|
||||||
|
transactionObject.insert(TRANSACTION_ID_KEY, uuidStringWithoutCurlyBraces(_uuid));
|
||||||
|
transactionObject.insert(TRANSACTION_DESTINATION_WALLET_ID_KEY, uuidStringWithoutCurlyBraces(_destinationUUID));
|
||||||
|
transactionObject.insert(TRANSACTION_AMOUNT_KEY, _amount);
|
||||||
|
|
||||||
|
return transactionObject;
|
||||||
|
}
|
||||||
|
|
||||||
|
void WalletTransaction::loadFromJson(const QJsonObject& jsonObject) {
|
||||||
|
// pull the destination wallet and ID of the transaction to match it
|
||||||
|
QJsonObject transactionObject = jsonObject.value("data").toObject().value(ROOT_OBJECT_TRANSACTION_KEY).toObject();
|
||||||
|
|
||||||
|
_uuid = QUuid(transactionObject.value(TRANSACTION_ID_KEY).toString());
|
||||||
|
_destinationUUID = QUuid(transactionObject.value(TRANSACTION_DESTINATION_WALLET_ID_KEY).toString());
|
||||||
|
_amount = transactionObject.value(TRANSACTION_AMOUNT_KEY).toDouble();
|
||||||
|
}
|
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
class WalletTransaction : public QObject {
|
class WalletTransaction : public QObject {
|
||||||
public:
|
public:
|
||||||
|
WalletTransaction();
|
||||||
WalletTransaction(const QUuid& destinationUUID, double amount);
|
WalletTransaction(const QUuid& destinationUUID, double amount);
|
||||||
|
|
||||||
const QUuid& getUUID() const { return _uuid; }
|
const QUuid& getUUID() const { return _uuid; }
|
||||||
|
@ -33,6 +34,8 @@ public:
|
||||||
void setIsFinalized(bool isFinalized) { _isFinalized = isFinalized; }
|
void setIsFinalized(bool isFinalized) { _isFinalized = isFinalized; }
|
||||||
|
|
||||||
QJsonDocument postJson();
|
QJsonDocument postJson();
|
||||||
|
QJsonObject toJson();
|
||||||
|
void loadFromJson(const QJsonObject& jsonObject);
|
||||||
private:
|
private:
|
||||||
QUuid _uuid;
|
QUuid _uuid;
|
||||||
QUuid _destinationUUID;
|
QUuid _destinationUUID;
|
||||||
|
|
|
@ -136,7 +136,13 @@ void AccountManager::invokedRequest(const QString& path, QNetworkAccessManager::
|
||||||
QNetworkRequest authenticatedRequest;
|
QNetworkRequest authenticatedRequest;
|
||||||
|
|
||||||
QUrl requestURL = _authURL;
|
QUrl requestURL = _authURL;
|
||||||
|
|
||||||
|
if (path.startsWith("/")) {
|
||||||
requestURL.setPath(path);
|
requestURL.setPath(path);
|
||||||
|
} else {
|
||||||
|
requestURL.setPath("/" + path);
|
||||||
|
}
|
||||||
|
|
||||||
requestURL.setQuery("access_token=" + _accountInfo.getAccessToken().token);
|
requestURL.setQuery("access_token=" + _accountInfo.getAccessToken().token);
|
||||||
|
|
||||||
authenticatedRequest.setUrl(requestURL);
|
authenticatedRequest.setUrl(requestURL);
|
||||||
|
|
Loading…
Reference in a new issue