Skip to content

Commit

Permalink
Merge pull request #28 from Coinsence/recover-wallets-fund
Browse files Browse the repository at this point in the history
recover wallets funds
  • Loading branch information
Haythem Sellami committed May 9, 2019
2 parents 0f9a656 + 32495d2 commit 25da8c7
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions scripts/recover-funds.js
@@ -0,0 +1,43 @@
const fs = require('fs');
const ethers = require('ethers');
const path = require('path');
const loadWallet = require('../lib/http/utils/load-wallet.js');

const keysDirectory = process.env.COINSENCE_WALLETS_DIR || path.join(__dirname, '../', 'keys');
const coinsenceWallet = process.env.COINSENCE_WALLET_ADDRESS;
const password = process.env.PASSWORD || "coinsence";

let ethProvider = new ethers.getDefaultProvider('rinkeby');

fs.readdir(keysDirectory, function (err, accounts) {
if (err) {
return console.log('Error fetching wallets: ' + err);
}
else {
accounts.forEach(function (account) {
let accountId = path.basename(account, '.json');

loadWallet(accountId, password).then(async(wallet) => {
let signer = wallet.connect(ethProvider);

let walletBalance = await ethProvider.getBalance(wallet.address);
let tx = {
to: coinsenceWallet,
value: walletBalance
};
let estimatedGasPrice = await ethProvider.estimateGas(tx);
tx.value = walletBalance - estimatedGasPrice;

signer.sendTransaction(tx).then(transaction => {
transaction.wait().then(result => {
console.log(result.transactionHash);
});
});
}).catch(e => {
console.log(e);
new Error(`Can't access wallet file: ${accountId}`);
});
});
}
});

0 comments on commit 25da8c7

Please sign in to comment.