Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update wallet.js #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 63 additions & 62 deletions server/modules/users/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,77 +478,78 @@ exports.onConfirmWithdraw = function(req, res)

});

function ProcessWithdraw(userID, address, amount, coinName, callback)
{
const userAccount = utils.Encrypt(userID);

g_constants.dbTables['coins'].selectAll('ROWID AS id, *', 'name="'+coinName+'"', '', (err, rows) => {
if (err || !rows || !rows.length)
return callback({result: false, message: 'Coin "'+unescape(coinName)+'" not found'});
}

try { rows[0].info = JSON.parse(utils.Decrypt(rows[0].info));}
catch(e) {}

if (!rows[0].info || !rows[0].info.active)
return callback({result: false, message: 'Coin "'+unescape(coinName)+'" is not active'});

if (rows[0].info.withdraw == 'Disabled')
return callback({result: false, message: 'Coin "'+unescape(coinName)+'" withdraw is temporarily disabled'});

if (g_constants.share.tradeEnabled == false)
return callback({result: false, message: 'Trading is temporarily disabled'});
function ProcessWithdraw(userID, address, amount, coinName, callback) {
const userAccount = utils.Encrypt(userID);

const coin = rows[0];
const coinID = rows[0].id;

MoveBalance(g_constants.ExchangeBalanceAccountID, userID, coin, (amount*1+(rows[0].info.hold || 0.002)).toFixed(7)*1, ret => {
if (!ret || !ret.result)
return callback({result: false, message: '<b>Withdraw error (1):</b> '+ ret.message});
g_constants.dbTables['coins'].selectAll('ROWID AS id, *', 'name="' + coinName + '"', '', (err, rows) => {
if (err || !rows || !rows.length)
return callback({ result: false, message: 'Coin "' + unescape(coinName) + '" not found' });

const comment = JSON.stringify([{from: userAccount, to: address, amount: amount, time: Date.now()}]);
const walletPassphrase = g_constants.walletpassphrase(coin.ticker);

console.log('RPC call from ProcessWithdraw1');
try { rows[0].info = JSON.parse(utils.Decrypt(rows[0].info)); }
catch (e) { }

if (!rows[0].info || !rows[0].info.active)
return callback({ result: false, message: 'Coin "' + unescape(coinName) + '" is not active' });

if (rows[0].info.withdraw == 'Disabled')
return callback({ result: false, message: 'Coin "' + unescape(coinName) + '" withdraw is temporarily disabled' });

if (g_constants.share.tradeEnabled == false)
return callback({ result: false, message: 'Trading is temporarily disabled' });

const coin = rows[0];
const coinID = rows[0].id;

MoveBalance(g_constants.ExchangeBalanceAccountID, userID, coin, (amount * 1 + (rows[0].info.hold || 0.002)).toFixed(7) * 1, ret => {
if (!ret || !ret.result)
return callback({ result: false, message: '<b>Withdraw error (1):</b> ' + ret.message });

const comment = JSON.stringify([{ from: userAccount, to: address, amount: amount, time: Date.now() }]);


console.log('RPC call from ProcessWithdraw1');
if (g_constants.walletpassphrase(coin.ticker)) {
walletPassphrase = g_constants.walletpassphrase(coin.ticker);
RPC.send3(coinID, commands.walletpassphrase, [walletPassphrase, 60], ret => {
if ((!ret || !ret.result || ret.result != 'success') && ret.data && ret.data.length)
{
if ((!ret || !ret.result || ret.result != 'success') && ret.data && ret.data.length) {
const err = ret.data;
//if false then return coins to user balance
MoveBalance(userID, g_constants.ExchangeBalanceAccountID, coin, amount, ret =>{});
return callback({result: false, message: '<b>Withdraw error (2):</b> '+ err});
}

const rpcParams = (coin.ticker == 'WAVI' || coin.ticker == 'DASH') ?
[userAccount, address, (amount*1).toFixed(7)*1, coin.info.minconf || 3, false, comment] :
[userAccount, address, (amount*1).toFixed(7)*1, coin.info.minconf || 3, comment];

console.log('RPC call from ProcessWithdraw2');
RPC.send3(coinID, commands.sendfrom, rpcParams, ret => {
if (ret && ret.result && ret.result == 'success')
{
exports.ResetBalanceCache(userID);
return callback({result: true, data: ret.data});
}
//if false then try one more time
console.log('RPC call from ProcessWithdraw3');
setTimeout(RPC.send3, 5000, coinID, commands.sendfrom, rpcParams, ret => {
exports.ResetBalanceCache(userID);
if (ret && ret.result && ret.result == 'success')
return callback({result: true, data: ret.data});

const err = ret ? ret.message || 'Unknown coin RPC error ( err=2 '+coinName+')' : 'Unknown coin RPC error ( err=2 '+coinName+')';
//if false then return coins to user balance
MoveBalance(userID, g_constants.ExchangeBalanceAccountID, coin, amount, ret =>{});
callback({result: false, message: '<b>Withdraw error (3):</b> '+ err});
});

});
MoveBalance(userID, g_constants.ExchangeBalanceAccountID, coin, amount, ret => { });
return callback({ result: false, message: '<b>Withdraw error (2):</b> ' + err });
};
});
}

const rpcParams = (coin.ticker == 'WAVI' || coin.ticker == 'DASH') ?
[userAccount, address, (amount * 1).toFixed(7) * 1, coin.info.minconf || 3, false, comment] :
[userAccount, address, (amount * 1).toFixed(7) * 1, coin.info.minconf || 3, comment];

console.log('RPC call from ProcessWithdraw2');
RPC.send3(coinID, commands.sendfrom, rpcParams, ret => {
if (ret && ret.result && ret.result == 'success') {
exports.ResetBalanceCache(userID);
return callback({ result: true, data: ret.data });
}
//if false then try one more time
console.log('RPC call from ProcessWithdraw3');
setTimeout(RPC.send3, 5000, coinID, commands.sendfrom, rpcParams, ret => {
exports.ResetBalanceCache(userID);
if (ret && ret.result && ret.result == 'success')
return callback({ result: true, data: ret.data });

const err = ret ? ret.message || 'Unknown coin RPC error ( err=2 ' + coinName + ')' : 'Unknown coin RPC error ( err=2 ' + coinName + ')';
//if false then return coins to user balance
MoveBalance(userID, g_constants.ExchangeBalanceAccountID, coin, amount, ret => { });
callback({ result: false, message: '<b>Withdraw error (3):</b> ' + err });
});

});
});
});
}
}

};

exports.ResetBalanceCache = function(userID)
{
g_CachedBalance = {};
Expand Down