implemented completed deposit (3 confirmations) notifications

This commit is contained in:
Akinwale Ariwodola 2017-09-25 17:10:34 +01:00
parent 929c1ea9ae
commit 2395ad3b66
3 changed files with 57 additions and 3 deletions

43
app.js
View file

@ -28,6 +28,7 @@ const messageTemplates = {};
const templateNames = [
'onbalance',
'ondeposit',
'ondeposit.completed',
'ongild',
'ongild.insufficientfunds',
'onsendtip',
@ -148,6 +149,41 @@ const createOrGetUserId = (username, callback) => {
], callback);
};
const processCompletedDeposits = (callback) => {
async.waterfall([
(cb) => {
db.query('SELECT C.DepositId, D.Amount, U.Username, U.Balance FROM CompletedDepositConfirmations C JOIN Deposits D ON D.Id = C.DepositId JOIN Users U ON U.Id = C.UserId', cb);
},
(res, fields, cb) => {
if (res.length > 0) {
return async.eachSeries(res, (completedDeposit, ecb) => {
setTimeout(() => {
sendPMUsingTemplate('ondeposit.complete', { amount: completedDeposit.Amount, balance: completedDeposit.Balance },
'Deposit completed!', completedDeposit.Username, (err) => {
if (err) {
return ecb(err, null);
}
// remove the entry from the DB
return db.query('DELETE FROM CompletedDepositConfirmations WHERE DepositId = ?', [C.DepositId], (ierr) => {
if (ierr) {
return ecb(ierr, null);
}
// success
return ecb(null, true);
});
});
}, 2000); // Wait 2 seconds between each request (if there are multiple to send)
// TODO: Implement inserting messages into a pending message queue instead
}, cb);
}
return cb(null, null);
}
], callback);
};
const getBalance = (userId, callback) => {
db.query('SELECT Balance FROM Users WHERE Id = ?', [userId], (err, res) => {
if (err) {
@ -953,7 +989,7 @@ const processMessage = function(message, callback) {
const runBot = () => {
async.waterfall([
(cb) => {
if (!accessTokenTime || moment.duration(moment().diff(accessTokenTime)).asMinutes() >= 55) {
if (!accessTokenTime || moment.duration(moment().diff(accessTokenTime)).asMinutes() >= 59) {
// remove old or expired tokens
// TODO: Implement refreshToken
if (fs.existsSync(config.accessTokenPath)) {
@ -975,7 +1011,10 @@ const runBot = () => {
},
(token, cb) => {
globalAccessToken = token;
retrieveUnreadMessages(token, cb);
processCompletedDeposits(cb);
},
(success, cb) => {
retrieveUnreadMessages(globalAccessToken, cb);
},
(unread, cb) => {
async.eachSeries(unread, (message, ecb) => {

View file

@ -52,6 +52,15 @@ CREATE TABLE PendingMessageQueue
PRIMARY KEY `PK_PendingMessageId` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
CREATE TABLE CompletedDepositConfirmations
(
`DepositId` BIGINT UNSIGNED NOT NULL,
`UserId` BIGINT UNSIGNED NOT NULL,
PRIMARY KEY `PK_DepositConfirmationId` (`DepositId`),
FOREIGN KEY `FK_CompletedDepositConfirmation` (`DepositId`) REFERENCES `Deposits` (`Id`),
FOREIGN KEY `FK_CompletedDepositUser` (`UserId`) REFERENCES `Users` (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE utf8mb4_unicode_ci ROW_FORMAT=COMPRESSED KEY_BLOCK_SIZE=4;
CREATE TABLE Deposits
(
`Id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
@ -86,6 +95,7 @@ FOR EACH ROW
BEGIN
IF NEW.Confirmations >= 3 THEN
UPDATE Users U SET U.Balance = U.Balance + NEW.Amount WHERE U.Id = NEW.UserId;
INSERT INTO CompletedDepositConfirmations (DepositId, UserId) VALUES (NEW.Id, NEW.UserId);
END IF;
END;
@ -94,7 +104,8 @@ CREATE TRIGGER `Trg_OnDepositUpdated`
FOR EACH ROW
BEGIN
IF OLD.Confirmations < 3 AND NEW.Confirmations >= 3 THEN
UPDATE Users U SET U.Balance = U.Balance + NEW.Amount WHERE U.Id = NEW.UserId;
UPDATE Users U SET U.Balance = U.Balance + OLD.Amount WHERE U.Id = OLD.UserId;
INSERT INTO CompletedDepositConfirmations (DepositId, UserId) VALUES (OLD.Id, OLD.UserId);
END IF;
END;
//

View file

@ -0,0 +1,4 @@
Great news! Your **{amount} LBC** deposit was completed successfully with 3 confirmations. Your new balance is **{balance} LBC**.
----
[^How ^to ^use]({how_to_use_url}) ^&bull; [^What ^is ^LBRY?](https://lbry.io/faq/what-is-lbry) ^&bull; ^r/lbry