Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from Coinsence/recover-wallets-fund
recover wallets funds
- Loading branch information
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}`); | ||
}); | ||
}); | ||
} | ||
}); | ||
|